Skip to content

Commit ae2797f

Browse files
committed
feat: added support for non standard http status codes
1 parent ddda419 commit ae2797f

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

openapi_python_client/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"kebabcase": utils.kebab_case,
3232
"pascalcase": utils.pascal_case,
3333
"any": any,
34+
"all": all,
3435
}
3536

3637

openapi_python_client/parser/responses.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__all__ = ["HTTPStatusPattern", "Response", "Responses", "response_from_data"]
2-
32
from collections.abc import Iterator
3+
from http import HTTPStatus
44
from typing import TypedDict
55

66
from attrs import define
@@ -53,11 +53,15 @@ class HTTPStatusPattern:
5353

5454
pattern: str
5555
range: tuple[int, int] | None
56+
is_official: bool
5657

5758
def __init__(self, *, pattern: str, code_range: tuple[int, int] | None):
5859
"""Initialize with a range of status codes or None for the default case."""
5960
self.pattern = pattern
6061
self.range = code_range
62+
self.is_official = self.range is None or all(
63+
code in HTTPStatus for code in range(self.range[0], self.range[1] + 1)
64+
)
6165

6266
@staticmethod
6367
def parse(pattern: str) -> "HTTPStatusPattern | ParseError":

openapi_python_client/templates/endpoint_module.py.jinja

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from http import HTTPStatus
21
from typing import Any, cast
32
from urllib.parse import quote
43

@@ -12,6 +11,13 @@ from ... import errors
1211
{{ relative }}
1312
{% endfor %}
1413

14+
{% if endpoint.responses|list|length == 0 or endpoint.responses|map(attribute="status_code")|map(attribute="is_official")|all %}
15+
import http
16+
HTTPStatus = http.HTTPStatus
17+
{% else %}
18+
HTTPStatus = int
19+
{% endif %}
20+
1521
{% from "endpoint_macros.py.jinja" import header_params, cookie_params, query_params,
1622
arguments, client, kwargs, parse_response, docstring, body_to_kwarg %}
1723

@@ -92,7 +98,7 @@ def _parse_response(*, client: AuthenticatedClient | Client, response: httpx.Res
9298
{% endif %}
9399

94100

95-
def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Response[{{ return_string }}]:
101+
def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Response[{{ return_string }}, HTTPStatus]:
96102
return Response(
97103
status_code=HTTPStatus(response.status_code),
98104
content=response.content,
@@ -103,7 +109,7 @@ def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Res
103109

104110
def sync_detailed(
105111
{{ arguments(endpoint) | indent(4) }}
106-
) -> Response[{{ return_string }}]:
112+
) -> Response[{{ return_string }}, HTTPStatus]:
107113
{{ docstring(endpoint, return_string, is_detailed=true) | indent(4) }}
108114

109115
kwargs = _get_kwargs(
@@ -129,7 +135,7 @@ def sync(
129135

130136
async def asyncio_detailed(
131137
{{ arguments(endpoint) | indent(4) }}
132-
) -> Response[{{ return_string }}]:
138+
) -> Response[{{ return_string }}, HTTPStatus]:
133139
{{ docstring(endpoint, return_string, is_detailed=true) | indent(4) }}
134140

135141
kwargs = _get_kwargs(

openapi_python_client/templates/types.py.jinja

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ class File:
3838

3939

4040
T = TypeVar("T")
41+
S = TypeVar("S", bound=HTTPStatus | int)
4142

4243

4344
@define
44-
class Response(Generic[T]):
45+
class Response(Generic[T, S]):
4546
""" A response from an endpoint """
4647

47-
status_code: HTTPStatus
48+
status_code: S
4849
content: bytes
4950
headers: MutableMapping[str, str]
5051
parsed: T | None

0 commit comments

Comments
 (0)