fastapi_contrib.common package

Submodules

fastapi_contrib.common.middlewares module

class fastapi_contrib.common.middlewares.StateRequestIDMiddleware(app: Callable[[MutableMapping[str, Any], Callable[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], dispatch: Optional[Callable[[starlette.requests.Request, Callable[[starlette.requests.Request], Awaitable[starlette.responses.Response]]], Awaitable[starlette.responses.Response]]] = None)[source]

Bases: starlette.middleware.base.BaseHTTPMiddleware

Middleware to store Request ID headers value inside request’s state object.

Use this class as a first argument to add_middleware func:

app = FastAPI()

@app.on_event('startup')
async def startup():
    app.add_middleware(StateRequestIDMiddleware)
async dispatch(request: starlette.requests.Request, call_next: Any)starlette.responses.Response[source]

Get header from request and save it in request’s state for future use. :param request: current Request instance :param call_next: next callable in list :return: response

property request_id_header_name: str

Gets the name of Request ID header from the project settings. :return: string with Request ID header name

fastapi_contrib.common.responses module

class fastapi_contrib.common.responses.UJSONResponse(content: Optional[Any] = None, status_code: int = 200, headers: Optional[dict] = None, media_type: Optional[str] = None, background: Optional[starlette.background.BackgroundTask] = None)[source]

Bases: starlette.responses.JSONResponse

Custom Response, based on default UJSONResponse, but with differences:
  • Allows to have forward slashes inside strings of JSON

  • Limits output to ASCII and escapes all extended characters above 127.

Should be used as response_class argument to routes of your app:

app = FastAPI()


@app.get("/", response_class=UJSONResponse)
async def root():
    return {"a": "b"}
render(content: Any)bytes[source]

fastapi_contrib.common.utils module

fastapi_contrib.common.utils.async_timing(func)[source]

Decorator for logging timing of async functions. Used in this library internally for tracking DB functions performance.

Parameters

func – function to be decorated

Returns

wrapped function

fastapi_contrib.common.utils.get_current_app()fastapi.applications.FastAPI[source]

Retrieves FastAPI app instance from the path, specified in project’s conf. :return: FastAPI app

fastapi_contrib.common.utils.get_logger()Any[source]

Gets logger that will be used throughout this whole library. First it finds and imports the logger, then if it can be configured using loguru-compatible config, it does so.

Returns

desired logger (pre-configured if loguru)

fastapi_contrib.common.utils.get_now()datetime.datetime[source]

Retrieves now function from the path, specified in project’s conf. :return: datetime of “now”

fastapi_contrib.common.utils.get_timezone()[source]

Retrieves timezone name from settings and tries to create tzinfo from it. :return: tzinfo object

fastapi_contrib.common.utils.resolve_dotted_path(path: str)Any[source]

Retrieves attribute (var, function, class, etc.) from module by dotted path

from datetime.datetime import utcnow as default_utcnow
utcnow = resolve_dotted_path('datetime.datetime.utcnow')
assert utcnow == default_utcnow
Parameters

path – dotted path to the attribute in module

Returns

desired attribute or None

Module contents