Skip to content

Commit c3b208c

Browse files
committed
Make HTTPClient more readable. Change _do_request name to _make_request to distinguish from APIClient method with same name
1 parent ed91068 commit c3b208c

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

cognite/client/_http_client.py

+27-23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import socket
66
import time
77
from http import cookiejar
8-
from typing import Any, Callable, Literal, MutableMapping
8+
from typing import Any, Callable, ClassVar, Literal, MutableMapping
99

1010
import requests
1111
import requests.adapters
@@ -96,6 +96,18 @@ def should_retry(self, status_code: int | None) -> bool:
9696

9797

9898
class HTTPClient:
99+
_TIMEOUT_EXCEPTIONS: ClassVar[tuple[type[Exception], ...]] = (
100+
socket.timeout,
101+
urllib3.exceptions.ReadTimeoutError,
102+
requests.exceptions.ReadTimeout,
103+
)
104+
_CONNECTION_EXCEPTIONS: ClassVar[tuple[type[Exception], ...]] = (
105+
ConnectionError,
106+
urllib3.exceptions.ConnectionError,
107+
urllib3.exceptions.ConnectTimeoutError,
108+
requests.exceptions.ConnectionError,
109+
)
110+
99111
def __init__(
100112
self,
101113
config: HTTPClientConfig,
@@ -114,7 +126,7 @@ def request(self, method: str, url: str, **kwargs: Any) -> requests.Response:
114126
last_status = None
115127
while True:
116128
try:
117-
res = self._do_request(method=method, url=url, **kwargs)
129+
res = self._make_request(method=method, url=url, **kwargs)
118130
if global_config.usage_tracking_callback:
119131
# We use the RequestDetails as an indirection to avoid the user mutating the request:
120132
global_config.usage_tracking_callback(RequestDetails.from_response(res))
@@ -125,53 +137,45 @@ def request(self, method: str, url: str, **kwargs: Any) -> requests.Response:
125137
# Cache .json() return value in order to avoid redecoding JSON if called multiple times
126138
res.json = functools.lru_cache(maxsize=1)(res.json) # type: ignore[assignment]
127139
return res
128-
except CogniteReadTimeout as e:
140+
141+
except CogniteReadTimeout:
129142
retry_tracker.read += 1
130143
if not retry_tracker.should_retry(status_code=last_status):
131-
raise e
132-
except CogniteConnectionError as e:
144+
raise
145+
146+
except CogniteConnectionError:
133147
retry_tracker.connect += 1
134148
if not retry_tracker.should_retry(status_code=last_status):
135-
raise e
149+
raise
136150

137151
# During a backoff loop, our credentials might expire, so we check and maybe refresh:
138152
time.sleep(retry_tracker.get_backoff_time())
139153
if headers is not None:
140154
# TODO: Refactoring needed to make this "prettier"
141155
self.refresh_auth_header(headers)
142156

143-
def _do_request(self, method: str, url: str, **kwargs: Any) -> requests.Response:
157+
def _make_request(self, method: str, url: str, **kwargs: Any) -> requests.Response:
144158
"""requests/urllib3 adds 2 or 3 layers of exceptions on top of built-in networking exceptions.
145159
146160
Sometimes the appropriate built-in networking exception is not in the context, sometimes the requests
147161
exception is not in the context, so we need to check for the appropriate built-in exceptions,
148162
urllib3 exceptions, and requests exceptions.
149163
"""
150164
try:
151-
res = self.session.request(method=method, url=url, **kwargs)
152-
return res
165+
return self.session.request(method=method, url=url, **kwargs)
153166
except Exception as e:
154-
if self._any_exception_in_context_isinstance(
155-
e, (socket.timeout, urllib3.exceptions.ReadTimeoutError, requests.exceptions.ReadTimeout)
156-
):
167+
if self._any_exception_in_context_isinstance(e, self._TIMEOUT_EXCEPTIONS):
157168
raise CogniteReadTimeout from e
158-
if self._any_exception_in_context_isinstance(
159-
e,
160-
(
161-
ConnectionError,
162-
urllib3.exceptions.ConnectionError,
163-
urllib3.exceptions.ConnectTimeoutError,
164-
requests.exceptions.ConnectionError,
165-
),
166-
):
169+
170+
if self._any_exception_in_context_isinstance(e, self._CONNECTION_EXCEPTIONS):
167171
if self._any_exception_in_context_isinstance(e, ConnectionRefusedError):
168172
raise CogniteConnectionRefused from e
169173
raise CogniteConnectionError from e
170-
raise e
174+
raise
171175

172176
@classmethod
173177
def _any_exception_in_context_isinstance(
174-
cls, exc: BaseException, exc_types: tuple[type[BaseException], ...] | type[BaseException]
178+
cls, exc: BaseException, exc_types: tuple[type[Exception], ...] | type[Exception]
175179
) -> bool:
176180
"""requests does not use the "raise ... from ..." syntax, so we need to access the underlying exceptions using
177181
the __context__ attribute.

0 commit comments

Comments
 (0)