Adapters#

See Framework adapters for complete examples. Each helper imports its framework lazily; anza never requires one.

FastAPI / Starlette#

FastAPI / Starlette responses for pages and envelopes.

html_response(doc: Document, status_code: int = 200) → Any[source]#

Wrap a rendered page in an HTML response.

Parameters:
  • doc – The page from Page.

  • status_code – The HTTP status.

Returns:

A fastapi.responses.HTMLResponse (or Starlette’s); the bare HTML string when neither FastAPI nor Starlette is installed.

json_response(envelope: Envelope, status_code: int = 200) → Any[source]#

Wrap a fragment envelope in a JSON response.

Parameters:
  • envelope – The envelope from Fragment.

  • status_code – The HTTP status.

Returns:

A fastapi.responses.JSONResponse (or Starlette’s) of Envelope.to_dict(); the bare dict when neither FastAPI nor Starlette is installed.

Flask#

Flask responses for pages and envelopes.

html_response(doc: Document, status_code: int = 200) → Any[source]#

Wrap a rendered page in an HTML response.

Parameters:
  • doc – The page from Page.

  • status_code – The HTTP status.

Returns:

A flask.Response with text/html; the bare HTML string when Flask is not installed.

json_response(envelope: Envelope, status_code: int = 200) → Any[source]#

Wrap a fragment envelope in a JSON response.

Parameters:
  • envelope – The envelope from Fragment.

  • status_code – The HTTP status.

Returns:

A flask.jsonify response of – meth:Envelope.to_dict() <anza.Envelope.to_dict>; the bare dict when Flask is not installed.

ASGI#

Raw ASGI responses for pages and envelopes.

async send_html(send: Callable[[dict[str, Any]], Awaitable[None]], doc: Document, status: int = 200) → None[source]#

Send a page as a complete ASGI HTTP response.

Sends http.response.start (content-type: text/html; charset=utf-8 and content-length) and one http.response.body.

Parameters:
  • send – The ASGI send callable.

  • doc – The page from Page.

  • status – The HTTP status.

async send_json(send: Callable[[dict[str, Any]], Awaitable[None]], envelope: Envelope, status: int = 200) → None[source]#

Send an envelope as a complete ASGI HTTP JSON response.

The body is Envelope.to_json() (content-type: application/json; charset=utf-8).

Parameters:
  • send – The ASGI send callable.

  • envelope – The envelope from Fragment.

  • status – The HTTP status.

WSGI#

Raw WSGI responses for pages and envelopes.

send_html(start_response: Callable[[...], Any], doc: Document, status: str = '200 OK') → list[bytes][source]#

Start a WSGI HTML response and return its body.

Parameters:
  • start_response – The WSGI start_response callable.

  • doc – The page from Page.

  • status – The WSGI status line.

Returns:

list[bytes] – The body iterable to return from the WSGI app.

send_json(start_response: Callable[[...], Any], envelope: Envelope, status: str = '200 OK') → list[bytes][source]#

Start a WSGI JSON response for an envelope and return its body.

Parameters:
  • start_response – The WSGI start_response callable.

  • envelope – The envelope from Fragment.

  • status – The WSGI status line.

Returns:

list[bytes] – The body iterable (Envelope.to_json() as UTF-8).