Skip to main content

A collection of utilities for running FastAPI applications at Explosion AI

Project description

FastAPI Extras

This library is a collection of utilities for running FastAPI applications at Explosion AI.

HttpizeErrorsAPIRouter

This custom router's main functionality is to handle errors per route instead of through a global exception handler by adding the httpize_errors keyword argument to the FastAPI route declaration. This allows each route to return normal informative Python errors instead of the FastAPI HTTPException class to get valid responses.

It also times each request and sets the X-Response-Time header on the Response

An example route

@router.get("/testing", httpize_errors={ValueError: 400})
def test_route(i: int):
    if i < 1:
        raise ValueError("Bad Input Data")
    return {"i": i}

If the ValueError is raised, this custom router knows to return a Response with a status code of 400 (Bad Request) and the message provided to the ValueError

Usage

FastAPI doesn't have built-in support for overriding the app Router, however this is required since we add a new keyword argument to the route declaration. FastAPI doesn't pass **kwargs forward, it only passes explict named keyword arguments.

To get around this, we need to overwrite the app router manually and refresh the routes after all of them have been included in the main app. This looks like:

from fastapi import FastAPI
from fastapi_extras import HttpizeErrorsAPIRouter, init_app
import uvicorn


# API Router (could be in another module)
api_router = HttpizeErrorsAPIRouter(tags=["tests"])


@api_router.get("/testing", httpize_errors={ValueError: 400})
def test_route(i: int):
    if i < 1:
        raise ValueError("Bad Input Data")
    return {"i": i}


# Main app definition
app = FastAPI()

# Overwrite App Router to use the custom HttpizeErrorsAPIRouter
app.router = HttpizeErrorsAPIRouter.from_app(app)

# Include API Router from above
app.include_router(api_router)

# Refresh the App (this rebuilds the Starlette Middleware Stack)
init_app(app)

uvicorn.run(app)

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

fastapi-explosion-extras-0.1.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file fastapi-explosion-extras-0.1.2.tar.gz.

File metadata

  • Download URL: fastapi-explosion-extras-0.1.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.9

File hashes

Hashes for fastapi-explosion-extras-0.1.2.tar.gz
Algorithm Hash digest
SHA256 84b571796c54c799d9dde1350ef2401220a69ca68d158f6f608030d66c89f8f4
MD5 58ff23c1a08a89090398dd74bf05d887
BLAKE2b-256 2cf5935bfeb2bc2e83b3d36d750f8c31bf7a7ccf587870d51179f2e8292532f1

See more details on using hashes here.

File details

Details for the file fastapi_explosion_extras-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fastapi_explosion_extras-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.9

File hashes

Hashes for fastapi_explosion_extras-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 09425313850626bd685b87010ddfd2ed69c20e9696eb5d51e378935e6fb5655b
MD5 c571085a3bf05c565e56c02cc8fd1e72
BLAKE2b-256 c6b3590b9a5d3cc059fac95c36d9333290c621f3f02de15c4beb085f8747b145

See more details on using hashes here.

Supported by

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