diff --git a/README.md b/README.md index cea385093..acd33d674 100644 --- a/README.md +++ b/README.md @@ -170,3 +170,24 @@ By default, the timeout for retrieving the schema file via HTTP is 5 seconds. In [changelog.md]: CHANGELOG.md [poetry]: https://python-poetry.org/ + +### raise_on_error_status + +If true, generated endpoint functions raise exceptions on error response codes (status code >= 400). + +If false (the default), the parsed response content is returned in the same way both for success and error responses. + +Enabling this option also removes the error response types from the return type annotations. + +For now, the raised exceptions do not include the parsed response content even if the response code has a schema in the OpenAPI spec. + +### raise_on_unexpected_status + +Enable this to raise exceptions on undocumented response codes in all generated API call functions. + +By default, undocumented response content is parsed as `None`. + +Enabling this option also removes `None` from the return type annotations. + +This option has identical behavior to setting `Client.raise_on_unexpected_status`, but setting it at +client generation time results in simpler generated code and correct type annotations. diff --git a/end_to_end_tests/config.yml b/end_to_end_tests/config.yml index 05ac674fc..4f955eb43 100644 --- a/end_to_end_tests/config.yml +++ b/end_to_end_tests/config.yml @@ -9,3 +9,5 @@ class_overrides: class_name: AnEnumValue module_name: an_enum_value field_prefix: attr_ +raise_on_error_status: True +raise_on_unexpected_status: True 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 a2478b80d..dfca6da87 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -34,21 +34,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -56,17 +54,18 @@ def sync_detailed( *, client: Client, common: Union[Unset, None, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -79,24 +78,25 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( *, client: Client, common: Union[Unset, None, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -107,4 +107,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 99e7f21d7..11efb3788 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -34,21 +34,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -56,17 +54,18 @@ def sync_detailed( *, client: Client, common: Union[Unset, None, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -79,24 +78,25 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( *, client: Client, common: Union[Unset, None, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -107,4 +107,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 9249eab31..04ac85237 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -53,21 +53,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -80,7 +78,7 @@ def sync_detailed( integer_header: Union[Unset, int] = UNSET, int_enum_header: Union[Unset, GetLocationHeaderTypesIntEnumHeader] = UNSET, string_enum_header: Union[Unset, GetLocationHeaderTypesStringEnumHeader] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: boolean_header (Union[Unset, bool]): @@ -91,11 +89,12 @@ def sync_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -113,7 +112,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( @@ -125,7 +124,7 @@ async def asyncio_detailed( integer_header: Union[Unset, int] = UNSET, int_enum_header: Union[Unset, GetLocationHeaderTypesIntEnumHeader] = UNSET, string_enum_header: Union[Unset, GetLocationHeaderTypesStringEnumHeader] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: boolean_header (Union[Unset, bool]): @@ -136,11 +135,12 @@ async def asyncio_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -156,4 +156,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 0209c7319..0a98cba13 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,6 +1,6 @@ import datetime from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -58,21 +58,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -83,7 +81,7 @@ def sync_detailed( null_required: Union[Unset, None, datetime.datetime] = UNSET, null_not_required: Union[Unset, None, datetime.datetime] = UNSET, not_null_not_required: Union[Unset, None, datetime.datetime] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: not_null_required (datetime.datetime): @@ -92,11 +90,12 @@ def sync_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -112,7 +111,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( @@ -122,7 +121,7 @@ async def asyncio_detailed( null_required: Union[Unset, None, datetime.datetime] = UNSET, null_not_required: Union[Unset, None, datetime.datetime] = UNSET, not_null_not_required: Union[Unset, None, datetime.datetime] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: not_null_required (datetime.datetime): @@ -131,11 +130,12 @@ async def asyncio_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -149,4 +149,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 b8c33ec94..caee149e4 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -46,21 +46,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -72,7 +70,7 @@ def sync_detailed( integer_param: Union[Unset, None, int] = 0, header_param: Union[Unset, str] = UNSET, cookie_param: Union[Unset, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """Test different types of parameter references Args: @@ -83,11 +81,12 @@ def sync_detailed( cookie_param (Union[Unset, str]): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -104,7 +103,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( @@ -115,7 +114,7 @@ async def asyncio_detailed( integer_param: Union[Unset, None, int] = 0, header_param: Union[Unset, str] = UNSET, cookie_param: Union[Unset, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """Test different types of parameter references Args: @@ -126,11 +125,12 @@ async def asyncio_detailed( cookie_param (Union[Unset, str]): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -145,4 +145,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 2816ec304..8bd6490f4 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -35,21 +35,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -58,18 +56,19 @@ def sync_detailed( *, client: Client, param_query: Union[Unset, None, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -83,7 +82,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( @@ -91,18 +90,19 @@ async def asyncio_detailed( *, client: Client, param_query: Union[Unset, None, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -114,4 +114,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 f1d97abc5..d31104734 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -35,21 +35,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -58,7 +56,7 @@ def sync_detailed( *, client: Client, param_query: str = "overridden_in_GET", -) -> Response[Any]: +) -> Response[None]: """Test that if you have an overriding property from `PathItem` in `Operation`, it produces valid code Args: @@ -67,11 +65,12 @@ def sync_detailed( '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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -85,7 +84,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( @@ -93,7 +92,7 @@ async def asyncio_detailed( *, client: Client, param_query: str = "overridden_in_GET", -) -> Response[Any]: +) -> Response[None]: """Test that if you have an overriding property from `PathItem` in `Operation`, it produces valid code Args: @@ -102,11 +101,12 @@ async def asyncio_detailed( '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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -118,4 +118,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 ee9c35016..2272d306a 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -43,21 +43,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -68,7 +66,7 @@ def sync_detailed( param_query: Union[Unset, None, str] = UNSET, param_header: Union[Unset, str] = UNSET, param_cookie: Union[Unset, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: param_path (str): @@ -77,11 +75,12 @@ def sync_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -97,7 +96,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( @@ -107,7 +106,7 @@ async def asyncio_detailed( param_query: Union[Unset, None, str] = UNSET, param_header: Union[Unset, str] = UNSET, param_cookie: Union[Unset, str] = UNSET, -) -> Response[Any]: +) -> Response[None]: """ Args: param_path (str): @@ -116,11 +115,12 @@ async def asyncio_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -134,4 +134,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 347aa36b4..790dfa718 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -33,21 +33,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -58,7 +56,7 @@ def sync_detailed( param3: int, *, client: Client, -) -> Response[Any]: +) -> Response[None]: """ Args: param4 (str): @@ -67,11 +65,12 @@ def sync_detailed( param3 (int): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -87,7 +86,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( @@ -97,7 +96,7 @@ async def asyncio_detailed( param3: int, *, client: Client, -) -> Response[Any]: +) -> Response[None]: """ Args: param4 (str): @@ -106,11 +105,12 @@ async def asyncio_detailed( param3 (int): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -124,4 +124,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 b092e529d..6c11497b4 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 @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -30,27 +30,21 @@ def _get_kwargs( } -def _parse_response( - *, client: Client, response: httpx.Response -) -> Optional[PostResponsesUnionsSimpleBeforeComplexResponse200]: +def _parse_response(*, response: httpx.Response) -> PostResponsesUnionsSimpleBeforeComplexResponse200: if response.status_code == HTTPStatus.OK: response_200 = PostResponsesUnionsSimpleBeforeComplexResponse200.from_dict(response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response( - *, client: Client, response: httpx.Response -) -> Response[PostResponsesUnionsSimpleBeforeComplexResponse200]: +def _build_response(*, response: httpx.Response) -> Response[PostResponsesUnionsSimpleBeforeComplexResponse200]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -61,7 +55,8 @@ def sync_detailed( """Regression test for #603 Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -77,17 +72,18 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, -) -> Optional[PostResponsesUnionsSimpleBeforeComplexResponse200]: +) -> 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -106,7 +102,8 @@ async def asyncio_detailed( """Regression test for #603 Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -120,17 +117,18 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, -) -> Optional[PostResponsesUnionsSimpleBeforeComplexResponse200]: +) -> 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: 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 05f976082..3cd98a79a 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -27,35 +27,34 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) def sync_detailed( *, client: Client, -) -> Response[Any]: +) -> Response[None]: """ Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -67,20 +66,21 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( *, client: Client, -) -> Response[Any]: +) -> Response[None]: """ Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -90,4 +90,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 34a238b2d..f2bbb4410 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 @@ -1,12 +1,11 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union, cast +from typing import Any, Dict import httpx from ... import errors from ...client import Client from ...models.a_model import AModel -from ...models.http_validation_error import HTTPValidationError from ...types import Response @@ -33,26 +32,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - response_200 = cast(Any, response.json()) + response_200 = response.json() return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) - return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None - -def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -60,7 +53,7 @@ def sync_detailed( *, client: Client, json_body: AModel, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Path with callback Try sending a request related to a callback @@ -69,11 +62,12 @@ def sync_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -86,14 +80,14 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, json_body: AModel, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Path with callback Try sending a request related to a callback @@ -102,11 +96,12 @@ def sync( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return sync_detailed( @@ -119,7 +114,7 @@ async def asyncio_detailed( *, client: Client, json_body: AModel, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Path with callback Try sending a request related to a callback @@ -128,11 +123,12 @@ async def asyncio_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -143,14 +139,14 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, json_body: AModel, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Path with callback Try sending a request related to a callback @@ -159,11 +155,12 @@ async def asyncio( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return ( 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 414542116..f9f5f2142 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 @@ -1,6 +1,6 @@ import datetime from http import HTTPStatus -from typing import Any, Dict, List, Optional, Union, cast +from typing import Any, Dict, List, Union import httpx from dateutil.parser import isoparse @@ -8,7 +8,6 @@ from ... import errors from ...client import Client from ...models.an_enum import AnEnum -from ...models.http_validation_error import HTTPValidationError from ...models.model_with_union_property import ModelWithUnionProperty from ...types import UNSET, Response, Unset @@ -100,26 +99,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - response_200 = cast(Any, response.json()) + response_200 = response.json() return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) - - return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -137,7 +130,7 @@ def sync_detailed( enum_prop: AnEnum, model_prop: "ModelWithUnionProperty", required_model_prop: "ModelWithUnionProperty", -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Defaults Args: @@ -154,11 +147,12 @@ def sync_detailed( required_model_prop (ModelWithUnionProperty): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -181,7 +175,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( @@ -198,7 +192,7 @@ def sync( enum_prop: AnEnum, model_prop: "ModelWithUnionProperty", required_model_prop: "ModelWithUnionProperty", -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Defaults Args: @@ -215,11 +209,12 @@ def sync( required_model_prop (ModelWithUnionProperty): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return sync_detailed( @@ -252,7 +247,7 @@ async def asyncio_detailed( enum_prop: AnEnum, model_prop: "ModelWithUnionProperty", required_model_prop: "ModelWithUnionProperty", -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Defaults Args: @@ -269,11 +264,12 @@ async def asyncio_detailed( required_model_prop (ModelWithUnionProperty): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -294,7 +290,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( @@ -311,7 +307,7 @@ async def asyncio( enum_prop: AnEnum, model_prop: "ModelWithUnionProperty", required_model_prop: "ModelWithUnionProperty", -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Defaults Args: @@ -328,11 +324,12 @@ async def asyncio( required_model_prop (ModelWithUnionProperty): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return ( diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/description_with_backslash.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/description_with_backslash.py index 31923ad11..5bbfa1292 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/description_with_backslash.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/description_with_backslash.py @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -27,38 +27,37 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) def sync_detailed( *, client: Client, -) -> Response[Any]: +) -> Response[None]: r""" Test description with \ Test description with \ Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -70,23 +69,24 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( *, client: Client, -) -> Response[Any]: +) -> Response[None]: r""" Test description with \ Test description with \ Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -96,4 +96,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) 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 266e5eb41..178aa99aa 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 @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, cast import httpx @@ -27,23 +27,21 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[bool]]: +def _parse_response(*, response: httpx.Response) -> List[bool]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[bool], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[List[bool]]: +def _build_response(*, response: httpx.Response) -> Response[List[bool]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -56,7 +54,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -72,19 +71,20 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, -) -> Optional[List[bool]]: +) -> List[bool]: """Get Basic List Of Booleans 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -105,7 +105,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -119,19 +120,20 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, -) -> Optional[List[bool]]: +) -> List[bool]: """Get Basic List Of Booleans 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: 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 a1101a547..2028cbcab 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 @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, cast import httpx @@ -27,23 +27,21 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[float]]: +def _parse_response(*, response: httpx.Response) -> List[float]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[float], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[List[float]]: +def _build_response(*, response: httpx.Response) -> Response[List[float]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -56,7 +54,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -72,19 +71,20 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, -) -> Optional[List[float]]: +) -> List[float]: """Get Basic List Of Floats 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -105,7 +105,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -119,19 +120,20 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, -) -> Optional[List[float]]: +) -> List[float]: """Get Basic List Of Floats 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: 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 29627228c..dfd823ff2 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 @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, cast import httpx @@ -27,23 +27,21 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[int]]: +def _parse_response(*, response: httpx.Response) -> List[int]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[int], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[List[int]]: +def _build_response(*, response: httpx.Response) -> Response[List[int]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -56,7 +54,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -72,19 +71,20 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, -) -> Optional[List[int]]: +) -> List[int]: """Get Basic List Of Integers 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -105,7 +105,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -119,19 +120,20 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, -) -> Optional[List[int]]: +) -> List[int]: """Get Basic List Of Integers 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: 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 2c52de82b..4121b6cf9 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 @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, cast import httpx @@ -27,23 +27,21 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[List[str]]: +def _parse_response(*, response: httpx.Response) -> List[str]: if response.status_code == HTTPStatus.OK: response_200 = cast(List[str], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[List[str]]: +def _build_response(*, response: httpx.Response) -> Response[List[str]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -56,7 +54,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -72,19 +71,20 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, -) -> Optional[List[str]]: +) -> List[str]: """Get Basic List Of Strings 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -105,7 +105,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -119,19 +120,20 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, -) -> Optional[List[str]]: +) -> List[str]: """Get Basic List Of Strings 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: 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 cef38f2ff..72a774f6f 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 @@ -9,7 +9,6 @@ from ...models.a_model import AModel from ...models.an_enum import AnEnum from ...models.an_enum_with_null import AnEnumWithNull -from ...models.http_validation_error import HTTPValidationError from ...types import UNSET, Response @@ -71,9 +70,7 @@ def _get_kwargs( } -def _parse_response( - *, client: Client, response: httpx.Response -) -> Optional[Union[HTTPValidationError, List["AModel"]]]: +def _parse_response(*, response: httpx.Response) -> List["AModel"]: if response.status_code == HTTPStatus.OK: response_200 = [] _response_200 = response.json() @@ -83,28 +80,16 @@ def _parse_response( response_200.append(response_200_item) return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) - return response_422 - if response.status_code == HTTPStatus.LOCKED: - response_423 = HTTPValidationError.from_dict(response.json()) - return response_423 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None - - -def _build_response( - *, client: Client, response: httpx.Response -) -> Response[Union[HTTPValidationError, List["AModel"]]]: +def _build_response(*, response: httpx.Response) -> Response[List["AModel"]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -115,7 +100,7 @@ def sync_detailed( an_enum_value_with_null: List[Optional[AnEnumWithNull]], an_enum_value_with_only_null: List[None], some_date: Union[datetime.date, datetime.datetime], -) -> Response[Union[HTTPValidationError, List["AModel"]]]: +) -> Response[List["AModel"]]: """Get List Get a list of things @@ -127,11 +112,12 @@ def sync_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[HTTPValidationError, List['AModel']]] + Response[List['AModel']] """ kwargs = _get_kwargs( @@ -147,7 +133,7 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( @@ -157,7 +143,7 @@ def sync( an_enum_value_with_null: List[Optional[AnEnumWithNull]], an_enum_value_with_only_null: List[None], some_date: Union[datetime.date, datetime.datetime], -) -> Optional[Union[HTTPValidationError, List["AModel"]]]: +) -> List["AModel"]: """Get List Get a list of things @@ -169,11 +155,12 @@ def sync( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[HTTPValidationError, List['AModel']] + List['AModel'] """ return sync_detailed( @@ -192,7 +179,7 @@ async def asyncio_detailed( an_enum_value_with_null: List[Optional[AnEnumWithNull]], an_enum_value_with_only_null: List[None], some_date: Union[datetime.date, datetime.datetime], -) -> Response[Union[HTTPValidationError, List["AModel"]]]: +) -> Response[List["AModel"]]: """Get List Get a list of things @@ -204,11 +191,12 @@ async def asyncio_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[HTTPValidationError, List['AModel']]] + Response[List['AModel']] """ kwargs = _get_kwargs( @@ -222,7 +210,7 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( @@ -232,7 +220,7 @@ async def asyncio( an_enum_value_with_null: List[Optional[AnEnumWithNull]], an_enum_value_with_only_null: List[None], some_date: Union[datetime.date, datetime.datetime], -) -> Optional[Union[HTTPValidationError, List["AModel"]]]: +) -> List["AModel"]: """Get List Get a list of things @@ -244,11 +232,12 @@ async def asyncio( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[HTTPValidationError, List['AModel']] + List['AModel'] """ return ( 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 5a3ee4c7b..2979843a4 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 @@ -1,12 +1,11 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union, cast +from typing import Any, Dict import httpx from ... import errors from ...client import Client from ...models.an_int_enum import AnIntEnum -from ...models.http_validation_error import HTTPValidationError from ...types import UNSET, Response @@ -38,26 +37,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - response_200 = cast(Any, response.json()) + response_200 = response.json() return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) - return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None - -def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -65,18 +58,19 @@ def sync_detailed( *, client: Client, int_enum: AnIntEnum, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Int Enum 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -89,25 +83,26 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, int_enum: AnIntEnum, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Int Enum 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return sync_detailed( @@ -120,18 +115,19 @@ async def asyncio_detailed( *, client: Client, int_enum: AnIntEnum, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Int Enum 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -142,25 +138,26 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, int_enum: AnIntEnum, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Int Enum 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return ( 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 d482d0e3c..59334a32e 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 @@ -1,12 +1,11 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union, cast +from typing import Any, Dict import httpx from ... import errors from ...client import Client from ...models.a_model import AModel -from ...models.http_validation_error import HTTPValidationError from ...types import Response @@ -33,26 +32,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - response_200 = cast(Any, response.json()) + response_200 = response.json() return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) - return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None - -def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -60,7 +53,7 @@ def sync_detailed( *, client: Client, json_body: AModel, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Json Body Try sending a JSON body @@ -69,11 +62,12 @@ def sync_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -86,14 +80,14 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, json_body: AModel, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Json Body Try sending a JSON body @@ -102,11 +96,12 @@ def sync( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return sync_detailed( @@ -119,7 +114,7 @@ async def asyncio_detailed( *, client: Client, json_body: AModel, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Json Body Try sending a JSON body @@ -128,11 +123,12 @@ async def asyncio_detailed( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -143,14 +139,14 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, json_body: AModel, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Json Body Try sending a JSON body @@ -159,11 +155,12 @@ async def asyncio( 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return ( 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 075452512..41d0fc668 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -27,21 +27,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response_200 = response.json() + return response_200 + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -52,7 +51,8 @@ def sync_detailed( """No Response Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -68,7 +68,27 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +def sync( + *, + client: Client, +) -> Any: + """No Response + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return sync_detailed( + client=client, + ).parsed async def asyncio_detailed( @@ -78,7 +98,8 @@ async def asyncio_detailed( """No Response Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -92,4 +113,26 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +async def asyncio( + *, + client: Client, +) -> Any: + """No Response + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return ( + await asyncio_detailed( + client=client, + ) + ).parsed 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 68e0a7fd5..bba77887e 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 @@ -1,6 +1,6 @@ from http import HTTPStatus from io import BytesIO -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -28,23 +28,21 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[File]: +def _parse_response(*, response: httpx.Response) -> File: if response.status_code == HTTPStatus.OK: response_200 = File(payload=BytesIO(response.content)) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[File]: +def _build_response(*, response: httpx.Response) -> Response[File]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -55,7 +53,8 @@ def sync_detailed( """Octet Stream Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -71,17 +70,18 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, -) -> Optional[File]: +) -> File: """Octet Stream Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -100,7 +100,8 @@ async def asyncio_detailed( """Octet Stream Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -114,17 +115,18 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, -) -> Optional[File]: +) -> File: """Octet Stream Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: 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 07c0b225e..ced8ff833 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -30,21 +30,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response_200 = response.json() + return response_200 + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -58,7 +57,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -75,7 +75,31 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +def sync( + *, + client: Client, + form_data: AFormData, +) -> Any: + """Post form data + + Post form data + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return sync_detailed( + client=client, + form_data=form_data, + ).parsed async def asyncio_detailed( @@ -88,7 +112,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -103,4 +128,30 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +async def asyncio( + *, + client: Client, + form_data: AFormData, +) -> Any: + """Post form data + + Post form data + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return ( + await asyncio_detailed( + client=client, + form_data=form_data, + ) + ).parsed 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 e93a22986..8fef3f3b7 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -30,21 +30,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response_200 = response.json() + return response_200 + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -58,7 +57,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -75,7 +75,31 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +def sync( + *, + client: Client, + form_data: PostFormDataInlineData, +) -> Any: + """Post form data (inline schema) + + Post form data (inline schema) + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return sync_detailed( + client=client, + form_data=form_data, + ).parsed async def asyncio_detailed( @@ -88,7 +112,8 @@ 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.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -103,4 +128,30 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +async def asyncio( + *, + client: Client, + form_data: PostFormDataInlineData, +) -> Any: + """Post form data (inline schema) + + Post form data (inline schema) + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return ( + await asyncio_detailed( + client=client, + form_data=form_data, + ) + ).parsed 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 289eac03c..f00b6ac31 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 @@ -1,11 +1,10 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union, cast +from typing import Any, Dict, cast import httpx from ... import errors from ...client import Client -from ...models.http_validation_error import HTTPValidationError from ...types import Response @@ -32,26 +31,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[HTTPValidationError, str]]: +def _parse_response(*, response: httpx.Response) -> str: if response.status_code == HTTPStatus.OK: response_200 = cast(str, response.json()) return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) - return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None - -def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]: +def _build_response(*, response: httpx.Response) -> Response[str]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -59,18 +52,19 @@ def sync_detailed( *, client: Client, json_body: str, -) -> Response[Union[HTTPValidationError, str]]: +) -> Response[str]: """Json Body Which is String Args: json_body (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[HTTPValidationError, str]] + Response[str] """ kwargs = _get_kwargs( @@ -83,25 +77,26 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, json_body: str, -) -> Optional[Union[HTTPValidationError, str]]: +) -> str: """Json Body Which is String Args: json_body (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[HTTPValidationError, str] + str """ return sync_detailed( @@ -114,18 +109,19 @@ async def asyncio_detailed( *, client: Client, json_body: str, -) -> Response[Union[HTTPValidationError, str]]: +) -> Response[str]: """Json Body Which is String Args: json_body (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[HTTPValidationError, str]] + Response[str] """ kwargs = _get_kwargs( @@ -136,25 +132,26 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, json_body: str, -) -> Optional[Union[HTTPValidationError, str]]: +) -> str: """Json Body Which is String Args: json_body (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[HTTPValidationError, str] + str """ return ( 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 f7ce0eda3..aab0d9d57 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 @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -33,23 +33,21 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[TestInlineObjectsResponse200]: +def _parse_response(*, response: httpx.Response) -> TestInlineObjectsResponse200: if response.status_code == HTTPStatus.OK: response_200 = TestInlineObjectsResponse200.from_dict(response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[TestInlineObjectsResponse200]: +def _build_response(*, response: httpx.Response) -> Response[TestInlineObjectsResponse200]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -64,7 +62,8 @@ def sync_detailed( json_body (TestInlineObjectsJsonBody): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -81,21 +80,22 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, json_body: TestInlineObjectsJsonBody, -) -> Optional[TestInlineObjectsResponse200]: +) -> TestInlineObjectsResponse200: """Test Inline Objects Args: json_body (TestInlineObjectsJsonBody): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -119,7 +119,8 @@ async def asyncio_detailed( json_body (TestInlineObjectsJsonBody): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -134,21 +135,22 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, json_body: TestInlineObjectsJsonBody, -) -> Optional[TestInlineObjectsResponse200]: +) -> TestInlineObjectsResponse200: """Test Inline Objects Args: json_body (TestInlineObjectsJsonBody): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: 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 994f1ee4e..6354f7ff8 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -30,23 +30,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> 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(response.status_code, response.content) - else: - return None + response_200 = response.json() + return response_200 + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -63,7 +60,8 @@ def sync_detailed( my_token (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -80,7 +78,34 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +def sync( + *, + client: Client, + my_token: str, +) -> Any: + """TOKEN_WITH_COOKIE + + Test optional cookie parameters + + Args: + my_token (str): + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return sync_detailed( + client=client, + my_token=my_token, + ).parsed async def asyncio_detailed( @@ -96,7 +121,8 @@ async def asyncio_detailed( my_token (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -111,4 +137,33 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +async def asyncio( + *, + client: Client, + my_token: str, +) -> Any: + """TOKEN_WITH_COOKIE + + Test optional cookie parameters + + Args: + my_token (str): + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return ( + await asyncio_detailed( + client=client, + my_token=my_token, + ) + ).parsed 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 6b719cb38..5beb0ff5f 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -27,21 +27,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response_200 = response.json() + return response_200 + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -52,7 +51,8 @@ def sync_detailed( """Unsupported Content Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -68,7 +68,27 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +def sync( + *, + client: Client, +) -> Any: + """Unsupported Content + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return sync_detailed( + client=client, + ).parsed async def asyncio_detailed( @@ -78,7 +98,8 @@ async def asyncio_detailed( """Unsupported Content Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -92,4 +113,26 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) + + +async def asyncio( + *, + client: Client, +) -> Any: + """Unsupported Content + + Raises: + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. + httpx.TimeoutException: If the request takes longer than Client.timeout. + + Returns: + Any + """ + + return ( + await asyncio_detailed( + client=client, + ) + ).parsed 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 61f28a42e..5e4b82027 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 @@ -1,12 +1,11 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union, cast +from typing import Any, Dict 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 from ...types import Response @@ -33,26 +32,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - response_200 = cast(Any, response.json()) + response_200 = response.json() return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) - return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None - -def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -60,7 +53,7 @@ def sync_detailed( *, client: Client, multipart_data: BodyUploadFileTestsUploadPost, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Upload File Upload a file @@ -69,11 +62,12 @@ def sync_detailed( multipart_data (BodyUploadFileTestsUploadPost): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -86,14 +80,14 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, multipart_data: BodyUploadFileTestsUploadPost, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Upload File Upload a file @@ -102,11 +96,12 @@ def sync( multipart_data (BodyUploadFileTestsUploadPost): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return sync_detailed( @@ -119,7 +114,7 @@ async def asyncio_detailed( *, client: Client, multipart_data: BodyUploadFileTestsUploadPost, -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Upload File Upload a file @@ -128,11 +123,12 @@ async def asyncio_detailed( multipart_data (BodyUploadFileTestsUploadPost): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -143,14 +139,14 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, multipart_data: BodyUploadFileTestsUploadPost, -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Upload File Upload a file @@ -159,11 +155,12 @@ async def asyncio( multipart_data (BodyUploadFileTestsUploadPost): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return ( 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 58c459a1d..9d0fe4f3a 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 @@ -1,11 +1,10 @@ from http import HTTPStatus -from typing import Any, Dict, List, Optional, Union, cast +from typing import Any, Dict, List import httpx from ... import errors from ...client import Client -from ...models.http_validation_error import HTTPValidationError from ...types import File, Response @@ -36,26 +35,20 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Any, HTTPValidationError]]: +def _parse_response(*, response: httpx.Response) -> Any: if response.status_code == HTTPStatus.OK: - response_200 = cast(Any, response.json()) + response_200 = response.json() return response_200 - if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: - response_422 = HTTPValidationError.from_dict(response.json()) + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) - return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None - -def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: +def _build_response(*, response: httpx.Response) -> Response[Any]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), ) @@ -63,7 +56,7 @@ def sync_detailed( *, client: Client, multipart_data: List[File], -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Upload multiple files Upload several files in the same request @@ -72,11 +65,12 @@ def sync_detailed( multipart_data (List[File]): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -89,14 +83,14 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) def sync( *, client: Client, multipart_data: List[File], -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Upload multiple files Upload several files in the same request @@ -105,11 +99,12 @@ def sync( multipart_data (List[File]): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return sync_detailed( @@ -122,7 +117,7 @@ async def asyncio_detailed( *, client: Client, multipart_data: List[File], -) -> Response[Union[Any, HTTPValidationError]]: +) -> Response[Any]: """Upload multiple files Upload several files in the same request @@ -131,11 +126,12 @@ async def asyncio_detailed( multipart_data (List[File]): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[Any, HTTPValidationError]] + Response[Any] """ kwargs = _get_kwargs( @@ -146,14 +142,14 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio( *, client: Client, multipart_data: List[File], -) -> Optional[Union[Any, HTTPValidationError]]: +) -> Any: """Upload multiple files Upload several files in the same request @@ -162,11 +158,12 @@ async def asyncio( multipart_data (List[File]): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[Any, HTTPValidationError] + Any """ return ( 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 05967d439..dec0a0814 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,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional +from typing import Any, Dict import httpx @@ -34,21 +34,19 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: +def _parse_response(*, response: httpx.Response) -> None: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(response.status_code, response.content) - else: - return None + response.raise_for_status() + raise errors.UnexpectedStatus(response.status_code, response.content) -def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: +def _build_response(*, response: httpx.Response) -> Response[None]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + parsed=_parse_response(response=response), # type: ignore[func-returns-value] ) @@ -56,17 +54,18 @@ def sync_detailed( *, client: Client, import_: str, -) -> Response[Any]: +) -> Response[None]: """ Args: import_ (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -79,24 +78,25 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response(response=response) async def asyncio_detailed( *, client: Client, import_: str, -) -> Response[Any]: +) -> Response[None]: """ Args: import_ (str): Raises: - errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. + httpx.HTTPStatusError: If the server returns an error status code. + errors.UnexpectedStatus: If the server returns an undocumented status code. httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Any] + Response[None] """ kwargs = _get_kwargs( @@ -107,4 +107,4 @@ async def asyncio_detailed( async with httpx.AsyncClient(verify=client.verify_ssl) as _client: response = await _client.request(**kwargs) - return _build_response(client=client, response=response) + return _build_response(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 2f45c655b..4052906a1 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 @@ -18,6 +18,7 @@ class Client: 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. + Setting this has no effect, because it was already set in the client generation config. follow_redirects: Whether or not to follow redirects. Default value is False. """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/types.py b/end_to_end_tests/golden-record/my_test_api_client/types.py index 230efea92..2e9f7b08c 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/types.py +++ b/end_to_end_tests/golden-record/my_test_api_client/types.py @@ -38,7 +38,7 @@ class Response(Generic[T]): status_code: HTTPStatus content: bytes headers: MutableMapping[str, str] - parsed: Optional[T] + parsed: T __all__ = ["File", "Response", "FileJsonType"] 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 9aaf6c388..265b3fa40 100644 --- a/integration-tests/integration_tests/api/body/post_body_multipart.py +++ b/integration-tests/integration_tests/api/body/post_body_multipart.py @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -36,7 +36,7 @@ def _get_kwargs( def _parse_response( *, client: Client, response: httpx.Response -) -> Optional[Union[PostBodyMultipartResponse200, PublicError]]: +) -> Union[None, PostBodyMultipartResponse200, PublicError]: if response.status_code == HTTPStatus.OK: response_200 = PostBodyMultipartResponse200.from_dict(response.json()) @@ -53,7 +53,7 @@ def _parse_response( def _build_response( *, client: Client, response: httpx.Response -) -> Response[Union[PostBodyMultipartResponse200, PublicError]]: +) -> Response[Union[None, PostBodyMultipartResponse200, PublicError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, @@ -66,7 +66,7 @@ def sync_detailed( *, client: Client, multipart_data: PostBodyMultipartMultipartData, -) -> Response[Union[PostBodyMultipartResponse200, PublicError]]: +) -> Response[Union[None, PostBodyMultipartResponse200, PublicError]]: """ Args: multipart_data (PostBodyMultipartMultipartData): @@ -76,7 +76,7 @@ def sync_detailed( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[PostBodyMultipartResponse200, PublicError]] + Response[Union[None, PostBodyMultipartResponse200, PublicError]] """ kwargs = _get_kwargs( @@ -96,7 +96,7 @@ def sync( *, client: Client, multipart_data: PostBodyMultipartMultipartData, -) -> Optional[Union[PostBodyMultipartResponse200, PublicError]]: +) -> Union[None, PostBodyMultipartResponse200, PublicError]: """ Args: multipart_data (PostBodyMultipartMultipartData): @@ -106,7 +106,7 @@ def sync( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[PostBodyMultipartResponse200, PublicError] + Union[None, PostBodyMultipartResponse200, PublicError] """ return sync_detailed( @@ -119,7 +119,7 @@ async def asyncio_detailed( *, client: Client, multipart_data: PostBodyMultipartMultipartData, -) -> Response[Union[PostBodyMultipartResponse200, PublicError]]: +) -> Response[Union[None, PostBodyMultipartResponse200, PublicError]]: """ Args: multipart_data (PostBodyMultipartMultipartData): @@ -129,7 +129,7 @@ async def asyncio_detailed( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[PostBodyMultipartResponse200, PublicError]] + Response[Union[None, PostBodyMultipartResponse200, PublicError]] """ kwargs = _get_kwargs( @@ -147,7 +147,7 @@ async def asyncio( *, client: Client, multipart_data: PostBodyMultipartMultipartData, -) -> Optional[Union[PostBodyMultipartResponse200, PublicError]]: +) -> Union[None, PostBodyMultipartResponse200, PublicError]: """ Args: multipart_data (PostBodyMultipartMultipartData): @@ -157,7 +157,7 @@ async def asyncio( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[PostBodyMultipartResponse200, PublicError] + Union[None, PostBodyMultipartResponse200, PublicError] """ return ( 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 4d881bd0f..ef9bccff2 100644 --- a/integration-tests/integration_tests/api/parameters/post_parameters_header.py +++ b/integration-tests/integration_tests/api/parameters/post_parameters_header.py @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Union import httpx @@ -43,7 +43,7 @@ def _get_kwargs( def _parse_response( *, client: Client, response: httpx.Response -) -> Optional[Union[PostParametersHeaderResponse200, PublicError]]: +) -> Union[None, PostParametersHeaderResponse200, PublicError]: if response.status_code == HTTPStatus.OK: response_200 = PostParametersHeaderResponse200.from_dict(response.json()) @@ -60,7 +60,7 @@ def _parse_response( def _build_response( *, client: Client, response: httpx.Response -) -> Response[Union[PostParametersHeaderResponse200, PublicError]]: +) -> Response[Union[None, PostParametersHeaderResponse200, PublicError]]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, @@ -76,7 +76,7 @@ def sync_detailed( string_header: str, number_header: float, integer_header: int, -) -> Response[Union[PostParametersHeaderResponse200, PublicError]]: +) -> Response[Union[None, PostParametersHeaderResponse200, PublicError]]: """ Args: boolean_header (bool): @@ -89,7 +89,7 @@ def sync_detailed( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[PostParametersHeaderResponse200, PublicError]] + Response[Union[None, PostParametersHeaderResponse200, PublicError]] """ kwargs = _get_kwargs( @@ -115,7 +115,7 @@ def sync( string_header: str, number_header: float, integer_header: int, -) -> Optional[Union[PostParametersHeaderResponse200, PublicError]]: +) -> Union[None, PostParametersHeaderResponse200, PublicError]: """ Args: boolean_header (bool): @@ -128,7 +128,7 @@ def sync( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[PostParametersHeaderResponse200, PublicError] + Union[None, PostParametersHeaderResponse200, PublicError] """ return sync_detailed( @@ -147,7 +147,7 @@ async def asyncio_detailed( string_header: str, number_header: float, integer_header: int, -) -> Response[Union[PostParametersHeaderResponse200, PublicError]]: +) -> Response[Union[None, PostParametersHeaderResponse200, PublicError]]: """ Args: boolean_header (bool): @@ -160,7 +160,7 @@ async def asyncio_detailed( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Response[Union[PostParametersHeaderResponse200, PublicError]] + Response[Union[None, PostParametersHeaderResponse200, PublicError]] """ kwargs = _get_kwargs( @@ -184,7 +184,7 @@ async def asyncio( string_header: str, number_header: float, integer_header: int, -) -> Optional[Union[PostParametersHeaderResponse200, PublicError]]: +) -> Union[None, PostParametersHeaderResponse200, PublicError]: """ Args: boolean_header (bool): @@ -197,7 +197,7 @@ async def asyncio( httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: - Union[PostParametersHeaderResponse200, PublicError] + Union[None, PostParametersHeaderResponse200, PublicError] """ return ( diff --git a/integration-tests/integration_tests/client.py b/integration-tests/integration_tests/client.py index 2f45c655b..3abe15459 100644 --- a/integration-tests/integration_tests/client.py +++ b/integration-tests/integration_tests/client.py @@ -18,6 +18,8 @@ class Client: 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. + This can also be set in the client generation config, to get return type annotations that do + not include None. follow_redirects: Whether or not to follow redirects. Default value is False. """ diff --git a/integration-tests/integration_tests/types.py b/integration-tests/integration_tests/types.py index 230efea92..2e9f7b08c 100644 --- a/integration-tests/integration_tests/types.py +++ b/integration-tests/integration_tests/types.py @@ -38,7 +38,7 @@ class Response(Generic[T]): status_code: HTTPStatus content: bytes headers: MutableMapping[str, str] - parsed: Optional[T] + parsed: T __all__ = ["File", "Response", "FileJsonType"] diff --git a/openapi_python_client/__init__.py b/openapi_python_client/__init__.py index 109ce84c7..0e4ac8a3f 100644 --- a/openapi_python_client/__init__.py +++ b/openapi_python_client/__init__.py @@ -263,7 +263,7 @@ 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) + client_path.write_text(client_template.render(config=self.config), encoding=self.file_encoding) # Generate included Errors errors_path = self.package_dir / "errors.py" @@ -295,9 +295,7 @@ def _build_api(self) -> None: for endpoint in collection.endpoints: module_path = tag_dir / f"{utils.PythonIdentifier(endpoint.name, self.config.field_prefix)}.py" module_path.write_text( - endpoint_template.render( - endpoint=endpoint, - ), + endpoint_template.render(endpoint=endpoint, config=self.config), encoding=self.file_encoding, ) diff --git a/openapi_python_client/config.py b/openapi_python_client/config.py index afca35758..5ec1ecc39 100644 --- a/openapi_python_client/config.py +++ b/openapi_python_client/config.py @@ -35,6 +35,8 @@ class Config(BaseModel): ] field_prefix: str = "field_" http_timeout: int = 5 + raise_on_error_status: bool = False + raise_on_unexpected_status: bool = False @staticmethod def load_from_path(path: Path) -> "Config": diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index e4af95a68..459c997c5 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -514,14 +514,23 @@ def from_data( return result, schemas, parameters - def response_type(self) -> str: + def response_type(self, include_errors: bool, include_unexpected: bool) -> str: """Get the Python type of any response from this endpoint""" - types = sorted({response.prop.get_type_string(quoted=False) for response in self.responses}) - if len(types) == 0: - return "Any" + responses = self.responses + if not include_errors: + responses = [resp for resp in responses if resp.status_code < 400] + + types = {response.prop.get_type_string(quoted=False) for response in responses} + if include_unexpected: + types.add("None") + + if not types: + return "None" # Function cannot complete without raising exception if len(types) == 1: - return self.responses[0].prop.get_type_string(quoted=False) - return f"Union[{', '.join(types)}]" + return next(iter(types)) + if "Any" in types: + return "Any" # Any includes all other types + return f"Union[{', '.join(sorted(types))}]" def iter_all_parameters(self) -> Iterator[Property]: """Iterate through all the parameters of this endpoint""" diff --git a/openapi_python_client/parser/responses.py b/openapi_python_client/parser/responses.py index e1f2cb49a..795ed057e 100644 --- a/openapi_python_client/parser/responses.py +++ b/openapi_python_client/parser/responses.py @@ -9,7 +9,7 @@ from .. import schema as oai from ..utils import PythonIdentifier from .errors import ParseError, PropertyError -from .properties import AnyProperty, Property, Schemas, property_from_data +from .properties import NoneProperty, Property, Schemas, property_from_data @attr.s(auto_attribs=True, frozen=True) @@ -20,6 +20,21 @@ class Response: prop: Property source: str + def get_typed_source(self) -> str: + """Get Python statement that parses the response and returns the correct type. + + This might include a type cast if necessary (e.g. if we parse JSON + that can only be a string according to the OpenAPI schema) + """ + if self.source == self.prop.get_type_string(): + # No cast needed e.g. for `None` and `None`. + return self.source + if self.source == "response.json()" and self.prop.get_type_string() == "Any": + # response.json() is often used for untyped responses and already has + # the `Any` return type annotation. + return self.source + return f"cast({self.prop.get_type_string()}, {self.source})" + def _source_by_content_type(content_type: str) -> Optional[str]: known_content_types = { @@ -40,7 +55,7 @@ def empty_response( """Return an untyped response, for when no response type is defined""" return Response( status_code=status_code, - prop=AnyProperty( + prop=NoneProperty( name=response_name, default=None, nullable=False, diff --git a/openapi_python_client/templates/client.py.jinja b/openapi_python_client/templates/client.py.jinja index c6e6b2305..49fcbeab7 100644 --- a/openapi_python_client/templates/client.py.jinja +++ b/openapi_python_client/templates/client.py.jinja @@ -16,6 +16,12 @@ class Client: 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. +{% if config.raise_on_unexpected_status %} + Setting this has no effect, because it was already set in the client generation config. +{% else %} + This can also be set in the client generation config, to get return type annotations that do + not include None. +{% endif %} follow_redirects: Whether or not to follow redirects. Default value is False. """ diff --git a/openapi_python_client/templates/endpoint_macros.py.jinja b/openapi_python_client/templates/endpoint_macros.py.jinja index 090796537..0871f1234 100644 --- a/openapi_python_client/templates/endpoint_macros.py.jinja +++ b/openapi_python_client/templates/endpoint_macros.py.jinja @@ -141,7 +141,7 @@ json_body=json_body, {% endfor %} {% endmacro %} -{% macro docstring_content(endpoint, return_string, is_detailed) %} +{% macro docstring_content(endpoint, return_string, config, is_detailed) %} {% if endpoint.summary %}{{ endpoint.summary | wordwrap(100)}} {% endif -%} @@ -161,7 +161,14 @@ Args: {% endif %} Raises: +{% if config.raise_on_error_status %} + httpx.HTTPStatusError: If the server returns an error status code. +{% endif %} +{% if config.raise_on_unexpected_status %} + errors.UnexpectedStatus: If the server returns an undocumented status code. +{% else %} errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. +{% endif %} httpx.TimeoutException: If the request takes longer than Client.timeout. Returns: @@ -172,6 +179,6 @@ Returns: {% endif %} {% endmacro %} -{% macro docstring(endpoint, return_string, is_detailed) %} -{{ safe_docstring(docstring_content(endpoint, return_string, is_detailed)) }} +{% macro docstring(endpoint, return_string, config, is_detailed) %} +{{ safe_docstring(docstring_content(endpoint, return_string, config, is_detailed)) }} {% endmacro %} diff --git a/openapi_python_client/templates/endpoint_module.py.jinja b/openapi_python_client/templates/endpoint_module.py.jinja index a94c6f025..54fc3e72c 100644 --- a/openapi_python_client/templates/endpoint_module.py.jinja +++ b/openapi_python_client/templates/endpoint_module.py.jinja @@ -14,8 +14,16 @@ from ... import errors {% from "endpoint_macros.py.jinja" import header_params, cookie_params, query_params, json_body, multipart_body, arguments, client, kwargs, parse_response, docstring %} -{% set return_string = endpoint.response_type() %} -{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "Any" %} +{% set return_string = endpoint.response_type(include_errors=not config.raise_on_error_status, + include_unexpected=not config.raise_on_unexpected_status) %} +{% set responses = endpoint.responses %} +{% if config.raise_on_error_status %} +{% set responses = endpoint.responses | selectattr("status_code", "lessthan", 400) | list %} +{% endif %} +{% set parsed_responses = (responses | length > 0) and return_string != "None" %} + +{% set accept_client = "" if config.raise_on_unexpected_status else "client: Client, " %} +{% set pass_client = "" if config.raise_on_unexpected_status else "client=client, " %} def _get_kwargs( {{ arguments(endpoint) | indent(4) }} @@ -60,39 +68,51 @@ def _get_kwargs( } -def _parse_response(*, client: Client, response: httpx.Response) -> Optional[{{ return_string }}]: - {% for response in endpoint.responses %} +def _parse_response(*, {{ accept_client }}response: httpx.Response) -> {{ return_string }}: + {% for response in responses %} if response.status_code == HTTPStatus.{{ response.status_code.name }}: {% 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 }}) + {{ response.prop.python_name }} = {{ response.get_typed_source() }} {% endif %} return {{ response.prop.python_name }} {% else %} return None {% endif %} {% endfor %} + {% if config.raise_on_error_status %} + response.raise_for_status() + {% endif %} + {% if config.raise_on_unexpected_status %} + raise errors.UnexpectedStatus(response.status_code, response.content) + {% else %} if client.raise_on_unexpected_status: raise errors.UnexpectedStatus(response.status_code, response.content) else: return None + {% endif %} -def _build_response(*, client: Client, response: httpx.Response) -> Response[{{ return_string }}]: +def _build_response(*, {{ accept_client }}response: httpx.Response) -> Response[{{ return_string }}]: return Response( status_code=HTTPStatus(response.status_code), content=response.content, headers=response.headers, - parsed=_parse_response(client=client, response=response), + {% if parsed_responses %} + parsed=_parse_response({{ pass_client }}response=response), + {% else %} + {# If _parse_response() returns None, mypy complains: https://github.com/python/mypy/issues/6549 #} + parsed=_parse_response({{ pass_client }}response=response), # type: ignore[func-returns-value] + {% endif %} ) def sync_detailed( {{ arguments(endpoint) | indent(4) }} ) -> Response[{{ return_string }}]: - {{ docstring(endpoint, return_string, is_detailed=true) | indent(4) }} + {{ docstring(endpoint, return_string, config, is_detailed=true) | indent(4) }} kwargs = _get_kwargs( {{ kwargs(endpoint) }} @@ -103,13 +123,13 @@ def sync_detailed( **kwargs, ) - return _build_response(client=client, response=response) + return _build_response({{ pass_client }}response=response) {% if parsed_responses %} def sync( {{ arguments(endpoint) | indent(4) }} -) -> Optional[{{ return_string }}]: - {{ docstring(endpoint, return_string, is_detailed=false) | indent(4) }} +) -> {{ return_string }}: + {{ docstring(endpoint, return_string, config, is_detailed=false) | indent(4) }} return sync_detailed( {{ kwargs(endpoint) }} @@ -119,7 +139,7 @@ def sync( async def asyncio_detailed( {{ arguments(endpoint) | indent(4) }} ) -> Response[{{ return_string }}]: - {{ docstring(endpoint, return_string, is_detailed=true) | indent(4) }} + {{ docstring(endpoint, return_string, config, is_detailed=true) | indent(4) }} kwargs = _get_kwargs( {{ kwargs(endpoint) }} @@ -130,13 +150,13 @@ async def asyncio_detailed( **kwargs ) - return _build_response(client=client, response=response) + return _build_response({{ pass_client }}response=response) {% if parsed_responses %} async def asyncio( {{ arguments(endpoint) | indent(4) }} -) -> Optional[{{ return_string }}]: - {{ docstring(endpoint, return_string, is_detailed=false) | indent(4) }} +) -> {{ return_string }}: + {{ docstring(endpoint, return_string, config, is_detailed=false) | indent(4) }} return (await asyncio_detailed( {{ kwargs(endpoint) }} diff --git a/openapi_python_client/templates/types.py.jinja b/openapi_python_client/templates/types.py.jinja index c746db6e1..82a522c6f 100644 --- a/openapi_python_client/templates/types.py.jinja +++ b/openapi_python_client/templates/types.py.jinja @@ -39,7 +39,7 @@ class Response(Generic[T]): status_code: HTTPStatus content: bytes headers: MutableMapping[str, str] - parsed: Optional[T] + parsed: T __all__ = ["File", "Response", "FileJsonType"] diff --git a/tests/test_parser/test_openapi.py b/tests/test_parser/test_openapi.py index 8ef6b0a6a..115094fd0 100644 --- a/tests/test_parser/test_openapi.py +++ b/tests/test_parser/test_openapi.py @@ -1204,17 +1204,31 @@ def test_from_data_no_security(self, mocker): ) @pytest.mark.parametrize( - "response_types, expected", - (([], "Any"), (["Something"], "Something"), (["First", "Second", "Second"], "Union[First, Second]")), + "response_types, response_statuses, include_errors, include_unexpected, expected_type", + ( + ([], [], True, True, "None"), + (["Something"], [400], True, True, "Union[None, Something]"), + (["Something"], [400], True, False, "Something"), + (["Something"], [400], False, True, "None"), + (["Something"], [400], False, False, "None"), + (["First", "Second", "Second"], [200, 400, 409], True, True, "Union[First, None, Second]"), + (["First", "Second", "Second"], [200, 400, 409], True, False, "Union[First, Second]"), + (["First", "Second", "Second"], [200, 400, 409], False, True, "Union[First, None]"), + (["First", "Second", "Second"], [200, 400, 409], False, False, "First"), + ), ) - def test_response_type(self, response_types, expected): + def test_response_type(self, response_types, response_statuses, include_errors, include_unexpected, expected_type): endpoint = self.make_endpoint() - for response_type in response_types: + for response_type, status_code in zip(response_types, response_statuses): mock_response = MagicMock() mock_response.prop.get_type_string.return_value = response_type + mock_response.status_code = status_code endpoint.responses.append(mock_response) - assert endpoint.response_type() == expected + assert ( + endpoint.response_type(include_errors=include_errors, include_unexpected=include_unexpected) + == expected_type + ) class TestImportStringFromReference: diff --git a/tests/test_parser/test_responses.py b/tests/test_parser/test_responses.py index ab73cfb74..424be11ad 100644 --- a/tests/test_parser/test_responses.py +++ b/tests/test_parser/test_responses.py @@ -7,7 +7,7 @@ MODULE_NAME = "openapi_python_client.parser.responses" -def test_response_from_data_no_content(any_property_factory): +def test_response_from_data_no_content(none_property_factory): from openapi_python_client.parser.responses import Response, response_from_data response, schemas = response_from_data( @@ -20,7 +20,7 @@ def test_response_from_data_no_content(any_property_factory): assert response == Response( status_code=200, - prop=any_property_factory( + prop=none_property_factory( name="response_200", default=None, nullable=False, @@ -31,7 +31,7 @@ def test_response_from_data_no_content(any_property_factory): ) -def test_response_from_data_reference(any_property_factory): +def test_response_from_data_reference(none_property_factory): from openapi_python_client.parser.responses import Response, response_from_data response, schemas = response_from_data( @@ -44,7 +44,7 @@ def test_response_from_data_reference(any_property_factory): assert response == Response( status_code=200, - prop=any_property_factory( + prop=none_property_factory( name="response_200", default=None, nullable=False, @@ -65,7 +65,7 @@ def test_response_from_data_unsupported_content_type(): assert response == ParseError(data=data, detail="Unsupported content_type {'blah': None}") -def test_response_from_data_no_content_schema(any_property_factory): +def test_response_from_data_no_content_schema(none_property_factory): from openapi_python_client.parser.responses import Response, response_from_data data = oai.Response.construct(description="", content={"application/json": oai.MediaType.construct()}) @@ -75,7 +75,7 @@ def test_response_from_data_no_content_schema(any_property_factory): assert response == Response( status_code=200, - prop=any_property_factory( + prop=none_property_factory( name="response_200", default=None, nullable=False,