Add Response.is_error (#574)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 9df76cc · Browse files · 4 files·+11 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+11 added−3 removed
- tests:+1 added−0 removed
- other:+10 added−3 removed
Provenance
- Commit
fdaa012- Tree
83e9bcc- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
9df76cc
4 files changed+11 added−3 removed
modified:docs/compatibility.md+1 added
12 unmodified lines13141516171819202118 unmodified lines12 unmodified lines* `httpx.codes` - In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`,but also provide lower-cased versions for API compatibility with `requests`.* `stream=True`. - Streaming responses provide the `.stream()` and `.raw()` byte iterator interfaces, rather than the `.iter_content()` method and the `.raw` socket interface.* `.get`, `.delete`, `.head`, `.options` - These methods do not support `files`, `data`, or `json` arguments. Use `.request` if you need to need to send data using these http methods.* We don't support `response.is_ok` since the naming is ambiguous there, and might incorrectly imply an equivalence to `response.status_code == codes.OK`. Instead we provide an `.is_error` property.
## Advanced Usage
### requests.Session18 unmodified linesmodified:httpx/models.py+5 added-3 removed
845 unmodified lines846 847 return self._decoder848 849 @property850+ def is_error(self) -> bool:851+ return StatusCode.is_error(self.status_code)852+853+ @property854 def is_redirect(self) -> bool:855 return StatusCode.is_redirect(self.status_code) and "location" in self.headers856 857 def raise_for_status(self) -> None:6 unmodified lines864 )865 866 if StatusCode.is_client_error(self.status_code):867 message = message.format(self, error_type="Client Error")868+ raise HTTPError(message, response=self)869 elif StatusCode.is_server_error(self.status_code):870 message = message.format(self, error_type="Server Error")866- else:867- message = ""868- if message:871 raise HTTPError(message, response=self)872 873 def json(self, **kwargs: typing.Any) -> typing.Union[dict, list]:874 if self.charset_encoding is None and self.content and len(self.content) > 3:274 unmodified linesmodified:httpx/status_codes.py+4 added
48 unmodified lines49 StatusCode.PERMANENT_REDIRECT,50 )51 52 @classmethod53+ def is_error(cls, value: int) -> bool:54+ return 400 <= value <= 59955+56+ @classmethod57 def is_client_error(cls, value: int) -> bool:58 return 400 <= value <= 49959 60 @classmethod79 unmodified linesmodified:tests/models/test_responses.py+1 added
21 unmodified lines22 assert response.status_code == 20023 assert response.reason_phrase == "OK"24 assert response.text == "Hello, world!"25 assert response.elapsed == datetime.timedelta(0)26+ assert not response.is_error27 28 29 def test_response_repr():30 response = httpx.Response(200, content=b"Hello, world!")245 unmodified lines