Skip to main content

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"}

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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file os2mo_fastapi_utils-1.3.4.tar.gz.

File metadata

  • Download URL: os2mo_fastapi_utils-1.3.4.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.3 Linux/5.15.65+

File hashes

Hashes for os2mo_fastapi_utils-1.3.4.tar.gz
Algorithm Hash digest
SHA256 a5758ce2120e3c9fddd581ff92266408505f5f1f0a69d5df97cfc2bbe3cbdc57
MD5 4ffe60798a471427c478377dafd4a39b
BLAKE2b-256 54972a0d3f30a7a3375c84a70b23c402bced873da5d0117b1519fce65b6ad45f

See more details on using hashes here.

File details

Details for the file os2mo_fastapi_utils-1.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for os2mo_fastapi_utils-1.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 61eccb69a7efa49d0506b360712061524a4400544323d5d73bb18def6aaf86e2
MD5 ae68ff98fae92a097b559ae69d73a63c
BLAKE2b-256 3c19ff6021731ece6ba1dbf7bc73a6198d9a0bb347db11c98547b8c8a7fb8f39

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.3.4 This release

2 files

1.3.3

2 files

1.3.0

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 files

0.0.3

2 files

0.0.2

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page