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.3.0.tar.gz
(12.5 kB
view hashes)
Built Distribution
Close
Hashes for os2mo-fastapi-utils-1.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1dbf2bdb370cd38833c3c2a38a09404b7564d5216e702013b5e1b1e48d4ea02 |
|
MD5 | 019b0a5224dc8ecdfd0e35db35e1a2dd |
|
BLAKE2b-256 | 42eda6bd4dc5a999d230bf536e667c772c84b24b70687a855d2507627ffb62f3 |
Close
Hashes for os2mo_fastapi_utils-1.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f30c90bdff8d8f1f3b6ed48e64c14709066a366fed5a09a643accda212bd3ab |
|
MD5 | 31f87e94b9a95623fe057e07c839a5ac |
|
BLAKE2b-256 | 3bc543983e899ef9e32616e1e715fc0a70287c360a1abc038bdb256ce12141fa |