Home [FastAPI] 시작하기
Post
Cancel

[FastAPI] 시작하기

  • https://fastapi.tiangolo.com/
1
2
pip install fastapi
pip install "uvicorn[standard]" #  ASGI server 가 필요해서 설치 (A)

Example (GET)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}

Unicorn command

1
2
3
4
5
# main: python module 
# --reload: 코드 변경한 이후에 서버가 재시작 되도록 (개발 모드에서 사용)
unicorn main:app --reload 
# zsh: command not found: unicorn 
brew install unicorn

Example (PUT)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import Optional

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

# Basemodel로 아이템을 정의하고
class Item(BaseModel):
    name: str
    price: float
    is_offer: Optional[bool] = None


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}


@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}

Example (Enum)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from enum import Enum

from fastapi import FastAPI


class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name == ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}
  • 이렇게 만들면 APIdoc에서 selectBox로 확인가능

Query Parameters

1
2
3
4
5
6
7
8
9
10
11
from fastapi import FastAPI

app = FastAPI()

fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]


@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10): # limit 의 값 default로 설정이 가능
# async def read_item(item_id: str, q: Optional[str] = None): # default의 값을 none으로 설정 (optional 값을 처리할때)
    return fake_items_db[skip : skip + limit]

Request Body

  • Pydantic 모델을 사용해서 request body를 정의
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from typing import Optional

from fastapi import FastAPI
from pydantic import BaseModel


class Item(BaseModel):
    name: str # required
    description: Optional[str] = None # Optional
    price: float
    tax: Optional[float] = None


app = FastAPI()


@app.post("/items/")
async def create_item(item: Item):
    return item

Validation

1
2
3
4
5
6
7
8
9
10
11
12
13
from typing import Optional

from fastapi import FastAPI, Query # Query를 import

app = FastAPI()


@app.get("/items/")
async def read_items(q: Optional[str] = Query(None, max_length=50)): # 길이 50 # min_length 도 설정 가능, 시작도, regex도
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
  • min_length, regex 설정도 가능
  • required를 하기 위해서는 Query(..., min_length=3)), ...을 사용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from typing import Optional

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: Optional[str] = Query(
        None,
        title="Query string",
        description="Query string for the items to search in the database that have a good match", # description 추가도 가능
        min_length=3,
    )
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

마치며…

This post is licensed under CC BY 4.0 by the author.

[FastAPI] MySQL 연동

[Airflow] bashOperator 사용시에 jinja2.exceptions.TemplateNotFound