Skip to content

Commit

Permalink
Merge branch 'main' of github.com:jawah/niquests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ousret committed Sep 26, 2023
2 parents aa6ad51 + f5f2c59 commit deca5cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
10 changes: 10 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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)
------------------

Expand Down
32 changes: 18 additions & 14 deletions src/niquests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1076,34 +1076,35 @@ 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")
and info._is_text_encoding is False
):
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.
Expand All @@ -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
Expand Down

0 comments on commit deca5cb

Please sign in to comment.