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
Built Distribution
File details
Details for the file fastapi-explosion-extras-0.4.0.tar.gz
.
File metadata
- Download URL: fastapi-explosion-extras-0.4.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 466feab548c23d7d0a764fd9e88a6d610e800b14a2d894c37221b7bc2f6c54b4 |
|
MD5 | 42878dfcc7fac353bac0d62f11f0734c |
|
BLAKE2b-256 | 42728c19ee65c6e95646039c8d8c2c34c12e687bc5279589dff71b6267a23573 |
File details
Details for the file fastapi_explosion_extras-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_explosion_extras-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5b11ade0341914fcd9a2e22edeaa492134ea3c9590f299d2b343b87d30ace4e |
|
MD5 | 1328b101b93d4856f5108925795d955f |
|
BLAKE2b-256 | 77461c032d0f1a63500b6a3a49cb8bc3cf0130bfc60054aa7a471f351854e120 |