Skip to content

Commit 02e7b32

Browse files
committed
feat: use the official list of HTTPStatus
taken from python standard library, exists from python 3.5, described in [the documentation][1] as: > A subclass of enum.IntEnum that defines a set of HTTP status codes, > reason phrases and long descriptions written in English. [1]: https://docs.python.org/3/library/http.html
1 parent 13b2325 commit 02e7b32

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

openapi_python_client/parser/openapi.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import OrderedDict
33
from copy import deepcopy
44
from dataclasses import dataclass, field
5+
from http import HTTPStatus
56
from typing import Any, Dict, Iterator, List, Optional, Set, Tuple, Union
67

78
import attr
@@ -230,15 +231,16 @@ def _add_responses(
230231
endpoint = deepcopy(endpoint)
231232
for code, response_data in data.items():
232233

233-
status_code: int
234+
status_code: HTTPStatus
234235
try:
235-
status_code = int(code)
236+
status_code = HTTPStatus(int(code))
236237
except ValueError:
237238
endpoint.errors.append(
238239
ParseError(
239240
detail=(
240-
f"Invalid response status code {code} (not a number), "
241-
f"response will be ommitted from generated client"
241+
f"Invalid response status code {code} (not a valid HTTP "
242+
f"status code), response will be ommitted from generated "
243+
f"client"
242244
)
243245
)
244246
)

openapi_python_client/parser/responses.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
__all__ = ["Response", "response_from_data"]
22

3+
from http import HTTPStatus
34
from typing import Optional, Tuple, Union
45

56
import attr
@@ -15,7 +16,7 @@
1516
class Response:
1617
"""Describes a single response for an endpoint"""
1718

18-
status_code: int
19+
status_code: HTTPStatus
1920
prop: Property
2021
source: str
2122

@@ -28,7 +29,9 @@ class Response:
2829
}
2930

3031

31-
def empty_response(*, status_code: int, response_name: str, config: Config, description: Optional[str]) -> Response:
32+
def empty_response(
33+
*, status_code: HTTPStatus, response_name: str, config: Config, description: Optional[str]
34+
) -> Response:
3235
"""Return an untyped response, for when no response type is defined"""
3336
return Response(
3437
status_code=status_code,
@@ -46,7 +49,12 @@ def empty_response(*, status_code: int, response_name: str, config: Config, desc
4649

4750

4851
def response_from_data(
49-
*, status_code: int, data: Union[oai.Response, oai.Reference], schemas: Schemas, parent_name: str, config: Config
52+
*,
53+
status_code: HTTPStatus,
54+
data: Union[oai.Response, oai.Reference],
55+
schemas: Schemas,
56+
parent_name: str,
57+
config: Config,
5058
) -> Tuple[Union[Response, ParseError], Schemas]:
5159
"""Generate a Response from the OpenAPI dictionary representation of it"""
5260

openapi_python_client/templates/endpoint_module.py.jinja

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from http import HTTPStatus
12
from typing import Any, Dict, List, Optional, Union, cast
23

34
import httpx
@@ -75,7 +76,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[{{ return_string }}
7576

7677
def _build_response(*, response: httpx.Response) -> Response[{{ return_string }}]:
7778
return Response(
78-
status_code=response.status_code,
79+
status_code=HTTPStatus(response.status_code),
7980
content=response.content,
8081
headers=response.headers,
8182
{% if parsed_responses %}

openapi_python_client/templates/types.py.jinja

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" Contains some shared types for properties """
2+
from http import HTTPStatus
23
from typing import Any, BinaryIO, Generic, MutableMapping, Optional, Tuple, TypeVar
34

45
import attr
@@ -35,7 +36,7 @@ T = TypeVar("T")
3536
class Response(Generic[T]):
3637
""" A response from an endpoint """
3738

38-
status_code: int
39+
status_code: HTTPStatus
3940
content: bytes
4041
headers: MutableMapping[str, str]
4142
parsed: Optional[T]

0 commit comments

Comments
 (0)