From f5f2c59c5465f72b94595f94ca299a711448b6e4 Mon Sep 17 00:00:00 2001 From: TAHRI Ahmed R Date: Tue, 26 Sep 2023 14:01:23 +0200 Subject: [PATCH] :bookmark: Release 3.0.1 (#27) --- HISTORY.md | 10 ++++++++++ src/niquests/models.py | 32 ++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 6e6a093240..a8c1177f54 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,16 @@ Release History =============== +3.0.1 (2023-09-26) +------------------ + +**Fixed** +- Method `head` not accepting `allow_redirect` named argument. +- `PreparedRequest` did not uppercase the HTTP verb when constructed manually. + +**Changed** +- Minor code refactor in the `text`, and `json` methods within the `Response` class to be more concise. + 3.0.0 (2023-09-24) ------------------ diff --git a/src/niquests/models.py b/src/niquests/models.py index e02a51bd94..f482889cc6 100644 --- a/src/niquests/models.py +++ b/src/niquests/models.py @@ -293,7 +293,7 @@ def copy(self) -> PreparedRequest: def prepare_method(self, method: HttpMethodType | None) -> None: """Prepares the given HTTP method.""" - self.method = method + self.method = method.upper() if method else method @staticmethod def _get_idna_encoded_host(host: str) -> str: @@ -1076,16 +1076,12 @@ def text(self) -> str | None: non-HTTP knowledge to make a better guess at the encoding, you should set ``r.encoding`` appropriately before accessing this property. """ - - # Try charset from content-type - encoding = self.encoding - if not self.content: return "" - if encoding is not None: + if self.encoding is not None: try: - info = codecs.lookup(encoding) + info = codecs.lookup(self.encoding) if ( hasattr(info, "_is_text_encoding") @@ -1093,17 +1089,22 @@ def text(self) -> str | None: ): return None except LookupError: - encoding = None + #: We cannot accept unsupported or nonexistent encoding. Override. + self.encoding = None # Fallback to auto-detected encoding. if self.encoding is None: encoding_guess = from_bytes(self.content).best() - encoding = encoding_guess.encoding if encoding_guess else None - if encoding is None: + if encoding_guess: + #: We shall cache this inference. + self.encoding = encoding_guess.encoding + return str(encoding_guess) + + if self.encoding is None: return None - return str(self.content, encoding, errors="replace") + return str(self.content, self.encoding, errors="replace") def json(self, **kwargs: typing.Any) -> typing.Any: r"""Returns the json-encoded content of a response, if any. @@ -1113,12 +1114,15 @@ def json(self, **kwargs: typing.Any) -> typing.Any: contain valid json or if content-type is not about json. """ - if "json" not in self.headers.get("content-type", "").lower(): + if ( + not self.content + or "json" not in self.headers.get("content-type", "").lower() + ): raise RequestsJSONDecodeError( - "response content-type is not JSON", self.text or "", 0 + "response content is not JSON", self.text or "", 0 ) - if not self.encoding and self.content: + if not self.encoding: # No encoding set. JSON RFC 4627 section 3 states we should expect # UTF-8, -16 or -32. Detect which one to use; If the detection or # decoding fails, fall back to `self.text` (using charset_normalizer to make