목적
- calc API에 넘겨주는 인자에 따라 plus / minus기능을 구현함.
코드
from enum import Enum
from fastapi import FastAPI
#from typing import Optional
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/calc")
def read_item(operand: str, num1:int, num2:int):
if operand == 'plus':
return {"result": num1 + num2}
elif operand == 'minus':
return {"result": num1 - num2}
실행결과
$ curl -X 'GET' 'http://127.0.0.1:8000/calc?operand=plus&num1=3&num2=4' \
-H 'accept: application/json'
{"result":7}
$ curl -X 'GET' 'http://127.0.0.1:8000/calc?operand=minus&num1=3&num2=4' \
-H 'accept: application/json'
{"result":-1}
swagger를 이용해서 테스트 하기

생각
- 실제 API기능만 서비스 한다면, 어떤 모양일까?
- python code로 API를 접근하는 코드를 작성한다면?
'Python' 카테고리의 다른 글
| 결측치 채우는 fillna() method 두가지(ffill, bfill) (0) | 2023.12.31 |
|---|---|
| 랜덤 숫자로 성적표 mock 데이터 만들기 (0) | 2023.12.31 |
| 0~100숫자로 도수분포표 만들기 (0) | 2023.12.31 |
| pandas에서 누적합 구하기 (0) | 2023.12.31 |
| Python FastAPI 웹 프레임워크 구축 Hello World (0) | 2023.12.09 |