[FastAPI] 1. GET, POST, PUT, DELETE ๋“ฑ ๊ธฐ๋ณธ API ๋งŒ๋“ค๊ณ  ๋ฌธ์„œ ์ž๋™ํ™” ํ•ด๋ณด๊ธฐ

๋ฐ˜์‘ํ˜•

์ง€๋‚œ ๊ธ€์— ์ด์–ด์„œ FastAPI๋ฅผ ์ด์šฉํ•ด์„œ ๊ธฐ๋ณธ์ ์ธ REST API ๊ฐœ๋ฐœ ์ด์•ผ๊ธฐ ๋‘ ๋ฒˆ์งธ๋ฅผ ์ด์–ด๊ฐ€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

์šฐ๋ฆฌ๊ฐ€ REST API๋ฅผ ๊ฐœ๋ฐœํ•˜๋ฉด ์ฃผ์š” ๋ฉ”์†Œ๋“œ ์ข…๋ฅ˜ 5๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ์š”. ๋ฐ”๋กœ GET, POST, PUT, DELETE, PATCH ์ด๋ ‡๊ฒŒ 5๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด ์™ธ์—๋„ OPTIONS๊ฐ€ ์žˆ์ง€๋งŒ ์ด ๊ธ€์—์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

๊ธฐ๋ณธ API ๋งŒ๋“ค๊ธฐ (GET)

REST API์˜ ๊ธฐ๋ณธ API๋Š” ๋จผ์ € GET ๋ฉ”์†Œ๋“œ๋ถ€ํ„ฐ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. GET ๋ฉ”์†Œ๋“œ๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์“ฐ๋Š” READ API ์ž…๋‹ˆ๋‹ค. FastAPI์—์„œ๋Š” ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from fastapi import FastAPI

app = FastAPI()


@app.route('/health')
async def health_check():
    return "OK"
import uuid
from fastapi import FastAPI
from starlette.responses import JSONResponse

app = FastAPI()


@app.get("/{name}")
async def generate_id_for_name(name: str):
    return JSONResponse({
        'id': str(uuid.uuid4()),
        'name': name
    })

์ฒซ ๋ฒˆ์จฐ ๋ฐฉ๋ฒ•์€ route ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. route์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•  ๊ฒฝ์šฐ ์˜ค์ง String์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…๋งŒ์„ ๋ฐ˜ํ™˜ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  dict ํ˜•๋„ ๋ฐ˜ํ™˜์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ dict์ฒ˜๋Ÿผ Wrapper ์ž๋ฃŒํ˜•์—๋Š” ๋ฐ˜๋“œ์‹œ String๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. String์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋‚˜ ํ•จ์ˆ˜ ๋˜ํ•œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ์„œ๋ฒ„๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ๊ฐ„๋‹จํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” Health Check์šฉ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

 

GET ๋ฉ”์†Œ๋“œ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์ž๋ฃŒํ˜•์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JSONResponse ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ dict ์ž๋ฃŒํ˜•์„ JSON์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด ๋•Œ๋Š” json.dumps ๋ฉ”์†Œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 

 

URI์— ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ ์ค‘๊ด„ํ˜ธ๋กœ ๋งคํ•‘ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

๊ธฐ๋ณธ API ๋งŒ๋“ค๊ธฐ (POST)

POST ๋ฉ”์†Œ๋“œ๋Š” CREATE์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”. FastAPI์—์„œ๋Š” ์•„๋ž˜์˜ ๋ฉ”์†Œ๋“œ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from fastapi import FastAPI
from pydantic import BaseModel
from starlette.responses import JSONResponse

app = FastAPI()


class Item(BaseModel):
    user_id: str
    password: str


@app.post("/register")
async def register_item(item: Item):
    dicted_item = dict(item)
    dicted_item['success'] = True

    return JSONResponse(dicted_item)

FastAPI์—์„œ POST ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ‚ค๋งˆ ๋””ํŽœ๋˜์‹œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ pydantic์ธ๋ฐ์š”. ์ด์ „์— ์ €๋„ Python์—์„œ ์›น ๊ฐœ๋ฐœํ–ˆ์„ ๋•Œ๋Š” ์Šคํ‚ค๋งˆ ์ฒดํ‚น์œผ๋กœ marshmallow๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— Pydantic์œผ๋กœ ๊ฐˆ์•„ํƒ€๋ฉด์„œ๋ถ€ํ„ฐ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๋งŽ์ด ์ฒดํ—˜ํ•˜์˜€๋Š”๋ฐ์š”.

 

