Skip to content

FastAPI

FastAPI

FastAPI ํ”„๋ ˆ์ž„์›Œํฌ, ๊ณ ์„ฑ๋Šฅ, ๊ฐ„ํŽธํ•œ ํ•™์Šต, ๋น ๋ฅธ ์ฝ”๋“œ ์ž‘์„ฑ, ์ค€๋น„๋œ ํ”„๋กœ๋•์…˜

Test Coverage Package version


๋ฌธ์„œ: https://fastapi.tiangolo.com

์†Œ์Šค ์ฝ”๋“œ: https://github.com/tiangolo/fastapi


FastAPI๋Š” ํ˜„๋Œ€์ ์ด๊ณ , ๋น ๋ฅด๋ฉฐ(๊ณ ์„ฑ๋Šฅ), ํŒŒ์ด์ฌ ํ‘œ์ค€ ํƒ€์ž… ํžŒํŠธ์— ๊ธฐ์ดˆํ•œ Python3.6+์˜ API๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•œ ์›น ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.

์ฃผ์š” ํŠน์ง•์œผ๋กœ:

  • ๋น ๋ฆ„: (Starlette๊ณผ Pydantic ๋•๋ถ„์—) NodeJS ๋ฐ Go์™€ ๋Œ€๋“ฑํ•  ์ •๋„๋กœ ๋งค์šฐ ๋†’์€ ์„ฑ๋Šฅ. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ๋น ๋ฅธ ํŒŒ์ด์ฌ ํ”„๋ ˆ์ž„์›Œํฌ ์ค‘ ํ•˜๋‚˜.

  • ๋น ๋ฅธ ์ฝ”๋“œ ์ž‘์„ฑ: ์•ฝ 200%์—์„œ 300%๊นŒ์ง€ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ ์†๋„ ์ฆ๊ฐ€. *

  • ์ ์€ ๋ฒ„๊ทธ: ์‚ฌ๋žŒ(๊ฐœ๋ฐœ์ž)์— ์˜ํ•œ ์—๋Ÿฌ ์•ฝ 40% ๊ฐ์†Œ. *
  • ์ง๊ด€์ : ํ›Œ๋ฅญํ•œ ํŽธ์ง‘๊ธฐ ์ง€์›. ๋ชจ๋“  ๊ณณ์—์„œ ์ž๋™์™„์„ฑ. ์ ์€ ๋””๋ฒ„๊น… ์‹œ๊ฐ„.
  • ์‰ฌ์›€: ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ๋ฐฐ์šฐ๋„๋ก ์„ค๊ณ„. ์ ์€ ๋ฌธ์„œ ์ฝ๊ธฐ ์‹œ๊ฐ„.
  • ์งง์Œ: ์ฝ”๋“œ ์ค‘๋ณต ์ตœ์†Œํ™”. ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ ์–ธ์˜ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ. ์ ์€ ๋ฒ„๊ทธ.
  • ๊ฒฌ๊ณ ํ•จ: ์ค€๋น„๋œ ํ”„๋กœ๋•์…˜ ์šฉ ์ฝ”๋“œ๋ฅผ ์–ป์œผ์‹ญ์‹œ์˜ค. ์ž๋™ ๋Œ€ํ™”ํ˜• ๋ฌธ์„œ์™€ ํ•จ๊ป˜.
  • ํ‘œ์ค€ ๊ธฐ๋ฐ˜: API์— ๋Œ€ํ•œ (์™„์ „ํžˆ ํ˜ธํ™˜๋˜๋Š”) ๊ฐœ๋ฐฉํ˜• ํ‘œ์ค€ ๊ธฐ๋ฐ˜: OpenAPI (์ด์ „์— Swagger๋กœ ์•Œ๋ ค์กŒ๋˜) ๋ฐ JSON ์Šคํ‚ค๋งˆ.

* ๋‚ด๋ถ€ ๊ฐœ๋ฐœํŒ€์˜ ํ”„๋กœ๋•์…˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋นŒ๋“œํ•œ ํ…Œ์ŠคํŠธ์— ๊ทผ๊ฑฐํ•œ ์ธก์ •

