๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Back-end & Server/FastAPI

[FastAPI] ๋ชจ๋ธ

728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿฐ FastAPI ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด์„œ ๊ฐœ์ธ์ ์œผ๋กœ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.

 

์‘๋‹ต ๋ชจ๋ธ

FastAPI๋Š” response_model์„ ์‚ฌ์šฉํ•˜์—ฌ ์‘๋‹ต์„ ์œ„ํ•œ ๋ชจ๋ธ์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • @app.get()
  • @app.post()
  • @app.put()
  • @app.delete()
  • Etc

 

response_model์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ ์„ ์–ป๋Š”๋‹ค.

  • ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ํƒ€์ž… ์„ ์–ธ์œผ๋กœ ๋ณ€ํ™˜
  • ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ
  • OpenAPI ๊ฒฝ๋กœ ์ž‘๋™์˜ ์‘๋‹ต์— JSON ์Šคํ‚ค๋งˆ ์ถ”๊ฐ€
  • ์ž๋™ ์ƒ์„ฑ ๋ฌธ์„œ ์‹œ์Šคํ…œ์— ์‚ฌ์šฉ
  • ํ•ด๋‹น ๋ชจ๋ธ์˜ ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ์ œํ•œ
from typing import Any, Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()

# SignIn model


class UserIn(BaseModel):
    usename: str
    password: str  # ์‹ค์ œ ํŒจ์Šค์›Œ๋“œ๋Š” ์ ˆ๋Œ€ ํ‰๋ฌธ์ž‘์„ฑ X
    email: EmailStr
    full_name: Union[str, None] = None

# User Info Output


class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: Union[str, None] = None

# reponse_model์— UseOut ๋ชจ๋ธ๋กœ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—
# ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์€ ๊ฐ’์„ ์‘๋‹ตํ•จ.


@app.post('/user/', response_model=UserOut)
async def create_user(user: UserIn) -> Any:
    return user

 

์ด์ฒ˜๋Ÿผ response_model์— ๋ชจ๋ธ์„ ์ง€์ •ํ•˜๋ฉด ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์€ ์‘๋‹ต๊ฐ’๋งŒ ํ•„ํ„ฐ๋งํ•ด์„œ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

์ถ”๊ฐ€๋กœ response_model_exclude_unset์„ ์‚ฌ์šฉํ•˜์—ฌ ์‘๋‹ต ๋ชจ๋ธ์—์„œ unset์ƒํƒœ(๊ฐ’์ด ํ• ๋‹น๋˜์ง€ ์•Š์€ ์ƒํƒœ)์˜ ํ•„๋“œ๋ฅผ ์ œ์™ธํ•˜์—ฌ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ์‘๋‹ต๊ฐ’์ด ์—„์ฒญ ๊ธด ๊ฒฝ์šฐ ํ•„์š”์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์™ธํ•  ์ˆ˜ ์žˆ๋‹ค.

from typing import List, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: float = 10.5
    tags: List[str] = []


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]

 

/items/foo๋ฅผ ํ•˜๋ฉด ์š”์ฒญ์œผ๋กœ {"name":"Foo", "price":50.2}๊ฐ€ ๊ฐ€๊ฒŒ๋˜๊ณ  response_model_exclude_uset=True ๋•๋ถ„์— ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋œ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค์„ ์ œ์™ธํ•œ ์‘๋‹ต์„ ํ•œ๋‹ค.

 

 

Multiple ๋ชจ๋ธ

์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณด์•ˆ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

ํšŒ์›๊ฐ€์ž… ๊ณผ์ •์„ ์˜ˆ์‹œ๋กœ ์‚ดํŽด๋ณด์ž.

from typing import Union, Any
from hashlib import sha256
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    id: str
    pw: str
    email: EmailStr
    fullName: Union[str, None] = None


class UserOut(BaseModel):
    id: str
    email: EmailStr
    fullName: Union[str, None] = None


class UserDB(BaseModel):
    id: str
    hashPW: Any
    email: EmailStr
    fullName: Union[str, None] = None


def toHash(password: str) -> Any:
    return sha256(password.encode())


def saveUser(user: UserIn) -> UserDB:
    hashPW = toHash(user.pw)
    fakeDB = UserDB(**user.model_dump(), hashPW=hashPW)
    print("Save User in DB")
    return fakeDB


@app.post("/user/", response_model=UserOut)
def signIN(user: UserIn) -> UserOut:
    result: UserDB = saveUser(user)
    return result

 

728x90
๋ฐ˜์‘ํ˜•

'Back-end & Server > FastAPI' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[FastAPI] ๊ฒฝ๋กœ ์ž‘๋™ ์„ค์ •  (0) 2024.02.20
[FastAPI] Form  (0) 2024.02.20
[FastAPI] ๋งค๊ฐœ๋ณ€์ˆ˜  (0) 2024.02.20
[FastAPI] ๋™์‹œ์„ฑ๊ณผ async / await  (0) 2024.02.19
[FastAPI] ํƒ€์ž… ์ง€์ •, Pydantic  (0) 2024.02.19