Skip to main content

Helpers for implementing errors nicely with OpenAPI and FastAPI.

Project description

Identity Lib

This Python package contains utilities enabling nicer error descriptions in OpenAPI specs generated by FastAPI.

Use

Install fastapi-openapierrors using pip:

pip install fastapi-openapierrors

The module can then be used as fastapi_openapierrors to annotate routes:

from fastapi import FastAPI
from fastapi_openapierrors import NotFoundResponse, BadRequestResponse
from pydantic import BaseModel

app = FastAPI()

class Widget(BaseModel):
    name: str

@api.get(
    '/widget/{name}',
    summary="Get a single widget",
    description="Get a single widget by name",
    response_model=Widget,
    responses={400: BadRequestResponse, 404: NotFoundResponse}
)
async def get_widget(
      name: str = Path(
        ...,
        example=f"frobnicator2000",
        title="The name of the widget"
      )):
    # FastAPI automatically returns a Bad Request response if the request does
    # not match the route.

    # Look for widget by name.
    widget, was_found = get_widget_by_name(name)

    if not was_found:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)

    return widget

Developer quickstart

This project contains a dockerized testing environment which wraps tox.

Tests can be run using the ./test.sh command:

# Run all PyTest tests and Flake8 checks
$ ./test.sh

# Run just PyTest
$ ./test.sh -e py3

# Run a single test file within PyTest
$ ./test.sh -e py3 -- tests/test_identifiers.py

# Run a single test file within PyTest with verbose logging
$ ./test.sh -e py3 -- tests/test_identifiers.py -vvv

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-openapierrors-0.9.0.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

fastapi_openapierrors-0.9.0-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

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