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.1.0.tar.gz
(12.3 kB
view hashes)
Built Distribution
Close
Hashes for os2mo-fastapi-utils-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34b1dcef56d4828581554f3287991c5e50b8cad3c2db5715b58526a06502d02c |
|
MD5 | 8d1e1ba08600f595f87da605d05c6257 |
|
BLAKE2b-256 | a381ab6c8848d783fb0fc3cb62b92283c56d4cf13eab6927fa168632e99023ae |
Close
Hashes for os2mo_fastapi_utils-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 735de1bb21da0096567d2f73ad184c1ad973ef8662bbaacd8a0f9897aa7d5588 |
|
MD5 | f564c9d4d81b1ad8e0626423254594a5 |
|
BLAKE2b-256 | 97b7df8988be3a7d7d7e75e26ca14647a67212edcbfab0a3e01159d5ebac3823 |