Skip to content

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 {user_id:int}.

required
methods

Allowed HTTP methods. Defaults to ["GET"].

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 send.

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 root_path).

required
method

HTTP method (e.g. "GET").

required

Returns:

Type Description

Tuple[callable, dict]: (handler, path_kwargs).

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. "GET").

path

URL path adjusted for root_path when deployed under a sub-path.

headers

Parsed ASGI headers (:class:starlette.datastructures.Headers).

cookies

Parsed cookies (:class:http.cookies.SimpleCookie).

user

Optional user context (framework/userland can set this).

Notes
  • :meth:json is tolerant and returns {} on invalid JSON.
  • :meth:form parses multipart form data via Starlette's parser.

stream() async

Yield request body chunks as bytes.

Returns:

Type Description

An async iterator of bytes chunks.

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 {} if empty or invalid.

form() async

Parse and return multipart form data (cached).

Outgoing HTTP response.

Parameters:

Name Type Description Default
body

Response body (str or bytes).

''
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