generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from ministryofjustice/feature/LGA-2816-fast-a…
…pi-versioning Feature/lga 2816 fast api versioning
- Loading branch information
Showing
10 changed files
with
109 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
from fastapi import FastAPI | ||
from .routers import case_information, security | ||
from .config.docs import config as docs_config | ||
from fastapi_versionizer.versionizer import Versionizer | ||
|
||
|
||
def create_app() -> FastAPI: | ||
app = FastAPI(**docs_config) | ||
app.include_router(case_information.router) | ||
app.include_router(security.router) | ||
|
||
Versionizer( | ||
app=app, | ||
prefix_format="/v{major}", | ||
semantic_version_format="{major}", | ||
latest_prefix="/latest", | ||
sort_routes=True, | ||
).versionize() | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
title: API Versioning | ||
--- | ||
|
||
# API Versioning | ||
Version control helps manage changes to an API without breaking existing integrations or disrupting users relying on older versions. | ||
Version control in our API is done using URL versioning. | ||
|
||
## URL Versioning | ||
URL versioning involves specifying the version number in the endpoint url path. | ||
For example: | ||
``` | ||
/latest/cases | ||
/v1/cases/{case_id} | ||
``` | ||
|
||
All versioning is handled via the external python package fastapi-versionizer ```https://pypi.org/project/fastapi-versionizer/```. | ||
This handles all additional changes to routes. By default, fastapi-versionizer creates version 1 and adds all endpoints to it. | ||
|
||
### Updating the version | ||
To add a new version, add the @api_version to the router. This will update all routers with the new version and | ||
update the latest to this version. For example: | ||
|
||
``` | ||
from fastapi_versionizer.versionizer import api_version | ||
|
||
@api_version(2) | ||
@router.get( | ||
"/{case_id}", | ||
tags=["cases"], | ||
response_model=CaseResponse, | ||
dependencies=[Security(get_current_active_user, scopes=[UserScopes.READ])], | ||
) | ||
``` | ||
|
||
### Removing an endpoint | ||
To remove an endpoint, define the version the endpoint is no longer available in: | ||
|
||
``` | ||
from fastapi_versionizer.versionizer import api_version | ||
|
||
@api_version(1, remove_in_major=2) | ||
@router.get( | ||
"/{case_id}", | ||
tags=["cases"], | ||
response_model=CaseResponse, | ||
dependencies=[Security(get_current_active_user, scopes=[UserScopes.READ])], | ||
) | ||
``` | ||
|
||
The above example will remove the endpoint in version 2. This means that /v1/cases/{case_id} will still be available. | ||
|
||
### Deprecating an endpoint | ||
To deprecate an endpoint, add the following to the router: | ||
|
||
``` | ||
@router.get( | ||
"/{case_id}", | ||
tags=["cases"], | ||
response_model=CaseResponse, | ||
dependencies=[Security(get_current_active_user, scopes=[UserScopes.READ])], | ||
deprecated=True, | ||
) | ||
``` | ||
|
||
### Documentation | ||
All swaggerdocs in relation to the version can be seen by using the version suffix in the url, for example | ||
```http://0.0.0.0:8027/v1/``` or ```http://0.0.0.0:8027/latest/```. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ argon2_cffi | |
structlog | ||
typer | ||
tabulate==0.9.0 | ||
fastapi-versionizer==4.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters