Skip to main content

FastAPI Custom Responses

Project description

FastAPI Custom Responses

Provides normalized response objects and error handling

Example

from http import HTTPStatus
from fastapi_custom_responses import EXCEPTION_HANDLERS, Response
from fastapi import APIRouter

# Initialize FastAPI
router = APIRouter()

app = FastAPI(
    title="API",
    description="My API",
    version="1.0.0",
    lifespan=lifespan,
    exception_handlers=EXCEPTION_HANDLERS, # Use error handler from library
)

# Define data model
class Data(Response):
    example: str

# Routes
@router.get(
    "/",
    response_model=Response[Data], # Use Data model
)
async def index(_: FastAPIRequest) -> Response[Data]:
    """Index route."""

    return Response(
        success=True,
        data=Data(example="hello"),
    )

@router.get(
    "/return-error",
    response_model=Response[Data], # Use Data model
)
async def error_route(_: FastAPIRequest) -> Response:
    """Index route."""

    raise ErrorResponse(error="Your request is invalid.", status_code=HTTPStatus.BAD_REQUEST)

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_custom_responses-0.0.6.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

fastapi_custom_responses-0.0.6-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_custom_responses-0.0.6.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_custom_responses-0.0.6.tar.gz
Algorithm Hash digest
SHA256 b6fbb1aba7e3aecf4cc0207ced1c40f2703ebe44cbbf92953916d6a9fad67fa1
MD5 1506d4afcd19a42dbc95ffd223aaf971
BLAKE2b-256 2acdc61e613b2a0911fcd7f9fa2474a033e6c7bc2e74f463e0f395d18edd5797

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_custom_responses-0.0.6.tar.gz:

Publisher: publish-to-pypi.yml on julien777z/fastapi-custom-responses

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for fastapi_custom_responses-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 162170990cff8c5d59a2663c90dca5eb18219f5beba5f9783b23f7dff0e9c670
MD5 55bcf8fcc955a4d6f11cf280eecad8c1
BLAKE2b-256 6b3c68f781f41f84b75bb6dd88cbfa5ed65c05599a03850bc6a8d9b82c9c2e29

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_custom_responses-0.0.6-py3-none-any.whl:

Publisher: publish-to-pypi.yml on julien777z/fastapi-custom-responses

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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