From 867e93089d92d9c54726dab578bfb2c7023ce772 Mon Sep 17 00:00:00 2001 From: Robert Schweizer Date: Wed, 15 Mar 2023 09:24:39 +0100 Subject: [PATCH 1/2] Add `raise_on_error_status` option --- .../templates/client.py.jinja | 7 +++-- .../templates/endpoint_macros.py.jinja | 1 + .../templates/endpoint_module.py.jinja | 17 +++++++++-- .../templates/errors.py.jinja | 29 +++++++++++++++++-- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/openapi_python_client/templates/client.py.jinja b/openapi_python_client/templates/client.py.jinja index 3155f30bf..b3ddcd28a 100644 --- a/openapi_python_client/templates/client.py.jinja +++ b/openapi_python_client/templates/client.py.jinja @@ -14,8 +14,10 @@ class Client: httpx.TimeoutException if this is exceeded. verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production, but can be set to False for testing purposes. - raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a - status code that was not documented in the source OpenAPI document. + 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. Can be combined with `raise_on_error_status`. + raise_on_error_status: Whether or not to raise an errors.ErrorStatus on all status codes >= 400, regardless of + whether they are expected or not. Can be combined with `raise_on_unexpected_status`. """ base_url: str @@ -24,6 +26,7 @@ class Client: timeout: float = attr.ib(5.0, kw_only=True) verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True) raise_on_unexpected_status: bool = attr.ib(False, kw_only=True) + raise_on_error_status: bool = attr.ib(False, kw_only=True) def get_headers(self) -> Dict[str, str]: """ Get headers to be used in all endpoints """ diff --git a/openapi_python_client/templates/endpoint_macros.py.jinja b/openapi_python_client/templates/endpoint_macros.py.jinja index 4dc0575f9..20148a24e 100644 --- a/openapi_python_client/templates/endpoint_macros.py.jinja +++ b/openapi_python_client/templates/endpoint_macros.py.jinja @@ -160,6 +160,7 @@ Args: {% endif %} Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. diff --git a/openapi_python_client/templates/endpoint_module.py.jinja b/openapi_python_client/templates/endpoint_module.py.jinja index 26d313f16..5add32ec6 100644 --- a/openapi_python_client/templates/endpoint_module.py.jinja +++ b/openapi_python_client/templates/endpoint_module.py.jinja @@ -68,15 +68,26 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[{{ {% else %} {{ response.prop.python_name }} = cast({{ response.prop.get_type_string() }}, {{ response.source }}) {% endif %} + {% if response.status_code >= 400 %} + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") + {% endif %} return {{ response.prop.python_name }} {% else %} + {% if response.status_code >= 400 %} + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") + {% endif %} return None {% endif %} {% endfor %} - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[{{ return_string }}]: diff --git a/openapi_python_client/templates/errors.py.jinja b/openapi_python_client/templates/errors.py.jinja index 7445a2dad..332cb7dc4 100644 --- a/openapi_python_client/templates/errors.py.jinja +++ b/openapi_python_client/templates/errors.py.jinja @@ -1,7 +1,30 @@ """ Contains shared errors types that can be raised from API functions """ + class UnexpectedStatus(Exception): - """ Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True """ - ... + """ Raised by api functions when the response status is undocumented and Client.raise_on_unexpected_status is True """ + + +class ErrorStatus(Exception): + """ Raised by api functions when the response status marks an error """ + + +class ExpectedErrorStatus(ErrorStatus): + pass + + +class UnexpectedErrorStatus(ErrorStatus, UnexpectedStatus): + pass + + +class UnexpectedSuccessStatus(UnexpectedStatus): + pass + -__all__ = ["UnexpectedStatus"] +__all__ = [ + "ErrorStatus", + "ExpectedErrorStatus", + "UnexpectedErrorStatus", + "UnexpectedStatus", + "UnexpectedSuccessStatus", +] From 316c42372918d06e7e6d70ca05737713e49d353e Mon Sep 17 00:00:00 2001 From: Robert Schweizer Date: Wed, 15 Mar 2023 17:59:36 +0100 Subject: [PATCH 2/2] Update tests --- .../api/default/get_common_parameters.py | 11 +++++--- .../api/default/post_common_parameters.py | 11 +++++--- .../api/location/get_location_header_types.py | 11 +++++--- .../get_location_query_optionality.py | 11 +++++--- .../get_parameter_references_path_param.py | 11 +++++--- ...lete_common_parameters_overriding_param.py | 11 +++++--- .../get_common_parameters_overriding_param.py | 11 +++++--- .../get_same_name_multiple_locations_param.py | 11 +++++--- .../parameters/multiple_path_parameters.py | 11 +++++--- ..._responses_unions_simple_before_complex.py | 13 +++++++--- .../api/tag1/get_tag_with_number.py | 11 +++++--- .../api/tests/callback_test.py | 15 ++++++++--- .../api/tests/defaults_tests_defaults_post.py | 15 ++++++++--- .../api/tests/get_basic_list_of_booleans.py | 13 +++++++--- .../api/tests/get_basic_list_of_floats.py | 13 +++++++--- .../api/tests/get_basic_list_of_integers.py | 13 +++++++--- .../api/tests/get_basic_list_of_strings.py | 13 +++++++--- .../api/tests/get_user_list.py | 17 +++++++++--- .../api/tests/int_enum_tests_int_enum_post.py | 15 ++++++++--- .../tests/json_body_tests_json_body_post.py | 15 ++++++++--- .../no_response_tests_no_response_get.py | 11 +++++--- .../octet_stream_tests_octet_stream_get.py | 13 +++++++--- .../api/tests/post_form_data.py | 11 +++++--- .../api/tests/post_form_data_inline.py | 11 +++++--- .../api/tests/post_tests_json_body_string.py | 15 ++++++++--- .../api/tests/test_inline_objects.py | 13 +++++++--- ..._with_cookie_auth_token_with_cookie_get.py | 13 +++++++--- ...d_content_tests_unsupported_content_get.py | 11 +++++--- .../tests/upload_file_tests_upload_post.py | 15 ++++++++--- ...upload_multiple_files_tests_upload_post.py | 15 ++++++++--- .../my_test_api_client/api/true_/false_.py | 11 +++++--- .../my_test_api_client/client.py | 7 +++-- .../my_test_api_client/errors.py | 26 ++++++++++++++++--- .../api/body/post_body_multipart.py | 15 ++++++++--- .../api/parameters/post_parameters_header.py | 15 ++++++++--- integration-tests/integration_tests/client.py | 7 +++-- integration-tests/integration_tests/errors.py | 26 ++++++++++++++++--- 37 files changed, 378 insertions(+), 109 deletions(-) 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 eea2a39cb..7120dc7c0 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 @@ -36,10 +36,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -61,6 +64,7 @@ def sync_detailed( common (Union[Unset, None, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -91,6 +95,7 @@ async def asyncio_detailed( common (Union[Unset, None, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 54f11f8dc..7cc19c625 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 @@ -36,10 +36,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -61,6 +64,7 @@ def sync_detailed( common (Union[Unset, None, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -91,6 +95,7 @@ async def asyncio_detailed( common (Union[Unset, None, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 ab6ae3180..424846a0e 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 @@ -55,10 +55,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -90,6 +93,7 @@ def sync_detailed( string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -135,6 +139,7 @@ async def asyncio_detailed( string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 427cf04dc..ef4596d5a 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 @@ -60,10 +60,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -91,6 +94,7 @@ def sync_detailed( not_null_not_required (Union[Unset, None, datetime.datetime]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -130,6 +134,7 @@ async def asyncio_detailed( not_null_not_required (Union[Unset, None, datetime.datetime]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 bdb518de5..292ea3569 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 @@ -46,10 +46,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -80,6 +83,7 @@ def sync_detailed( cookie_param (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -123,6 +127,7 @@ async def asyncio_detailed( cookie_param (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 6ddea0265..519658336 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 @@ -37,10 +37,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -64,6 +67,7 @@ def sync_detailed( param_query (Union[Unset, None, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -97,6 +101,7 @@ async def asyncio_detailed( param_query (Union[Unset, None, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 2089e9c59..898fcd0aa 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 @@ -37,10 +37,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -66,6 +69,7 @@ def sync_detailed( 'overridden_in_GET'. Example: an example string. Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -101,6 +105,7 @@ async def asyncio_detailed( 'overridden_in_GET'. Example: an example string. Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 c6e0f4736..5be816617 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 @@ -45,10 +45,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -76,6 +79,7 @@ def sync_detailed( param_cookie (Union[Unset, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -115,6 +119,7 @@ async def asyncio_detailed( param_cookie (Union[Unset, str]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 a005a85ac..bf97faa24 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 @@ -35,10 +35,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -66,6 +69,7 @@ def sync_detailed( param3 (int): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -105,6 +109,7 @@ async def asyncio_detailed( param3 (int): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 811633348..f784fbe04 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 @@ -36,10 +36,13 @@ def _parse_response( response_200 = PostResponsesUnionsSimpleBeforeComplexResponse200.from_dict(response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response( @@ -60,6 +63,7 @@ def sync_detailed( """Regression test for #603 Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -86,6 +90,7 @@ def sync( """Regression test for #603 Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -105,6 +110,7 @@ async def asyncio_detailed( """Regression test for #603 Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -129,6 +135,7 @@ async def asyncio( """Regression test for #603 Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 5df86a828..e5a4c909c 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 @@ -29,10 +29,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -50,6 +53,7 @@ def sync_detailed( ) -> Response[Any]: """ Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -75,6 +79,7 @@ async def asyncio_detailed( ) -> Response[Any]: """ Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 ca87484c4..98bec9025 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 @@ -39,11 +39,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: @@ -68,6 +73,7 @@ def sync_detailed( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -101,6 +107,7 @@ def sync( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -127,6 +134,7 @@ async def asyncio_detailed( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -158,6 +166,7 @@ async def asyncio( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 44a2cc859..da4beb340 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 @@ -106,11 +106,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: @@ -153,6 +158,7 @@ def sync_detailed( required_model_prop (ModelWithUnionProperty): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -214,6 +220,7 @@ def sync( required_model_prop (ModelWithUnionProperty): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -268,6 +275,7 @@ async def asyncio_detailed( required_model_prop (ModelWithUnionProperty): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -327,6 +335,7 @@ async def asyncio( required_model_prop (ModelWithUnionProperty): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 ce71633d9..96e48ea1c 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 @@ -31,10 +31,13 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis response_200 = cast(List[bool], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[List[bool]]: @@ -55,6 +58,7 @@ def sync_detailed( Get a list of booleans Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -83,6 +87,7 @@ def sync( Get a list of booleans Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -104,6 +109,7 @@ async def asyncio_detailed( Get a list of booleans Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -130,6 +136,7 @@ async def asyncio( Get a list of booleans Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 dcb97ade5..70256e44b 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 @@ -31,10 +31,13 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis response_200 = cast(List[float], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[List[float]]: @@ -55,6 +58,7 @@ def sync_detailed( Get a list of floats Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -83,6 +87,7 @@ def sync( Get a list of floats Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -104,6 +109,7 @@ async def asyncio_detailed( Get a list of floats Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -130,6 +136,7 @@ async def asyncio( Get a list of floats Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 800c29608..d6b76461d 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 @@ -31,10 +31,13 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis response_200 = cast(List[int], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[List[int]]: @@ -55,6 +58,7 @@ def sync_detailed( Get a list of integers Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -83,6 +87,7 @@ def sync( Get a list of integers Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -104,6 +109,7 @@ async def asyncio_detailed( Get a list of integers Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -130,6 +136,7 @@ async def asyncio( Get a list of integers Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 2a84b2b5e..18cc3de17 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 @@ -31,10 +31,13 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis response_200 = cast(List[str], response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[List[str]]: @@ -55,6 +58,7 @@ def sync_detailed( Get a list of strings Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -83,6 +87,7 @@ def sync( Get a list of strings Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -104,6 +109,7 @@ async def asyncio_detailed( Get a list of strings Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -130,6 +136,7 @@ async def asyncio( Get a list of strings Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 6cffd7741..341d93c84 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 @@ -85,15 +85,22 @@ def _parse_response( if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 if response.status_code == HTTPStatus.LOCKED: response_423 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_423 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response( @@ -126,6 +133,7 @@ def sync_detailed( some_date (Union[datetime.date, datetime.datetime]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -168,6 +176,7 @@ def sync( some_date (Union[datetime.date, datetime.datetime]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -203,6 +212,7 @@ async def asyncio_detailed( some_date (Union[datetime.date, datetime.datetime]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -243,6 +253,7 @@ async def asyncio( some_date (Union[datetime.date, datetime.datetime]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 4e23476f5..17a1c67cc 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 @@ -44,11 +44,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: @@ -71,6 +76,7 @@ def sync_detailed( int_enum (AnIntEnum): An enumeration. Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -102,6 +108,7 @@ def sync( int_enum (AnIntEnum): An enumeration. Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -126,6 +133,7 @@ async def asyncio_detailed( int_enum (AnIntEnum): An enumeration. Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -155,6 +163,7 @@ async def asyncio( int_enum (AnIntEnum): An enumeration. Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 383522958..60b1cc078 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 @@ -39,11 +39,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: @@ -68,6 +73,7 @@ def sync_detailed( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -101,6 +107,7 @@ def sync( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -127,6 +134,7 @@ async def asyncio_detailed( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -158,6 +166,7 @@ async def asyncio( json_body (AModel): A Model for testing all the ways custom objects can be used Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 933184d3f..40062114c 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 @@ -29,10 +29,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -51,6 +54,7 @@ def sync_detailed( """No Response Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -77,6 +81,7 @@ async def asyncio_detailed( """No Response Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 99859332e..612fe1297 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 @@ -32,10 +32,13 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Fil response_200 = File(payload=BytesIO(response.content)) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[File]: @@ -54,6 +57,7 @@ def sync_detailed( """Octet Stream Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -80,6 +84,7 @@ def sync( """Octet Stream Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -99,6 +104,7 @@ async def asyncio_detailed( """Octet Stream Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -123,6 +129,7 @@ async def asyncio( """Octet Stream Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 ff9e887f4..138c4c802 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 @@ -32,10 +32,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -57,6 +60,7 @@ def sync_detailed( Post form data Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -87,6 +91,7 @@ async def asyncio_detailed( Post form data Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 46536a27a..ad07f3132 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 @@ -32,10 +32,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -57,6 +60,7 @@ def sync_detailed( Post form data (inline schema) Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -87,6 +91,7 @@ async def asyncio_detailed( Post form data (inline schema) Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 6a1d178dc..10d96431f 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 @@ -38,11 +38,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]: @@ -65,6 +70,7 @@ def sync_detailed( json_body (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -96,6 +102,7 @@ def sync( json_body (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -120,6 +127,7 @@ async def asyncio_detailed( json_body (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -149,6 +157,7 @@ async def asyncio( json_body (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 d1533c0e1..f2f8dd0dc 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 @@ -37,10 +37,13 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Tes response_200 = TestInlineObjectsResponse200.from_dict(response.json()) return response_200 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[TestInlineObjectsResponse200]: @@ -63,6 +66,7 @@ def sync_detailed( json_body (TestInlineObjectsJsonBody): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -94,6 +98,7 @@ def sync( json_body (TestInlineObjectsJsonBody): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -118,6 +123,7 @@ async def asyncio_detailed( json_body (TestInlineObjectsJsonBody): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -147,6 +153,7 @@ async def asyncio( json_body (TestInlineObjectsJsonBody): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 85d53d9da..252739ca6 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 @@ -33,11 +33,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any if response.status_code == HTTPStatus.OK: return None if response.status_code == HTTPStatus.UNAUTHORIZED: + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -62,6 +67,7 @@ def sync_detailed( my_token (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -95,6 +101,7 @@ async def asyncio_detailed( my_token (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 2daec1319..ecb9e5cc9 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 @@ -29,10 +29,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -51,6 +54,7 @@ def sync_detailed( """Unsupported Content Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -77,6 +81,7 @@ async def asyncio_detailed( """Unsupported Content Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 d00ec5e40..d3731e3b0 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 @@ -39,11 +39,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: @@ -68,6 +73,7 @@ def sync_detailed( multipart_data (BodyUploadFileTestsUploadPost): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -101,6 +107,7 @@ def sync( multipart_data (BodyUploadFileTestsUploadPost): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -127,6 +134,7 @@ async def asyncio_detailed( multipart_data (BodyUploadFileTestsUploadPost): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -158,6 +166,7 @@ async def asyncio( multipart_data (BodyUploadFileTestsUploadPost): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 34add0e4e..4e3fa8ed9 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 @@ -42,11 +42,16 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: response_422 = HTTPValidationError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_422 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Any, HTTPValidationError]]: @@ -71,6 +76,7 @@ def sync_detailed( multipart_data (List[File]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -104,6 +110,7 @@ def sync( multipart_data (List[File]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -130,6 +137,7 @@ async def asyncio_detailed( multipart_data (List[File]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -161,6 +169,7 @@ async def asyncio( multipart_data (List[File]): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 f8332a87a..f50568f96 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 @@ -36,10 +36,13 @@ def _get_kwargs( def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: if response.status_code == HTTPStatus.OK: return None - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: @@ -61,6 +64,7 @@ def sync_detailed( import_ (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -91,6 +95,7 @@ async def asyncio_detailed( import_ (str): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 99aeffa53..48af0fbc0 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 @@ -16,8 +16,10 @@ class Client: httpx.TimeoutException if this is exceeded. verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production, but can be set to False for testing purposes. - raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a - status code that was not documented in the source OpenAPI document. + 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. Can be combined with `raise_on_error_status`. + raise_on_error_status: Whether or not to raise an errors.ErrorStatus on all status codes >= 400, regardless of + whether they are expected or not. Can be combined with `raise_on_unexpected_status`. """ base_url: str @@ -26,6 +28,7 @@ class Client: timeout: float = attr.ib(5.0, kw_only=True) verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True) raise_on_unexpected_status: bool = attr.ib(False, kw_only=True) + raise_on_error_status: bool = attr.ib(False, kw_only=True) def get_headers(self) -> Dict[str, str]: """Get headers to be used in all endpoints""" diff --git a/end_to_end_tests/golden-record/my_test_api_client/errors.py b/end_to_end_tests/golden-record/my_test_api_client/errors.py index a508e1360..ff425e762 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/errors.py +++ b/end_to_end_tests/golden-record/my_test_api_client/errors.py @@ -2,9 +2,29 @@ class UnexpectedStatus(Exception): - """Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True""" + """Raised by api functions when the response status is undocumented and Client.raise_on_unexpected_status is True""" - ... +class ErrorStatus(Exception): + """Raised by api functions when the response status marks an error""" -__all__ = ["UnexpectedStatus"] + +class ExpectedErrorStatus(ErrorStatus): + pass + + +class UnexpectedErrorStatus(ErrorStatus, UnexpectedStatus): + pass + + +class UnexpectedSuccessStatus(UnexpectedStatus): + pass + + +__all__ = [ + "ErrorStatus", + "ExpectedErrorStatus", + "UnexpectedErrorStatus", + "UnexpectedStatus", + "UnexpectedSuccessStatus", +] 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 303443af7..c56417ce7 100644 --- a/integration-tests/integration_tests/api/body/post_body_multipart.py +++ b/integration-tests/integration_tests/api/body/post_body_multipart.py @@ -43,11 +43,16 @@ def _parse_response( if response.status_code == HTTPStatus.BAD_REQUEST: response_400 = PublicError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_400 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response( @@ -71,6 +76,7 @@ def sync_detailed( multipart_data (PostBodyMultipartMultipartData): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -101,6 +107,7 @@ def sync( multipart_data (PostBodyMultipartMultipartData): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -124,6 +131,7 @@ async def asyncio_detailed( multipart_data (PostBodyMultipartMultipartData): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -152,6 +160,7 @@ async def asyncio( multipart_data (PostBodyMultipartMultipartData): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. 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 c89c4c304..f290d8197 100644 --- a/integration-tests/integration_tests/api/parameters/post_parameters_header.py +++ b/integration-tests/integration_tests/api/parameters/post_parameters_header.py @@ -50,11 +50,16 @@ def _parse_response( if response.status_code == HTTPStatus.BAD_REQUEST: response_400 = PublicError.from_dict(response.json()) + if client.raise_on_error_status: + raise errors.ExpectedErrorStatus(f"Failed status code: {response.status_code}") return response_400 - if client.raise_on_unexpected_status: - raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") + if response.status_code >= 400: + if client.raise_on_unexpected_status or client.raise_on_error_status: + raise errors.UnexpectedErrorStatus(f"Unexpected status code: {response.status_code}") else: - return None + if client.raise_on_unexpected_status: + raise errors.UnexpectedSuccessStatus(f"Unexpected status code: {response.status_code}") + return None def _build_response( @@ -84,6 +89,7 @@ def sync_detailed( integer_header (int): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -123,6 +129,7 @@ def sync( integer_header (int): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -155,6 +162,7 @@ async def asyncio_detailed( integer_header (int): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. @@ -192,6 +200,7 @@ async def asyncio( integer_header (int): Raises: + errors.ErrorStatus: If the server returns an error status code and Client.raise_on_error_status is True. errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. httpx.TimeoutException: If the request takes longer than Client.timeout. diff --git a/integration-tests/integration_tests/client.py b/integration-tests/integration_tests/client.py index 99aeffa53..48af0fbc0 100644 --- a/integration-tests/integration_tests/client.py +++ b/integration-tests/integration_tests/client.py @@ -16,8 +16,10 @@ class Client: httpx.TimeoutException if this is exceeded. verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production, but can be set to False for testing purposes. - raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a - status code that was not documented in the source OpenAPI document. + 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. Can be combined with `raise_on_error_status`. + raise_on_error_status: Whether or not to raise an errors.ErrorStatus on all status codes >= 400, regardless of + whether they are expected or not. Can be combined with `raise_on_unexpected_status`. """ base_url: str @@ -26,6 +28,7 @@ class Client: timeout: float = attr.ib(5.0, kw_only=True) verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True) raise_on_unexpected_status: bool = attr.ib(False, kw_only=True) + raise_on_error_status: bool = attr.ib(False, kw_only=True) def get_headers(self) -> Dict[str, str]: """Get headers to be used in all endpoints""" diff --git a/integration-tests/integration_tests/errors.py b/integration-tests/integration_tests/errors.py index a508e1360..ff425e762 100644 --- a/integration-tests/integration_tests/errors.py +++ b/integration-tests/integration_tests/errors.py @@ -2,9 +2,29 @@ class UnexpectedStatus(Exception): - """Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True""" + """Raised by api functions when the response status is undocumented and Client.raise_on_unexpected_status is True""" - ... +class ErrorStatus(Exception): + """Raised by api functions when the response status marks an error""" -__all__ = ["UnexpectedStatus"] + +class ExpectedErrorStatus(ErrorStatus): + pass + + +class UnexpectedErrorStatus(ErrorStatus, UnexpectedStatus): + pass + + +class UnexpectedSuccessStatus(UnexpectedStatus): + pass + + +__all__ = [ + "ErrorStatus", + "ExpectedErrorStatus", + "UnexpectedErrorStatus", + "UnexpectedStatus", + "UnexpectedSuccessStatus", +]