Similar to :meth:`http.client.HTTPResponse.read`, but with two additional parameters: ``decode_content`` and ``cache_content``. :param amt: How much of the content to read. If specified, caching is skipped because it doesn't make sense to cache parti
(
self,
amt: int | None = None,
decode_content: bool | None = None,
cache_content: bool = False,
)
| 1057 | return data |
| 1058 | |
| 1059 | def read( |
| 1060 | self, |
| 1061 | amt: int | None = None, |
| 1062 | decode_content: bool | None = None, |
| 1063 | cache_content: bool = False, |
| 1064 | ) -> bytes: |
| 1065 | class="st">""" |
| 1066 | Similar to :meth:`http.client.HTTPResponse.read`, but with two additional |
| 1067 | parameters: ``decode_content`` and ``cache_content``. |
| 1068 | |
| 1069 | :param amt: |
| 1070 | How much of the content to read. If specified, caching is skipped |
| 1071 | because it doesn&class="cm">#x27;t make sense to cache partial content as the full |
| 1072 | response. |
| 1073 | |
| 1074 | :param decode_content: |
| 1075 | If True, will attempt to decode the body based on the |
| 1076 | &class="cm">#x27;content-encoding' header. |
| 1077 | |
| 1078 | :param cache_content: |
| 1079 | If True, will save the returned data such that the same result is |
| 1080 | returned despite of the state of the underlying file object. This |
| 1081 | is useful if you want the ``.data`` property to continue working |
| 1082 | after having ``.read()`` the file object. (Overridden if ``amt`` is |
| 1083 | set.) |
| 1084 | class="st">""" |
| 1085 | self._init_decoder() |
| 1086 | if decode_content is None: |
| 1087 | decode_content = self.decode_content |
| 1088 | |
| 1089 | if amt and amt < 0: |
| 1090 | class="cm"># Negative numbers and `None` should be treated the same. |
| 1091 | amt = None |
| 1092 | elif amt is not None: |
| 1093 | cache_content = False |
| 1094 | |
| 1095 | if ( |
| 1096 | self._decoder |
| 1097 | and self._decoder.has_unconsumed_tail |
| 1098 | and len(self._decoded_buffer) < amt |
| 1099 | ): |
| 1100 | decoded_data = self._decode( |
| 1101 | bclass="st">"", |
| 1102 | decode_content, |
| 1103 | flush_decoder=False, |
| 1104 | max_length=amt - len(self._decoded_buffer), |
| 1105 | ) |
| 1106 | self._decoded_buffer.put(decoded_data) |
| 1107 | if len(self._decoded_buffer) >= amt: |
| 1108 | return self._decoded_buffer.get(amt) |
| 1109 | |
| 1110 | data = self._raw_read(amt) |
| 1111 | if not cache_content: |
| 1112 | self._uncached_read_occurred = True |
| 1113 | |
| 1114 | flush_decoder = amt is None or (amt != 0 and not data) |
| 1115 | |
| 1116 | if ( |