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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file fastapi-openapierrors-0.9.0.tar.gz.

File metadata

  • Download URL: fastapi-openapierrors-0.9.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for fastapi-openapierrors-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4a053f84ac99097c15a5bf370c407c4fdbcdefd3f5967b97cc2f2d4dec4d39da
MD5 188ac1eb8feb0c1067ae39640a6f18a0
BLAKE2b-256 3d496427c124678eb2044f110d7bb14dc5c7954bec6f9acf4e009448e7bddc28

See more details on using hashes here.

File details

Details for the file fastapi_openapierrors-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_openapierrors-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for fastapi_openapierrors-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aefa0769a333cf73c406d01e5188b1c1427a7e960c440491b8d46af67964360b
MD5 357dead1799377179307bb16d61711b9
BLAKE2b-256 0401b89238396b943ced9e1e39f68523b75975b2b3167584826205d94beae702

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