Skip to main content

No project description provided

Project description

verselect

FastAPI based framework to select an API version.

Battle-tested at Monite in production.

Explanation

At Monite, we are using header-based versioning, meaning that the client has to specify the version of the API it wants to use in the header in the YYYY-MM-DD format.

The issue with this approach is that when releasing a new API version, we need to support it in every service, which is a lot of work. verselect resolves this issue.

How it works

verselect supports waterflowing the requests to the latest version of the API if the request header doesn't match any of the versions.

If the app has two versions: 2022-01-02 and 2022-01-05, and the request header is 2022-01-03, then the request will be routed to 2022-01-02 version as it the closest version, but lower than the request header.

Exact match is always preferred over partial match and a request will never be matched to the higher versioned route

Also non-versioned routes and routers can be added for the endpoints, like webhooks

Usage

from contextvars import ContextVar
from datetime import date

from fastapi import APIRouter
import uvicorn
from starlette.responses import Response

from verselect import HeaderRoutingFastAPI

api_version_var: ContextVar[date] = ContextVar("api_version")
router = APIRouter()
webhook_router = APIRouter()


@router.get("/users")
def users():
    return Response(f"Hello from {api_version_var.get()}", media_type="text/plain")

@webhook_router.get("/webhooks")
def webhooks():
    return Response("webhooks", media_type="text/plain")

versions = [
    "2022-01-10",
    "2022-02-11",
    "1998-11-15",
    "2022-03-12",
    "2027-11-15",
    "2022-04-14",
]
mixed_hosts_app = HeaderRoutingFastAPI(
    api_version_header_name="X-API-Version",
    api_version_var=api_version_var,
)
for version in versions:
    mixed_hosts_app.add_header_versioned_routers(
        router,
        header_value=version,
    )
    mixed_hosts_app.add_unversioned_routers(webhook_router)

if __name__ == '__main__':
    uvicorn.run(mixed_hosts_app)

By running the app, at http://localhost:8000/docs you will see a dashboard with the available versions.

More examples can be found in the tests._resources folder.

Download files

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

Source Distribution

verselect-0.0.6.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

verselect-0.0.6-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file verselect-0.0.6.tar.gz.

File metadata

  • Download URL: verselect-0.0.6.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.4 Darwin/23.0.0

File hashes

Hashes for verselect-0.0.6.tar.gz
Algorithm Hash digest
SHA256 28af344795afe89f7bc25972e66b94e17c2f82a18a9847d8460607e30acf830e
MD5 520b07f41909268b5fcac3d8b118b5db
BLAKE2b-256 2647ca720aa7b480d98c122dd5fcd99da90a0bdd43c006e2cb48f4323a681ddd

See more details on using hashes here.

File details

Details for the file verselect-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: verselect-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.4 Darwin/23.0.0

File hashes

Hashes for verselect-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7ee29bd424bca89875997c1348bf500fdd3b589f749d51db0c560af10976f8b3
MD5 a73198b3623734c1cd3c456432c71305
BLAKE2b-256 28e0a9a185c5348eaffa76d955532fcf24506d1b6da32bde75edbb79368a1bc1

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