FastAPI์—์„œ๋Š” ์ด๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฑ„ํƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— Pydantic์—์„œ ์ œ๊ณตํ•˜๋Š” BaseModel ์ƒ์† ์—†์ด๋Š” POST ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

 

BaseModel ์ƒ์† ์—†์ด ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ ์œ„์™€ ๊ฐ™์ด Item ํด๋ž˜์Šค๊ฐ€ pydantic field type์ด ์•„๋‹ˆ๋ผ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋‚ด๋ฑ‰์Šต๋‹ˆ๋‹ค.

 

 

 

 

๊ธฐ๋ณธ API ๋งŒ๋“ค๊ธฐ (PUT, PATCH)

PUT๊ณผ PATCH ๋ฉ”์†Œ๋“œ๋Š” UPDATE์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ์ฐจ์ด๋Š” ๋ชจ๋“  ๋‚ด์šฉ์„ ๋ฐ”๊พธ๋ƒ(PUT), ์•„๋‹ˆ๋ฉด ์ผ๋ถ€์˜ ๋‚ด์šฉ์„ ๋ฐ”๊พธ๋ƒ(PATCH)์˜ ์ฐจ์ด์ธ๋ฐ์š”. ์–ด๋–ค์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

from fastapi import FastAPI
from pydantic import BaseModel
from starlette.responses import JSONResponse

app = FastAPI()


class Item(BaseModel):
    user_id: str
    password: str


@app.post("/register")
async def register_item(item: Item):
    global dicted_item
    dicted_item = dict(item)
    dicted_item['success'] = True

    return JSONResponse(dicted_item)


@app.put("/update")
async def update_item(item: Item):
    dicted_item = {k:v for k, v in dict(item).items()}
    dicted_item['success'] = True
    
    return JSONResponse(dicted_item)

PUT ๋ฉ”์†Œ๋“œ๋Š” ์œ„์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. register ํฌ์ธํŠธ๋ฅผ ํ†ตํ•ด ๋“ฑ๋กํ•œ ํšŒ์› ์ •๋ณด๋ฅผ update ํฌ์ธํŠธ๋ฅผ ํ˜ธ์ถœํ•ด ์š”์ฒญํ•˜๋ฉด ์ „๋ถ€ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

from fastapi import FastAPI
from pydantic import BaseModel
from starlette.responses import JSONResponse

app = FastAPI()


class Item(BaseModel):
    user_id: str
    password: str


@app.post("/register")
async def register_item(item: Item):
    global dicted_item
    dicted_item = dict(item)
    dicted_item['success'] = True

    return JSONResponse(dicted_item)


@app.put("/update")
async def update_item(item: Item):
    dicted_item = {k:v for k, v in dict(item).items()}
    dicted_item['success'] = True
    
    return JSONResponse(dicted_item)


@app.patch("/update")
async def update_item_sub(item: Item):
    for k, v in dict(item).items():
        dicted_item[k] = v
    dicted_item['success'] = True

    return JSONResponse(dicted_item)

์ž ๊ทธ๋Ÿผ PATCH API๋Š” ์–ด๋–จ๊นŒ์š”? ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•„ ๋ณด์ด์ง€๋งŒ...

 

๋ถˆํ–‰ํ•˜๊ฒŒ๋„ ์œ„์™€ ๊ฐ™์€ 422 ์˜ค๋ฅ˜๋ฅผ ๋‚ด๋ฟœ์Šต๋‹ˆ๋‹ค. ์™œ ๊ทธ๋Ÿด๊นŒ์š”? PATCH ๋ฉ”์†Œ๋“œ๋Š” ์ผ๋ถ€๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค๊ณ  ํ•˜์—ฌ Password๋งŒ ๋ณ€๊ฒฝํ•œ๋‹ค๋ฉด ์ด๋Ÿฐ์‹์œผ๋กœ ์˜ค๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์›์ธ์€ ๋ฐ”๋กœ Pydantic์œผ๋กœ ๊ตฌํ˜„ํ•œ Item ํด๋ž˜์Šค์— ์žˆ์Šต๋‹ˆ๋‹ค.

