Package for useful fastapi utils
Project description
FastAPI Helper
Simple and customizable HTTP exceptions
import uvicorn
from fastapi import FastAPI
from starlette import status
from starlette.requests import Request
from starlette.responses import JSONResponse
from src.fastapi_fancy_exceptions import FancyHTTPException
app = FastAPI()
class AuthException(FancyHTTPException):
code = "auth_error"
type = "AuthError"
message = "Auth error"
status_code = status.HTTP_401_UNAUTHORIZED
@app.exception_handler(FancyHTTPException)
async def http_exception_accept_handler(request: Request, exc: FancyHTTPException) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content=[{"code": exc.code, "type": exc.type, "message": exc.message}]
)
@app.get("/")
async def root():
raise AuthException()
uvicorn.run(app, host="localhost", port=8000)
This code will lead to this response with status code 401:
[
{
"code": "auth_error",
"type": "AuthError",
"message": "Auth error"
}
]
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
fastapi-helper-0.0.12.tar.gz
(8.6 kB
view hashes)
Built Distribution
Close
Hashes for fastapi_helper-0.0.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6999509cef40f7aa00b08b789b7c2269e7b36130e739293b7b1fcc32b32b168 |
|
MD5 | 2900b367dc0f1dbc3f4953cfb8adf0dd |
|
BLAKE2b-256 | ad029783c91cb20b151a8f19f4b9daa5548292faeb724e1c3c252e9e1138d1ed |