birchrest.app package
Submodules
birchrest.app.birchrest module
Module contents
- class birchrest.app.BirchRest(log_level: str = 'debug', base_path: str = '')[source]
Bases:
objectThe 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.