๊ณจ๋“œ ์Šคํฐ์„œ

๋‹ค๋ฅธ ์Šคํฐ์„œ

์˜๊ฒฌ๋“ค

"[...] ์ €๋Š” ์š”์ฆ˜ FastAPI๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. [...] ์‚ฌ์‹ค ์šฐ๋ฆฌ ํŒ€์˜ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ ML ์„œ๋น„์Šค ์ „๋ถ€๋ฅผ ๋ฐ”๊ฟ€ ๊ณ„ํš์ž…๋‹ˆ๋‹ค. ๊ทธ์ค‘ ์ผ๋ถ€๋Š” ํ•ต์‹ฌ Windows์™€ ๋ช‡๋ช‡์˜ Office ์ œํ’ˆ๋“ค์ด ํ†ตํ•ฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค."

Kabir Khan - ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ (ref)

"FastAPI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฑ„ํƒํ•˜์—ฌ ์˜ˆ์ธก์„ ์–ป๊ธฐ ์œ„ํ•ด ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ๋Š” REST ์„œ๋ฒ„๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. [Ludwig์„ ์œ„ํ•ด]"

Piero Molino, Yaroslav Dudin ๊ทธ๋ฆฌ๊ณ  Sai Sumanth Miryala - ์šฐ๋ฒ„ (ref)

"Netflix๋Š” ์šฐ๋ฆฌ์˜ ์˜คํ”ˆ ์†Œ์Šค ๋ฐฐํฌํŒ์ธ ์œ„๊ธฐ ๊ด€๋ฆฌ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋ฐœํ‘œํ•  ์ˆ˜ ์žˆ์–ด ๊ธฐ์ฉ๋‹ˆ๋‹ค: ๋ฐ”๋กœ Dispatch์ž…๋‹ˆ๋‹ค! [FastAPI๋กœ ๋นŒ๋“œ]"

Kevin Glisson, Marc Vilanova, Forest Monsen - ๋„ทํ”Œ๋ฆญ์Šค (ref)

"FastAPI๊ฐ€ ๋„ˆ๋ฌด ์ข‹์•„์„œ ๊ตฌ๋ฆ„ ์œ„๋ฅผ ๊ฑท๋Š”๋“ฏ ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ง ์ฆ๊ฒ์Šต๋‹ˆ๋‹ค!"

Brian Okken - Python Bytes ํŒŸ์บ์ŠคํŠธ ํ˜ธ์ŠคํŠธ (ref)

"์†”์งํžˆ, ๋‹น์‹ ์ด ๋งŒ๋“  ๊ฒƒ์€ ๋งค์šฐ ๊ฒฌ๊ณ ํ•˜๊ณ  ์„ธ๋ จ๋˜์–ด ๋ณด์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋ฉด์—์„œ Hug๊ฐ€ ์ด๋ ‡๊ฒŒ ๋˜์—ˆ์œผ๋ฉด ํ•ฉ๋‹ˆ๋‹ค - ๊ทธ๊ฑธ ๋งŒ๋“  ๋ˆ„๊ตฐ๊ฐ€๋ฅผ ๋ณด๋Š” ๊ฒƒ์€ ๋งŽ์€ ์˜๊ฐ์„ ์ค๋‹ˆ๋‹ค."

Timothy Crosley - Hug ์ œ์ž‘์ž (ref)

"REST API๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ˜„๋Œ€์ ์ธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ฐพ๊ณ  ์žˆ๋‹ค๋ฉด FastAPI๋ฅผ ํ™•์ธํ•ด ๋ณด์‹ญ์‹œ์˜ค. [...] ๋น ๋ฅด๊ณ , ์“ฐ๊ธฐ ์‰ฝ๊ณ , ๋ฐฐ์šฐ๊ธฐ๋„ ์‰ฝ์Šต๋‹ˆ๋‹ค [...]"

