duck.backend.django.middlewares.duckshare¶
Duck-Django Middleware Integration
This module facilitates seamless data sharing between Duck and Django by providing
middleware that processes and decompiles custom headers. It ensures that shared
data is injected into Django’s request META for consistent handling in Django views
and middleware.
Key Components:
DuckMiddleware: A middleware class that processes and removes shared headers, restoring them to Django’s request
META.DJANGO_MIDDLEWARE: A dynamically imported middleware responsible for restoring HTTP requests and managing shared data between Duck and Django.
Usage:
Add
DuckMiddlewareto theMIDDLEWARElist in Django’s settings to enable this functionality. The integration relies onDJANGO_MIDDLEWARE.restore_django_request.
Dependencies:
MiddlewareMixin: Django’s middleware base class for compatibility.x_import: Utility for dynamic imports, used to loadDJANGO_MIDDLEWARE.
Example:
# settings.py
MIDDLEWARE = [
...
"duck.http.middlewares.DuckMiddleware",
...
]
Module Contents¶
Classes¶
Middleware for processing and sharing custom headers between Duck and Django. |
Data¶
API¶
- duck.backend.django.middlewares.duckshare.DJANGO_MIDDLEWARE¶
‘x_import(…)’
- class duck.backend.django.middlewares.duckshare.DuckMiddleware(get_response)¶
Bases:
django.contrib.sessions.middleware.MiddlewareMixinMiddleware for processing and sharing custom headers between Duck and Django.
This middleware is responsible for identifying and decompiling headers that start with a specific prefix to facilitate seamless data sharing between Duck’s HTTP layer and Django’s request handling. Shared headers are removed from the original request headers after processing, and their corresponding data is injected into Django’s request
META.Responsibilities:
Decompile and process headers with a specific prefix that indicate shared data between Duck and Django.
Inject processed header data into Django’s
METAdictionary, making it accessible within Django views or middleware.Safely discard unsupported header types to ensure stability.
Notes:
Shared headers that do not match the expected format or have unsupported data types will be discarded and will not be added to Django’s request
META.This middleware depends on
DJANGO_MIDDLEWARE.restore_django_requestfor restoring and processing the request.
Methods:
process_request(request):
Restores and processes the incoming request to inject shared header data into Django’s request
META.
Usage:
Add
DuckMiddlewareto theMIDDLEWARElist in your Django settings to enable header processing and data sharing.
Example:
# settings.py MIDDLEWARE = [ ... "duck.http.middlewares.DuckMiddleware", ... ]Initialization
- process_request(request)¶
Processes the incoming request to restore and decompile headers shared between Duck and Django.
- Parameters:
request – The incoming HTTP request object.
Workflow:
Calls
DJANGO_MIDDLEWARE.restore_django_requestto process headers and restore them to Django’s requestMETA.Removes processed headers from the original request object to prevent redundancy or leakage of shared data.
- Returns:
The method modifies the request object in place.
- Return type:
None