Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
2
Share

ApiRegistry

Registry that creates and stores FastAPI sub-apps. Each registered app will be mounted on the main app at the specified path.

Registration methods:

  • register_api(path, ...) -- creates a FastAPI app at the given mount path. Pass with_exception_handlers=True to add the standard exception handlers automatically. Pass silent_access_log=True to downgrade access logs for this path to DEBUG level.

  • register_brick_app(brick_name, ...) -- convenience wrapper that calls register_api with path /brick/{brick_name}/ and with_exception_handlers=True by default.

  • configure_exception_handlers(app) -- public helper that adds the three standard exception handlers (HTTPException, RequestValidationError, generic Exception) to any FastAPI app.

Usage from an external brick::

from gws_core import ApiRegistry

eln_app = ApiRegistry.register_brick_api("gws_eln")

@eln_app.get("/my-route")
def my_route():
    return {"hello": "world"}
Functions
clear @classmethod

Clear all registered apis. Useful for tests.

configure_exception_handlers @classmethod

Add standard exception handlers to a FastAPI app.

Adds handlers for HTTPException, RequestValidationError, and generic Exception using the standard ExceptionHandler.

The generic Exception handler's responses never traverse the app's middleware (they are produced above it, by ServerErrorMiddleware), so the headers the app's middleware would have set are stamped manually — hence the two parameters below, which must mirror the app's middleware setup.

app : FastAPI
The FastAPI app to configure
cors_policy : gws_core.core.classes.cors_config.CorsPolicy | None
The app's CORS policy override (None for the lab default), applied to the generic handler's 500 responses
with_security_headers : bool - True
get_all_apis @classmethod

Return all registered apis.

Return type : dict
get_brick_api_path @classmethod

Get the mount path for a registered brick API.

brick_name : str
The brick name
Return type : str
install_access_log_filter @classmethod

Install the access log filter on uvicorn's access logger.

Downgrades log entries to DEBUG for all paths registered with silent_access_log=True. Call this before uvicorn.run().

register_api @classmethod

Create and register a FastAPI sub-app at the given mount path.

path : str
The mount path (e.g. "/core-api/" or "/s3-server/")
docs_url : str | None
The docs_url passed to FastAPI (None to disable docs)
with_exception_handlers : bool - True
If True, add the standard exception handlers (HTTPException, RequestValidationError, generic Exception)
with_security_headers : bool - True
If True (default), add the standard security headers (CSP, HSTS, ...) to every response of this app. Disable only for machine-to-machine APIs that no browser ever calls.
silent_access_log : bool - False
If True, downgrade access logs for this path to DEBUG level (visible only with --log-level=DEBUG)
cors : gws_core.core.classes.cors_config.CorsPolicy | None
A :class:`CorsPolicy` this app applies to **all** of its routes, replacing the lab default (lab sub-domains only, with credentials) for this app. Declare the policy here rather than adding a CORS middleware on the returned app yourself, so it replaces the lab default instead of stacking on top of it.
Return type : FastAPI
register_brick_api @classmethod

Create and register a FastAPI sub-app for an external brick.

The app is mounted at /brick/{brick_name}/ and has the standard exception handlers automatically configured by default.

brick_name : str
The brick name (used as route prefix)
docs_url : str | None - /docs
The docs_url passed to FastAPI (default "/docs")
with_exception_handlers : bool - True
If True (default), add the standard exception handlers
with_security_headers : bool - True
If True (default), add the standard security headers to every response (see ``register_api``)
cors : gws_core.core.classes.cors_config.CorsPolicy | None
A :class:`CorsPolicy` overriding the lab-wide CORS for every route of this brick's app (see ``register_api``)
Return type : FastAPI
Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.