"์šฐ๋ฆฌ API๋ฅผ FastAPI๋กœ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค [...] ์•„๋งˆ ์—ฌ๋Ÿฌ๋ถ„๋„ ์ข‹์•„ํ•˜์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค [...]"

Ines Montani - Matthew Honnibal - Explosion AI ์„ค๋ฆฝ์ž - spaCy ์ œ์ž‘์ž (ref) - (ref)

Typer, FastAPI์˜ CLI

์›น API ๋Œ€์‹  ํ„ฐ๋ฏธ๋„์—์„œ ์‚ฌ์šฉํ•  CLI ์•ฑ์„ ๋งŒ๋“ค๊ณ  ์žˆ๋‹ค๋ฉด, Typer๋ฅผ ํ™•์ธํ•ด ๋ณด์‹ญ์‹œ์˜ค.

Typer๋Š” FastAPI์˜ ๋™์ƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  FastAPI์˜ CLI๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. โŒจ๏ธ ๐Ÿš€

์š”๊ตฌ์‚ฌํ•ญ

Python 3.7+

FastAPI๋Š” ๊ฑฐ์ธ๋“ค์˜ ์–ด๊นจ ์œ„์— ์„œ ์žˆ์Šต๋‹ˆ๋‹ค:

  • ์›น ๋ถ€๋ถ„์„ ์œ„ํ•œ Starlette.
  • ๋ฐ์ดํ„ฐ ๋ถ€๋ถ„์„ ์œ„ํ•œ Pydantic.

์„ค์น˜

$ pip install fastapi

---> 100%

ํ”„๋กœ๋•์…˜์„ ์œ„ํ•ด Uvicorn ๋˜๋Š” Hypercorn๊ณผ ๊ฐ™์€ ASGI ์„œ๋ฒ„๋„ ํ•„์š”ํ•  ๊ฒ๋‹ˆ๋‹ค.

$ pip install "uvicorn[standard]"

---> 100%

์˜ˆ์ œ

๋งŒ๋“ค๊ธฐ

  • main.py ํŒŒ์ผ์„ ๋งŒ๋“œ์‹ญ์‹œ์˜ค:
from typing import Union

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: Union[str, None] = None):
    return {"item_id": item_id, "q": q}
๋˜๋Š” async def ์‚ฌ์šฉํ•˜๊ธฐ...

์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ๊ฐ€ async / await์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, async def๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.

from typing import Union

from fastapi import FastAPI

app = FastAPI()


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


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

Note:

์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉด, ๋ฌธ์„œ์—์„œ async์™€ await์— ๊ด€ํ•œ "๊ธ‰ํ•˜์„ธ์š”?" ์„น์…˜์„ ํ™•์ธํ•ด ๋ณด์‹ญ์‹œ์˜ค.

์‹คํ–‰ํ•˜๊ธฐ

์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜์‹ญ์‹œ์˜ค:

$ uvicorn main:app --reload

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [28720]
INFO:     Started server process [28722]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
uvicorn main:app --reload ๋ช…๋ น์— ๊ด€ํ•˜์—ฌ...

๋ช…๋ น uvicorn main:app์€ ๋‹ค์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค:

  • main: main.py ํŒŒ์ผ (ํŒŒ์ด์ฌ "๋ชจ๋“ˆ").
  • app: the object created inside of main.py with the line app = FastAPI().
  • --reload: ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋œ ํ›„ ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ํ•˜๊ธฐ. ๊ฐœ๋ฐœํ™˜๊ฒฝ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.

ํ™•์ธํ•˜๊ธฐ

๋ธŒ๋ผ์šฐ์ €๋กœ http://127.0.0.1:8000/items/5?q=somequery๋ฅผ ์—ด์–ด๋ณด์‹ญ์‹œ์˜ค.

์•„๋ž˜์˜ JSON ์‘๋‹ต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

{"item_id": 5, "q": "somequery"}

