Skip to main content

FastAPI utils for os2mo

Project description

OS2MO-FastAPI-Utils

Utility library with various reusable FastAPI components.

Instrumentation Usage

Install into your project using pip:

pip install os2mo-fastapi-utils

Then import it inside a Python file:

from fastapi import FastAPI
from os2mo_fastapi_utils.tracing import setup_instrumentation

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

app = setup_instrumentation(app)

Keycloak Autentication

The auth package in this module provides a Keycloak auth Depends coroutine for FastAPI. Do (e.g.) the following to use this in FastAPI:

from fastapi import FastAPI, Depends
from structlog import get_logger

from os2mo_fastapi_utils.auth.exceptions import AuthenticationError
from os2mo_fastapi_utils.auth.oidc import get_auth_dependency
from os2mo_fastapi_utils.auth.oidc import get_auth_exception_handler
from myproject.models import MyToken

logger = get_logger()

auth = get_auth_dependency(
           host='keycloak',
           port=8081,
           realm='mo',
           token_url_path='service/token',
           token_model=MyToken
       )

app = FastAPI()
app.add_exception_handler(
    AuthenticationError,
    get_auth_exception_handler(logger)
)

@app.get("/")
async def root(token: Token = Depends(auth)):
    return {"message": "Hello World"}

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

os2mo_fastapi_utils-1.3.4.tar.gz (12.6 kB view hashes)

Uploaded Source

Built Distribution

os2mo_fastapi_utils-1.3.4-py3-none-any.whl (20.3 kB view hashes)

Uploaded Python 3

Supported by

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