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.3.tar.gz
(12.6 kB
view hashes)
Built Distribution
Close
Hashes for os2mo_fastapi_utils-1.3.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c271ebe315968d887ecccf82816bc542bbcaa5f0575a372ea3bb6c6e4c8ab6f |
|
MD5 | 75d672b4066f4dbab1701b6a6d03d328 |
|
BLAKE2b-256 | c2ae493d86dec02071a8e3d9a87ea2f95ea42ed0dd5889e8705b36072337d58f |
Close
Hashes for os2mo_fastapi_utils-1.3.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc857a616891fdfafa4e5287252f471cdbb6c50c9520691ac7b574f9fac9cbbf |
|
MD5 | 1144e2a7da90bfcdc5244cf679787408 |
|
BLAKE2b-256 | f5374d167dcd6270571c31741851ae63b984c4bffdf7a3d07e05235a8a6698dc |