Skip to main content

Flask extend headers module for API versioning.

Project description

flask-extend-headers

Flask extend headers module for API versioning.

PyPI: flask-extend-headers

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.0.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.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for Flask-extend-headers-1.1.0.tar.gz
Algorithm Hash digest
SHA256 29ac6848f732d3ba3aa6b012e21e56fee6f197ec11b8bb8ec90f907d6092e4ac
MD5 7b9475f9ec3a8e6c27d327d355098bf2
BLAKE2b-256 553cd441aa6d096aa41a3a19d55b5433eae9b7356518cbc6b5269292eacec82f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Flask_extend_headers-1.1.0-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.9.7 Linux/5.8.0-1042-azure

File hashes

Hashes for Flask_extend_headers-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41cc123dc96e208f81de74cb0212b6e51c56c9ccae68aaed602d4ffba9b48618
MD5 45ff73e955dbe102c0cfc37e3dfe9817
BLAKE2b-256 934133a179dd898d90c05913a659db7b8485e143d5d38fc55c287f8b3e57013a

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