birchrest package
Subpackages
- birchrest.app package
- birchrest.decorators package
- Submodules
- birchrest.decorators.body module
- birchrest.decorators.controller module
- birchrest.decorators.delete module
- birchrest.decorators.get module
- birchrest.decorators.head module
- birchrest.decorators.middleware module
- birchrest.decorators.options module
- birchrest.decorators.params module
- birchrest.decorators.patch module
- birchrest.decorators.post module
- birchrest.decorators.protected module
- birchrest.decorators.put module
- birchrest.decorators.queries module
- Module contents
- birchrest.exceptions package
- Submodules
- birchrest.exceptions.api_error module
- birchrest.exceptions.invalid_controller_registration module
- birchrest.exceptions.missing_auth_handler_error module
- Module contents
ApiErrorBadRequestConflictForbiddenGoneInternalServerErrorInvalidControllerRegistrationInvalidValidationModelLengthRequiredMethodNotAllowedMissingAuthHandlerErrorNotFoundPayloadTooLargePaymentRequiredRequestTimeoutServiceUnavailableTooManyRequestsUnauthorizedUnprocessableEntityUnsupportedMediaTypeUpgradeRequired
- birchrest.http package
- Submodules
- birchrest.http.request module
- birchrest.http.response module
- birchrest.http.server module
- birchrest.http.status module
HttpStatusHttpStatus.ACCEPTEDHttpStatus.BAD_GATEWAYHttpStatus.BAD_REQUESTHttpStatus.CONFLICTHttpStatus.CONTINUEHttpStatus.CREATEDHttpStatus.EARLY_HINTSHttpStatus.EXPECTATION_FAILEDHttpStatus.FORBIDDENHttpStatus.FOUNDHttpStatus.GATEWAY_TIMEOUTHttpStatus.GONEHttpStatus.HTTP_VERSION_NOT_SUPPORTEDHttpStatus.IM_A_TEAPOTHttpStatus.INSUFFICIENT_STORAGEHttpStatus.INTERNAL_SERVER_ERRORHttpStatus.LENGTH_REQUIREDHttpStatus.LOOP_DETECTEDHttpStatus.METHOD_NOT_ALLOWEDHttpStatus.MOVED_PERMANENTLYHttpStatus.MULTIPLE_CHOICESHttpStatus.NETWORK_AUTHENTICATION_REQUIREDHttpStatus.NON_AUTHORITATIVE_INFORMATIONHttpStatus.NOT_ACCEPTABLEHttpStatus.NOT_EXTENDEDHttpStatus.NOT_FOUNDHttpStatus.NOT_IMPLEMENTEDHttpStatus.NOT_MODIFIEDHttpStatus.NO_CONTENTHttpStatus.OKHttpStatus.PARTIAL_CONTENTHttpStatus.PAYLOAD_TOO_LARGEHttpStatus.PAYMENT_REQUIREDHttpStatus.PERMANENT_REDIRECTHttpStatus.PRECONDITION_FAILEDHttpStatus.PRECONDITION_REQUIREDHttpStatus.PROCESSINGHttpStatus.PROXY_AUTHENTICATION_REQUIREDHttpStatus.RANGE_NOT_SATISFIABLEHttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGEHttpStatus.REQUEST_TIMEOUTHttpStatus.RESET_CONTENTHttpStatus.SEE_OTHERHttpStatus.SERVICE_UNAVAILABLEHttpStatus.SWITCHING_PROTOCOLSHttpStatus.TEMPORARY_REDIRECTHttpStatus.TOO_EARLYHttpStatus.TOO_MANY_REQUESTSHttpStatus.UNAUTHORIZEDHttpStatus.UNPROCESSABLE_ENTITYHttpStatus.UNSUPPORTED_MEDIA_TYPEHttpStatus.UPGRADE_REQUIREDHttpStatus.URI_TOO_LONGHttpStatus.VARIANT_ALSO_NEGOTIATESHttpStatus.description()
- Module contents
HttpStatusHttpStatus.ACCEPTEDHttpStatus.BAD_GATEWAYHttpStatus.BAD_REQUESTHttpStatus.CONFLICTHttpStatus.CONTINUEHttpStatus.CREATEDHttpStatus.EARLY_HINTSHttpStatus.EXPECTATION_FAILEDHttpStatus.FORBIDDENHttpStatus.FOUNDHttpStatus.GATEWAY_TIMEOUTHttpStatus.GONEHttpStatus.HTTP_VERSION_NOT_SUPPORTEDHttpStatus.IM_A_TEAPOTHttpStatus.INSUFFICIENT_STORAGEHttpStatus.INTERNAL_SERVER_ERRORHttpStatus.LENGTH_REQUIREDHttpStatus.LOOP_DETECTEDHttpStatus.METHOD_NOT_ALLOWEDHttpStatus.MOVED_PERMANENTLYHttpStatus.MULTIPLE_CHOICESHttpStatus.NETWORK_AUTHENTICATION_REQUIREDHttpStatus.NON_AUTHORITATIVE_INFORMATIONHttpStatus.NOT_ACCEPTABLEHttpStatus.NOT_EXTENDEDHttpStatus.NOT_FOUNDHttpStatus.NOT_IMPLEMENTEDHttpStatus.NOT_MODIFIEDHttpStatus.NO_CONTENTHttpStatus.OKHttpStatus.PARTIAL_CONTENTHttpStatus.PAYLOAD_TOO_LARGEHttpStatus.PAYMENT_REQUIREDHttpStatus.PERMANENT_REDIRECTHttpStatus.PRECONDITION_FAILEDHttpStatus.PRECONDITION_REQUIREDHttpStatus.PROCESSINGHttpStatus.PROXY_AUTHENTICATION_REQUIREDHttpStatus.RANGE_NOT_SATISFIABLEHttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGEHttpStatus.REQUEST_TIMEOUTHttpStatus.RESET_CONTENTHttpStatus.SEE_OTHERHttpStatus.SERVICE_UNAVAILABLEHttpStatus.SWITCHING_PROTOCOLSHttpStatus.TEMPORARY_REDIRECTHttpStatus.TOO_EARLYHttpStatus.TOO_MANY_REQUESTSHttpStatus.UNAUTHORIZEDHttpStatus.UNPROCESSABLE_ENTITYHttpStatus.UNSUPPORTED_MEDIA_TYPEHttpStatus.UPGRADE_REQUIREDHttpStatus.URI_TOO_LONGHttpStatus.VARIANT_ALSO_NEGOTIATESHttpStatus.description()
RequestResponseServer
- birchrest.middlewares package
- birchrest.routes package
- birchrest.types package
- birchrest.unittest package
- Submodules
- birchrest.unittest.test_adapter module
- Module contents
BirchRestTestCaseBirchRestTestCase.assertBadRequest()BirchRestTestCase.assertBodyContains()BirchRestTestCase.assertForbidden()BirchRestTestCase.assertHasHeader()BirchRestTestCase.assertHeader()BirchRestTestCase.assertInternalServerError()BirchRestTestCase.assertNotFound()BirchRestTestCase.assertNotOk()BirchRestTestCase.assertOk()BirchRestTestCase.assertRedirect()BirchRestTestCase.assertStatus()BirchRestTestCase.assertUnauthorized()
TestAdapter
- birchrest.utils package
Submodules
birchrest.core module
birchrest.version module
The package version
Module contents
This module serves as the main entry point for the BirchRest framework.
Modules imported include: - app: Contains the core BirchRest class to handle application setup. - decorators: Provides decorators like get, post, controller, and middleware helpers. - routes: Defines controllers for managing routes. - http: Handles HTTP requests, responses, and status codes. - types: Defines core types such as middleware functions and route handlers. - exceptions: Manages framework-specific errors and exceptions. - middlewares: Includes various middleware like RateLimiter, Logger, and Cors. - unittest: Includes the TestAdapter for unittesting.
- class birchrest.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.
- class birchrest.Controller[source]
Bases:
objectBase class for defining a collection of routes and subcontrollers for an HTTP API.
The Controller class allows organizing routes and middleware in a structured manner. It collects routes defined on its methods, handles subcontrollers, and resolves the base path for each route. Each method decorated with HTTP method decorators (like GET, POST, etc.) is treated as a route, and the controller can apply middleware or mark routes as protected.
- Attributes:
_base_path (str): The base path that is prefixed to all routes in this controller. _middlewares (List[MiddlewareFunction]): The list of middleware applied to this controller. _is_protected (str): Indicates if routes in this controller require protection (e.g., authentication). routes (List[Route]): The list of routes collected from the controller’s methods. controllers (List[Controller]): The list of subcontrollers attached to this controller.
- collect_routes() Generator[Route, None, None][source]
Collect and yield all routes defined in this controller and its subcontrollers.
This method generates all routes defined in the controller, including those in any attached subcontrollers, recursively.
- Yield:
The routes defined in this controller and its subcontrollers.
- resolve_paths(prefix: str = '', middlewares: List[Callable[[Request, Response, Callable[[], Awaitable[None]]], Awaitable[None]]] = []) None[source]
Resolve and apply the base paths and middleware to all routes in this controller.
This method resolves the complete path for each route in the controller by combining the provided prefix with the controller’s base path. It also ensures that any middleware applied to the controller is propagated to its routes. If the controller has subcontrollers, their routes are resolved recursively.
- Parameters:
prefix – The URL path prefix to prepend to the controller’s base path.
middlewares – A list of middleware functions to apply to all routes in the controller.