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 | Noneall() -> 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
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 faststack-0.5.0.tar.gz.
File metadata
- Download URL: faststack-0.5.0.tar.gz
- Upload date:
- Size: 29.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36e424a805c1fe7a2d7dd3c0424e96c484d4cf0e074f5ffc57e8b3f304c26683
|
|
| MD5 |
e1eefc50f364fcc11bf574a6681fa731
|
|
| BLAKE2b-256 |
5fac93621509b4ec6549ca306a2b58fe70bb94a55582c9997799c3285885d963
|
Provenance
The following attestation bundles were made for faststack-0.5.0.tar.gz:
Publisher:
release.yml on rmasters/faststack
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
faststack-0.5.0.tar.gz -
Subject digest:
36e424a805c1fe7a2d7dd3c0424e96c484d4cf0e074f5ffc57e8b3f304c26683 - Sigstore transparency entry: 172588389
- Sigstore integration time:
-
Permalink:
rmasters/faststack@0bd66139bcc6d8e1f6b74e5785cc57eada8da955 -
Branch / Tag:
refs/tags/0.5.0 - Owner: https://github.com/rmasters
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0bd66139bcc6d8e1f6b74e5785cc57eada8da955 -
Trigger Event:
release
-
Statement type:
File details
Details for the file faststack-0.5.0-py3-none-any.whl.
File metadata
- Download URL: faststack-0.5.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94e0432d8b22a5c599d42396475ef270552ff517ddafe2082289aba37dc161ed
|
|
| MD5 |
529ec2fc79116d1ec992a1d23664f6ab
|
|
| BLAKE2b-256 |
5d26a8a2488bbeea48a6a7ac6d41885759d0a15dca6154e36d1be21960bcbfeb
|
Provenance
The following attestation bundles were made for faststack-0.5.0-py3-none-any.whl:
Publisher:
release.yml on rmasters/faststack
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
faststack-0.5.0-py3-none-any.whl -
Subject digest:
94e0432d8b22a5c599d42396475ef270552ff517ddafe2082289aba37dc161ed - Sigstore transparency entry: 172588392
- Sigstore integration time:
-
Permalink:
rmasters/faststack@0bd66139bcc6d8e1f6b74e5785cc57eada8da955 -
Branch / Tag:
refs/tags/0.5.0 - Owner: https://github.com/rmasters
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0bd66139bcc6d8e1f6b74e5785cc57eada8da955 -
Trigger Event:
release
-
Statement type: