如何设置快速 API 版本以允许 HTTP 可以在 accept header 中指定版本?

how to set fast API version to allow HTTP can specify version in accept header?

我正在从事一个需要我对 API 端点进行快速版本化的项目。我们想通过 HTTP accept header 对端点进行版本控制,例如headers={'Accept': 'application/json;version=1.0.1'}, headers={'Accept': 'application/json;version=1.0.2'}。像这样只设置 api 版本似乎不起作用:

app = FastAPI(
        version=version,
        title="A title",
        description="Some description.",
    )

有人知道我还需要用这个做什么吗?

嗯,也许路径 url 中的版本会更好

sub apps docs

from fastapi import FastAPI

app = FastAPI()
v1 = FastAPI()

@v1.get("/app/")
def read_main():
    return {"message": "Hello World from api v1"}

v2 = FastAPI()

@v2.get("/app/")
def read_sub():
    return {"message": "Hello World from  api v2"}

app.mount("/api/v1", v1)
app.mount("/api/v2", v2)

您将看到每个应用程序的自动文档

localhost:8000/api/v1/docs

localhost:8000/api/v2/docs

但是你总是在请求中得到 headers

from starlette.requests import Request

from fastapi import FastAPI
app = FastAPI()

@app.post("/hyper_mega_fast_service")
def fast_service(request: Request, ):

    aceept = request.headers.get('Accept')

    value = great_fuction_to_get_version_from_header(aceept)
    if value == '1.0.1': 
        "Do something"
        
    if value == '1.0.2': 
        "Do something"

尝试 api 快速版本控制api Web 应用程序

安装

pip install fastapi-versioning

例子

from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI, version

app = FastAPI(title="My App")


@app.get("/greet")
@version(1, 0)
def greet_with_hello():
    return "Hello"


@app.get("/greet")
@version(1, 1)
def greet_with_hi():
    return "Hi"


app = VersionedFastAPI(app)

这将生成两个端点:

/v1_0/greet
/v1_1/greet

以及:

/docs
/v1_0/docs
/v1_1/docs
/v1_0/openapi.json
/v1_1/openapi.json

还有可能添加一组额外的端点 重定向最新的 API 版本。为此,提出论点 enable_latest 正确:

app = VersionedFastAPI(app, enable_latest=True)

这将生成以下附加端点:

/latest/greet
/latest/docs
/latest/openapi.json

在此示例中,/latest 个端点将反映与 /v1.1 相同的数据。

试试看:

pip install pipenv
pipenv install --dev
pipenv run uvicorn example.annotation.app:app
# pipenv run uvicorn example.folder_name.app:app

没有小版本的使用

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

超快API 构造函数参数

重要的是要注意只有来自原始 FastAPI 的 title 提供给 VersionedAPI 应用程序。如果你有任何中间件,事件处理程序 等等,这些参数也需要提供给 VersionedAPI 函数 调用,如下例所示

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')
    ]
)