Programming Language/Python

[FastAPI] 2. 경로 매개변수(Path Parameters)

LeeJaeJun 2024. 6. 18. 23:26
728x90
반응형

경로 매개변수(Path parameters)

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id):
    return {"item_id": item_id}
  • path에 {}로 둘러싸인 부분은 path parameter를 나타냄
    • /items/{item_id}에서 {item_id}는 path paramter
  • URL 경로의 일부로서 변수 값을 전달받을 수 있도록 함
from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
  • path parameter를 type과 함께 선언 가능
    • 기본적으로 URL에서 읽어들인 값들은 string 타입임
    • item_id: int 로 선언하게 되면 기본 타입이 int형이 되어 string이 아닌 int형으로 data가 변환
    • http://127.0.0.1:8000/items/42 이라는 요청이 오면 item_id에는 '42'가 아닌 int형인 42가 됨
  • 만약 일치하지 않는 타입의 값이 들어오게 된다면 다음과 같은 에러가 발생(Data validation 역할)
{
  "detail": [
    {
      "type": "int_parsing",
      "loc": [
        "path",
        "item_id"
      ],
      "msg": "Input should be a valid integer, unable to parse string as an integer",
      "input": "foo",
      "url": "https://errors.pydantic.dev/2.1/v/int_parsing"
    }
  ]
}
  • float형의 경우 int형으로 변환되지 않기 때문에 주의
from fastapi import FastAPI

app = FastAPI()


@app.get("/users/me")
async def read_user_me():
    return {"user_id": "the current user"}


@app.get("/users/{user_id}")
async def read_user(user_id: str):
    return {"user_id": user_id}
  • path를 선언할 때 순서 중요
  • 이 예시에서 . /users/me 경로를 먼저 선언하지 않으면, /users/{user_id} 경로가 /users/me 경로와 충돌
    • 이는 /users/{user_id} 경로가 /users/me 경로를 "me"라는 user_id 값을 가진 것으로 인식하게 되기 때문
  • 즉, 경로가 같으면(같은 것으로 해석할 수 있다면) 먼저 정의된 부분이 실행됨
from fastapi import FastAPI

app = FastAPI()


@app.get("/users")
async def read_users():
    return ["Rick", "Morty"]


@app.get("/users")
async def read_users2():
    return ["Bean", "Elfo"]
  • 이 경우 read_user2()는 실행되지 않을 것임
  • /users로 들어온 요청은 먼저 선언된 read_users()함수로 다 처리되기 때문

 

Enum 클래스로 Path Parameter 값 미리 정의하기

from enum import Enum

from fastapi import FastAPI


class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name is ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}

    return {"model_name": model_name, "message": "Have some residuals"}
  • path paramteter 값으로 특정한 값을 지정해서 그 값들만 받고 싶다면 Enum 클래스를 활용
  • .value를 사용해서 접근하면 실제 값을 얻을 수 있음
    • model_name.value를 통해서 model_name에 해당하는 valued의 실제 값(여기서는 string 타입의 값)을 얻을 수 있음
  1. Enum 클래스 Import
  2. 값으로 받을 타입과 Enum을 상속하는 sub-class 생성
    • API 문서가 값이 해당 타입임을 인식하고 올바르게 렌더링하기 위해서 값으로 받을 타입도 같이 상속
    • 위 코드에서는 값으로 string이 들어가니까 str 상속
  3. 생성한 class 안에 고정된 값들을 정의
    • alexnet = "alexnet"
    • resnet = "resnet"
    • lenet = "lenet"
  4. 경로 매개변수를 선언할 때, Enum 클래스를 타입으로 지정
    • model_name: ModelName

 

Path parameter로 경로 받기

  • path parameter에서는 / 단위로 처리가 되기 때문에 경로를 값으로 넣고 싶어도 제대로 처리되지 않음
from fastapi import FastAPI

app = FastAPI()

@app.get("/files/{file_path}")
async def read_file(file_path: str):
    return {"file_path": file_path}
  • /files/home/johndoe/myfile.txt라고 요청이 왔다고 하면 이 경우 file_path 매개변수는 'home'이라는 단일 값만가지게 되고, 나머지 /johndoe/myfile.txt는 새로운 path segment로 인식하게 되어 처리되지 않음
from fastapi import FastAPI

app = FastAPI()

@app.get("/files/{file_path:path}")
async def read_file(file_path: str):
    return {"file_path": file_path}
  • 경로 변환기(:path)를 사용하여 path parameter에 /를 포함한 값을 전달할 수 있음
  • 이 경우 file_path는 home/johndoe/myfile.txt라는 값을 가지게 됨
/files//home/johndoe/myfile.txt
  • 만약 파일 경로가 /home/johndoe/myfile.txt와 같이 /로 시작해야하는 경우에는 위와 같이 이중 슬래시를 사용하여 함

https://fastapi.tiangolo.com/tutorial/path-params/

 

Path Parameters - FastAPI

FastAPI framework, high performance, easy to learn, fast to code, ready for production

fastapi.tiangolo.com

 

728x90
반응형