Skip to content

Commit 56a7943

Browse files
Int status codes (#92)
* Use plain int for response.status_code * Linting
1 parent 920925c commit 56a7943

13 files changed

Lines changed: 42 additions & 39 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ Let's get started...
2121
```python
2222
>>> import http3
2323
>>> r = http3.get('https://www.example.org/')
24+
>>> r
25+
<Response [200 OK]>
2426
>>> r.status_code
25-
<StatusCode.OK: 200>
27+
200
2628
>>> r.protocol
2729
'HTTP/2'
2830
>>> r.headers['content-type']

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*An HTTP response.*
3838

3939
* `def __init__(...)`
40-
* `.status_code` - **int** *(Typically a `StatusCode` IntEnum.)*
40+
* `.status_code` - **int**
4141
* `.reason_phrase` - **str**
4242
* `.protocol` - `"HTTP/2"` or `"HTTP/1.1"`
4343
* `.url` - **URL**

docs/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ Let's get started...
2323
```python
2424
>>> import http3
2525
>>> r = http3.get('https://www.example.org/')
26+
>>> r
27+
<Response [200 OK]>
2628
>>> r.status_code
27-
<StatusCode.OK: 200>
29+
200
2830
>>> r.protocol
2931
'HTTP/2'
3032
>>> r.headers['content-type']

docs/quickstart.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Now, let’s try to get a webpage.
1515

1616
```python
1717
>>> r = http3.get('https://httpbin.org/get')
18+
>>> r
19+
<Response [200 OK]>
1820
```
1921

2022
Similarly, to make an HTTP POST request:
@@ -233,21 +235,13 @@ We can inspect the HTTP status code of the response:
233235
```python
234236
>>> r = http3.get('https://httpbin.org/get')
235237
>>> r.status_code
236-
<StatusCode.OK: 200>
237-
```
238-
239-
The status code is an integer enum, meaning that the Python representation gives
240-
use some descriptive information, but the value itself can be used as a regular integer.
241-
242-
```python
243-
>>> r.status_code == 200
244-
True
238+
200
245239
```
246240

247241
HTTP3 also includes an easy shortcut for accessing status codes by their text phrase.
248242

249243
```python
250-
>>> r.status_code == requests.codes.OK
244+
>>> r.status_code == http3.codes.OK
251245
True
252246
```
253247

@@ -256,7 +250,7 @@ We can raise an exception for any Client or Server error responses (4xx or 5xx s
256250
```python
257251
>>> not_found = http3.get('https://httpbin.org/status/404')
258252
>>> not_found.status_code
259-
<StatusCode.NOT_FOUND: 404>
253+
404
260254
>>> not_found.raise_for_status()
261255
Traceback (most recent call last):
262256
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.
348342
>>> r.url
349343
URL('https://github.com/')
350344
>>> r.status_code
351-
<StatusCode.OK: 200>
345+
200
352346
>>> r.history
353-
[<Response [301]>]
347+
[<Response [301 Moved Permanently]>]
354348
```
355349

356350
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:
370364
>>> r.url
371365
'https://github.com/'
372366
>>> r.history
373-
[<Response [301]>]
367+
[<Response [301 Moved Permanently]>]
374368
```
375369

376370
## Timeouts

http3/client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ def sync_on_close() -> None:
609609

610610
response = Response(
611611
status_code=async_response.status_code,
612-
reason_phrase=async_response.reason_phrase,
613612
protocol=async_response.protocol,
614613
headers=async_response.headers,
615614
content=sync_content,

http3/dispatch/http11.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@ async def send(
6767
event = await self._receive_event(timeout)
6868

6969
assert isinstance(event, h11.Response)
70-
reason_phrase = event.reason.decode("ascii", errors="ignore")
7170
status_code = event.status_code
7271
headers = event.headers
7372
content = self._body_iter(timeout)
7473

7574
return AsyncResponse(
7675
status_code=status_code,
77-
reason_phrase=reason_phrase,
7876
protocol="HTTP/1.1",
7977
headers=headers,
8078
content=content,

http3/dispatch/threaded.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ async def async_on_close() -> None:
6464

6565
return AsyncResponse(
6666
status_code=sync_response.status_code,
67-
reason_phrase=sync_response.reason_phrase,
6867
protocol=sync_response.protocol,
6968
headers=sync_response.headers,
7069
content=async_content,

http3/models.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,21 +658,23 @@ def __init__(
658658
self,
659659
status_code: int,
660660
*,
661-
reason_phrase: str = None,
662661
protocol: str = None,
663662
headers: HeaderTypes = None,
664663
request: BaseRequest = None,
665664
on_close: typing.Callable = None,
666665
):
667-
self.status_code = StatusCode.enum_or_int(status_code)
668-
self.reason_phrase = StatusCode.get_reason_phrase(status_code)
666+
self.status_code = status_code
669667
self.protocol = protocol
670668
self.headers = Headers(headers)
671669

672670
self.request = request
673671
self.on_close = on_close
674672
self.next = None # typing.Optional[typing.Callable]
675673

674+
@property
675+
def reason_phrase(self) -> str:
676+
return StatusCode.get_reason_phrase(self.status_code)
677+
676678
@property
677679
def url(self) -> typing.Optional[URL]:
678680
"""
@@ -807,15 +809,14 @@ def cookies(self) -> "Cookies":
807809
return self._cookies
808810

809811
def __repr__(self) -> str:
810-
return f"<Response({self.status_code}, {self.reason_phrase!r})>"
812+
return f"<Response [{self.status_code} {self.reason_phrase}])>"
811813

812814

813815
class AsyncResponse(BaseResponse):
814816
def __init__(
815817
self,
816818
status_code: int,
817819
*,
818-
reason_phrase: str = None,
819820
protocol: str = None,
820821
headers: HeaderTypes = None,
821822
content: AsyncResponseContent = None,
@@ -825,7 +826,6 @@ def __init__(
825826
):
826827
super().__init__(
827828
status_code=status_code,
828-
reason_phrase=reason_phrase,
829829
protocol=protocol,
830830
headers=headers,
831831
request=request,
@@ -896,7 +896,6 @@ def __init__(
896896
self,
897897
status_code: int,
898898
*,
899-
reason_phrase: str = None,
900899
protocol: str = None,
901900
headers: HeaderTypes = None,
902901
content: ResponseContent = None,
@@ -906,7 +905,6 @@ def __init__(
906905
):
907906
super().__init__(
908907
status_code=status_code,
909-
reason_phrase=reason_phrase,
910908
protocol=protocol,
911909
headers=headers,
912910
request=request,

http3/status_codes.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ def __new__(cls, value: int, phrase: str = "") -> "StatusCode":
2525
def __str__(self) -> str:
2626
return str(self.value)
2727

28-
@classmethod
29-
def enum_or_int(cls, value: int) -> int:
30-
try:
31-
return StatusCode(value)
32-
except ValueError:
33-
return value
34-
3528
@classmethod
3629
def get_reason_phrase(cls, value: int) -> str:
3730
try:

tests/client/test_async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async def test_get(server):
1212
assert response.text == "Hello, world!"
1313
assert response.protocol == "HTTP/1.1"
1414
assert response.headers
15-
assert repr(response) == "<Response(200, 'OK')>"
15+
assert repr(response) == "<Response [200 OK])>"
1616

1717

1818
@pytest.mark.asyncio

0 commit comments

Comments
 (0)