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)
Built Distribution
Close
Hashes for os2mo_fastapi_utils-1.3.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5758ce2120e3c9fddd581ff92266408505f5f1f0a69d5df97cfc2bbe3cbdc57 |
|
MD5 | 4ffe60798a471427c478377dafd4a39b |
|
BLAKE2b-256 | 54972a0d3f30a7a3375c84a70b23c402bced873da5d0117b1519fce65b6ad45f |
Close
Hashes for os2mo_fastapi_utils-1.3.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61eccb69a7efa49d0506b360712061524a4400544323d5d73bb18def6aaf86e2 |
|
MD5 | ae68ff98fae92a097b559ae69d73a63c |
|
BLAKE2b-256 | 3c19ff6021731ece6ba1dbf7bc73a6198d9a0bb347db11c98547b8c8a7fb8f39 |