duck.http.response_payload¶
Module representing response payload classes.
Module Contents¶
Classes¶
BaseResponsePayload class. |
|
ResponsePayload class for storing response top header and headers. |
|
A class representing a simplified HTTP response payload, storing basic response metadata such as the top header (status line) and HTTP headers. |
API¶
- class duck.http.response_payload.BaseResponsePayload[source]¶
BaseResponsePayload class.
- property cookies: http.cookies.SimpleCookie¶
Property getter for cookies. If not already initialized, it initializes the cookies.
- Returns:
The cookies for the response.
- Return type:
SimpleCookie
- delete_cookie(key: str, path: str = '/', domain: Optional[str] = None) None[source]¶
Delete a cookie from the response payload by setting its expiration date to the past. This will prompt the client to remove the cookie.
- Parameters:
key – The name of the cookie to delete.
path – The path for which the cookie was set. Defaults to “/”.
domain – The domain for which the cookie was set. Defaults to None.
- get_all_cookies() Dict[str, str][source]¶
Retrieves all cookies as a dictionary.
- Returns:
A dictionary of all cookies, where the key is the cookie name and the value is the cookie value (without other cookie properties).
- Return type:
Dict[str, str]
- get_cookie(name: str) str[source]¶
Retrieves the value of a specific cookie by name.
- Parameters:
name – The name of the cookie to retrieve.
- Returns:
The cookie value, or an empty string if the cookie does not exist.
- Return type:
str
- get_cookie_obj(name: str) Optional[http.cookies.Morsel][source]¶
Retrieves the cookie object/morsel of a specific cookie by name.
- Parameters:
name – The name of the cookie to retrieve.
- Returns:
The cookie object, or None if the cookie does not exist.
- Return type:
Optional[Morsel]
- get_cookie_str(name: str, include_cookie_name: bool = True) str[source]¶
Returns the cookie string for the provided name with all fields including max-age, domain, path, etc.
- Parameters:
include_cookie_name – Whether to cookie name e.g.
cookie=something;. Defaults to True.
- abstract property raw¶
- set_cookie(key: str, value: str = '', domain: Optional[str] = None, path: str = '/', max_age: Optional[Union[int, datetime.timedelta]] = None, expires: Optional[Union[datetime.datetime, str]] = None, secure: bool = False, httponly: bool = False, samesite: Optional[str] = 'Lax') None[source]¶
Set a custom cookie on the response payload.
- Parameters:
key – The name of the cookie (e.g., ‘csrftoken’).
value – The value of the cookie (e.g., ‘some_random_token’).
domain – The domain to set for the cookie. Defaults to None.
path – The path for the cookie. Defaults to ‘/’ indicating the whole site.
max_age – The maximum age of the cookie in seconds or as a timedelta object.
expires – The expiration date of the cookie. Defaults to None.
secure – Whether the cookie should only be sent over HTTPS connections. Defaults to False.
httponly – Whether the cookie should be inaccessible to JavaScript. Defaults to False.
samesite – The SameSite attribute for the cookie. Default is ‘Lax’. Other possible values are ‘Strict’ or ‘None’.
- Raises:
ValueError – If an invalid value is provided for
samesite.
- class duck.http.response_payload.HttpResponsePayload(**kwargs)[source]¶
Bases:
duck.http.response_payload.BaseResponsePayloadResponsePayload class for storing response top header and headers.
Example payload:
HTTP/1.1 200 OK Connection: close Content-Type: text/htmlInitialization
- Parameters:
status_code – The status code for the response. Default value is 200.
status_message – The status message corresponding to the status code.
headers – The headers to attach to the response payload.
http_version – The HTTP version for the server. Default value is ‘HTTP/2.1’
explanation – A longer explanation of the status message.
- get_header(header: str, default_value=None) Optional[str][source]¶
Returns a header value of default if not found.
- parse_status(code: int, msg: str = None, explanation: str = None)[source]¶
Parses topheader status for the payload.
- property raw: bytes¶
Returns the raw bytes representing the payload (header section).
- class duck.http.response_payload.SimpleHttpResponsePayload(topheader: str = '', headers: Optional[Dict] = None)[source]¶
Bases:
duck.http.response_payload.BaseResponsePayloadA class representing a simplified HTTP response payload, storing basic response metadata such as the top header (status line) and HTTP headers.
This class is used for lightweight storage and manipulation of HTTP response headers and status-related information. It does not handle the response body.
- Variables:
cookies – The http.cookies.SimpleCookie object for all cookies.
topheader – The status line of the HTTP response (e.g., “200 OK”). Defaults to an empty string.
headers – A
Headersobject containing the HTTP response headers. Defaults toNone.
Methods:
set_topheader(topheader: str): Updates the top header (status line) of the response.
set_headers(headers: dict): Updates the HTTP headers using a dictionary input.
Properties:
status_code (int): Extracts and returns the status code from the
topheader.status_message (str): Extracts and returns the status message from the
topheader. Defaults to an empty string if thetopheaderis not set.explanation (str): A placeholder for additional explanation or description of the status. Currently always returns an empty string.
Initialization
Initializes a SimpleHttpResponsePayload instance.
- Parameters:
topheader – The HTTP response status line (e.g., “HTTP/1.1 200 OK”). Defaults to an empty string.
headers – A dictionary representing the HTTP response headers. Defaults to
None.
- property explanation: str¶
Placeholder for additional status explanation.
- Returns:
Always returns an empty string.
- Return type:
str
- get_header(header: str, default_value=None) Optional[str][source]¶
Returns a header value of default if not found.
- property http_version: str¶
Extracts and returns the HTTP version from the
topheader.This property analyzes the
topheader(status line) of the HTTP response and retrieves the HTTP version, such as “HTTP/1.1” or “HTTP/2”. If thetopheaderis not set or improperly formatted, it defaults to the last version inHttpRequest.SUPPORTED_HTTP_VERSIONS.- Returns:
The HTTP version extracted from the
topheaderor the default version if unavailable.- Return type:
str
- property raw: bytes¶
Constructs and returns the raw HTTP response as bytes. This property generates a raw representation of the HTTP response by combining the `topheader` (status line) and all headers stored in the `headers` attribute. Each header is formatted as "Header-Name: value", separated by carriage return and newline characters (``). The resulting raw response is encoded as UTF-8.
Returns: bytes: The raw HTTP response as bytes.
- set_topheader(topheader: str)[source]¶
Updates the status line of the HTTP response.
- Parameters:
topheader – The new HTTP status line (e.g., “404 Not Found”).
- property status_code: int¶
Extracts and returns the HTTP status code from the status line.
- Returns:
The HTTP status code (e.g., 200).
- Return type:
int
- property status_message: str¶
Extracts and returns the HTTP status message from the status line.
- Returns:
The HTTP status message (e.g., “OK”).
- Return type:
str