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()
Application error codes
All client-error exception constructors accept the optional, keyword-only
code argument. It is returned as errors[].code so clients can distinguish
errors without matching message text. Existing calls default to 0; HTTP
statuses and response structure are unchanged.
raise ForbiddenException(message="Reason is not accessible", code=40304)
raise NotFoundException(code=40401)
raise UnprocessableEntityException(
message="Campaign is required",
fields=["campaign_id"],
code=42203,
)
raise ConflictException(data=[{"id": 123}], code=40901)
Codes are chosen by the application, not assigned by this library.
BadRequestException (400), PaymentRequiredException (402), and
TooManyRequestsException (429) are also available from
basalam.backbone_api.exceptions and basalam.backbone_api.exceptions.client_error.
Each accepts optional message, fields, and keyword-only code arguments:
from basalam.backbone_api.exceptions import BadRequestException, TooManyRequestsException
raise BadRequestException(message="Insufficient balance", fields=["amount"], code=40001)
raise TooManyRequestsException(code=42901)
Registering the same client_error_exception_handler handles these exceptions
with the correct HTTP status and preserves their error fields. The corresponding
BadRequestResponse, PaymentRequiredResponse, and TooManyRequestsResponse
models are exported from basalam.backbone_api.responses for OpenAPI declarations.
UnauthorizedException and the base ClientErrorException also accept code.
For ClientErrorException(http_status=400, code=40001), the code is applied to
the default error detail. If an explicit errors list is supplied, its details
and individual codes are preserved instead:
from basalam.backbone_api.exceptions.client_error import ClientErrorException, ErrorDetail
raise ClientErrorException(
http_status=422,
errors=[
ErrorDetail(code=42210, message="Field is required", fields=["amount"]),
ErrorDetail(code=42211, message="Invalid integer", fields=["user_id"]),
],
)
Credits
This project was inspired by the work of Mr.MohammadAli Soltanipoor on OpenAPI.
Metadata
Release files for basalam.backbone-api 0.2.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| basalam_backbone_api-0.2.11.tar.gz | 13.3 kB | Without provenance | Details for basalam_backbone_api-0.2.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| basalam_backbone_api-0.2.11-py3-none-any.whl | Python 3 | none | any | Details for basalam_backbone_api-0.2.11-py3-none-any.whl |
Total release size: 34.8 kB
Release files / basalam_backbone_api-0.2.11.tar.gz
| Download URL | basalam_backbone_api-0.2.11.tar.gz |
|---|---|
| Size | 13.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f419006a15a3ab55db387f3bed16bab5baadd24332867f94d3ec2cdf6c467917
|
|
BLAKE2b-256 checksum How to use checksums |
eb16d3f094ef04c2a95e807d467a8c8b7b1d5980e0020189cbd226cb389fdba8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / basalam_backbone_api-0.2.11-py3-none-any.whl
| Download URL | basalam_backbone_api-0.2.11-py3-none-any.whl |
|---|---|
| Size | 21.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0213731081dd9545af039c593f7bed08249d81bd632d865b3af95a55570f5709
|
|
BLAKE2b-256 checksum How to use checksums |
e1a8a22600140226f4bccc68234bb2e5d9912b5135429d78f5596ae000c4ebed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for basalam.backbone-api 0.2.11
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.