Core utilities for building web APIs: middleware, schemas, logging, health checks
Project description
ulfblk-core
Core utilities for building web APIs with FastAPI: middleware, schemas, structured logging, and health checks.
Part of Bloques Reciclables - an open source ecosystem of reusable code blocks.
Installation
uv add ulfblk-core
# or
pip install ulfblk-core
Features
Middleware
RequestIDMiddleware - Distributed tracing via X-Request-ID header. Generates UUID4 if not present, propagates through contextvars for logging.
TimingMiddleware - Measures request duration and adds X-Process-Time header. Logs warnings for slow requests (>1s).
from fastapi import FastAPI
from ulfblk_core.middleware import RequestIDMiddleware, TimingMiddleware
app = FastAPI()
app.add_middleware(TimingMiddleware)
app.add_middleware(RequestIDMiddleware)
Schemas
Standardized Pydantic v2 schemas for API responses:
from ulfblk_core.schemas import PaginatedResponse, ErrorResponse, SuccessResponse, HealthResponse
# Paginated response
response = PaginatedResponse.create(
items=my_items,
total=100,
page=1,
page_size=20,
)
# Health check
health = HealthResponse.healthy(service="my-api", version="1.0.0")
Structured Logging
Structured logging with structlog, automatic request ID injection, and JSON/console output:
from ulfblk_core.logging import setup_logging, get_logger
setup_logging(level="INFO", json_format=True, service_name="my-api")
logger = get_logger(__name__)
logger.info("user.created", user_id="123", tenant="acme")
# Output: {"event": "user.created", "user_id": "123", "request_id": "abc-def", ...}
Health Check Router
Ready-to-use health check endpoint:
from ulfblk_core.health import health_router
app.include_router(health_router)
# GET /health -> {"status": "healthy", "service": "api", "version": "0.1.0", ...}
Full Example
from fastapi import FastAPI
from ulfblk_core.middleware import RequestIDMiddleware, TimingMiddleware
from ulfblk_core.health import health_router
from ulfblk_core.logging import setup_logging
app = FastAPI(title="My API")
setup_logging()
app.add_middleware(TimingMiddleware)
app.add_middleware(RequestIDMiddleware)
app.include_router(health_router)
@app.get("/")
async def root():
return {"message": "Running with ulfblk-core"}
See the api-simple example for a runnable version.
Requirements
- Python 3.11+
- FastAPI 0.115+
- Pydantic 2.0+
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ulfblk_core-0.1.0.tar.gz.
File metadata
- Download URL: ulfblk_core-0.1.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69f0be3281bb2c436f9c635c596dd508cd23de6defa74d028d79c4a20c999661
|
|
| MD5 |
235628fee2a851aac4588d91e72ede5f
|
|
| BLAKE2b-256 |
59137c276961317fcd4aa69aeeeb62dfc30f81f2af12775a56ff3cbf109b927c
|
File details
Details for the file ulfblk_core-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ulfblk_core-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5329bcafb667d2eb5ce2b247de006269db5b912318a328eecaa03b2efffe5873
|
|
| MD5 |
7e6515400b0386e4bd95b86c603c0416
|
|
| BLAKE2b-256 |
09089d1e108142c4cc523d364332e9d47559f7f55fbdf81530bcb755396d73c9
|