|
| 1 | +import inspect |
| 2 | +from binascii import crc32 |
| 3 | + |
| 4 | +import aiobotocore.endpoint |
| 5 | +import aiobotocore.retryhandler |
| 6 | +from aiobotocore.endpoint import HttpxStreamingBody, StreamingBody |
| 7 | +from aiobotocore.retryhandler import ChecksumError, logger |
| 8 | + |
| 9 | +try: |
| 10 | + import httpx |
| 11 | +except ImportError: |
| 12 | + httpx = None |
| 13 | + |
| 14 | + |
| 15 | +async def _fixed_check_response(self, attempt_number, response): |
| 16 | + http_response = response[0] |
| 17 | + expected_crc = http_response.headers.get(self._header_name) |
| 18 | + if expected_crc is None: |
| 19 | + logger.debug( |
| 20 | + "crc32 check skipped, the %s header is not in the http response.", |
| 21 | + self._header_name, |
| 22 | + ) |
| 23 | + else: |
| 24 | + if inspect.isawaitable(http_response.content): |
| 25 | + data_buf = await http_response.content |
| 26 | + else: |
| 27 | + data_buf = http_response.content |
| 28 | + |
| 29 | + actual_crc32 = crc32(data_buf) & 0xFFFFFFFF |
| 30 | + if not actual_crc32 == int(expected_crc): |
| 31 | + logger.debug( |
| 32 | + "retry needed: crc32 check failed, expected != actual: %s != %s", |
| 33 | + int(expected_crc), |
| 34 | + actual_crc32, |
| 35 | + ) |
| 36 | + raise ChecksumError( |
| 37 | + checksum_type="crc32", |
| 38 | + expected_checksum=int(expected_crc), |
| 39 | + actual_checksum=actual_crc32, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +async def convert_to_response_dict(http_response, operation_model): |
| 44 | + """Convert an HTTP response object to a request dict. |
| 45 | +
|
| 46 | + This converts the HTTP response object to a dictionary. |
| 47 | +
|
| 48 | + :type http_response: botocore.awsrequest.AWSResponse |
| 49 | + :param http_response: The HTTP response from an AWS service request. |
| 50 | +
|
| 51 | + :rtype: dict |
| 52 | + :return: A response dictionary which will contain the following keys: |
| 53 | + * headers (dict) |
| 54 | + * status_code (int) |
| 55 | + * body (string or file-like object) |
| 56 | +
|
| 57 | + """ |
| 58 | + response_dict = { |
| 59 | + "headers": http_response.headers, |
| 60 | + "status_code": http_response.status_code, |
| 61 | + "context": { |
| 62 | + "operation_name": operation_model.name, |
| 63 | + }, |
| 64 | + } |
| 65 | + if response_dict["status_code"] >= 300: |
| 66 | + if inspect.isawaitable(http_response.content): |
| 67 | + response_dict["body"] = await http_response.content |
| 68 | + else: |
| 69 | + response_dict["body"] = http_response.content |
| 70 | + elif operation_model.has_event_stream_output: |
| 71 | + response_dict["body"] = http_response.raw |
| 72 | + elif operation_model.has_streaming_output: |
| 73 | + if httpx and isinstance(http_response.raw, httpx.Response): |
| 74 | + response_dict["body"] = HttpxStreamingBody(http_response.raw) |
| 75 | + else: |
| 76 | + length = response_dict["headers"].get("content-length") |
| 77 | + response_dict["body"] = StreamingBody(http_response.raw, length) |
| 78 | + else: |
| 79 | + if inspect.isawaitable(http_response.content): |
| 80 | + response_dict["body"] = await http_response.content |
| 81 | + else: |
| 82 | + response_dict["body"] = http_response.content |
| 83 | + return response_dict |
| 84 | + |
| 85 | + |
| 86 | +aiobotocore.retryhandler.AioCRC32Checker._check_response = _fixed_check_response |
| 87 | +aiobotocore.endpoint.convert_to_response_dict = convert_to_response_dict |
0 commit comments