Skip to content

Commit 77a01bf

Browse files
feat: Add raise_on_unexpected_status flag to generated Client [#593]. Thanks @JamesHinshelwood, @ramnes, @gwenshap, @theFong!
Co-authored-by: Dylan Anthony <[email protected]>
1 parent 99638b1 commit 77a01bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+925
-198
lines changed

end_to_end_tests/golden-record/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ client = AuthenticatedClient(
6161
)
6262
```
6363

64+
There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info.
65+
6466
Things to know:
6567
1. Every path/method combo becomes a Python module with four functions:
6668
1. `sync`: Blocking request that returns parsed data (if successful) or `None`

end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Union
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

6+
from ... import errors
67
from ...client import Client
78
from ...types import UNSET, Response, Unset
89

@@ -32,12 +33,21 @@ def _get_kwargs(
3233
}
3334

3435

35-
def _build_response(*, response: httpx.Response) -> Response[Any]:
36+
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
37+
if response.status_code == HTTPStatus.OK:
38+
return None
39+
if client.raise_on_unexpected_status:
40+
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
41+
else:
42+
return None
43+
44+
45+
def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]:
3646
return Response(
3747
status_code=HTTPStatus(response.status_code),
3848
content=response.content,
3949
headers=response.headers,
40-
parsed=None,
50+
parsed=_parse_response(client=client, response=response),
4151
)
4252

4353

@@ -50,6 +60,10 @@ def sync_detailed(
5060
Args:
5161
common (Union[Unset, None, str]):
5262
63+
Raises:
64+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
65+
httpx.TimeoutException: If the request takes longer than Client.timeout.
66+
5367
Returns:
5468
Response[Any]
5569
"""
@@ -64,7 +78,7 @@ def sync_detailed(
6478
**kwargs,
6579
)
6680

67-
return _build_response(response=response)
81+
return _build_response(client=client, response=response)
6882

6983

7084
async def asyncio_detailed(
@@ -76,6 +90,10 @@ async def asyncio_detailed(
7690
Args:
7791
common (Union[Unset, None, str]):
7892
93+
Raises:
94+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
95+
httpx.TimeoutException: If the request takes longer than Client.timeout.
96+
7997
Returns:
8098
Response[Any]
8199
"""
@@ -88,4 +106,4 @@ async def asyncio_detailed(
88106
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
89107
response = await _client.request(**kwargs)
90108

91-
return _build_response(response=response)
109+
return _build_response(client=client, response=response)

end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Union
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

6+
from ... import errors
67
from ...client import Client
78
from ...types import UNSET, Response, Unset
89

@@ -32,12 +33,21 @@ def _get_kwargs(
3233
}
3334

3435

35-
def _build_response(*, response: httpx.Response) -> Response[Any]:
36+
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
37+
if response.status_code == HTTPStatus.OK:
38+
return None
39+
if client.raise_on_unexpected_status:
40+
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
41+
else:
42+
return None
43+
44+
45+
def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]:
3646
return Response(
3747
status_code=HTTPStatus(response.status_code),
3848
content=response.content,
3949
headers=response.headers,
40-
parsed=None,
50+
parsed=_parse_response(client=client, response=response),
4151
)
4252

4353

@@ -50,6 +60,10 @@ def sync_detailed(
5060
Args:
5161
common (Union[Unset, None, str]):
5262
63+
Raises:
64+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
65+
httpx.TimeoutException: If the request takes longer than Client.timeout.
66+
5367
Returns:
5468
Response[Any]
5569
"""
@@ -64,7 +78,7 @@ def sync_detailed(
6478
**kwargs,
6579
)
6680

67-
return _build_response(response=response)
81+
return _build_response(client=client, response=response)
6882

6983

7084
async def asyncio_detailed(
@@ -76,6 +90,10 @@ async def asyncio_detailed(
7690
Args:
7791
common (Union[Unset, None, str]):
7892
93+
Raises:
94+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
95+
httpx.TimeoutException: If the request takes longer than Client.timeout.
96+
7997
Returns:
8098
Response[Any]
8199
"""
@@ -88,4 +106,4 @@ async def asyncio_detailed(
88106
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
89107
response = await _client.request(**kwargs)
90108

91-
return _build_response(response=response)
109+
return _build_response(client=client, response=response)

end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_header_types.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict, Union
2+
from typing import Any, Dict, Optional, Union
33

44
import httpx
55

6+
from ... import errors
67
from ...client import Client
78
from ...models.get_location_header_types_int_enum_header import GetLocationHeaderTypesIntEnumHeader
89
from ...models.get_location_header_types_string_enum_header import GetLocationHeaderTypesStringEnumHeader
@@ -51,12 +52,21 @@ def _get_kwargs(
5152
}
5253

5354

54-
def _build_response(*, response: httpx.Response) -> Response[Any]:
55+
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
56+
if response.status_code == HTTPStatus.OK:
57+
return None
58+
if client.raise_on_unexpected_status:
59+
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
60+
else:
61+
return None
62+
63+
64+
def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]:
5565
return Response(
5666
status_code=HTTPStatus(response.status_code),
5767
content=response.content,
5868
headers=response.headers,
59-
parsed=None,
69+
parsed=_parse_response(client=client, response=response),
6070
)
6171

6272

@@ -79,6 +89,10 @@ def sync_detailed(
7989
int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]):
8090
string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]):
8191
92+
Raises:
93+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
94+
httpx.TimeoutException: If the request takes longer than Client.timeout.
95+
8296
Returns:
8397
Response[Any]
8498
"""
@@ -98,7 +112,7 @@ def sync_detailed(
98112
**kwargs,
99113
)
100114

101-
return _build_response(response=response)
115+
return _build_response(client=client, response=response)
102116

103117

104118
async def asyncio_detailed(
@@ -120,6 +134,10 @@ async def asyncio_detailed(
120134
int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]):
121135
string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]):
122136
137+
Raises:
138+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
139+
httpx.TimeoutException: If the request takes longer than Client.timeout.
140+
123141
Returns:
124142
Response[Any]
125143
"""
@@ -137,4 +155,4 @@ async def asyncio_detailed(
137155
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
138156
response = await _client.request(**kwargs)
139157

140-
return _build_response(response=response)
158+
return _build_response(client=client, response=response)

end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_query_optionality.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import datetime
22
from http import HTTPStatus
3-
from typing import Any, Dict, Union
3+
from typing import Any, Dict, Optional, Union
44

55
import httpx
66

7+
from ... import errors
78
from ...client import Client
89
from ...types import UNSET, Response, Unset
910

@@ -56,12 +57,21 @@ def _get_kwargs(
5657
}
5758

5859

59-
def _build_response(*, response: httpx.Response) -> Response[Any]:
60+
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
61+
if response.status_code == HTTPStatus.OK:
62+
return None
63+
if client.raise_on_unexpected_status:
64+
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
65+
else:
66+
return None
67+
68+
69+
def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]:
6070
return Response(
6171
status_code=HTTPStatus(response.status_code),
6272
content=response.content,
6373
headers=response.headers,
64-
parsed=None,
74+
parsed=_parse_response(client=client, response=response),
6575
)
6676

6777

@@ -80,6 +90,10 @@ def sync_detailed(
8090
null_not_required (Union[Unset, None, datetime.datetime]):
8191
not_null_not_required (Union[Unset, None, datetime.datetime]):
8292
93+
Raises:
94+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
95+
httpx.TimeoutException: If the request takes longer than Client.timeout.
96+
8397
Returns:
8498
Response[Any]
8599
"""
@@ -97,7 +111,7 @@ def sync_detailed(
97111
**kwargs,
98112
)
99113

100-
return _build_response(response=response)
114+
return _build_response(client=client, response=response)
101115

102116

103117
async def asyncio_detailed(
@@ -115,6 +129,10 @@ async def asyncio_detailed(
115129
null_not_required (Union[Unset, None, datetime.datetime]):
116130
not_null_not_required (Union[Unset, None, datetime.datetime]):
117131
132+
Raises:
133+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
134+
httpx.TimeoutException: If the request takes longer than Client.timeout.
135+
118136
Returns:
119137
Response[Any]
120138
"""
@@ -130,4 +148,4 @@ async def asyncio_detailed(
130148
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
131149
response = await _client.request(**kwargs)
132150

133-
return _build_response(response=response)
151+
return _build_response(client=client, response=response)

end_to_end_tests/golden-record/my_test_api_client/api/parameter_references/get_parameter_references_path_param.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from http import HTTPStatus
2-
from typing import Any, Dict
2+
from typing import Any, Dict, Optional
33

44
import httpx
55

6+
from ... import errors
67
from ...client import Client
78
from ...types import UNSET, Response
89

@@ -42,12 +43,21 @@ def _get_kwargs(
4243
}
4344

4445

45-
def _build_response(*, response: httpx.Response) -> Response[Any]:
46+
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
47+
if response.status_code == HTTPStatus.OK:
48+
return None
49+
if client.raise_on_unexpected_status:
50+
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
51+
else:
52+
return None
53+
54+
55+
def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]:
4656
return Response(
4757
status_code=HTTPStatus(response.status_code),
4858
content=response.content,
4959
headers=response.headers,
50-
parsed=None,
60+
parsed=_parse_response(client=client, response=response),
5161
)
5262

5363

@@ -69,6 +79,10 @@ def sync_detailed(
6979
header_param (str):
7080
cookie_param (str):
7181
82+
Raises:
83+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
84+
httpx.TimeoutException: If the request takes longer than Client.timeout.
85+
7286
Returns:
7387
Response[Any]
7488
"""
@@ -87,7 +101,7 @@ def sync_detailed(
87101
**kwargs,
88102
)
89103

90-
return _build_response(response=response)
104+
return _build_response(client=client, response=response)
91105

92106

93107
async def asyncio_detailed(
@@ -108,6 +122,10 @@ async def asyncio_detailed(
108122
header_param (str):
109123
cookie_param (str):
110124
125+
Raises:
126+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
127+
httpx.TimeoutException: If the request takes longer than Client.timeout.
128+
111129
Returns:
112130
Response[Any]
113131
"""
@@ -124,4 +142,4 @@ async def asyncio_detailed(
124142
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
125143
response = await _client.request(**kwargs)
126144

127-
return _build_response(response=response)
145+
return _build_response(client=client, response=response)

0 commit comments

Comments
 (0)