์—ฌ๋Ÿฌ๋ถ„์€ ๋ฒŒ์จ API๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค:

  • ๊ฒฝ๋กœ / ๋ฐ /items/{item_id}์—์„œ HTTP ์š”์ฒญ ๋ฐ›๊ธฐ.
  • ๋‘ ๊ฒฝ๋กœ ๋ชจ๋‘ GET ์—ฐ์‚ฐ(HTTP ๋ฉ”์†Œ๋“œ ๋กœ ์•Œ๋ ค์ง„)์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ๊ฒฝ๋กœ /items/{item_id}๋Š” ๊ฒฝ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜ intํ˜• ์ด์–ด์•ผ ํ•˜๋Š” item_id๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฒฝ๋กœ /items/{item_id}๋Š” ์„ ํƒ์ ์ธ strํ˜• ์ด์–ด์•ผ ํ•˜๋Š” ๊ฒฝ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜ q๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ€ํ™”ํ˜• API ๋ฌธ์„œ

์ด์ œ http://127.0.0.1:8000/docs๋กœ ๊ฐ€๋ณด์‹ญ์‹œ์˜ค.

์ž๋™ ๋Œ€ํ™”ํ˜• API ๋ฌธ์„œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (Swagger UI ์ œ๊ณต):

Swagger UI

๋Œ€์•ˆ API ๋ฌธ์„œ

๊ทธ๋ฆฌ๊ณ  ์ด์ œ http://127.0.0.1:8000/redoc๋กœ ๊ฐ€๋ด…์‹œ๋‹ค.

๋‹ค๋ฅธ ์ž๋™ ๋ฌธ์„œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(ReDoc ์ œ๊ณต):

ReDoc

์˜ˆ์ œ ์‹ฌํ™”

์ด์ œ PUT ์š”์ฒญ์— ์žˆ๋Š” ๋ณธ๋ฌธ(Body)์„ ๋ฐ›๊ธฐ ์œ„ํ•ด main.py๋ฅผ ์ˆ˜์ •ํ•ด๋ด…์‹œ๋‹ค.

Pydantic์„ ์ด์šฉํ•ด ํŒŒ์ด์ฌ ํ‘œ์ค€ ํƒ€์ž…์œผ๋กœ ๋ณธ๋ฌธ์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    price: float
    is_offer: Union[bool, None] = None


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


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = 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}

์„œ๋ฒ„๊ฐ€ ์ž๋™์œผ๋กœ ๋ฆฌ๋กœ๋”ฉ ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค (์œ„์—์„œ uvicorn ๋ช…๋ น์— --reload์„ ์ถ”๊ฐ€ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค).

๋Œ€ํ™”ํ˜• API ๋ฌธ์„œ ์—…๊ทธ๋ ˆ์ด๋“œ

์ด์ œ http://127.0.0.1:8000/docs๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

  • ๋Œ€ํ™”ํ˜• API ๋ฌธ์„œ๊ฐ€ ์ƒˆ ๋ณธ๋ฌธ๊ณผ ํ•จ๊ป˜ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•ฉ๋‹ˆ๋‹ค:

Swagger UI

  • "Try it out" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฑ„์šธ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๊ณ  ์ง์ ‘ API์™€ ์ƒํ˜ธ์ž‘์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

Swagger UI interaction

  • ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ "Execute" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋Š” API์™€ ํ†ต์‹ ํ•˜๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „์†กํ•˜๋ฉฐ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ™”๋ฉด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค:

Swagger UI interaction

๋Œ€์•ˆ API ๋ฌธ์„œ ์—…๊ทธ๋ ˆ์ด๋“œ

๊ทธ๋ฆฌ๊ณ  ์ด์ œ, http://127.0.0.1:8000/redoc๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

  • ๋Œ€์•ˆ ๋ฌธ์„œ ์—ญ์‹œ ์ƒˆ ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ณธ๋ฌธ์„ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค:

ReDoc

์š”์•ฝ

