Skip to main content

Flask extend headers module for API versioning.

Project description

flask-extend-headers

Flask extend headers module for API versioning.

PyPI version

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-extend-headers-1.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_extend_headers-1.1.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file Flask-extend-headers-1.1.1.tar.gz.

File metadata

  • Download URL: Flask-extend-headers-1.1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Linux/5.8.0-1042-azure

File hashes

Hashes for Flask-extend-headers-1.1.1.tar.gz
Algorithm Hash digest
SHA256 d3a2ba0e0ed44834b3d5e04a4c460a983e9e5a85d1dcd518c4b79cbf0a2ac7c6
MD5 3b6f95d6856d7ef93c33f2412a74a421
BLAKE2b-256 7b82f89d0300645d1ebc6aa16d6c853ef5f54e0730f053bf1c1ad3099597eb29

See more details on using hashes here.

File details

Details for the file Flask_extend_headers-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: Flask_extend_headers-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Linux/5.8.0-1042-azure

File hashes

Hashes for Flask_extend_headers-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d3a6a2e447a777f1e94777dceecdc8b061beed2dc4bc572b5f840aefe352bb7
MD5 2c69b61bfd44c56dcdf179d874f872c3
BLAKE2b-256 3045baa60aff04f0fee615fa3ae80e2e7fd4de14da06e84a6921bb00b241b503

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page