]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Int status codes (#92)
authorTom Christie <tom@tomchristie.com>
Mon, 17 Jun 2019 15:53:39 +0000 (16:53 +0100)
committerGitHub <noreply@github.com>
Mon, 17 Jun 2019 15:53:39 +0000 (16:53 +0100)
* Use plain int for response.status_code

* Linting

13 files changed:
README.md
docs/api.md
docs/index.md
docs/quickstart.md
http3/client.py
http3/dispatch/http11.py
http3/dispatch/threaded.py
http3/models.py
http3/status_codes.py
tests/client/test_async_client.py
tests/client/test_client.py
tests/models/test_responses.py
tests/test_status_codes.py [new file with mode: 0644]

index 4a693392940db947f7c5cd9a6a91b8f030e3e154..c8ce90468f0231292fa800dbe419d7a96c55965c 100644 (file)
--- 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
+<Response [200 OK]>
 >>> r.status_code
-<StatusCode.OK: 200>
+200
 >>> r.protocol
 'HTTP/2'
 >>> r.headers['content-type']
index 40d90dd5fefce993ee440052f7afc939977b2366..1e55aa8c74afc958e32543edce33a33b2f24df45 100644 (file)
@@ -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**
index 92579c61a96f66e2a32def93791863750270c1d7..e65da7fee4af3e48d5998386dc32eec493fed494 100644 (file)
@@ -23,8 +23,10 @@ Let's get started...
 ```python
 >>> import http3
 >>> r = http3.get('https://www.example.org/')
+>>> r
+<Response [200 OK]>
 >>> r.status_code
-<StatusCode.OK: 200>
+200
 >>> r.protocol
 'HTTP/2'
 >>> r.headers['content-type']
index b0b6a757e0b4ed6ae0095a18eb6b007a322941db..5f5eca2d84369379cd2887018f133515eadd7abd 100644 (file)
@@ -15,6 +15,8 @@ Now, let’s try to get a webpage.
 
 ```python
 >>> r = http3.get('https://httpbin.org/get')
+>>> r
+<Response [200 OK]>
 ```
 
 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
-<StatusCode.OK: 200>
-```
-
-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
-<StatusCode.NOT_FOUND: 404>
+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
-<StatusCode.OK: 200>
+200
 >>> r.history
-[<Response [301]>]
+[<Response [301 Moved Permanently]>]
 ```
 
 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
-[<Response [301]>]
+[<Response [301 Moved Permanently]>]
 ```
 
 ## Timeouts
index 13c88270ce11c079ddc331bae18e297da21e2eb5..fd45de544239abe32cbab049e5059034de514e72 100644 (file)
@@ -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,
index f19b3d3dc160ac702af74f7940e422c0e6247c5e..6a45a04937a0645f9322a2cc2a28a0bf6e278456 100644 (file)
@@ -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,
index dbcd4dab1960e21a0df909987ff91044f5e61c00..e4fdfd5ed152dcf1e2a98f6d4b75caff2f23733e 100644 (file)
@@ -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,
index 70dad80f9006a4f3e99a9902ea6777241ed6a65d..94720a51e1c49691e7caa1d4e39af5681e63e091 100644 (file)
@@ -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"<Response({self.status_code}, {self.reason_phrase!r})>"
+        return f"<Response [{self.status_code} {self.reason_phrase}])>"
 
 
 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,
index bca2d8ce5ecb36ce5998cd237eeb00cb1911fbd5..1c547770685c710d9d5620b7652018162231a75e 100644 (file)
@@ -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:
index bf7fe5730148c4f513ea3cabfa879452f80a2c01..443ca22d519697b59217533f3ace069c7ea6208c 100644 (file)
@@ -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) == "<Response(200, 'OK')>"
+    assert repr(response) == "<Response [200 OK])>"
 
 
 @pytest.mark.asyncio
index d8e8f4a07cddd280f15deddd485b57e1ee7b76eb..48924cb59adee3f08d5694102ed8cb867a3a2901 100644 (file)
@@ -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) == "<Response(200, 'OK')>"
+    assert repr(response) == "<Response [200 OK])>"
 
 
 @threadpool
index 140d7f341879bff6e38c381fea4b9575b4e116b7..6bff37f664730a0559c1deb0ddca6691186a947f 100644 (file)
@@ -22,7 +22,7 @@ def test_response():
 
 def test_response_repr():
     response = http3.Response(200, content=b"Hello, world!")
-    assert repr(response) == "<Response(200, 'OK')>"
+    assert repr(response) == "<Response [200 OK])>"
 
 
 def test_response_content_type_encoding():
diff --git a/tests/test_status_codes.py b/tests/test_status_codes.py
new file mode 100644 (file)
index 0000000..f7fe2bc
--- /dev/null
@@ -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) == ""