■ FastAPI 클래스의 get 메소드를 사용해 열거형 값을 전달하는 방법을 보여준다.
▶ main.py
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 |
from enum import Enum from fastapi import FastAPI class Season(str, Enum): spring = "봄" summer = "여름" autumn = "가을" winter = "겨울" fastAPI = FastAPI() @fastAPI.get("/season/{season}") def getSeasonName(season:Season): if season is Season.spring: return {"season" : season, "message" : "테스트 메시지 1"} if season.value == "여름": return {"season" : season, "message" : "테스트 메시지 2"} if season.value == "가을": return {"season" : season, "message" : "테스트 메시지 3"} return {"season" : season, "message" : "테스트 메시지 4"} |
▶ requirements.txt
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
annotated-types==0.6.0 anyio==4.3.0 certifi==2024.2.2 click==8.1.7 colorama==0.4.6 dnspython==2.6.1 email_validator==2.1.1 fastapi==0.111.0 fastapi-cli==0.0.3 h11==0.14.0 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 idna==3.7 itsdangerous==2.2.0 Jinja2==3.1.4 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 orjson==3.10.3 pip==24.0 pydantic_core==2.18.2 pydantic==2.7.1 pydantic-extra-types==2.7.0 pydantic-settings==2.2.1 Pygments==2.18.0 python-dotenv==1.0.1 python-multipart==0.0.9 PyYAML==6.0.1 rich==13.7.1 shellingham==1.5.4 sniffio==1.3.1 starlette==0.37.2 typer==0.12.3 typing_extensions==4.11.0 ujson==5.10.0 uvicorn==0.29.0 watchfiles==0.21.0 websockets==12.0 |