Generic and common response schemas for FastAPI
Project description
☄️ FastAPI Response Schema
Overview
This package extends the FastAPI response model schema allowing you to have a common response wrapper via a fastapi.routing.APIRoute
.
Getting started
Install the package
pip install fastapi-responseschema
If you are planning to use the pagination integration, you can install the package including fastapi-pagination
pip install fastapi-responseschema[pagination]
Usage
from typing import Generic, TypeVar, Any, Optional, List
from pydantic import BaseModel
from fastapi import FastAPI
from fastapi_responseschema import AbstractResponseSchema, SchemaAPIRoute, wrap_app_responses
# Build your "Response Schema"
class ResponseMetadata(BaseModel):
error: bool
message: Optional[str]
T = TypeVar("T")
class ResponseSchema(AbstractResponseSchema[T], Generic[T]):
data: Any
meta: ResponseMetadata
@classmethod
def from_exception(cls, reason, status_code, message: str = "Error", **others):
return cls(
data=reason,
meta=ResponseMetadata(error=status_code >= 400, message=message)
)
@classmethod
def from_api_route_params(
cls, content: Any, status_code: int, description: Optional[str] = None, **others
):
return cls(
data=content,
meta=ResponseMetadata(error=status_code >= 400, message=description)
)
# Create an APIRoute
class Route(SchemaAPIRoute):
response_schema = ResponseSchema
# Integrate in FastAPI app
app = FastAPI()
wrap_app_responses(app, Route)
class Item(BaseModel):
id: int
name: str
@app.get("/items", response_model=List[Item], description="This is a route")
def get_operation():
return [Item(id=0, name="ciao"), Item(id=1, name="hola"), Item(id=1, name="hello")]
Te result of GET /items
:
HTTP/1.1 200 OK
content-length: 131
content-type: application/json
{
"data": [
{
"id": 0,
"name": "ciao"
},
{
"id": 1,
"name": "hola"
},
{
"id": 1,
"name": "hello"
}
],
"meta": {
"error": false,
"message": "This is a route"
}
}
Contributing
Contributions are very welcome!
How to contribute
Just open an issue or submit a pull request on GitHub.
While submitting a pull request describe what changes have been made.
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
File details
Details for the file fastapi-responseschema-1.1.0.tar.gz
.
File metadata
- Download URL: fastapi-responseschema-1.1.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.8.13 Linux/5.15.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63cd17958b91074629e5a848f5b9ecb1082a9d8f4bcb6bc2c43d2dd9b6c5baad |
|
MD5 | b082166338d61d264aa498e47689ceae |
|
BLAKE2b-256 | c3315f73cb20edcac7ef13c7bcb02d01f3bf892dde2800423bbd0fb2c0e42fe2 |
File details
Details for the file fastapi_responseschema-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_responseschema-1.1.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.8.13 Linux/5.15.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc7fe7bca2e96886be6194ca34843e41d6149a41eb1fc509288e746e36361ca1 |
|
MD5 | c63b35651d0056ba2d2fe73c00a5127e |
|
BLAKE2b-256 | 4296450571febf5ecc2095434e50e7d2989a858885d8996ba6135f4caf974854 |