class Item(BaseModel):
    user_id: str
    password: str

 

ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์œ„์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋ฉด ๊ฐ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” Required ํ˜•ํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•ด์•ผ ํ•˜๋Š”๋ฐ, PATCH ๋ฉ”์†Œ๋“œ์—์„œ๋Š” user_id๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

from typing import Optional


class PatchItem(BaseModel):
    user_id: Optional[str]
    password: Optional[str]
    

@app.patch("/update")
async def update_item_sub(item: PatchItem):
    dicted_item = {}
    for k, v in dict(item).items():
        if v:
            dicted_item[k] = v
    dicted_item['success'] = True

    return JSONResponse(dicted_item)

๋‹ค์†Œ ๋ฒˆ๊ฑฐ๋กญ์ง€๋งŒ ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” PATCH๋ฅผ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ๋” ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ๊ฐ ๋ฉค๋ฒ„๋“ค์—๊ฒŒ Optional ํƒ€์ž…์„ ๋ถ™์—ฌ์คŒ์œผ๋กœ์จ Pydantic์ด ํ•ด๋‹น ๊ฐ’ ์ค‘ ์–ด๋–ค ํ•˜๋‚˜๋ผ๋„ ์ž…๋ ฅ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋ผ๋„ API๊ฐ€ ํ—ˆ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

๊ธฐ๋ณธ API ๋งŒ๋“ค๊ธฐ (DELETE)

์‚ญ์ œ API๋Š” delete ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@app.delete("/delete")
async def delete_item():
    dicted_item = None
    return Response(status_code=HTTP_204_NO_CONTENT)

์‚ญ์ œ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ์‹œ์—๋Š” ๋ณดํ†ต ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ EmptyResponse๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Starlette์—์„œ๋Š” EmptyResponse ๊ฐ์ฒด๋ฅผ ๋ณ„๋„๋กœ ์ œ๊ณตํ•ด์ฃผ์ง€ ์•Š์œผ๋ฏ€๋กœ Response ๊ฐ์ฒด์— 204 ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

API ๋ฌธ์„œ ๋งŒ๋“ค๊ณ  ๋‹ค๋“ฌ๊ธฐ

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , http://127.0.0.1:8000/redoc ํ˜น์€ http://127.0.0.1:8000/docs ๋กœ ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์–ด๋Š ์ •๋„์˜ ๋ฌธ์„œ๊ฐ€ ๋งŒ๋“ค์–ด ์กŒ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„ ํ™”๋ฉด์€ redoc์˜ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์Šคํ‚ค๋งˆ ๋ชจ๋ธ๋กœ  Pydantic์„ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ์š”. FastAPI๋Š” Pydantic์—์„œ ์ œ๊ณตํ•˜๋Š” BaseModel์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ๊ธˆ Request, Response ํƒ€์ž…์˜ ๋ฌธ์„œ๋ฅผ ์ž๋™ํ™” ์‹œ์ผœ์ค๋‹ˆ๋‹ค. 

 

๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์„œ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํƒ€์ž… ๋“ฑ ๊น”๋”ํ•˜๊ฒŒ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๊ธด ํ•˜์ง€๋งŒ ์กฐ๊ธˆ ์„ค๋ช…์ด ๋ถ€์กฑํ•œ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ์— ์„ค๋ช… ๋“ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์ด๋ฅผ ๋‹ค๋“ฌ๊ธฐ ์œ„ํ•ด์„œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@app.get("/{name}", description="์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ๋ฐ›๊ณ  ID๋ฅผ ์ƒ์„ฑํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค.")
async def generate_id_for_name(name: str):
    return JSONResponse({
        'id': str(uuid.uuid4()),
        'name': name
    })

๊ฐ decorator์—๋Š” description ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ฐ„๋‹จํ•œ ์„ค๋ช…์„ ๋„ฃ์œผ๋ฉด...

 

์ด๋ ‡๊ฒŒ API ์ œ๋ชฉ ๋ฐ‘์— ๊ฐ„๋‹จํ•œ ์„ค๋ช…์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ œ๋ชฉ๋„ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด...

@app.get("/{name}", name="์‚ฌ์šฉ์ž ID ์ƒ์„ฑ", description="์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ๋ฐ›๊ณ  ID๋ฅผ ์ƒ์„ฑํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค.")
async def generate_id_for_name(name: str):
    return JSONResponse({
        'id': str(uuid.uuid4()),
        'name': name
    })

name ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋‚˜์™€ ์žˆ์ง€๋งŒ ์‘๋‹ต ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ์ƒ์„ธ ์‚ฌํ•ญ์€ ๋‚˜์™€ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์‘๋‹ต ๋ชจ๋ธ์— ๋Œ€ํ•œ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 

class Item(BaseModel):
    user_id: str
    password: str


class ResponseItem(Item):
    success: bool


@app.post("/register", response_model=ResponseItem)
async def register_item(item: Item):
    global dicted_item
    dicted_item = dict(item)
    dicted_item['success'] = True

    return JSONResponse(dicted_item)

์œ„์ฒ˜๋Ÿผ Response Model์„ ์ •์˜ํ•˜๋ฉด ๋ฌธ์„œ์— ์ƒ˜ํ”Œ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ์ „ํžˆ ๋ถ€์กฑํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์–ด๋–ค ๊ฑธ ๋‚˜ํƒ€๋‚ด๋Š”์ง€ ํ•œ ๋‹จ์–ด๋กœ ํ‘œ์‹œํ•˜๊ธฐ ์–ด๋ ค์šธ ๊ฒฝ์šฐ ๊ฐ„๋‹จํ•œ description์ด ์žˆ๋‹ค๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

class Item(BaseModel):
    user_id: str = Field(title='์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  ID')
    password: str = Field(title='์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  Password')


class ResponseItem(Item):
    success: bool = Field(True, "์ฒ˜๋ฆฌ ์—ฌ๋ถ€/๊ฒฐ๊ณผ")


class PatchItem(BaseModel):
    user_id: Optional[str] = Field(None, title='์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  ID')
    password: Optional[str] = Field(None, title='์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  Password')

์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” Pydantic์—์„œ ์ œ๊ณตํ•˜๋Š” Field๋ฅผ ์‚ฌ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํƒ€์ž…๊ณผ ํ•จ๊ป˜ ๊ฐ„๋‹จํ•œ ์ฝ”๋ฉ˜ํŠธ๊ฐ€ ํ‘œ์‹œ๋จ์œผ๋กœ์จ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ API ๋ฌธ์„œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. 

 

 

 

 

๋งˆ์น˜๋ฉฐ...

์—ฌ๊ธฐ๊นŒ์ง€ ์•„์ฃผ ๊ฐ„๋‹จํ•œ API๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ฉด์„œ ๋ฌธ์„œ ์ž๋™ํ™” ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ๊ฒƒ์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. Pydantic์˜ ์„ฑ๋Šฅ์„ ๊ฐ€์ ธ๊ฐ๊ณผ ๋™์‹œ์— FastAPI๊ฐ€ ์ œ๊ณตํ•˜๋Š” API ๋ฌธ์„œ ์ž๋™ํ™”๋Š” REST API ์„œ๋ฒ„๋ฅผ ์‰ฝ๊ณ  ์žฌ๋ฐŒ๊ฒŒ ๊ฐœ๋ฐœํ•ด์ฃผ๋Š” ํ”Œ๋Ÿฌ์Šค ์š”์†Œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋”์šฑ์ด ๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ•์„ ํ†ตํ•˜์—ฌ ๋ฒ„๊ทธ์™€ ์˜ค๋ฅ˜๋ฅผ ์ค„์ด๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์€ ์•„์ฃผ ์ข‹์€ ์‹œ๋„ˆ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์—ˆ๊ณ , ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ํŒŒ์ด์ฌ์—์„œ ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋ณด๋‹ค ์‰ฝ๊ณ  ๊ฐ•๋ ฅํ•˜๊ฒŒ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ์š”.

 

๋‹ค์Œ ๊ธ€ ๋ถ€ํ„ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๋™์„ ์‹œ์ž‘์œผ๋กœ ORM์„ ์ด์šฉํ•œ FastAPI ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ๋Œ€ํ•ด ์ข€ ๋” ์‹ฌ์ธต์ ์œผ๋กœ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•
TAGS.

Tistory Comments