Add your description here
Project description
ClassApi
ClassApi is a small convenience layer on top of FastAPI that enables class-based views (CBV). It preserves FastAPI's features (typing, dependencies, automatic docs) while letting you organize handlers as classes.
Key features
- Use
BaseViewas a base class for handlers (get,post,put, ...). - Support for
pre_processandpre_<method>hooks to validate or transform requests. - Register route modules by module path (supports relative module paths like
.src.urls). - Combined signatures from
pre_processand the handler method are exposed to FastAPI for documentation and form generation.
Development setup (using uv helper)
- Create a virtual environment:
python -m venv .venv
- Use your
uvhelper to runpipinside the project environment and install dependencies:
uv run pip install fastapi uvicorn
# or install from requirements: uv run pip install -r requirements.txt
Quickstart
Create main.py:
from classapi import ClassApi
app = ClassApi()
app.include_routers(".src.urls")
Example routes and views layout (tests/app_test/src):
# tests/app_test/src/urls.py
from .views import HelloWorldView
urls = [
{"path": "/hello", "view": HelloWorldView}
]
# tests/app_test/src/views.py
from classapi import View, Header, HTTPException
from typing import Annotated
class ValidateUser(BaseView):
def pre_process(self, jwt: Annotated[str | None, Header()] = None):
if jwt != "valid_jwt":
raise HTTPException(status_code=401, detail="Unauthorized")
class HelloWorldView(ValidateUser, BaseView):
methods = ["GET"]
def get(self, name: str = "World"):
return {"Hello": name}
Supported urls formats
- Dict entries:
{"path": "/x", "view": MyView, ...fastapi kwargs...}— extra kwargs (e.g.response_model) are forwarded toadd_api_route. - Tuple/list entries:
("/x", MyView). - You may use relative imports from the calling module:
app.include_routers(".src.urls").
View classes
- Define HTTP methods:
get,post,put,delete,patch. - Limit exposed methods with
methods = ["GET"]on the class. - Hooks:
pre_process(self, ...)— runs before any handler.pre_get(self, ...),pre_post(...), ... — run before a specific handler.
- Signatures from
pre_process,pre_<method>and the handler itself are merged and exposed to FastAPI; annotate parameters withAnnotated[..., Header()],Cookie(), etc., to appear correctly in/docs.
Example: header extraction in pre_process:
def pre_process(self, jwt: Annotated[str|None, Header()] = None):
...
If you accidentally place Annotated[...] as a default (e.g. jwt = Annotated[...]), ClassApi attempts to normalize it so FastAPI recognizes the dependency. Still, annotate parameters properly when possible.
Running the app
- Use
uvto run the app with reload during development:
uv run uvicorn tests.app_test.main:app --reload
or, if you use the helper script test_init.py at the repository root:
uv run .\test_init.py
Debugging endpoint signatures If docs don't show expected parameters, you can inspect endpoint signatures at runtime:
for r in app.routes:
print(r.path, getattr(r.endpoint, '__signature__', None))
Editor integration (VSCode / Pylance)
Pylance is a static analyzer and doesn't pick up runtime-generated signatures. To get editor hovers that match your runtime docs, create a .pyi stub next to your views module describing the public signatures (this does not change runtime behavior).
Contributing
- Open issues or PRs.
- Add tests under
tests/and run them withpytest.
If you want, I can generate a views.pyi stub for your views, add example tests, or add a minimal pyproject.toml. Which should I do next?
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 classapi-0.1.0.1.tar.gz.
File metadata
- Download URL: classapi-0.1.0.1.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b093ac6db8ef79d676a45b65bdf57b3ca59c5b834c74709702893899b776d205
|
|
| MD5 |
4f4ca7074de419fc57686fdbd6454daa
|
|
| BLAKE2b-256 |
35e9b6e9556ef414967456f1d61bee974bfd691ee93c23a2de1f578b06be709a
|
File details
Details for the file classapi-0.1.0.1-py3-none-any.whl.
File metadata
- Download URL: classapi-0.1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37628c90fd43c44a493c9d0c23e4ac776bac205337c2e228a26ae43620cdfefc
|
|
| MD5 |
be8475c566ee7648a89781cda6ca4bd0
|
|
| BLAKE2b-256 |
3d284e570c7ad2204b8cb03acf3123316c2676abb7e546df84bc2dcb80d1dccf
|