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
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_custom_responses-0.0.13.tar.gz.
File metadata
- Download URL: fastapi_custom_responses-0.0.13.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89a62265f29d5fa18096096ae295f129505837dcc63f73eb97c19442fd209220
|
|
| MD5 |
a79d76a266b53f4d33b31a3c23cbc3ee
|
|
| BLAKE2b-256 |
a951a42e93a14dd1b54993eed3ddd3bb202d0e26cd04c49eec233c4b40748a48
|
Provenance
The following attestation bundles were made for fastapi_custom_responses-0.0.13.tar.gz:
Publisher:
publish-to-pypi.yml on julien777z/fastapi-custom-responses
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_custom_responses-0.0.13.tar.gz -
Subject digest:
89a62265f29d5fa18096096ae295f129505837dcc63f73eb97c19442fd209220 - Sigstore transparency entry: 745008727
- Sigstore integration time:
-
Permalink:
julien777z/fastapi-custom-responses@4bad467f96427368c474d00ad2907308b522a708 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@4bad467f96427368c474d00ad2907308b522a708 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file fastapi_custom_responses-0.0.13-py3-none-any.whl.
File metadata
- Download URL: fastapi_custom_responses-0.0.13-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9c85d020565d76d5de863a0910d064aad233dad9ee10865471ae558880b517e
|
|
| MD5 |
68e6f9ef556988d2e454ef9124cb8c2b
|
|
| BLAKE2b-256 |
8956d37be226f9b46ffe2cfeed022c297e312801573a78a9cc2a262e7f2e7448
|
Provenance
The following attestation bundles were made for fastapi_custom_responses-0.0.13-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on julien777z/fastapi-custom-responses
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_custom_responses-0.0.13-py3-none-any.whl -
Subject digest:
b9c85d020565d76d5de863a0910d064aad233dad9ee10865471ae558880b517e - Sigstore transparency entry: 745008750
- Sigstore integration time:
-
Permalink:
julien777z/fastapi-custom-responses@4bad467f96427368c474d00ad2907308b522a708 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@4bad467f96427368c474d00ad2907308b522a708 -
Trigger Event:
workflow_dispatch
-
Statement type: