Framework
Jetio application object (ASGI).
Jetio registers routes, resolves dependencies, manages a per-request
database session, and returns ASGI responses.
Examples:
Minimal app:
app = Jetio()
@app.route("/")
async def home():
return {"hello": "world"}
Run with Uvicorn:
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000)
Attributes:
| Name | Type | Description |
|---|---|---|
title |
OpenAPI title (also used by Swagger UI). |
|
version |
OpenAPI version. |
|
routes |
Registered routes. |
add_middleware(middleware_cls, **kwargs)
Adds a middleware to the application stack. Middleware is processed in reverse order of addition.
add_error_page(status_code: int, template_name: str)
Registers a custom HTML template for a specific HTTP error status code.
route(path, methods=None)
Register a route handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
URL path pattern. Supports placeholders like |
required | |
methods
|
Allowed HTTP methods. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
|
A decorator that registers the handler and returns it. |
Notes
The handler docstring's first line is used as the OpenAPI summary
in :func:jetio.openapi.generate_openapi_schema.
on_event(event_type: str)
A decorator to register a startup or shutdown event handler.
__call__(scope, receive, send)
async
The main ASGI entry point for the application.
handle_request(scope, receive, send)
async
Handle an incoming HTTP request.
Flow:
1) Create a per-request database session
2) Match a route by path + method
3) Resolve handler arguments (path params, Request, db session, Pydantic body)
4) Resolve dependencies declared via :class:Depends
5) Call the handler (async or sync)
6) Convert result to :class:Response / :class:JsonResponse
7) Handle exceptions and close the DB session
Returns:
| Type | Description |
|---|---|
|
None. Sends an ASGI response via |
find_handler(path, method)
Find a matching handler for a path and HTTP method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Request path (already adjusted for |
required | |
method
|
HTTP method (e.g. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple[callable, dict]: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If no route matches the path. |
MethodNotAllowedError
|
If path matches but method is not allowed. |
run(host='127.0.0.1', port=8000)
Run the app with Uvicorn.
This rebuilds forward references for any registered Jetio models' Pydantic schemas before starting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
Bind host. |
'127.0.0.1'
|
|
port
|
Bind port. |
8000
|
"Incoming HTTP request wrapper.
Provides convenient access to method, path, headers, cookies and body parsing utilities.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
HTTP method (e.g. |
|
path |
URL path adjusted for |
|
headers |
Parsed ASGI headers (:class: |
|
cookies |
Parsed cookies (:class: |
|
user |
Optional user context (framework/userland can set this). |
Notes
- :meth:
jsonis tolerant and returns{}on invalid JSON. - :meth:
formparses multipart form data via Starlette's parser.
stream()
async
Yield request body chunks as bytes.
Returns:
| Type | Description |
|---|---|
|
An async iterator of |
body() -> bytes
async
Return the full request body as bytes (cached).
json()
async
Parse and return the request body as JSON (cached).
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Parsed JSON object. Returns |
form()
async
Parse and return multipart form data (cached).
Outgoing HTTP response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
Response body ( |
''
|
|
status_code
|
HTTP status code. |
200
|
|
content_type
|
MIME type for Content-Type header. |
'text/html'
|
|
headers
|
Optional headers dict. |
None
|
Notes
- Strings are UTF-8 encoded automatically.
- Content-Length is set automatically.
__call__(scope, receive, send)
async
The ASGI callable interface.
Bases: Response
JSON response helper.
Serializes Python objects to JSON and sets Content-Type: application/json.
Supports:
- Pydantic models (serialized via model_dump(mode="json"))
- Jetio ORM models (:class:~jetio.orm.JetioModel) serialized via their
auto-generated read schema (__pydantic_read_model__)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any JSON-serializable object. |
required | |
status_code
|
HTTP status code (default 200). |
200
|
Dependency injection marker for Jetio route handlers.
Use :class:Depends as a default value for a route handler parameter to
indicate that the framework should call the given dependency and inject its
return value.
Dependencies may declare any of the following parameters and Jetio will pass
them if available:
- request: :class:~jetio.framework.Request
- db: :class:~sqlalchemy.ext.asyncio.AsyncSession
- any path parameters if the dependency function accepts them (either via
named parameters or **kwargs)
Examples:
A basic dependency:
async def get_current_user(request: Request):
...
return user
Inject it into a handler:
@app.route("/profile")
async def profile(user = Depends(get_current_user)):
return {"username": user.username}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dependency
|
callable
|
The callable to resolve (sync or async). |
required |