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
This module provides a collection of decorators for defining routes, controllers, middleware, and handling various aspects of HTTP requests in the BirchRest framework. These decorators simplify the process of routing, request validation, and protecting endpoints in the application.
Decorators: - HTTP method decorators: Define routes for specific HTTP methods.
@get: Defines a route that handles HTTP GET requests.
@post: Defines a route that handles HTTP POST requests.
@patch: Defines a route that handles HTTP PATCH requests.
@put: Defines a route that handles HTTP PUT requests.
@delete: Defines a route that handles HTTP DELETE requests.
@options: Defines a route that handles HTTP OPTIONS requests.
@head: Defines a route that handles HTTP HEAD requests.
Controller decorator: - @controller: Marks a class as a controller, where routes can be organized for better structure and reusability.
Middleware decorator: - @middleware: Attaches middleware to specific routes or controllers for processing requests before they reach the handler.
Protected route decorator: - @protected: Protects routes or controllers by enforcing authentication and authorization mechanisms.
Request body and query parameter decorators: - @body: Validates and injects the body of the request into the handler. - @queries: Validates and injects query parameters from the URL into the handler. - @params: Validates and injects URL parameters into the handler.
Usage: These decorators are used to define routes, middleware, and request-handling behavior in a declarative way. This enhances readability and modularity in the BirchRest framework by keeping routing and request-handling logic organized.
Example of usage:
```python from birchrest.decorators import get, post, controller, middleware, protected, body
@controller(“user”) class UserController:
@get(“:id”) def get_users(self, req, res):
# Handle GET request to /users res.send({“message”: “List of users”})
@post() def create_user(self, req, res):
# Handle POST request to /users with middleware, body validation, and protection new_user = req.body # User object automatically parsed res.status(201).send({“message”: “User created”, “user”: new_user})
- birchrest.decorators.body(model: Any) Callable[[FuncType], FuncType][source]
Decorator to attach a model for body validation to a function.
- birchrest.decorators.controller(base_path: str = '') Callable[[Type[T]], Type[T]][source]
Decorator to attach a base path to a controller class.
- birchrest.decorators.delete(sub_route: str = '') Callable[[FuncType], FuncType][source]
Decorator to define a DELETE route inside an API class.
- birchrest.decorators.get(sub_route: str = '') Callable[[FuncType], FuncType][source]
Decorator to define a GET route inside an API class.
- birchrest.decorators.head(sub_route: str = '') Callable[[FuncType], FuncType][source]
Decorator to define a HEAD route inside an API class.
- birchrest.decorators.middleware(handler: Callable[[Request, Response, Callable[[], Awaitable[None]]], Awaitable[None]]) Callable[[T], T][source]
Decorator to define middleware for a route (method) or an API class.
- birchrest.decorators.options(sub_route: str = '') Callable[[FuncType], FuncType][source]
Decorator to define an OPTIONS route inside an API class.
- birchrest.decorators.params(model: Any) Callable[[FuncType], FuncType][source]
Decorator to attach a model for parameter validation to a function.
- birchrest.decorators.patch(sub_route: str = '') Callable[[FuncType], FuncType][source]
Decorator to define a PATCH route inside an API class.
- birchrest.decorators.post(sub_route: str = '') Callable[[FuncType], FuncType][source]
Decorator to define a POST route inside an API class.
- birchrest.decorators.produces(model: Any) Callable[[FuncType], FuncType][source]
Decorator to attach a model for return type of a route
- birchrest.decorators.protected() Callable[[FuncType], FuncType][source]
Decorator to mark a route as protected inside an API class.
- birchrest.decorators.put(sub_route: str = '') Callable[[FuncType], FuncType][source]
Decorator to define a PUT route inside an API class.