참조: https://pythonrepo.com/repo/DeanWay-fastapi-versioning-python-fastapi-projects
참조: https://medium.com/geoblinktech/fastapi-with-api-versioning-for-data-applications-2b178b0f843f
FastAPI 서버 API 버전 관리
정리. 수알치 오상문
1. fastapi-versioning 패키지를 설치한다.
pip install fastapi-versioning
2. 예제 테스트
from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI, version
app = FastAPI(title="My App")
@app.get("/greet")
@version(1, 0) # 버전 v1_0
def greet_with_hello():
return "Hello"
@app.get("/greet")
@version(1, 1) # 버전 v1_1
def greet_with_hi():
return "Hi"
app = VersionedFastAPI(app)
이제 API 서비스 접속은 다음처럼 가능하다.
/v1_0/greet
/v1_1/greet
그리고 아래처럼 문서나 스웨거 서비스도 지원한다.
/docs
/v1_0/docs
/v1_1/docs
/v1_0/openapi.json
/v1_1/openapi.json
최종 버전에 접속하는 경로를 지원하고 싶으면 아래처럼 처리한다.
app = VersionedFastAPI(app, enable_latest=True)
그러면 아래처럼 최종 버전을 지원하는 API 경로를 지원한다.
/latest/greet
/latest/docs
/latest/openapi.json
만약 메이저 버전 V1, V2 형식으로 지원하고 싶다면 아래처럼 처리한다.
from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI, version
app = FastAPI(title='My App')
@app.get('/greet')
@version(1)
def greet():
return 'Hello'
@app.get('/greet')
@version(2)
def greet():
return 'Hi'
app = VersionedFastAPI(app,
version_format='{major}',
prefix_format='/v{major}')
이제 마이너 버전 표기 없이 메이저 버전으로 처리할 수 있다.
/v1/greet
/v2/greet
...
/docs
/v1/docs
/v2/docs
/v1/openapi.json
/v2/openapi.json
[참고] Extra FastAPI constructor arguments
------------------------------------------------------------------------------------
It's important to note that only the title from the original FastAPI will be provided to the VersionedAPI app. If you have any middleware, event handlers etc these arguments will also need to be provided to the VersionedAPI function call, as in the example below
from fastapi import FastAPI, Request
from fastapi_versioning import VersionedFastAPI, version
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware
app = FastAPI(
title='My App',
description='Greet uses with a nice message',
middleware=[
Middleware(SessionMiddleware, secret_key='mysecretkey')
]
)
@app.get('/greet')
@version(1)
def greet(request: Request):
request.session['last_version_used'] = 1
return 'Hello'
@app.get('/greet')
@version(2)
def greet(request: Request):
request.session['last_version_used'] = 2
return 'Hi'
@app.get('/version')
def last_version(request: Request):
return f'Your last greeting was sent from version {request.session["last_version_used"]}'
app = VersionedFastAPI(app,
version_format='{major}',
prefix_format='/v{major}',
description='Greet users with a nice message',
middleware=[
Middleware(SessionMiddleware, secret_key='mysecretkey')
]
)
-------------------------------------------------------------------------
'FastAPI' 카테고리의 다른 글
FastAPI 세 가지 에러 핸들링 방법 (0) | 2022.01.23 |
---|---|
gunicorn 설정 (0) | 2022.01.12 |
FastAPI + HTTPS (Windows) (0) | 2021.12.30 |
Fast API, HTTPS (Fast API + Traefik + Webserver) (0) | 2021.12.30 |
채팅 서버를 위한 FastAPI와 WebSocket (0) | 2021.12.28 |