[docs]@abstractmethoddefprocess_template(self,*args,**kwargs):"""Process template with middleware Raises: NotImplementedError: abstract method """raiseNotImplementedError
[docs]@abstractmethoddefprocess_exception(self,exception:Exception):"""Process exception with middleware Args: exception (Exception): exception class Raises: exception: exception from arguments """raiseNotImplementedError
[docs]classSessionMiddleware(BaseMiddleware):""" This class describes a session (cookie) middleware. """
[docs]defto_request(self,request:Request):"""Apply cookies to request Args: request (Request): request object """cookie=request.environ.get("HTTP_COOKIE",None)ifnotcookie:returnsession_id=parse_qs(cookie)["session_id"][0]logger.debug(f"Set session_id={session_id} for request {request.method}{request.path}")request.extra["session_id"]=session_id
[docs]defto_response(self,response:Response):"""Get session uuid by response Args: response (Response): response """ifnotresponse.request.session_id:session_id=uuid4()logger.debug(f"Set session_id={session_id} for response {response.status_code}{response.request.path}")response.add_headers([("Set-Cookie",f"session_id={session_id}"),])
[docs]defprocess_template(self,*args,**kwargs):"""Process template with middleware Raises: NotImplementedError: abstract method """raiseNotImplementedError
[docs]defprocess_exception(self,exception:Exception):"""Process exception with middleware Args: exception (Exception): exception class Raises: exception: exception from arguments """ifnotisinstance(exception,pyEchoNextException)ornotisinstance(exception,WebError):raiseexception