Skip to main content

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

basalam_backbone_api-0.2.8.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

basalam_backbone_api-0.2.8-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file basalam_backbone_api-0.2.8.tar.gz.

File metadata

  • Download URL: basalam_backbone_api-0.2.8.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for basalam_backbone_api-0.2.8.tar.gz
Algorithm Hash digest
SHA256 341c68bba09221072ec06018d7b594200e128c391770d35abeaea1d76baad608
MD5 9f1f2022102709b1bfe2531195ac2f23
BLAKE2b-256 8c5a8284f01b8270920c7f02f2d1f44720a2e7964d18dde41041be5f94a6791c

See more details on using hashes here.

File details

Details for the file basalam_backbone_api-0.2.8-py3-none-any.whl.

File metadata

File hashes

Hashes for basalam_backbone_api-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 27675fe7d12ba4f127a10d7b2e2bd15c9bcccc63bd302f73aa18d261306164ac
MD5 bf683009ddc33f94ade976b6cd08df93
BLAKE2b-256 ff7df669a25d2fb4e7cbe3e0adad498d6da109d425dfd0c6d1d7eca8463156ce

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.9

2 files

This release

0.2.8 This release

2 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3

2 files

0.1.2

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page