Skip to content

Commit eae975b

Browse files
committed
refactor(http): centralize response error handling with helper function
1 parent 0e67ec4 commit eae975b

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

mpt_api_client/http/async_client.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import os
22
from typing import Any
33

4-
from httpx import AsyncClient, HTTPError, HTTPStatusError, RequestError
4+
from httpx import AsyncClient, HTTPError, RequestError
55
from httpx_retries import Retry, RetryTransport
66

77
from mpt_api_client.constants import APPLICATION_JSON
8-
from mpt_api_client.exceptions import MPTError, MPTMaxRetryError, transform_http_status_exception
8+
from mpt_api_client.exceptions import MPTError, MPTMaxRetryError
99
from mpt_api_client.http.client import json_to_file_payload
1010
from mpt_api_client.http.client_utils import get_query_params, validate_base_url
1111
from mpt_api_client.http.query_options import QueryOptions
12+
from mpt_api_client.http.request_response_utils import handle_response_http_error
1213
from mpt_api_client.http.types import HeaderTypes, QueryParam, RequestFiles, Response
1314

1415

@@ -102,10 +103,8 @@ async def request( # noqa: WPS211
102103
except HTTPError as err:
103104
raise MPTError(f"HTTP Error: {err}") from err
104105

105-
try:
106-
response.raise_for_status()
107-
except HTTPStatusError as http_status_exception:
108-
raise transform_http_status_exception(http_status_exception) from http_status_exception
106+
handle_response_http_error(response)
107+
109108
return Response(
110109
headers=dict(response.headers),
111110
status_code=response.status_code,

mpt_api_client/http/client.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import os
33
from typing import Any
44

5-
from httpx import Client, HTTPError, HTTPStatusError, RequestError
5+
from httpx import Client, HTTPError, RequestError
66
from httpx_retries import Retry, RetryTransport
77

88
from mpt_api_client.constants import APPLICATION_JSON
9-
from mpt_api_client.exceptions import MPTError, MPTMaxRetryError, transform_http_status_exception
9+
from mpt_api_client.exceptions import MPTError, MPTMaxRetryError
1010
from mpt_api_client.http.client_utils import get_query_params, validate_base_url
1111
from mpt_api_client.http.query_options import QueryOptions
12+
from mpt_api_client.http.request_response_utils import handle_response_http_error
1213
from mpt_api_client.http.types import HeaderTypes, QueryParam, RequestFiles, Response
1314
from mpt_api_client.models import ResourceData
1415

@@ -111,10 +112,8 @@ def request( # noqa: WPS211
111112
except HTTPError as err:
112113
raise MPTError(f"HTTP Error: {err}") from err
113114

114-
try:
115-
response.raise_for_status()
116-
except HTTPStatusError as http_status_exception:
117-
raise transform_http_status_exception(http_status_exception) from http_status_exception
115+
handle_response_http_error(response)
116+
118117
return Response(
119118
headers=dict(response.headers),
120119
status_code=response.status_code,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from httpx import HTTPStatusError
2+
from httpx import Response as HTTPXResponse
3+
4+
from mpt_api_client.exceptions import transform_http_status_exception
5+
6+
7+
def handle_response_http_error(response: HTTPXResponse) -> None:
8+
"""Handles HTTP response error by raising a transformed HTTPStatusError exception."""
9+
try:
10+
response.raise_for_status()
11+
except HTTPStatusError as http_status_exception:
12+
raise transform_http_status_exception(http_status_exception) from http_status_exception

0 commit comments

Comments
 (0)