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, ErrorResponse, ErrorResponseModel, Response, SuccessResponse
from fastapi import APIRouter, FastAPI, Request

# Initialize FastAPI
router = APIRouter()

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

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

# Routes
@router.get(
    "/",
    response_model=Response[Data],
    responses={
        400: {"model": ErrorResponseModel, "description": "Bad request"},
        500: {"model": ErrorResponseModel, "description": "Internal server error"},
    },
)
async def index(_: Request) -> Response[Data]:
    """Index route."""

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

@router.get(
    "/return-error",
    response_model=Response[Data],
    responses={
        HTTPStatus.FORBIDDEN: {
            "description": "User belongs to a different organization",
            "model": ErrorResponseModel,
            "content": {
                "application/json": {
                    "example": {
                        "success": False,
                        "error": "User belongs to a different organization",
                    },
                },
            },
        },
    },
)
async def error_route(_: Request) -> Response:
    """Error route."""

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

Note: When using OpenAPI generators, use SuccessResponse instead of Response if your endpoint has no data to return.

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.14.tar.gz (3.7 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.14-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_custom_responses-0.0.14.tar.gz
Algorithm Hash digest
SHA256 a7c94d34b97bd8425c61a3752fe5f47bcc021f71d71c29ca7d8cd2dda1fc1dac
MD5 7cbd42d53a7716301a3d558510ed434a
BLAKE2b-256 4c51349dd3ce4b684cc4011e32cb986a6fcf44c42654c4ec8ff24a9081fcdaf8

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_custom_responses-0.0.14.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.14-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_custom_responses-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 6622ff51c3e6778f482f3d87f2128fb71e772edd8af9863f473649bfdfb92ddb
MD5 6845a077f7b487dca38a6e474a6a89d0
BLAKE2b-256 611d581222055587ee9e11f7f699ae95ac1cdb5b34019998906ee6f665b48f4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_custom_responses-0.0.14-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