FastAPI Path Parameters(열거형) - 06

2022. 12. 21. 14:19·Develop/FastAPI
728x90
SMALL

1. Enum 모듈을 삽입

  • 사전에 정해진 범위에 값이 있을 때 사용
  • from enum import Enum

​

2. 새로운 클래스를 생성

  • Enums 상속
  • str 상속 -> 내부 클래스 변수들은 string 형식이라고 명
class DirectionName(str, Enum):
    north = "North"
    south = "South"
    east = "East"
    west = "West"
 

3. Path Parameters(경로 매개변수)

  • url에서 값을 받음
@app.get("/directions/{direction_name}")
async def get_direction(direction_name: DirectionName):
    if direction_name == DirectionName.north:
        return {"Direction":direction_name, "sub":"Up"}
    if direction_name == DirectionName.south:
        return {"Direction":direction_name, "sub":"Down"}
    if direction_name == DirectionName.west:
        return {"Direction":direction_name, "sub":"Left"}
    return {"Direction":direction_name, "sub":"Right"}
  • 함수의 매개변수 direction_name의 범위를 DirectionName 인스턴스로 정의
  • url로 받을 수 있는 값은 사전에 정의 DirectionName의 값만 가능

 

  • 사전에 정의 된 값 이외에 값이 들어갔을 때
728x90
SMALL
저작자표시 비영리 변경금지 (새창열림)

'Develop > FastAPI' 카테고리의 다른 글

FastAPI Query Parameters - 08  (0) 2022.12.22
FastAPI Path Parameters(추가) - 07  (0) 2022.12.21
FastAPI 요청 메서드 로직 - 05  (0) 2022.12.21
FastAPI Swagger / HTTP 데코레이터 - 04  (0) 2022.12.21
FastAPI - Swagger, HTTP 요청 관련 - 03  (0) 2022.12.20
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI Query Parameters - 08
  • FastAPI Path Parameters(추가) - 07
  • FastAPI 요청 메서드 로직 - 05
  • FastAPI Swagger / HTTP 데코레이터 - 04
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI Path Parameters(열거형) - 06
상단으로

티스토리툴바