Python OpenApi Utilities for FastAPI
Project description
backbone-api
OpenAPI request and response models
Installation & Upgrade
pip install basalam.backbone-api
TODO List
- Add Message Toast Field
- Add Pagination Query Params Dependency
Usage Example
import uvicorn
from fastapi import APIRouter
from fastapi import FastAPI
from pydantic import BaseModel
from basalam.backbone_api.responses import (
ForbiddenResponse,
NotFoundResponse,
UnauthorizedResponse,
UnprocessableContentResponse,
BulkResponse, ConflictResponse
)
app = FastAPI()
class User(BaseModel):
id: int
name: str
router = APIRouter(responses={
401: {"model": UnauthorizedResponse},
403: {"model": ForbiddenResponse},
404: {"model": NotFoundResponse},
409: {"model": ConflictResponse},
422: {"model": UnprocessableContentResponse}
})
@router.get("/", response_model=BulkResponse[User])
async def root():
ls = [
User(id=1, name="John Doe"),
User(id=2, name="Jane Boe")
]
return BulkResponse(data=ls).as_json_response()
app.include_router(router)
if __name__=="__main__":
uvicorn.run(app, host="localhost", port=8000)
Using Exceptions
in app.py
from fastapi import FastAPI
from basalam.backbone_api.exceptions.client_error.handlers import client_error_exception_handler
from basalam.backbone_api.exceptions.client_error import (
ClientErrorException,
ForbiddenException,
UnauthorizedException,
ConflictException,
NotFoundException,
UnprocessableEntityException
)
app = FastAPI()
exception_handlers = {
ClientErrorException: client_error_exception_handler,
ForbiddenException: client_error_exception_handler,
UnauthorizedException: client_error_exception_handler,
ConflictException: client_error_exception_handler,
NotFoundException: client_error_exception_handler,
UnprocessableEntityException: client_error_exception_handler,
}
...
If you raise any of these exceptions everywhere in you FastAPI project FastAPI will return a client error response based on the excpetion.
Example Usage
def view_or_somthing_else():
raise ForbiddenException()
Credits
This project was inspired by the work of Mr.MohammadAli Soltanipoor on OpenAPI.
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 basalam_backbone_api-0.2.6.tar.gz.
File metadata
- Download URL: basalam_backbone_api-0.2.6.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af84206dfa0f83bf285f6221cb623b74b3e7565fd34cd86c110e34ca0658dfd9
|
|
| MD5 |
e016449ae2b67c72284d245fa5cd35eb
|
|
| BLAKE2b-256 |
35eaeee19ce7bb675efec0714179db8d344c0996917138460d6e2eda8b01e662
|
File details
Details for the file basalam_backbone_api-0.2.6-py3-none-any.whl.
File metadata
- Download URL: basalam_backbone_api-0.2.6-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c024ceea841f9be76955ffa75ee1c93b894cbffe51cf9ee20cf0330cf83b7601
|
|
| MD5 |
6108f94627c609fd5948b51aac8147b2
|
|
| BLAKE2b-256 |
a20c7e597145065a143b3fb3a9bc073691f80a8f69033e7a6cfd35096916d431
|