API Reference

This section provides a detailed reference for the Xyra framework's main classes and methods.

The App Class

The App class is the main entry point of every Xyra application.

Python
class App:
    def __init__(self, templates_directory: str | None = None, static_directory: str | None = None, swagger_options: dict | None = None):
        ...

Initialization Parameters

  • templates_directory: Path to the directory for HTML templates (enables Jinja2).
  • swagger_options: A dictionary of options to configure Swagger/OpenAPI documentation.

Key Methods

  • listen(port, host, logger): Starts the web server.
  • get(path, middleware): Decorator to register a GET route.
  • post(path, middleware): Decorator for POST routes.
  • put(path, middleware): Decorator for PUT routes.
  • delete(path, middleware): Decorator for DELETE routes.
  • patch(path, middleware): Decorator for PATCH routes.
  • use(middleware): Adds a global middleware to the application.
  • static_files(path, directory): Serves static files from a specific directory at a given path.
  • websocket(path, handlers): Registers a WebSocket route.

The Request Class

Represents an incoming HTTP request and is passed to every route handler.

Properties

  • params: Dictionary of route parameters.
  • query: Dictionary of URL query parameters.
  • headers: Dictionary of request headers.

Async Methods

  • await json(): Reads and parses the request body as JSON.
  • await form(): Reads and parses the request body as form data.
  • await text(): Reads the request body as a raw string.

The Response Class

Used to construct and send the HTTP response back to the client.

Methods

  • status(code): Sets the HTTP status code (e.g., 200, 404).
  • header(name, value): Sets a response header.
  • text(content): Sends a plain text response.
  • json(data): Sends a JSON response.
  • html(content): Sends an HTML response.
  • render(template_name, **context): Renders a Jinja2 HTML template.
  • redirect(url, status_code=302): Redirects the client to a different URL.

Concurrency Utilities

Xyra provides utilities for handling concurrent operations.

Functions

  • to_thread(func): Decorator to run synchronous functions in a thread pool.

Background Tasks

Utilities for running asynchronous tasks in the background.

Functions

  • create_background_task(func, *args, **kwargs): Creates and starts a background task.