์š”์•ฝํ•˜๋ฉด, ์—ฌ๋Ÿฌ๋ถ„์€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…, ๋ณธ๋ฌธ ๋“ฑ์„ ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ์„œ ํ•œ๋ฒˆ์— ์„ ์–ธํ–ˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๋Ÿฌ๋ถ„์€ ํ˜„๋Œ€ ํ‘œ์ค€ ํŒŒ์ด์ฌ ํƒ€์ž…์œผ๋กœ ์ด๋ฅผ ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ƒˆ๋กœ์šด ๋ฌธ๋ฒ•, ํŠน์ • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฉ”์†Œ๋“œ๋‚˜ ํด๋ž˜์Šค ๋“ฑ์„ ๋ฐฐ์šธ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๊ทธ์ € ํ‘œ์ค€ Python 3.6+์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, int์— ๋Œ€ํ•ด์„ :

item_id: int

๋˜๋Š” ์ข€ ๋” ๋ณต์žกํ•œ Item ๋ชจ๋ธ์— ๋Œ€ํ•ด์„ :

item: Item

...๊ทธ๋ฆฌ๊ณ  ๋‹จ ํ•˜๋‚˜์˜ ์„ ์–ธ์œผ๋กœ ์—ฌ๋Ÿฌ๋ถ„์ด ์–ป๋Š” ๊ฒƒ์€:

  • ๋‹ค์Œ์„ ํฌํ•จํ•œ ํŽธ์ง‘๊ธฐ ์ง€์›:
    • ์ž๋™์™„์„ฑ.
    • ํƒ€์ž… ๊ฒ€์‚ฌ.
  • ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ:
    • ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์„ ๋•Œ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ช…ํ™•ํ•œ ์—๋Ÿฌ.
    • ์ค‘์ฒฉ๋œ JSON ๊ฐ์ฒด์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ.
  • ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜: ๋„คํŠธ์›Œํฌ์—์„œ ํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ ๋ฐ ํƒ€์ž…์œผ๋กœ ์ „์†ก. ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค:
    • JSON.
    • ๊ฒฝ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜.
    • ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜.
    • ์ฟ ํ‚ค.
    • ํ—ค๋”.
    • ํผ(Forms).
    • ํŒŒ์ผ.
  • ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜: ํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ ๋ฐ ํƒ€์ž…์„ ๋„คํŠธ์›Œํฌ ๋ฐ์ดํ„ฐ๋กœ ์ „ํ™˜(JSON ํ˜•์‹์œผ๋กœ):
    • ํŒŒ์ด์ฌ ํƒ€์ž… ๋ณ€ํ™˜ (str, int, float, bool, list, ๋“ฑ).
    • datetime ๊ฐ์ฒด.
    • UUID ๊ฐ์ฒด.
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋ธ.
    • ...๋” ๋งŽ์€ ๊ฒƒ๋“ค.
  • ๋Œ€์•ˆ๊ฐ€๋Šฅํ•œ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ 2๊ฐœ ํฌํ•จํ•œ ์ž๋™ ๋Œ€ํ™”ํ˜• API ๋ฌธ์„œ:
    • Swagger UI.
    • ReDoc.

