Skip to main content

Python OpenApi Utilities for FastAPI

Project description

from basalam.backbone_api.exceptions.client_error import ForbiddenException

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 await 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


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.1.3.tar.gz (6.9 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.1.3-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: basalam_backbone_api-0.1.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for basalam_backbone_api-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b21b6a5d361033a5e9295a3a3932a8dba03ffbb32525d6e0b112a70f02bea0ca
MD5 78729cd4eacc9bdbd3b09315a8e6f5bf
BLAKE2b-256 5c49ec34b00bebb7c1c47086447b5cf0ba31eecf5a4e82411fbffe30c596f9dc

See more details on using hashes here.

File details

Details for the file basalam.backbone_api-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for basalam.backbone_api-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 da3290b647c2d419fdbdab5d5c45e99fb15f03749807bb6e2ba2bdaba8d0cc25
MD5 5d74cb8ece79072220d82e95762b6741
BLAKE2b-256 b4b97e2046f1396929e5b2708c6ecbe0668e1d277aba5dad5ee72fed2ddee2bb

See more details on using hashes here.

Supported by

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