https://github.com/mxnoob/fastapi-header-versions
https://github.com/mxnoob/fastapi-header-versions
fastapi
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mxnoob/fastapi-header-versions
- Owner: mxnoob
- License: mit
- Created: 2025-03-07T09:30:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-07T11:13:19.000Z (over 1 year ago)
- Last Synced: 2025-03-07T11:34:38.562Z (over 1 year ago)
- Topics: fastapi
- Language: Python
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastapi-header-versions
This package adds versioning by Accept-header into FastAPI
### Installation
```shell
pip install fastapi-header-versions
```
### Defining app and routes
```python
from enum import StrEnum
import fastapi
from fastapi_header_version import VersionedRouter, InlineVersionedRouter, init_fastapi_versioning
class AppType(StrEnum):
some_name: "some.name"
some_name2: "some.name2"
router = VersionedRouter()
inline_router = InlineVersionedRouter()
@router.get("/test/")
@router.set_api_version((1, 0), app_names={AppType.some_name, AppType.some_name2})
async def test_get() -> dict:
return {"version": (1, 0)}
@inline_router.get("/test/", version=1, app_names=AppType.some_name)
async def test_get_v1() -> dict:
return {"version": (2, 0)}
@inline_router.get("/test/", version=(2, 0), app_names=AppType.some_name)
async def test_get_v2() -> dict:
return {"version": (2, 0)}
app = fastapi.FastAPI()
app.include_router(router)
app.include_router(inline_router)
init_fastapi_versioning(app=app)
```
### Query Examples
```bash
# call 1.0 version
curl -X 'GET' 'https://test.ru/test/' -H 'accept: application/vnd.some.name+json; version=1.0'
# call 2.0 version
curl -X 'GET' 'https://test.ru/test/' -H 'accept: application/vnd.some.name+json; version=2.0'
```