Справочник: middleware¶
Механика, которая объявляет версии, находит их и наследует между ними роуты. Импортируется из корня пакета:
from fastapi_easy_versioning import (
API_VERSION_KEY,
VersioningMiddleware,
rebuild_versioning,
versionize,
)
VersioningMiddleware¶
fastapi_easy_versioning.VersioningMiddleware
¶
Middleware that provides and manages versioned APIs.
Adds support for versioned APIs by mounting version-specific subapplications
under a single FastAPI instance. On the first ASGI event it inherits versioned
endpoints from older versions into newer ones and regenerates each version's
OpenAPI schema. Requests themselves are routed by the mounts; use
rebuild_versioning to pick up
routes added at runtime.
Add it to the application that directly mounts the version sub-apps —
each declared with versionize — never
to the sub-apps themselves. Several instances may coexist in one application
tree — each versions only the sub-apps mounted directly under its own app, so
independent APIs version separately.
Only endpoints marked with
versioning are inherited, and a
version that declares its own endpoint on the same path shadows the inherited
one — at runtime and in the OpenAPI schema alike.
Usage
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
ASGIApp
|
The wrapped application, supplied by |
required |
rebuild_openapi
|
bool
|
Regenerate each version's OpenAPI schema
after inheritance. With |
True
|
versionize¶
fastapi_easy_versioning.versionize
¶
Declare the API version of a sub-application.
Marks app as version version for the
VersioningMiddleware of the
application it gets mounted under, and returns the very same object, so the
call can wrap the app inline. Not to be confused with
versioning: versionize declares
which version an application is, versioning() marks which of its endpoints
are inherited by later versions.
The declaration is read when versioning is built — on the middleware's first
ASGI event. A version declared or mounted after that needs an explicit
rebuild_versioning call.
Usage
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The version sub-application; it is modified in place. |
required |
version
|
int
|
The version number. Any |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FastAPI |
_AppT
|
The same |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
ValueError
|
|
rebuild_versioning¶
fastapi_easy_versioning.rebuild_versioning
¶
Build (or explicitly rebuild) versioned routes of an application.
VersioningMiddleware calls
this once on its first ASGI event — the lifespan startup under a real server,
the first request when the middleware sits on a mounted sub-app or in a test
client. Nothing is versioned before that, and anything registered afterwards
needs an explicit call:
The call is idempotent, and a default (undeclared) until is re-resolved
against the new latest version. An application that mounts no version sub-app
is a graceful no-op.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Starlette
|
The application that directly mounts the version sub-apps. |
required |
rebuild_openapi
|
bool
|
Regenerate each version's OpenAPI schema
after inheritance. Defaults to |
True
|
API_VERSION_KEY¶
fastapi_easy_versioning.API_VERSION_KEY
module-attribute
¶
Name of the deprecated FastAPI() keyword that declares a sub-app's version.
Declare versions with versionize
instead. On FastAPI < 0.145 unknown keywords still land in FastAPI.extra,
which is where the middleware reads this one from when the app has no
versionize declaration. The value must then be an int (0 is valid, bool
is not); a value of another type emits a UserWarning and leaves the app out
of versioning.
Warning
Deprecated since 0.4.2. FastAPI 0.145 removed **extra, so the keyword
only works on FastAPI < 0.145, and declaring a version with it emits a
FutureWarning. The keyword and this constant will be removed together
with support for FastAPI < 0.145.