Skip to content

Commit 5a3fa1c

Browse files
feat: Parse JSON responses without schema
It makes sense to return the parsed JSON here instead of None.
1 parent e743c9e commit 5a3fa1c

File tree

5 files changed

+189
-9
lines changed

5 files changed

+189
-9
lines changed

end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional
2+
from typing import Any, Dict, Optional, cast
33

44
import httpx
55

@@ -29,7 +29,8 @@ def _get_kwargs(
2929

3030
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
3131
if response.status_code == HTTPStatus.OK:
32-
return None
32+
response_200 = cast(Any, response.json())
33+
return response_200
3334
if client.raise_on_unexpected_status:
3435
raise errors.UnexpectedStatus(response.status_code, response.content)
3536
else:
@@ -71,6 +72,25 @@ def sync_detailed(
7172
return _build_response(client=client, response=response)
7273

7374

75+
def sync(
76+
*,
77+
client: Client,
78+
) -> Optional[Any]:
79+
"""No Response
80+
81+
Raises:
82+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
83+
httpx.TimeoutException: If the request takes longer than Client.timeout.
84+
85+
Returns:
86+
Response[Any]
87+
"""
88+
89+
return sync_detailed(
90+
client=client,
91+
).parsed
92+
93+
7494
async def asyncio_detailed(
7595
*,
7696
client: Client,
@@ -93,3 +113,24 @@ async def asyncio_detailed(
93113
response = await _client.request(**kwargs)
94114

95115
return _build_response(client=client, response=response)
116+
117+
118+
async def asyncio(
119+
*,
120+
client: Client,
121+
) -> Optional[Any]:
122+
"""No Response
123+
124+
Raises:
125+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
126+
httpx.TimeoutException: If the request takes longer than Client.timeout.
127+
128+
Returns:
129+
Response[Any]
130+
"""
131+
132+
return (
133+
await asyncio_detailed(
134+
client=client,
135+
)
136+
).parsed

end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional
2+
from typing import Any, Dict, Optional, cast
33

44
import httpx
55

@@ -32,7 +32,8 @@ def _get_kwargs(
3232

3333
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
3434
if response.status_code == HTTPStatus.OK:
35-
return None
35+
response_200 = cast(Any, response.json())
36+
return response_200
3637
if client.raise_on_unexpected_status:
3738
raise errors.UnexpectedStatus(response.status_code, response.content)
3839
else:
@@ -78,6 +79,29 @@ def sync_detailed(
7879
return _build_response(client=client, response=response)
7980

8081

82+
def sync(
83+
*,
84+
client: Client,
85+
form_data: AFormData,
86+
) -> Optional[Any]:
87+
"""Post form data
88+
89+
Post form data
90+
91+
Raises:
92+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
93+
httpx.TimeoutException: If the request takes longer than Client.timeout.
94+
95+
Returns:
96+
Response[Any]
97+
"""
98+
99+
return sync_detailed(
100+
client=client,
101+
form_data=form_data,
102+
).parsed
103+
104+
81105
async def asyncio_detailed(
82106
*,
83107
client: Client,
@@ -104,3 +128,28 @@ async def asyncio_detailed(
104128
response = await _client.request(**kwargs)
105129

106130
return _build_response(client=client, response=response)
131+
132+
133+
async def asyncio(
134+
*,
135+
client: Client,
136+
form_data: AFormData,
137+
) -> Optional[Any]:
138+
"""Post form data
139+
140+
Post form data
141+
142+
Raises:
143+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
144+
httpx.TimeoutException: If the request takes longer than Client.timeout.
145+
146+
Returns:
147+
Response[Any]
148+
"""
149+
150+
return (
151+
await asyncio_detailed(
152+
client=client,
153+
form_data=form_data,
154+
)
155+
).parsed

end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data_inline.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional
2+
from typing import Any, Dict, Optional, cast
33

44
import httpx
55

@@ -32,7 +32,8 @@ def _get_kwargs(
3232

3333
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
3434
if response.status_code == HTTPStatus.OK:
35-
return None
35+
response_200 = cast(Any, response.json())
36+
return response_200
3637
if client.raise_on_unexpected_status:
3738
raise errors.UnexpectedStatus(response.status_code, response.content)
3839
else:
@@ -78,6 +79,29 @@ def sync_detailed(
7879
return _build_response(client=client, response=response)
7980

8081

82+
def sync(
83+
*,
84+
client: Client,
85+
form_data: PostFormDataInlineData,
86+
) -> Optional[Any]:
87+
"""Post form data (inline schema)
88+
89+
Post form data (inline schema)
90+
91+
Raises:
92+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
93+
httpx.TimeoutException: If the request takes longer than Client.timeout.
94+
95+
Returns:
96+
Response[Any]
97+
"""
98+
99+
return sync_detailed(
100+
client=client,
101+
form_data=form_data,
102+
).parsed
103+
104+
81105
async def asyncio_detailed(
82106
*,
83107
client: Client,
@@ -104,3 +128,28 @@ async def asyncio_detailed(
104128
response = await _client.request(**kwargs)
105129

106130
return _build_response(client=client, response=response)
131+
132+
133+
async def asyncio(
134+
*,
135+
client: Client,
136+
form_data: PostFormDataInlineData,
137+
) -> Optional[Any]:
138+
"""Post form data (inline schema)
139+
140+
Post form data (inline schema)
141+
142+
Raises:
143+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
144+
httpx.TimeoutException: If the request takes longer than Client.timeout.
145+
146+
Returns:
147+
Response[Any]
148+
"""
149+
150+
return (
151+
await asyncio_detailed(
152+
client=client,
153+
form_data=form_data,
154+
)
155+
).parsed

end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Optional
2+
from typing import Any, Dict, Optional, cast
33

44
import httpx
55

@@ -29,7 +29,8 @@ def _get_kwargs(
2929

3030
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
3131
if response.status_code == HTTPStatus.OK:
32-
return None
32+
response_200 = cast(Any, response.json())
33+
return response_200
3334
if client.raise_on_unexpected_status:
3435
raise errors.UnexpectedStatus(response.status_code, response.content)
3536
else:
@@ -71,6 +72,25 @@ def sync_detailed(
7172
return _build_response(client=client, response=response)
7273

7374

75+
def sync(
76+
*,
77+
client: Client,
78+
) -> Optional[Any]:
79+
"""Unsupported Content
80+
81+
Raises:
82+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
83+
httpx.TimeoutException: If the request takes longer than Client.timeout.
84+
85+
Returns:
86+
Response[Any]
87+
"""
88+
89+
return sync_detailed(
90+
client=client,
91+
).parsed
92+
93+
7494
async def asyncio_detailed(
7595
*,
7696
client: Client,
@@ -93,3 +113,24 @@ async def asyncio_detailed(
93113
response = await _client.request(**kwargs)
94114

95115
return _build_response(client=client, response=response)
116+
117+
118+
async def asyncio(
119+
*,
120+
client: Client,
121+
) -> Optional[Any]:
122+
"""Unsupported Content
123+
124+
Raises:
125+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
126+
httpx.TimeoutException: If the request takes longer than Client.timeout.
127+
128+
Returns:
129+
Response[Any]
130+
"""
131+
132+
return (
133+
await asyncio_detailed(
134+
client=client,
135+
)
136+
).parsed

openapi_python_client/templates/endpoint_module.py.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from ... import errors
1515
arguments, client, kwargs, parse_response, docstring %}
1616

1717
{% set return_string = endpoint.response_type() %}
18-
{% set parsed_responses = (endpoint.responses | length > 0) and return_string not in ("Any", "None") %}
18+
{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "None" %}
1919

2020
def _get_kwargs(
2121
{{ arguments(endpoint) | indent(4) }}

0 commit comments

Comments
 (0)