From: Tom Christie Date: Mon, 17 Jun 2019 15:53:39 +0000 (+0100) Subject: Int status codes (#92) X-Git-Tag: 0.5.0~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56a79432065f2c9052dd0dd7ddbcf63a24f915b1;p=thirdparty%2Fhttpx.git Int status codes (#92) * Use plain int for response.status_code * Linting --- diff --git a/README.md b/README.md index 4a693392..c8ce9046 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,10 @@ Let's get started... ```python >>> import http3 >>> r = http3.get('https://www.example.org/') +>>> r + >>> r.status_code - +200 >>> r.protocol 'HTTP/2' >>> r.headers['content-type'] diff --git a/docs/api.md b/docs/api.md index 40d90dd5..1e55aa8c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -37,7 +37,7 @@ *An HTTP response.* * `def __init__(...)` -* `.status_code` - **int** *(Typically a `StatusCode` IntEnum.)* +* `.status_code` - **int** * `.reason_phrase` - **str** * `.protocol` - `"HTTP/2"` or `"HTTP/1.1"` * `.url` - **URL** diff --git a/docs/index.md b/docs/index.md index 92579c61..e65da7fe 100644 --- a/docs/index.md +++ b/docs/index.md @@ -23,8 +23,10 @@ Let's get started... ```python >>> import http3 >>> r = http3.get('https://www.example.org/') +>>> r + >>> r.status_code - +200 >>> r.protocol 'HTTP/2' >>> r.headers['content-type'] diff --git a/docs/quickstart.md b/docs/quickstart.md index b0b6a757..5f5eca2d 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -15,6 +15,8 @@ Now, let’s try to get a webpage. ```python >>> r = http3.get('https://httpbin.org/get') +>>> r + ``` Similarly, to make an HTTP POST request: @@ -233,21 +235,13 @@ We can inspect the HTTP status code of the response: ```python >>> r = http3.get('https://httpbin.org/get') >>> r.status_code - -``` - -The status code is an integer enum, meaning that the Python representation gives -use some descriptive information, but the value itself can be used as a regular integer. - -```python ->>> r.status_code == 200 -True +200 ``` HTTP3 also includes an easy shortcut for accessing status codes by their text phrase. ```python ->>> r.status_code == requests.codes.OK +>>> r.status_code == http3.codes.OK True ``` @@ -256,7 +250,7 @@ We can raise an exception for any Client or Server error responses (4xx or 5xx s ```python >>> not_found = http3.get('https://httpbin.org/status/404') >>> not_found.status_code - +404 >>> not_found.raise_for_status() Traceback (most recent call last): File "/Users/tomchristie/GitHub/encode/httpcore/http3/models.py", line 776, in raise_for_status @@ -348,9 +342,9 @@ For example, GitHub redirects all HTTP requests to HTTPS. >>> r.url URL('https://github.com/') >>> r.status_code - +200 >>> r.history -[] +[] ``` You can modify the default redirection handling with the allow_redirects parameter: @@ -370,7 +364,7 @@ If you’re making a `HEAD` request, you can use this to enable redirection: >>> r.url 'https://github.com/' >>> r.history -[] +[] ``` ## Timeouts diff --git a/http3/client.py b/http3/client.py index 13c88270..fd45de54 100644 --- a/http3/client.py +++ b/http3/client.py @@ -609,7 +609,6 @@ class Client(BaseClient): response = Response( status_code=async_response.status_code, - reason_phrase=async_response.reason_phrase, protocol=async_response.protocol, headers=async_response.headers, content=sync_content, diff --git a/http3/dispatch/http11.py b/http3/dispatch/http11.py index f19b3d3d..6a45a049 100644 --- a/http3/dispatch/http11.py +++ b/http3/dispatch/http11.py @@ -67,14 +67,12 @@ class HTTP11Connection: event = await self._receive_event(timeout) assert isinstance(event, h11.Response) - reason_phrase = event.reason.decode("ascii", errors="ignore") status_code = event.status_code headers = event.headers content = self._body_iter(timeout) return AsyncResponse( status_code=status_code, - reason_phrase=reason_phrase, protocol="HTTP/1.1", headers=headers, content=content, diff --git a/http3/dispatch/threaded.py b/http3/dispatch/threaded.py index dbcd4dab..e4fdfd5e 100644 --- a/http3/dispatch/threaded.py +++ b/http3/dispatch/threaded.py @@ -64,7 +64,6 @@ class ThreadedDispatcher(AsyncDispatcher): return AsyncResponse( status_code=sync_response.status_code, - reason_phrase=sync_response.reason_phrase, protocol=sync_response.protocol, headers=sync_response.headers, content=async_content, diff --git a/http3/models.py b/http3/models.py index 70dad80f..94720a51 100644 --- a/http3/models.py +++ b/http3/models.py @@ -658,14 +658,12 @@ class BaseResponse: self, status_code: int, *, - reason_phrase: str = None, protocol: str = None, headers: HeaderTypes = None, request: BaseRequest = None, on_close: typing.Callable = None, ): - self.status_code = StatusCode.enum_or_int(status_code) - self.reason_phrase = StatusCode.get_reason_phrase(status_code) + self.status_code = status_code self.protocol = protocol self.headers = Headers(headers) @@ -673,6 +671,10 @@ class BaseResponse: self.on_close = on_close self.next = None # typing.Optional[typing.Callable] + @property + def reason_phrase(self) -> str: + return StatusCode.get_reason_phrase(self.status_code) + @property def url(self) -> typing.Optional[URL]: """ @@ -807,7 +809,7 @@ class BaseResponse: return self._cookies def __repr__(self) -> str: - return f"" + return f"" class AsyncResponse(BaseResponse): @@ -815,7 +817,6 @@ class AsyncResponse(BaseResponse): self, status_code: int, *, - reason_phrase: str = None, protocol: str = None, headers: HeaderTypes = None, content: AsyncResponseContent = None, @@ -825,7 +826,6 @@ class AsyncResponse(BaseResponse): ): super().__init__( status_code=status_code, - reason_phrase=reason_phrase, protocol=protocol, headers=headers, request=request, @@ -896,7 +896,6 @@ class Response(BaseResponse): self, status_code: int, *, - reason_phrase: str = None, protocol: str = None, headers: HeaderTypes = None, content: ResponseContent = None, @@ -906,7 +905,6 @@ class Response(BaseResponse): ): super().__init__( status_code=status_code, - reason_phrase=reason_phrase, protocol=protocol, headers=headers, request=request, diff --git a/http3/status_codes.py b/http3/status_codes.py index bca2d8ce..1c547770 100644 --- a/http3/status_codes.py +++ b/http3/status_codes.py @@ -25,13 +25,6 @@ class StatusCode(IntEnum): def __str__(self) -> str: return str(self.value) - @classmethod - def enum_or_int(cls, value: int) -> int: - try: - return StatusCode(value) - except ValueError: - return value - @classmethod def get_reason_phrase(cls, value: int) -> str: try: diff --git a/tests/client/test_async_client.py b/tests/client/test_async_client.py index bf7fe573..443ca22d 100644 --- a/tests/client/test_async_client.py +++ b/tests/client/test_async_client.py @@ -12,7 +12,7 @@ async def test_get(server): assert response.text == "Hello, world!" assert response.protocol == "HTTP/1.1" assert response.headers - assert repr(response) == "" + assert repr(response) == "" @pytest.mark.asyncio diff --git a/tests/client/test_client.py b/tests/client/test_client.py index d8e8f4a0..48924cb5 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -37,7 +37,7 @@ def test_get(server): assert response.request.url == http3.URL(url) assert response.headers assert response.is_redirect is False - assert repr(response) == "" + assert repr(response) == "" @threadpool diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py index 140d7f34..6bff37f6 100644 --- a/tests/models/test_responses.py +++ b/tests/models/test_responses.py @@ -22,7 +22,7 @@ def test_response(): def test_response_repr(): response = http3.Response(200, content=b"Hello, world!") - assert repr(response) == "" + assert repr(response) == "" def test_response_content_type_encoding(): diff --git a/tests/test_status_codes.py b/tests/test_status_codes.py new file mode 100644 index 00000000..f7fe2bcd --- /dev/null +++ b/tests/test_status_codes.py @@ -0,0 +1,18 @@ +import http3 + + +def test_status_code_as_int(): + assert http3.codes.NOT_FOUND == 404 + assert str(http3.codes.NOT_FOUND) == "404" + + +def test_lowercase_status_code(): + assert http3.codes.not_found == 404 + + +def test_reason_phrase_for_status_code(): + assert http3.codes.get_reason_phrase(404) == "Not Found" + + +def test_reason_phrase_for_unknown_status_code(): + assert http3.codes.get_reason_phrase(499) == ""