duck.http.content¶
Module to represent a request/response Content class.
Module Contents¶
Classes¶
Content class to represent the data to be sent in the response. |
Data¶
API¶
- duck.http.content.COMPRESSION_ENCODING¶
‘get(…)’
- duck.http.content.COMPRESSION_LEVEL¶
‘get(…)’
- duck.http.content.COMPRESSION_MAX_SIZE¶
‘get(…)’
- duck.http.content.COMPRESSION_MIMETYPES¶
‘get(…)’
- duck.http.content.COMPRESSION_MIN_SIZE¶
‘get(…)’
- duck.http.content.COMPRESS_STREAMING_RESPONSES¶
‘get(…)’
- duck.http.content.CONTENT_COMPRESSION¶
None
- class duck.http.content.Content(data: bytes = b'', filepath: str = None, content_type: str = None, encoding: str = 'identity', compression_min_size: int = COMPRESSION_MIN_SIZE, compression_max_size: int = COMPRESSION_MAX_SIZE, compression_level: int = COMPRESSION_LEVEL, compression_mimetypes: list = COMPRESSION_MIMETYPES, suppress_errors: bool = False, auto_read_file: bool = True)¶
Content class to represent the data to be sent in the response.
Initialization
Initialize the content object.
- Parameters:
data – The data to be set.
filepath – The file path to read data from.
content_type – The content type of the data.
encoding – The encoding of the data.
compression_min_size – The minimum size of data to compress.
compression_max_size – The maximum size of data to compress.
compression_level – The compression level to use.
compression_mimetypes – The list of mimetypes to compress.
suppress_errors – Whether to suppress errors by trying to fix any issues.
auto_read_file – Automatically read file and set as data if data not provided.
- __repr__()¶
- __slots__¶
(‘_Content__data’, ‘_Content__filepath’, ‘_Content__encoding’, ‘_Content__content_type’, ‘_force_siz…
- _compress(data: bytes, encoding: str, **kwargs) Tuple[bytes, bool]¶
Compress data for the provided encoding.
- Parameters:
data – The data to compress.
encoding – The encoding to use.
**kwargs – Additional arguments for zlib, brotli or deflate compression
- Returns:
The data and bool whether compression was successful.
- Return type:
(data, success) (Tuple[bytes, bool])
Conditions:
enable_content_compression= True.size<= compression_max_size andsize>= compression_min_sizecontent_typeset.encodingis recognized.datais in bytes.
- _decompress(data: bytes) Tuple[bytes, bool]¶
Decompress data for the provided encoding.
- Returns:
The data and bool whether decompression was successful.
- Return type:
(data, success) (Tuple[bytes, bool])
Conditions:
enable_content_compression= True.content_typeset.size<= compression_max_sizeencodingis recognized.datais in bytes.
- compress(encoding: str, **kwargs) bool¶
Compress the content data.
- Parameters:
encoding – The encoding to use.
**kwargs – Additional arguments for zlib, brotli or deflate compression
- Returns:
Whether compression has been successfull.
- Return type:
bool
- property compressed¶
Check if the content data is compressed.
- property content_type: str¶
Get the content type.
- Returns:
The current content type.
- Return type:
str
- correct_encoding()¶
Returns the calculated current correct encoding depending on the current data.
- property data¶
- decompress()¶
Decompress the content data.
- Returns:
Whether compression has been successfull.
- Return type:
bool
- property encoding: str¶
Get the file encoding.
- Returns:
The current encoding of the file.
- Return type:
str
- property filepath: str¶
Get the file path.
- Returns:
The current file path.
- Return type:
str
- force_set_data(data)¶
Forcily set data regardless of the type.
- mimetype_supported(mimetype: str) bool¶
Checks whether the given mimetype is supported for compression or decompression.
- parse_type(content_type=None)¶
Parse the mimetype of content data, if content_type is None, the content_type will be guessed.
- property raw¶
- remove_fake_size() None¶
Remove the forced fake size, restoring size calculation to be based on the actual data.
- set_content(data: bytes = b'', filepath: str = None, content_type=None)¶
Set the content and data should already be encoded to bytes.
- set_fake_size(size: int) None¶
Forcefully set a fake size for the data.
- Parameters:
size – The fake size to set.
- property size: int¶
Returns the size of the data. If a fake size is set, it returns the forced size. Otherwise, it calculates the size based on the current data.