flask-extend-headers
Flask extend headers module for API versioning.
Description
Custom header routing for versioning your Flask API.
Features
- Use it as a decorator
- Return with different views based on the custom header you set
Documentation
Installation
pip install flask-extend-headers
Quickstart
Below is an example of two API endpoints with different URL with version:
from flask import Flask
app = Flask(__name__)
@app.route('/api/v2/users')
def usersv2():
return "usersv2", 200
@app.route('/api/v1/users')
def users():
return "users", 200
if __name__ == '__main__':
app.run()
We could change this implementation using flask_extend_headers and specifying the version in the headers
from flask import Flask
from flask_extend_headers import ExtendHeaders
app = Flask(__name__)
app.config["EXTEND_HEADERS_KEY"] = "accept-version"
extend_headers = ExtendHeaders(app)
def usersv2():
return "usersv2", 200
@app.route('/api/users')
@extend_headers.register(
extensions={
"application/v2": usersv2,
},
default="application/v1"
)
def users():
return "users", 200
if __name__ == '__main__':
app.run()
If we call this API it'll return a 406:
> curl http://localhost:5000/api/users -I
HTTP/1.0 406 NOT ACCEPTABLE
Content-Type: text/html; charset=utf-8
Content-Length: 350
Server: Werkzeug/2.0.2 Python/3.9.6
Date: Mon, 18 Oct 2021 19:33:46 GMT
If we add the headers it'll return users:
> curl http://localhost:5000/api/users -I -H "Accept-Version: application/v1"
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 5
Server: Werkzeug/2.0.2 Python/3.9.6
Date: Mon, 18 Oct 2021 19:34:55 GMT
If we modify the headers it'll return usersv2
> curl http://localhost:5000/api/users -I -H "Accept-Version: application/v2"
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 5
Server: Werkzeug/2.0.2 Python/3.9.6
Date: Mon, 18 Oct 2021 19:35:48 GMT
Fallback on view
If you have a new version of a view but you want to fallback in a view without a specified version:
def usersv2():
return "usersv2", 200
@app.route('/api/users')
@extend_headers.register(
extensions={
"application/v2": usersv2,
}
)
def users():
return "users", 200
If we call the endpoint without headers it'll return the fallback view users:
> curl http://localhost:5000/api/users -I
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 5
Server: Werkzeug/2.0.2 Python/3.9.6
Date: Mon, 18 Oct 2021 19:42:05 GMT
If we call the endpoint specifying headers it'll return usersv2:
> curl http://localhost:5000/api/users -I -H "Accept-Version: application/v2"
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 7
Server: Werkzeug/2.0.2 Python/3.9.6
Date: Mon, 18 Oct 2021 19:42:36 GMT
Testing
Install poetry and execute pytest-cov
pip install poetry
poetry install
poetry run pytest --cov=flask_extend_headers tests
License
MIT
Metadata
Release files for flask-extend-headers 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Flask-extend-headers-1.1.1.tar.gz | 4.3 kB | Without provenance | Details for Flask-extend-headers-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Flask_extend_headers-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for Flask_extend_headers-1.1.1-py3-none-any.whl |
Total release size: 8.1 kB
Release files / Flask-extend-headers-1.1.1.tar.gz
| Download URL | Flask-extend-headers-1.1.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d3a2ba0e0ed44834b3d5e04a4c460a983e9e5a85d1dcd518c4b79cbf0a2ac7c6
|
|
BLAKE2b-256 checksum How to use checksums |
7b82f89d0300645d1ebc6aa16d6c853ef5f54e0730f053bf1c1ad3099597eb29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.11 CPython/3.10.0 Linux/5.8.0-1042-azure
|
Release files / Flask_extend_headers-1.1.1-py3-none-any.whl
| Download URL | Flask_extend_headers-1.1.1-py3-none-any.whl |
|---|---|
| Size | 3.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d3a6a2e447a777f1e94777dceecdc8b061beed2dc4bc572b5f840aefe352bb7
|
|
BLAKE2b-256 checksum How to use checksums |
3045baa60aff04f0fee615fa3ae80e2e7fd4de14da06e84a6921bb00b241b503
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.11 CPython/3.10.0 Linux/5.8.0-1042-azure
|
Security for flask-extend-headers 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.