duck.http.core.processor¶
Module containing RequestProcessor class for processing requests.
Module Contents¶
Classes¶
Asynchronous RequestProcessor class for enforcing middleware checks and processing requests. |
|
RequestProcessor class for enforcing middleware checks and processing requests. |
Functions¶
Returns whether the request URL is in DJANGO_SIDE_URLS in settings.py, this means that the request is meant to be handled by Django directly, and Duck doesn’t know anything about the urlpattern resolving to this request url. |
|
Returns whether the request URL is in DUCK_EXPLICIT_URLS meaning, The request with the url doesn’t need to be parsed to Django server first to produce a response, but rather to be handled by Duck directly. |
Data¶
API¶
- class duck.http.core.processor.AsyncRequestProcessor(request: duck.http.request.HttpRequest)¶
Bases:
duck.http.core.processor.RequestProcessorAsynchronous RequestProcessor class for enforcing middleware checks and processing requests.
Initialization
Initializes the request processor.
- Parameters:
request – HttpRequest object to process
- async check_middlewares() tuple[str, Optional[duck.http.middlewares.BaseMiddleware]]¶
Checks a request against all configured middlewares in settings.py.
This method iterates through the middlewares defined in settings and verifies if the request meets the requirements imposed by each middleware. If a middleware check fails, processing stops and the error middleware is returned. Otherwise, the method returns a tuple indicating successful request processing (“ok”, None).
… admonition:: Notes
If the request failed to satisfy any middleware, the key “FAIL_MIDDLEWARE” will be set.
- Returns:
A tuple containing two elements: - str: Indicates the outcome of the middleware checks. - “ok”: The request passed all middleware checks. - “bad”: The request failed a middleware check. - middleware (Optional[BaseMiddleware]): The middleware that the request failed to meet, or None if all checks passed.
- Return type:
tuple[str, BaseMiddleware]
- async get_django_response() duck.http.core.proxyhandler.HttpProxyResponse¶
Returns the streaming http response fetched from django server.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request if not a django_sided request.
RouteNotFoundError – If the requested url doesn’t match any registered routes if not a django_sided request.
Exception – Any other exceptions.
- async get_middleware_error_response(middleware) duck.http.response.HttpResponse¶
Returns middleware error response.
- async get_response(request: duck.http.request.HttpRequest) duck.http.response.HttpResponse¶
Returns the full response for a request, with all middlewares and other configurations applied.
- Returns:
Http response
- Return type:
- async get_view_response() duck.http.response.HttpResponse¶
Processes the appropriate view for the request and returns appropriate response.
This is called if request has not failed any of the middleware checks and is considered error free, ready to be parsed to the appropriate view to generate response.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request.
RouteNotFoundError – If the requested url doesn’t match any registered routes.
ExpectingNoResponse – Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
Exception – Any other exceptions.
Notes:
This also closes Django DB connections if any connection was made in view.
- async process_django_request() Union[duck.http.response.HttpResponse, duck.http.core.proxyhandler.HttpProxyResponse]¶
Processes the request and send it to Django proxy for receiving the appropriate http response.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request.
RouteNotFoundError – If the requested url doesn’t match any registered routes.
RequestSyntaxError – Raised if request has syntax error.
RequestUnsupportedVersionError – If request has unsupported HTTP version.
RequestError – Request based errors.
ExpectingNoResponse – Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
Exception – Any other exceptions.
- async process_request() duck.http.response.HttpResponse¶
Processes the http request and returns the appropriate http response.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request. RouteNotFoundError: If the requested url doesn’t match any registered routes. RequestSyntaxError: Raised if request has syntax error. RequestUnsupportedVersionError: If request has unsupported HTTP version. RequestError: Request based errors. ExpectingNoResponse: Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response. Exception: Any other exceptions.
- duck.http.core.processor.DJANGO_SIDE_PATTERNS¶
None
- duck.http.core.processor.DUCK_EXPLICIT_PATTERNS¶
None
- class duck.http.core.processor.RequestProcessor(request: duck.http.request.HttpRequest)¶
RequestProcessor class for enforcing middleware checks and processing requests.
Initialization
Initializes the request processor.
- Parameters:
request – HttpRequest object to process
- __slots__¶
(‘request’, ‘_route_info’)
- check_base_errors()¶
Checks base errors within the request.
RequestError: If the request has errors or is malformed anyhow.
- check_errors()¶
Checks for errors like MethodNotAllowedError and RouteNotFoundError.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request.
RouteNotFoundError – If the requested url doesn’t match any registered routes.
- check_middlewares() tuple[str, Optional[duck.http.middlewares.BaseMiddleware]]¶
Checks a request against all configured middlewares in settings.py.
This method iterates through the middlewares defined in settings and verifies if the request meets the requirements imposed by each middleware. If a middleware check fails, processing stops and the error middleware is returned. Otherwise, the method returns a tuple indicating successful request processing (“ok”, None).
Notes:
If the request failed to satisfy any middleware, the key “FAIL_MIDDLEWARE” will be set.
- Returns:
A tuple containing two elements: - str: Indicates the outcome of the middleware checks. - “ok”: The request passed all middleware checks. - “bad”: The request failed a middleware check. - middleware (Optional[BaseMiddleware]): The middleware that the request failed to meet, or None if all checks passed.
- Return type:
tuple[str, BaseMiddleware]
- get_django_response() duck.http.core.proxyhandler.HttpProxyResponse¶
Returns the streaming http response fetched from django server.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request if not django_sided request.
RouteNotFoundError – If the requested url doesn’t match any registered routes if not django_sided request.
Exception – Any other exceptions.
- get_middleware_error_response(middleware) duck.http.response.HttpResponse¶
Returns middleware error response.
- get_response(request: duck.http.request.HttpRequest) duck.http.response.HttpResponse¶
Returns the full response for a request, with all middlewares and other configurations applied.
- Returns:
Http response.
- Return type:
- Raises:
ExpectingNoResponse – Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
- get_view_response() duck.http.response.HttpResponse¶
Processes the appropriate view for the request and returns appropriate response.
This is called if request has not failed any of the middleware checks and is considered error free, ready to be parsed to the appropriate view to generate response.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request.
RouteNotFoundError – If the requested url doesn’t match any registered routes.
ExpectingNoResponse – Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
Exception – Any other exceptions.
- Returns:
Returns the appropriate http response.
- Return type:
Notes:
This also closes Django DB connections if any connection was made in view.
- normalize_request()¶
Normalizes the request object.
- process_django_request() Union[duck.http.response.HttpResponse, duck.http.core.proxyhandler.HttpProxyResponse]¶
Processes the request and send it to Django proxy for receiving the appropriate http response.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request.
RouteNotFoundError – If the requested url doesn’t match any registered routes.
RequestSyntaxError – Raised if request has syntax error.
RequestUnsupportedVersionError – If request has unsupported HTTP version.
RequestError – If request has some errors.
ExpectingNoResponse – Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
Exception – Any other exceptions.
- process_request() duck.http.response.HttpResponse¶
Processes the http request and returns the appropriate http response.
- Raises:
MethodNotAllowedError – If the requested method is not allowed for the current request.
RouteNotFoundError – If the requested url doesn’t match any registered routes.
RequestSyntaxError – Raised if request has syntax error.
RequestUnsupportedVersionError – If request has unsupported HTTP version.
RequestError – If request has some errors.
ExpectingNoResponse – Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
Exception – Any other exceptions.
- property route_info: Dict[str, Any]¶
Returns the resolved request route information.
Raturns: Dict: The dictionary with the results
- Raises:
RouteNotFoundError – If there is no information on the request path meaning, no urlpattern resolving to this request path has been registered.
RouteError – Other route-related errors.
- duck.http.core.processor.is_django_side_url(url: str) Optional[re.Pattern]¶
Returns whether the request URL is in DJANGO_SIDE_URLS in settings.py, this means that the request is meant to be handled by Django directly, and Duck doesn’t know anything about the urlpattern resolving to this request url.
- Returns:
The matched pattern or None.
- Return type:
Optional[re.Pattern]
- duck.http.core.processor.is_duck_explicit_url(url: str) Optional[re.Pattern]¶
Returns whether the request URL is in DUCK_EXPLICIT_URLS meaning, The request with the url doesn’t need to be parsed to Django server first to produce a response, but rather to be handled by Duck directly.
- Returns:
The matched pattern or None.
- Return type:
Optional[re.Pattern]
Notes:
This is only useful when USE_DJANGO=True as all requests all proxied to Django server to generate the http response.