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.7.tar.gz
(7.2 kB
view details)
Built Distribution
File details
Details for the file fastapi-helper-0.0.7.tar.gz
.
File metadata
- Download URL: fastapi-helper-0.0.7.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35aa79cee66dae9c7549ce66bedfff2ded87b464002b481f4d464405f0707a53 |
|
MD5 | f08a50907c22e0c5de96dc4dfcf1cac8 |
|
BLAKE2b-256 | 15c1d5dc5a4d691c4b459e71c6a3629df2cb646789a91216b0cba815149b295d |
File details
Details for the file fastapi_helper-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: fastapi_helper-0.0.7-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7591dc7023f90e75e165e0f07873c3654360935e47f30006bc246eecb49f8b3b |
|
MD5 | 1df39083d07829a2d3355148ec5c51d2 |
|
BLAKE2b-256 | 2ffcb89cc681cb4007ba18ad87c5e1de3e592f8463f3cb7654e7cb49f0fda54f |