birchrest.app package

Submodules

birchrest.app.birchrest module

Module contents

class birchrest.app.BirchRest(log_level: str = 'debug', base_path: str = '')[source]

Bases: object

The core application class for the BirchRest framework, responsible for registering controllers, middleware, authentication, error handling, and starting the HTTP server to serve the API.

Attributes:

controllers (List[Controller]): Registered route controllers. global_middlewares (List[MiddlewareFunction]): Global middleware applied to all routes. auth_handler (Optional[AuthHandlerFunction]): Authentication handler for protected routes. error_handler (Optional[ErrorHandler]): Error handler function for handling exceptions.

auth(auth_handler: Callable[[Request, Response], Awaitable[Any]]) None[source]

Sets the authentication handler for the application, used for protecting routes.

Args:

auth_handler (AuthHandlerFunction): A function to handle authentication logic.

error(handler: Callable[[Request, Response, Exception], Awaitable[None]]) None[source]

Registers a global error handler for the application.

Args:

handler (ErrorHandler): A function to handle errors during request processing.

async handle_request(request: Request) Response[source]

Handles incoming HTTP requests by matching them to routes, processing middleware, and handling exceptions asynchronously.

middleware(handler: Callable[[Request, Response, Callable[[], Awaitable[None]]], Awaitable[None]]) None[source]

Registers a global middleware that is applied to all routes.

Args:

handler (MiddlewareFunction): A middleware function to process requests.

register(*controllers: Type[Controller]) None[source]

Registers one or more route controllers to the application.

Args:

*controllers (Type[Controller]): One or more controller classes to register.

Raises:

InvalidControllerRegistration: If a registered controller does not inherit from Controller.

serve(host: str = '127.0.0.1', port: int = 13337) None[source]

Starts the HTTP server to serve the API on the specified host and port.

Args:

host (str): The hostname or IP address to bind the server to. Defaults to “127.0.0.1”. port (int): The port number to listen on. Defaults to 13337.