์ด์ „ ์ฝ”๋“œ ์˜ˆ์ œ๋กœ ๋Œ์•„๊ฐ€์„œ, FastAPI๋Š” ๋‹ค์Œ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค:

  • GET ๋ฐ PUT ์š”์ฒญ์— item_id๊ฐ€ ๊ฒฝ๋กœ์— ์žˆ๋Š”์ง€ ๊ฒ€์ฆ.
  • GET ๋ฐ PUT ์š”์ฒญ์— item_id๊ฐ€ int ํƒ€์ž…์ธ์ง€ ๊ฒ€์ฆ.
    • ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ํด๋ผ์ด์–ธํŠธ๋Š” ์œ ์šฉํ•˜๊ณ  ๋ช…ํ™•ํ•œ ์—๋Ÿฌ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • GET ์š”์ฒญ์— q๋ผ๋Š” ์„ ํƒ์ ์ธ ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๊ฒ€์‚ฌ(http://127.0.0.1:8000/items/foo?q=somequery์ฒ˜๋Ÿผ).
    • q ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” = None์œผ๋กœ ์„ ์–ธ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์„ ํƒ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค.
    • None์ด ์—†๋‹ค๋ฉด ํ•„์ˆ˜์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค(PUT์˜ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ).
  • /items/{item_id}์œผ๋กœ์˜ PUT ์š”์ฒญ์€ ๋ณธ๋ฌธ์„ JSON์œผ๋กœ ์ฝ์Œ:
    • name์„ ํ•„์ˆ˜ ์†์„ฑ์œผ๋กœ ๊ฐ–๊ณ  str ํ˜•์ธ์ง€ ๊ฒ€์‚ฌ.
    • price์„ ํ•„์ˆ˜ ์†์„ฑ์œผ๋กœ ๊ฐ–๊ณ  float ํ˜•์ธ์ง€ ๊ฒ€์‚ฌ.
    • ๋งŒ์•ฝ ์ฃผ์–ด์ง„๋‹ค๋ฉด, is_offer๋ฅผ ์„ ํƒ ์†์„ฑ์œผ๋กœ ๊ฐ–๊ณ  bool ํ˜•์ธ์ง€ ๊ฒ€์‚ฌ.
    • ์ด ๋ชจ๋“  ๊ฒƒ์€ ๊นŠ์ด ์ค‘์ฒฉ๋œ JSON ๊ฐ์ฒด์—๋„ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • JSON์„ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ JSON์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์ž๋™ํ™”.
  • ๋‹ค์Œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์„ OpenAPI๋กœ ๋ฌธ์„œํ™”:
    • ๋Œ€ํ™”ํ˜• ๋ฌธ์„œ ์‹œ์Šคํ…œ.
    • ์—ฌ๋Ÿฌ ์–ธ์–ด๋“ค์— ๋Œ€ํ•œ ์ž๋™ ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์ƒ์„ฑ ์‹œ์Šคํ…œ.
  • 2๊ฐœ์˜ ๋Œ€ํ™”ํ˜• ๋ฌธ์„œ ์›น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง์ ‘ ์ œ๊ณต.

์šฐ๋ฆฌ๋Š” ๊ทธ์ € ์ˆ˜๋ฐ• ๊ฒ‰ํ•ก๊ธฐ๋งŒ ํ–ˆ์„ ๋ฟ์ธ๋ฐ ์—ฌ๋Ÿฌ๋ถ„์€ ๋ฒŒ์จ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ค„์„ ๋ฐ”๊ฟ”๋ณด์‹ญ์‹œ์˜ค:

    return {"item_name": item.name, "item_id": item_id}

...์—์„œ:

        ... "item_name": item.name ...

...์œผ๋กœ:

        ... "item_price": item.price ...

...๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ์—ฌ๋Ÿฌ๋ถ„์˜ ํŽธ์ง‘๊ธฐ๊ฐ€ ์†์„ฑ๊ณผ ํƒ€์ž…์„ ์•Œ๊ณ  ์ž๋™ ์™„์„ฑํ•˜๋Š”์ง€ ๋ณด์‹ญ์‹œ์˜ค:

editor support

๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•œ ๋ณด๋‹ค ์™„์ „ํ•œ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ, ํŠœํ† ๋ฆฌ์–ผ - ์‚ฌ์šฉ์ž ๊ฐ€์ด๋“œ๋ฅผ ๋ณด์‹ญ์‹œ์˜ค.

์Šคํฌ์ผ๋Ÿฌ ์ฃผ์˜: ํŠœํ† ๋ฆฌ์–ผ - ์‚ฌ์šฉ์ž ๊ฐ€์ด๋“œ๋Š”:

  • ์„œ๋กœ ๋‹ค๋ฅธ ์žฅ์†Œ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ ์–ธ: ํ—ค๋”, ์ฟ ํ‚ค, ํผ ํ•„๋“œ ๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ.
  • maximum_length ๋˜๋Š” regex์ฒ˜๋Ÿผ ์œ ํšจ์„ฑ ์ œ์•ฝํ•˜๋Š” ๋ฐฉ๋ฒ•.
  • ๊ฐ•๋ ฅํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šด ์˜์กด์„ฑ ์ฃผ์ž… ์‹œ์Šคํ…œ.
  • OAuth2 ์ง€์›์„ ํฌํ•จํ•œ JWT tokens ๋ฐ HTTP Basic์„ ๊ฐ–๋Š” ๋ณด์•ˆ๊ณผ ์ธ์ฆ.
  • (Pydantic ๋•๋ถ„์—) ๊นŠ์€ ์ค‘์ฒฉ JSON ๋ชจ๋ธ์„ ์„ ์–ธํ•˜๋Š”๋ฐ ๋” ์ง„๋ณดํ•œ (ํ•˜์ง€๋งŒ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‰ฌ์šด) ๊ธฐ์ˆ .
  • (Starlette ๋•๋ถ„์—) ๋งŽ์€ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ:
    • ์›น ์†Œ์ผ“
    • GraphQL
    • HTTPX ๋ฐ pytest์— ๊ธฐ๋ฐ˜ํ•œ ๊ทนํžˆ ์‰ฌ์šด ํ…Œ์ŠคํŠธ
    • CORS
    • ์ฟ ํ‚ค ์„ธ์…˜
    • ...๊ธฐํƒ€ ๋“ฑ๋“ฑ.

์„ฑ๋Šฅ

๋…๋ฆฝ๋œ TechEmpower ๋ฒค์น˜๋งˆํฌ์—์„œ Uvicorn์—์„œ ์ž‘๋™ํ•˜๋Š” FastAPI ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ๋น ๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ ์ค‘ ํ•˜๋‚˜๋กœ Starlette์™€ Uvicorn(FastAPI์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ)์—๋งŒ ๋ฐ‘๋Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (*)

์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ฒค์น˜๋งˆํฌ ์„น์…˜์„ ๋ณด์‹ญ์‹œ์˜ค.

์„ ํƒ๊ฐ€๋Šฅํ•œ ์˜์กด์„ฑ

Pydantic์ด ์‚ฌ์šฉํ•˜๋Š”:

  • ujson - ๋” ๋น ๋ฅธ JSON "ํŒŒ์‹ฑ".
  • email_validator - ์ด๋ฉ”์ผ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ.

Starlette์ด ์‚ฌ์šฉํ•˜๋Š”:

  • HTTPX - TestClient๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•„์š”.
  • jinja2 - ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ ์„ค์ •์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•„์š”.
  • python-multipart - request.form()๊ณผ ํ•จ๊ป˜ "parsing"์˜ ์ง€์›์„ ์›ํ•˜๋ฉด ํ•„์š”.
  • itsdangerous - SessionMiddleware ์ง€์›์„ ์œ„ํ•ด ํ•„์š”.
  • pyyaml - Starlette์˜ SchemaGenerator ์ง€์›์„ ์œ„ํ•ด ํ•„์š” (FastAPI์™€ ์“ธ๋•Œ๋Š” ํ•„์š” ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค).
  • graphene - GraphQLApp ์ง€์›์„ ์œ„ํ•ด ํ•„์š”.
  • ujson - UJSONResponse๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•„์š”.

FastAPI / Starlette์ด ์‚ฌ์šฉํ•˜๋Š”:

  • uvicorn - ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋กœ๋“œํ•˜๊ณ  ์ œ๊ณตํ•˜๋Š” ์„œ๋ฒ„.
  • orjson - ORJSONResponse์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•„์š”.

pip install fastapi[all]๋ฅผ ํ†ตํ•ด ์ด ๋ชจ๋‘๋ฅผ ์„ค์น˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ผ์ด์„ผ์Šค

์ด ํ”„๋กœ์ ํŠธ๋Š” MIT ๋ผ์ด์„ผ์Šค ์กฐ์•ฝ์— ๋”ฐ๋ผ ๋ผ์ด์„ผ์Šค๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค.