Utility library with various reusable FastAPI components
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.0.1.tar.gz
(12.3 kB
view hashes)
Built Distribution
Close
Hashes for os2mo-fastapi-utils-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac4a301536011c3d035fbadd5b36a50c86f5a5c9a0650b8663f73e042ae9ce93 |
|
MD5 | 21d2ffdf61d96780a63eefb493990f27 |
|
BLAKE2b-256 | d4d99274f480af407aad0b067d5e038670a3456e95770aca222ab978be17549a |
Close
Hashes for os2mo_fastapi_utils-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad2807bf87c1c5fcfbd4b7c7878da109eaa94165ca246fe3be41df2589038986 |
|
MD5 | 5dfd0415db563b9b4dc6192c205a87c3 |
|
BLAKE2b-256 | 5c6e4129177755c4e2403868db46b5afef345f08e9ea9afa5005e0e7598d58ea |