diff --git a/end_to_end_tests/golden-record/README.md b/end_to_end_tests/golden-record/README.md index 09f9ba7a9..3def2172e 100644 --- a/end_to_end_tests/golden-record/README.md +++ b/end_to_end_tests/golden-record/README.md @@ -61,6 +61,8 @@ client = AuthenticatedClient( ) ``` +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. + Things to know: 1. Every path/method combo becomes a Python module with four functions: 1. `sync`: Blocking request that returns parsed data (if successful) or `None` diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py b/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py index 73b39c755..eea2a39cb 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict, Union +from typing import Any, Dict, Optional, Union import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response, Unset @@ -32,12 +33,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -50,6 +60,10 @@ def sync_detailed( Args: common (Union[Unset, None, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -64,7 +78,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -76,6 +90,10 @@ async def asyncio_detailed( Args: common (Union[Unset, None, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -88,4 +106,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py b/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py index 512f5acec..54f11f8dc 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict, Union +from typing import Any, Dict, Optional, Union import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response, Unset @@ -32,12 +33,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -50,6 +60,10 @@ def sync_detailed( Args: common (Union[Unset, None, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -64,7 +78,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -76,6 +90,10 @@ async def asyncio_detailed( Args: common (Union[Unset, None, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -88,4 +106,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_header_types.py b/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_header_types.py index 9232ff57b..ab6ae3180 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_header_types.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_header_types.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict, Union +from typing import Any, Dict, Optional, Union import httpx +from ... import errors from ...client import Client from ...models.get_location_header_types_int_enum_header import GetLocationHeaderTypesIntEnumHeader from ...models.get_location_header_types_string_enum_header import GetLocationHeaderTypesStringEnumHeader @@ -51,12 +52,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -79,6 +89,10 @@ def sync_detailed( int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]): string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -98,7 +112,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -120,6 +134,10 @@ async def asyncio_detailed( int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]): string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -137,4 +155,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_query_optionality.py b/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_query_optionality.py index b30237822..427cf04dc 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_query_optionality.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_query_optionality.py @@ -1,9 +1,10 @@ import datetime from http import HTTPStatus -from typing import Any, Dict, Union +from typing import Any, Dict, Optional, Union import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response, Unset @@ -56,12 +57,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -80,6 +90,10 @@ def sync_detailed( null_not_required (Union[Unset, None, datetime.datetime]): not_null_not_required (Union[Unset, None, datetime.datetime]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -97,7 +111,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -115,6 +129,10 @@ async def asyncio_detailed( null_not_required (Union[Unset, None, datetime.datetime]): not_null_not_required (Union[Unset, None, datetime.datetime]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -130,4 +148,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/parameter_references/get_parameter_references_path_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/parameter_references/get_parameter_references_path_param.py index 5966089d0..bdb518de5 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/parameter_references/get_parameter_references_path_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/parameter_references/get_parameter_references_path_param.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response @@ -42,12 +43,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -69,6 +79,10 @@ def sync_detailed( header_param (str): cookie_param (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -87,7 +101,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -108,6 +122,10 @@ async def asyncio_detailed( header_param (str): cookie_param (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -124,4 +142,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/delete_common_parameters_overriding_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/delete_common_parameters_overriding_param.py index ff0026f6f..6ddea0265 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/delete_common_parameters_overriding_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/delete_common_parameters_overriding_param.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict, Union +from typing import Any, Dict, Optional, Union import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response, Unset @@ -33,12 +34,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -53,6 +63,10 @@ def sync_detailed( param_path (str): param_query (Union[Unset, None, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -68,7 +82,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -82,6 +96,10 @@ async def asyncio_detailed( param_path (str): param_query (Union[Unset, None, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -95,4 +113,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_common_parameters_overriding_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_common_parameters_overriding_param.py index 9742bbaf3..2089e9c59 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_common_parameters_overriding_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_common_parameters_overriding_param.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response @@ -33,12 +34,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -55,6 +65,10 @@ def sync_detailed( param_query (str): A parameter with the same name as another. Default: 'overridden_in_GET'. Example: an example string. + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -70,7 +84,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -86,6 +100,10 @@ async def asyncio_detailed( param_query (str): A parameter with the same name as another. Default: 'overridden_in_GET'. Example: an example string. + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -99,4 +117,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py index 122081859..c6e0f4736 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/get_same_name_multiple_locations_param.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict, Union +from typing import Any, Dict, Optional, Union import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response, Unset @@ -41,12 +42,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -65,6 +75,10 @@ def sync_detailed( param_header (Union[Unset, str]): param_cookie (Union[Unset, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -82,7 +96,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -100,6 +114,10 @@ async def asyncio_detailed( param_header (Union[Unset, str]): param_cookie (Union[Unset, str]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -115,4 +133,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/multiple_path_parameters.py b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/multiple_path_parameters.py index 772f405c7..a005a85ac 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/parameters/multiple_path_parameters.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/parameters/multiple_path_parameters.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import Response @@ -31,12 +32,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -55,6 +65,10 @@ def sync_detailed( param1 (str): param3 (int): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -72,7 +86,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -90,6 +104,10 @@ async def asyncio_detailed( param1 (str): param3 (int): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -105,4 +123,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/responses/post_responses_unions_simple_before_complex.py b/end_to_end_tests/golden-record/my_test_api_client/api/responses/post_responses_unions_simple_before_complex.py index 115936303..811633348 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/responses/post_responses_unions_simple_before_complex.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/responses/post_responses_unions_simple_before_complex.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.post_responses_unions_simple_before_complex_response_200 import ( PostResponsesUnionsSimpleBeforeComplexResponse200, @@ -28,20 +29,27 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[PostResponsesUnionsSimpleBeforeComplexResponse200]: +def _parse_response( + *, client: Client, response: httpx.Response +) -> Optional[PostResponsesUnionsSimpleBeforeComplexResponse200]: if response.status_code == HTTPStatus.OK: response_200 = PostResponsesUnionsSimpleBeforeComplexResponse200.from_dict(response.json()) return response_200 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[PostResponsesUnionsSimpleBeforeComplexResponse200]: +def _build_response( + *, client: Client, response: httpx.Response +) -> Response[PostResponsesUnionsSimpleBeforeComplexResponse200]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -51,6 +59,10 @@ def sync_detailed( ) -> Response[PostResponsesUnionsSimpleBeforeComplexResponse200]: """Regression test for #603 + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[PostResponsesUnionsSimpleBeforeComplexResponse200] """ @@ -64,7 +76,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -73,6 +85,10 @@ def sync( ) -> Optional[PostResponsesUnionsSimpleBeforeComplexResponse200]: """Regression test for #603 + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[PostResponsesUnionsSimpleBeforeComplexResponse200] """ @@ -88,6 +104,10 @@ async def asyncio_detailed( ) -> Response[PostResponsesUnionsSimpleBeforeComplexResponse200]: """Regression test for #603 + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[PostResponsesUnionsSimpleBeforeComplexResponse200] """ @@ -99,7 +119,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -108,6 +128,10 @@ async def asyncio( ) -> Optional[PostResponsesUnionsSimpleBeforeComplexResponse200]: """Regression test for #603 + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[PostResponsesUnionsSimpleBeforeComplexResponse200] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tag1/get_tag_with_number.py b/end_to_end_tests/golden-record/my_test_api_client/api/tag1/get_tag_with_number.py index 35999bf9f..5df86a828 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tag1/get_tag_with_number.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tag1/get_tag_with_number.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import Response @@ -25,12 +26,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -39,6 +49,10 @@ def sync_detailed( client: Client, ) -> Response[Any]: """ + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -52,7 +66,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -60,6 +74,10 @@ async def asyncio_detailed( client: Client, ) -> Response[Any]: """ + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -71,4 +89,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/callback_test.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/callback_test.py index 4b2655c7c..ca87484c4 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/callback_test.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/callback_test.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.a_model import AModel from ...models.http_validation_error import HTTPValidationError @@ -31,7 +32,7 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == HTTPStatus.OK: response_200 = cast(Any, response.json()) return response_200 @@ -39,15 +40,18 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPVali response_422 = HTTPValidationError.from_dict(response.json()) return response_422 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -63,6 +67,10 @@ def sync_detailed( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -77,7 +85,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -92,6 +100,10 @@ def sync( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -114,6 +126,10 @@ async def asyncio_detailed( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -126,7 +142,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -141,6 +157,10 @@ async def asyncio( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py index 4d9d0bec0..44a2cc859 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py @@ -5,6 +5,7 @@ import httpx from dateutil.parser import isoparse +from ... import errors from ...client import Client from ...models.an_enum import AnEnum from ...models.http_validation_error import HTTPValidationError @@ -98,7 +99,7 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == HTTPStatus.OK: response_200 = cast(Any, response.json()) return response_200 @@ -106,15 +107,18 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPVali response_422 = HTTPValidationError.from_dict(response.json()) return response_422 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -148,6 +152,10 @@ def sync_detailed( model_prop (ModelWithUnionProperty): required_model_prop (ModelWithUnionProperty): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -172,7 +180,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -205,6 +213,10 @@ def sync( model_prop (ModelWithUnionProperty): required_model_prop (ModelWithUnionProperty): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -255,6 +267,10 @@ async def asyncio_detailed( model_prop (ModelWithUnionProperty): required_model_prop (ModelWithUnionProperty): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -277,7 +293,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -310,6 +326,10 @@ async def asyncio( model_prop (ModelWithUnionProperty): required_model_prop (ModelWithUnionProperty): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py index abe983938..ce71633d9 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...types import Response @@ -25,20 +26,23 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[List[bool]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[bool]]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[bool], response.json()) return response_200 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[List[bool]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[List[bool]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -50,6 +54,10 @@ def sync_detailed( Get a list of booleans + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[bool]] """ @@ -63,7 +71,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -74,6 +82,10 @@ def sync( Get a list of booleans + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[bool]] """ @@ -91,6 +103,10 @@ async def asyncio_detailed( Get a list of booleans + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[bool]] """ @@ -102,7 +118,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -113,6 +129,10 @@ async def asyncio( Get a list of booleans + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[bool]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py index 54f5228c9..dcb97ade5 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...types import Response @@ -25,20 +26,23 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[List[float]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[float]]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[float], response.json()) return response_200 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[List[float]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[List[float]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -50,6 +54,10 @@ def sync_detailed( Get a list of floats + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[float]] """ @@ -63,7 +71,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -74,6 +82,10 @@ def sync( Get a list of floats + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[float]] """ @@ -91,6 +103,10 @@ async def asyncio_detailed( Get a list of floats + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[float]] """ @@ -102,7 +118,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -113,6 +129,10 @@ async def asyncio( Get a list of floats + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[float]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py index 860c52dec..800c29608 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...types import Response @@ -25,20 +26,23 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[List[int]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[int]]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[int], response.json()) return response_200 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[List[int]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[List[int]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -50,6 +54,10 @@ def sync_detailed( Get a list of integers + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[int]] """ @@ -63,7 +71,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -74,6 +82,10 @@ def sync( Get a list of integers + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[int]] """ @@ -91,6 +103,10 @@ async def asyncio_detailed( Get a list of integers + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[int]] """ @@ -102,7 +118,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -113,6 +129,10 @@ async def asyncio( Get a list of integers + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[int]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py index 96ceb3b9b..2a84b2b5e 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...types import Response @@ -25,20 +26,23 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[List[str]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[str]]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[str], response.json()) return response_200 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[List[str]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[List[str]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -50,6 +54,10 @@ def sync_detailed( Get a list of strings + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[str]] """ @@ -63,7 +71,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -74,6 +82,10 @@ def sync( Get a list of strings + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[str]] """ @@ -91,6 +103,10 @@ async def asyncio_detailed( Get a list of strings + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[str]] """ @@ -102,7 +118,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -113,6 +129,10 @@ async def asyncio( Get a list of strings + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[List[str]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_user_list.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_user_list.py index 0fd856e12..6cffd7741 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_user_list.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_user_list.py @@ -4,6 +4,7 @@ import httpx +from ... import errors from ...client import Client from ...models.a_model import AModel from ...models.an_enum import AnEnum @@ -69,7 +70,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, List["AModel"]]]: +def _parse_response( + *, client: Client, response: httpx.Response +) -> Optional[Union[HTTPValidationError, List["AModel"]]]: if response.status_code == HTTPStatus.OK: response_200 = [] _response_200 = response.json() @@ -87,15 +90,20 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidatio response_423 = HTTPValidationError.from_dict(response.json()) return response_423 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, List["AModel"]]]: +def _build_response( + *, client: Client, response: httpx.Response +) -> Response[Union[HTTPValidationError, List["AModel"]]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -117,6 +125,10 @@ def sync_detailed( an_enum_value_with_only_null (List[None]): some_date (Union[datetime.date, datetime.datetime]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, List['AModel']]] """ @@ -134,7 +146,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -155,6 +167,10 @@ def sync( an_enum_value_with_only_null (List[None]): some_date (Union[datetime.date, datetime.datetime]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, List['AModel']]] """ @@ -186,6 +202,10 @@ async def asyncio_detailed( an_enum_value_with_only_null (List[None]): some_date (Union[datetime.date, datetime.datetime]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, List['AModel']]] """ @@ -201,7 +221,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -222,6 +242,10 @@ async def asyncio( an_enum_value_with_only_null (List[None]): some_date (Union[datetime.date, datetime.datetime]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, List['AModel']]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py index 6fe848bf8..4e23476f5 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/int_enum_tests_int_enum_post.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.an_int_enum import AnIntEnum from ...models.http_validation_error import HTTPValidationError @@ -36,7 +37,7 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == HTTPStatus.OK: response_200 = cast(Any, response.json()) return response_200 @@ -44,15 +45,18 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPVali response_422 = HTTPValidationError.from_dict(response.json()) return response_422 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -66,6 +70,10 @@ def sync_detailed( Args: int_enum (AnIntEnum): An enumeration. + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -80,7 +88,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -93,6 +101,10 @@ def sync( Args: int_enum (AnIntEnum): An enumeration. + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -113,6 +125,10 @@ async def asyncio_detailed( Args: int_enum (AnIntEnum): An enumeration. + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -125,7 +141,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -138,6 +154,10 @@ async def asyncio( Args: int_enum (AnIntEnum): An enumeration. + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py index 9d4f1d32a..383522958 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/json_body_tests_json_body_post.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.a_model import AModel from ...models.http_validation_error import HTTPValidationError @@ -31,7 +32,7 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == HTTPStatus.OK: response_200 = cast(Any, response.json()) return response_200 @@ -39,15 +40,18 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPVali response_422 = HTTPValidationError.from_dict(response.json()) return response_422 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -63,6 +67,10 @@ def sync_detailed( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -77,7 +85,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -92,6 +100,10 @@ def sync( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -114,6 +126,10 @@ async def asyncio_detailed( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -126,7 +142,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -141,6 +157,10 @@ async def asyncio( Args: json_body (AModel): A Model for testing all the ways custom objects can be used + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py index 1804a98dd..933184d3f 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/no_response_tests_no_response_get.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import Response @@ -25,12 +26,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -40,6 +50,10 @@ def sync_detailed( ) -> Response[Any]: """No Response + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -53,7 +67,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -62,6 +76,10 @@ async def asyncio_detailed( ) -> Response[Any]: """No Response + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -73,4 +91,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/octet_stream_tests_octet_stream_get.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/octet_stream_tests_octet_stream_get.py index abc7c6d40..99859332e 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/octet_stream_tests_octet_stream_get.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/octet_stream_tests_octet_stream_get.py @@ -4,6 +4,7 @@ import httpx +from ... import errors from ...client import Client from ...types import File, Response @@ -26,20 +27,23 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[File]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[File]: if response.status_code == HTTPStatus.OK: response_200 = File(payload=BytesIO(response.content)) return response_200 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[File]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[File]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -49,6 +53,10 @@ def sync_detailed( ) -> Response[File]: """Octet Stream + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[File] """ @@ -62,7 +70,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -71,6 +79,10 @@ def sync( ) -> Optional[File]: """Octet Stream + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[File] """ @@ -86,6 +98,10 @@ async def asyncio_detailed( ) -> Response[File]: """Octet Stream + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[File] """ @@ -97,7 +113,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -106,6 +122,10 @@ async def asyncio( ) -> Optional[File]: """Octet Stream + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[File] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py index 78cc1c157..ff9e887f4 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...models.a_form_data import AFormData from ...types import Response @@ -28,12 +29,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -46,6 +56,10 @@ def sync_detailed( Post form data + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -60,7 +74,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -72,6 +86,10 @@ async def asyncio_detailed( Post form data + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -84,4 +102,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data_inline.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data_inline.py index 46352cb64..46536a27a 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data_inline.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_form_data_inline.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...models.post_form_data_inline_data import PostFormDataInlineData from ...types import Response @@ -28,12 +29,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -46,6 +56,10 @@ def sync_detailed( Post form data (inline schema) + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -60,7 +74,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -72,6 +86,10 @@ async def asyncio_detailed( Post form data (inline schema) + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -84,4 +102,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_tests_json_body_string.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_tests_json_body_string.py index cf8734816..6a1d178dc 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_tests_json_body_string.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_tests_json_body_string.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.http_validation_error import HTTPValidationError from ...types import Response @@ -30,7 +31,7 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, str]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[HTTPValidationError, str]]: if response.status_code == HTTPStatus.OK: response_200 = cast(str, response.json()) return response_200 @@ -38,15 +39,18 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidatio response_422 = HTTPValidationError.from_dict(response.json()) return response_422 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -60,6 +64,10 @@ def sync_detailed( Args: json_body (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, str]] """ @@ -74,7 +82,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -87,6 +95,10 @@ def sync( Args: json_body (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, str]] """ @@ -107,6 +119,10 @@ async def asyncio_detailed( Args: json_body (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, str]] """ @@ -119,7 +135,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -132,6 +148,10 @@ async def asyncio( Args: json_body (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[HTTPValidationError, str]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/test_inline_objects.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/test_inline_objects.py index 7ff96d63d..d1533c0e1 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/test_inline_objects.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/test_inline_objects.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.test_inline_objects_json_body import TestInlineObjectsJsonBody from ...models.test_inline_objects_response_200 import TestInlineObjectsResponse200 @@ -31,20 +32,23 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[TestInlineObjectsResponse200]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[TestInlineObjectsResponse200]: if response.status_code == HTTPStatus.OK: response_200 = TestInlineObjectsResponse200.from_dict(response.json()) return response_200 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[TestInlineObjectsResponse200]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[TestInlineObjectsResponse200]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -58,6 +62,10 @@ def sync_detailed( Args: json_body (TestInlineObjectsJsonBody): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[TestInlineObjectsResponse200] """ @@ -72,7 +80,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -85,6 +93,10 @@ def sync( Args: json_body (TestInlineObjectsJsonBody): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[TestInlineObjectsResponse200] """ @@ -105,6 +117,10 @@ async def asyncio_detailed( Args: json_body (TestInlineObjectsJsonBody): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[TestInlineObjectsResponse200] """ @@ -117,7 +133,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -130,6 +146,10 @@ async def asyncio( Args: json_body (TestInlineObjectsJsonBody): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[TestInlineObjectsResponse200] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py index 38ac8c9b4..85d53d9da 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/token_with_cookie_auth_token_with_cookie_get.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import Response @@ -28,12 +29,23 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if response.status_code == HTTPStatus.UNAUTHORIZED: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -49,6 +61,10 @@ def sync_detailed( Args: my_token (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -63,7 +79,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -78,6 +94,10 @@ async def asyncio_detailed( Args: my_token (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -90,4 +110,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py index 60a26957e..2daec1319 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/unsupported_content_tests_unsupported_content_get.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import Response @@ -25,12 +26,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -40,6 +50,10 @@ def sync_detailed( ) -> Response[Any]: """Unsupported Content + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -53,7 +67,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -62,6 +76,10 @@ async def asyncio_detailed( ) -> Response[Any]: """Unsupported Content + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -73,4 +91,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py index 4a967179b..d00ec5e40 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost from ...models.http_validation_error import HTTPValidationError @@ -31,7 +32,7 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == HTTPStatus.OK: response_200 = cast(Any, response.json()) return response_200 @@ -39,15 +40,18 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPVali response_422 = HTTPValidationError.from_dict(response.json()) return response_422 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -63,6 +67,10 @@ def sync_detailed( Args: multipart_data (BodyUploadFileTestsUploadPost): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -77,7 +85,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -92,6 +100,10 @@ def sync( Args: multipart_data (BodyUploadFileTestsUploadPost): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -114,6 +126,10 @@ async def asyncio_detailed( Args: multipart_data (BodyUploadFileTestsUploadPost): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -126,7 +142,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -141,6 +157,10 @@ async def asyncio( Args: multipart_data (BodyUploadFileTestsUploadPost): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_multiple_files_tests_upload_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_multiple_files_tests_upload_post.py index 58c3cef41..34add0e4e 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_multiple_files_tests_upload_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_multiple_files_tests_upload_post.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.http_validation_error import HTTPValidationError from ...types import File, Response @@ -34,7 +35,7 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: if response.status_code == HTTPStatus.OK: response_200 = cast(Any, response.json()) return response_200 @@ -42,15 +43,18 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[Any, HTTPVali response_422 = HTTPValidationError.from_dict(response.json()) return response_422 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -66,6 +70,10 @@ def sync_detailed( Args: multipart_data (List[File]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -80,7 +88,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -95,6 +103,10 @@ def sync( Args: multipart_data (List[File]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -117,6 +129,10 @@ async def asyncio_detailed( Args: multipart_data (List[File]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ @@ -129,7 +145,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -144,6 +160,10 @@ async def asyncio( Args: multipart_data (List[File]): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[Any, HTTPValidationError]] """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/true_/false_.py b/end_to_end_tests/golden-record/my_test_api_client/api/true_/false_.py index adc27ae4a..f8332a87a 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/true_/false_.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/true_/false_.py @@ -1,8 +1,9 @@ from http import HTTPStatus -from typing import Any, Dict +from typing import Any, Dict, Optional import httpx +from ... import errors from ...client import Client from ...types import UNSET, Response @@ -32,12 +33,21 @@ def _get_kwargs( } -def _build_response(*, response: httpx.Response) -> Response[Any]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: + if response.status_code == HTTPStatus.OK: + return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None + + +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=None, + parsed=_parse_response(client=client, response=response), ) @@ -50,6 +60,10 @@ def sync_detailed( Args: import_ (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -64,7 +78,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio_detailed( @@ -76,6 +90,10 @@ async def asyncio_detailed( Args: import_ (str): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Any] """ @@ -88,4 +106,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/client.py b/end_to_end_tests/golden-record/my_test_api_client/client.py index 1a39694de..4510c09f2 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/client.py +++ b/end_to_end_tests/golden-record/my_test_api_client/client.py @@ -6,13 +6,26 @@ @attr.s(auto_attribs=True) class Client: - """A class for keeping track of data related to the API""" + """A class for keeping track of data related to the API + + Attributes: + base_url: The base URL for the API, all requests are made to a relative path to this URL + cookies: A dictionary of cookies to be sent with every request + headers: A dictionary of headers to be sent with every request + timeout: The maximum amount of a time in seconds a request can take. API functions will raise + httpx.TimeoutException if this is exceeded. + verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production, + but can be set to False for testing purposes. + raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a + status code that was not documented in the source OpenAPI document. + """ base_url: str cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True) headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True) timeout: float = attr.ib(5.0, kw_only=True) verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True) + raise_on_unexpected_status: bool = attr.ib(False, kw_only=True) def get_headers(self) -> Dict[str, str]: """Get headers to be used in all endpoints""" diff --git a/end_to_end_tests/golden-record/my_test_api_client/errors.py b/end_to_end_tests/golden-record/my_test_api_client/errors.py new file mode 100644 index 000000000..a508e1360 --- /dev/null +++ b/end_to_end_tests/golden-record/my_test_api_client/errors.py @@ -0,0 +1,10 @@ +""" Contains shared errors types that can be raised from API functions """ + + +class UnexpectedStatus(Exception): + """Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True""" + + ... + + +__all__ = ["UnexpectedStatus"] diff --git a/integration-tests/integration_tests/api/body/post_body_multipart.py b/integration-tests/integration_tests/api/body/post_body_multipart.py index fd73dcafa..303443af7 100644 --- a/integration-tests/integration_tests/api/body/post_body_multipart.py +++ b/integration-tests/integration_tests/api/body/post_body_multipart.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.post_body_multipart_multipart_data import PostBodyMultipartMultipartData from ...models.post_body_multipart_response_200 import PostBodyMultipartResponse200 @@ -32,7 +33,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[PostBodyMultipartResponse200, PublicError]]: +def _parse_response( + *, client: Client, response: httpx.Response +) -> Optional[Union[PostBodyMultipartResponse200, PublicError]]: if response.status_code == HTTPStatus.OK: response_200 = PostBodyMultipartResponse200.from_dict(response.json()) @@ -41,15 +44,20 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[PostBodyMulti response_400 = PublicError.from_dict(response.json()) return response_400 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[PostBodyMultipartResponse200, PublicError]]: +def _build_response( + *, client: Client, response: httpx.Response +) -> Response[Union[PostBodyMultipartResponse200, PublicError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -62,6 +70,10 @@ def sync_detailed( Args: multipart_data (PostBodyMultipartMultipartData): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostBodyMultipartResponse200, PublicError]] """ @@ -76,7 +88,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -88,6 +100,10 @@ def sync( Args: multipart_data (PostBodyMultipartMultipartData): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostBodyMultipartResponse200, PublicError]] """ @@ -107,6 +123,10 @@ async def asyncio_detailed( Args: multipart_data (PostBodyMultipartMultipartData): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostBodyMultipartResponse200, PublicError]] """ @@ -119,7 +139,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -131,6 +151,10 @@ async def asyncio( Args: multipart_data (PostBodyMultipartMultipartData): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostBodyMultipartResponse200, PublicError]] """ diff --git a/integration-tests/integration_tests/api/parameters/post_parameters_header.py b/integration-tests/integration_tests/api/parameters/post_parameters_header.py index 22879a766..c89c4c304 100644 --- a/integration-tests/integration_tests/api/parameters/post_parameters_header.py +++ b/integration-tests/integration_tests/api/parameters/post_parameters_header.py @@ -3,6 +3,7 @@ import httpx +from ... import errors from ...client import Client from ...models.post_parameters_header_response_200 import PostParametersHeaderResponse200 from ...models.public_error import PublicError @@ -39,7 +40,9 @@ def _get_kwargs( } -def _parse_response(*, response: httpx.Response) -> Optional[Union[PostParametersHeaderResponse200, PublicError]]: +def _parse_response( + *, client: Client, response: httpx.Response +) -> Optional[Union[PostParametersHeaderResponse200, PublicError]]: if response.status_code == HTTPStatus.OK: response_200 = PostParametersHeaderResponse200.from_dict(response.json()) @@ -48,15 +51,20 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[PostParameter response_400 = PublicError.from_dict(response.json()) return response_400 - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[Union[PostParametersHeaderResponse200, PublicError]]: +def _build_response( + *, client: Client, response: httpx.Response +) -> Response[Union[PostParametersHeaderResponse200, PublicError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(response=response), + parsed=_parse_response(client=client, response=response), ) @@ -75,6 +83,10 @@ def sync_detailed( number_header (float): integer_header (int): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostParametersHeaderResponse200, PublicError]] """ @@ -92,7 +104,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) def sync( @@ -110,6 +122,10 @@ def sync( number_header (float): integer_header (int): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostParametersHeaderResponse200, PublicError]] """ @@ -138,6 +154,10 @@ async def asyncio_detailed( number_header (float): integer_header (int): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostParametersHeaderResponse200, PublicError]] """ @@ -153,7 +173,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(response=response) + return _build_response(client=client, response=response) async def asyncio( @@ -171,6 +191,10 @@ async def asyncio( number_header (float): integer_header (int): + Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[Union[PostParametersHeaderResponse200, PublicError]] """ diff --git a/integration-tests/integration_tests/client.py b/integration-tests/integration_tests/client.py index 1a39694de..4510c09f2 100644 --- a/integration-tests/integration_tests/client.py +++ b/integration-tests/integration_tests/client.py @@ -6,13 +6,26 @@ @attr.s(auto_attribs=True) class Client: - """A class for keeping track of data related to the API""" + """A class for keeping track of data related to the API + + Attributes: + base_url: The base URL for the API, all requests are made to a relative path to this URL + cookies: A dictionary of cookies to be sent with every request + headers: A dictionary of headers to be sent with every request + timeout: The maximum amount of a time in seconds a request can take. API functions will raise + httpx.TimeoutException if this is exceeded. + verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production, + but can be set to False for testing purposes. + raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a + status code that was not documented in the source OpenAPI document. + """ base_url: str cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True) headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True) timeout: float = attr.ib(5.0, kw_only=True) verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True) + raise_on_unexpected_status: bool = attr.ib(False, kw_only=True) def get_headers(self) -> Dict[str, str]: """Get headers to be used in all endpoints""" diff --git a/integration-tests/integration_tests/errors.py b/integration-tests/integration_tests/errors.py new file mode 100644 index 000000000..a508e1360 --- /dev/null +++ b/integration-tests/integration_tests/errors.py @@ -0,0 +1,10 @@ +""" Contains shared errors types that can be raised from API functions """ + + +class UnexpectedStatus(Exception): + """Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True""" + + ... + + +__all__ = ["UnexpectedStatus"] diff --git a/openapi_python_client/__init__.py b/openapi_python_client/__init__.py index 3eeaa382c..ff0a132ca 100644 --- a/openapi_python_client/__init__.py +++ b/openapi_python_client/__init__.py @@ -259,12 +259,18 @@ def _build_models(self) -> None: models_init_template = self.env.get_template("models_init.py.jinja") models_init.write_text(models_init_template.render(imports=imports, alls=alls), encoding=self.file_encoding) + # pylint: disable=too-many-locals def _build_api(self) -> None: # Generate Client client_path = self.package_dir / "client.py" client_template = self.env.get_template("client.py.jinja") client_path.write_text(client_template.render(), encoding=self.file_encoding) + # Generate included Errors + errors_path = self.package_dir / "errors.py" + errors_template = self.env.get_template("errors.py.jinja") + errors_path.write_text(errors_template.render(), encoding=self.file_encoding) + # Generate endpoints api_dir = self.package_dir / "api" api_dir.mkdir() diff --git a/openapi_python_client/templates/README.md.jinja b/openapi_python_client/templates/README.md.jinja index f17983d6a..1d50c8d2a 100644 --- a/openapi_python_client/templates/README.md.jinja +++ b/openapi_python_client/templates/README.md.jinja @@ -61,6 +61,8 @@ client = AuthenticatedClient( ) ``` +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. + Things to know: 1. Every path/method combo becomes a Python module with four functions: 1. `sync`: Blocking request that returns parsed data (if successful) or `None` diff --git a/openapi_python_client/templates/client.py.jinja b/openapi_python_client/templates/client.py.jinja index 418ea17b0..be0a0479a 100644 --- a/openapi_python_client/templates/client.py.jinja +++ b/openapi_python_client/templates/client.py.jinja @@ -4,13 +4,26 @@ import attr @attr.s(auto_attribs=True) class Client: - """ A class for keeping track of data related to the API """ + """ A class for keeping track of data related to the API + + Attributes: + base_url: The base URL for the API, all requests are made to a relative path to this URL + cookies: A dictionary of cookies to be sent with every request + headers: A dictionary of headers to be sent with every request + timeout: The maximum amount of a time in seconds a request can take. API functions will raise + httpx.TimeoutException if this is exceeded. + verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production, + but can be set to False for testing purposes. + raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a + status code that was not documented in the source OpenAPI document. + """ base_url: str cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True) headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True) timeout: float = attr.ib(5.0, kw_only=True) verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True) + raise_on_unexpected_status: bool = attr.ib(False, kw_only=True) def get_headers(self) -> Dict[str, str]: """ Get headers to be used in all endpoints """ diff --git a/openapi_python_client/templates/endpoint_macros.py.jinja b/openapi_python_client/templates/endpoint_macros.py.jinja index 6eb4be11c..4dc0575f9 100644 --- a/openapi_python_client/templates/endpoint_macros.py.jinja +++ b/openapi_python_client/templates/endpoint_macros.py.jinja @@ -159,6 +159,10 @@ Args: {% endfor %} {% endif %} +Raises: + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.TimeoutException: If the request takes longer than Client.timeout. + Returns: Response[{{ return_string }}] """ diff --git a/openapi_python_client/templates/endpoint_module.py.jinja b/openapi_python_client/templates/endpoint_module.py.jinja index 89f6d8345..26d313f16 100644 --- a/openapi_python_client/templates/endpoint_module.py.jinja +++ b/openapi_python_client/templates/endpoint_module.py.jinja @@ -5,6 +5,7 @@ import httpx from ...client import AuthenticatedClient, Client from ...types import Response, UNSET +from ... import errors {% for relative in endpoint.relative_imports %} {{ relative }} @@ -58,32 +59,32 @@ def _get_kwargs( } -{% if parsed_responses %} -def _parse_response(*, response: httpx.Response) -> Optional[{{ return_string }}]: +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[{{ return_string }}]: {% for response in endpoint.responses %} if response.status_code == HTTPStatus.{{ response.status_code.name }}: - {% import "property_templates/" + response.prop.template as prop_template %} + {% if parsed_responses %}{% import "property_templates/" + response.prop.template as prop_template %} {% if prop_template.construct %} {{ prop_template.construct(response.prop, response.source) | indent(8) }} {% else %} {{ response.prop.python_name }} = cast({{ response.prop.get_type_string() }}, {{ response.source }}) {% endif %} return {{ response.prop.python_name }} + {% else %} + return None + {% endif %} {% endfor %} - return None -{% endif %} + if client.raise_on_unexpected_status: + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + else: + return None -def _build_response(*, response: httpx.Response) -> Response[{{ return_string }}]: +def _build_response(*, client: Client, response: httpx.Response) -> Response[{{ return_string }}]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - {% if parsed_responses %} - parsed=_parse_response(response=response), - {% else %} - parsed=None, - {% endif %} + parsed=_parse_response(client=client, response=response), ) @@ -101,7 +102,7 @@ def sync_detailed( **kwargs, ) - return _build_response(response=response) + return _build_response(client=client, response=response) {% if parsed_responses %} def sync( @@ -128,7 +129,7 @@ async def asyncio_detailed( **kwargs ) - return _build_response(response=response) + return _build_response(client=client, response=response) {% if parsed_responses %} async def asyncio( diff --git a/openapi_python_client/templates/errors.py.jinja b/openapi_python_client/templates/errors.py.jinja new file mode 100644 index 000000000..7445a2dad --- /dev/null +++ b/openapi_python_client/templates/errors.py.jinja @@ -0,0 +1,7 @@ +""" Contains shared errors types that can be raised from API functions """ + +class UnexpectedStatus(Exception): + """ Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True """ + ... + +__all__ = ["UnexpectedStatus"]