Source code for lback.core.base_middleware

from abc import ABC, abstractmethod 
from typing import Any, Optional


from .response import Response 


[docs] class BaseMiddleware(ABC): """ Abstract base class for all middleware classes in the application framework. Defines the required interface that all concrete middleware implementations must follow. Middlewares are processed by the MiddlewareManager during request and response handling. """
[docs] @abstractmethod def process_request(self, request: Any) -> Optional[Response]: """ Abstract method to process the incoming request before it reaches the view. Concrete middleware classes must implement this method. It can modify the request object or return a Response object to short-circuit the request processing chain and prevent the view from being called. Args: request: The incoming request object. Returns: A Response object to immediately return to the client (short-circuit), or None to allow the request to proceed to the next middleware or the view. """ pass
[docs] @abstractmethod def process_response(self, request: Any, response: Response) -> Response: """ Abstract method to process the outgoing response after the view has been executed or a middleware has short-circuited the request. Concrete middleware classes must implement this method. It can modify the response object before it is sent back to the client. This method is typically called in reverse order of middleware addition. Args: request: The incoming request object (potentially modified by process_request chain). response: The initial Response object generated by the view or a preceding middleware. Returns: The modified or original Response object. Must always return a Response. """ pass