Common utilities for FastAPI apps
Project description
faststack
A set of common utilities that I add to nearly every FastAPI project, including:
- A pure-Python healthcheck,
- An SQLModel-bound model repository class,
- An async-friendly Typer base-class, based on discussions in the Typer repo
The intent is to grow this organically into a more fully-featured web stack, based on FastAPI, SQLModel, and other async friendly packages.
Usage: install faststack
SQLModel repository class
The repository class binds to an AsyncSession, and provides a few common methods for model retrieval:
get(pk) -> Model | None
all() -> Sequence[Model]
from typing import Sequence
from faststack.models import SQLModelRepository
from sqlalchemy import desc
from sqlalchemy.orm import selectinload
from sqlmodel import select
from .models import Article, Comment
class ArticleRepository(SQLModelRepository[Article]): ...
class CommentRepository(SQLModelRepository[Comment]):
async def get_for_article(self, article: Article) -> Sequence[Comment]:
qry = select(Comment) \
.where(Comment.article == article) \
.order_by(desc(Comment.posted_at)) \
.options(selectinload(Comment.article))
return (await self.session.exec(qry)).fetch_all()
This will be extended to add pagination, magic methods, etc.
Healthchecks
Provides a healthcheck endpoint and a command-line tool to add a container healthcheck. The main motivation behind this is to remove the need to add cURL to the application container image.
The default healthcheck router adds an endpoint at /health
that returns a HTTP 204 response.
A command line utility is provided to hit this endpoint, and alert based on status.
from faststack.healthcheck import build_healthcheck_router
app = FastAPI()
app.include_router(build_healthcheck_router(path="/health"))
HEALTHCHECK CMD ["python", "-m", "faststack.healthcheck"]
# Supported arguments
EXPOSE 3000
HEALTHCHECK CMD ["python", "-m", "faststack.healthcheck", "--status=204", "--status=200", "--url=http://localhost:3000/custom-health"]
Async-friendly Typer
A stop-gap Typer class that supports async commands, based on comments from:
from faststack.cli import AsyncTyper
app = AsyncTyper()
@app.command()
async def my_command():
await asyncio.sleep(1)
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
Built Distribution
File details
Details for the file faststack-0.1.3.tar.gz
.
File metadata
- Download URL: faststack-0.1.3.tar.gz
- Upload date:
- Size: 23.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b9b632056a7bc616aa8932608c4e4d710d6cabfbcf29af9d55e90dc1ed003c7 |
|
MD5 | fd55d2614944fc9804ce10857edb8b41 |
|
BLAKE2b-256 | 92b24d5aa80f2106bfb6c385f44a1e958096d6d039fd241d3555e3ae565e9385 |
File details
Details for the file faststack-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: faststack-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f26be75c2a5bdfef055e64cbce63ce419d3c7c13de55951de6e9cad6b7ce9f3 |
|
MD5 | bd7cb243b3783de18aca0b7dd13741e9 |
|
BLAKE2b-256 | 8fed8f49d2f14c04fdb4c42491f503e02b39eea7f9cd0308391bf370f46cfac4 |