ContentDecodePolicy Class
Policy for decoding unstreamed response content.
Constructor
ContentDecodePolicy(response_encoding: str | None = None, **kwargs: Any)
Parameters
| Name | Description |
|---|---|
|
response_encoding
|
The encoding to use if known for this service (will disable auto-detection) Default value: None
|
Methods
| deserialize_from_http_generics |
Deserialize from HTTP response. Headers will tested for "content-type" |
| deserialize_from_text |
Decode response data according to content-type. Accept a stream of data as well, but will be load at once in memory for now. If no content-type, will return the string version (not bytes, not stream) |
| on_exception |
Is executed if an exception is raised while executing the next policy. This method is executed inside the exception handler. |
| on_request |
Set the response encoding in the request context. |
| on_response |
Extract data from the body of a REST response object. This will load the entire payload in memory. Will follow Content-Type to parse. We assume everything is UTF8 (BOM acceptable). |
deserialize_from_http_generics
Deserialize from HTTP response.
Headers will tested for "content-type"
deserialize_from_http_generics(response: _HttpResponseBase | _HttpResponseBase, encoding: str | None = None) -> Any
Parameters
| Name | Description |
|---|---|
|
response
Required
|
The HTTP response |
|
encoding
|
The encoding to use if known for this service (will disable auto-detection) Default value: None
|
Returns
| Type | Description |
|---|---|
|
A dict (JSON), XML tree or str, depending of the mime_type |
Exceptions
| Type | Description |
|---|---|
|
If deserialization fails |
deserialize_from_text
Decode response data according to content-type.
Accept a stream of data as well, but will be load at once in memory for now. If no content-type, will return the string version (not bytes, not stream)
deserialize_from_text(data: AnyStr | IO | None, mime_type: str | None = None, response: _HttpResponseBase | _HttpResponseBase | None = None) -> Any
Parameters
| Name | Description |
|---|---|
|
data
Required
|
The data to deserialize. |
|
response
|
The HTTP response. Default value: None
|
|
mime_type
|
The mime type. As mime type, charset is not expected. Default value: None
|
|
response
Required
|
If passed, exception will be annotated with that response |
Returns
| Type | Description |
|---|---|
|
A dict (JSON), XML tree or str, depending of the mime_type |
Exceptions
| Type | Description |
|---|---|
|
If deserialization fails |
on_exception
Is executed if an exception is raised while executing the next policy.
This method is executed inside the exception handler.
on_exception(request: PipelineRequest[HTTPRequestType]) -> None
Parameters
| Name | Description |
|---|---|
|
request
Required
|
The Pipeline request object |
on_request
Set the response encoding in the request context.
on_request(request: PipelineRequest[HttpRequest | HttpRequest]) -> None
Parameters
| Name | Description |
|---|---|
|
request
Required
|
The PipelineRequest object. |
on_response
Extract data from the body of a REST response object. This will load the entire payload in memory. Will follow Content-Type to parse. We assume everything is UTF8 (BOM acceptable).
on_response(request: PipelineRequest[HttpRequest | HttpRequest], response: PipelineResponse[HttpRequest | HttpRequest, _HttpResponseBase | _HttpResponseBase]) -> None
Parameters
| Name | Description |
|---|---|
|
request
Required
|
The PipelineRequest object. |
|
response
Required
|
The PipelineResponse object. |
Exceptions
| Type | Description |
|---|---|
|
JSONDecodeError
|
If JSON is requested and parsing is impossible. |
|
If bytes is not UTF8 |
|
|
If bytes is not valid XML |
|
|
If deserialization fails |
Attributes
CONTEXT_NAME
CONTEXT_NAME = 'deserialized_data'
JSON_REGEXP
JSON_REGEXP = re.compile('^(application|text)/([0-9a-z+.-]+\\+)?json$')