Skip to content

Commit 67c0ba7

Browse files
committed
Split _send_request from resp error handling
1 parent c653573 commit 67c0ba7

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

brainframe/api/stubs/base_stub.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,15 @@ def _send_authorized(self, request: requests.Request, timeout) \
285285
def _send_no_auth(self, request: requests.Request, timeout) \
286286
-> requests.Response:
287287
"""Sends the given request with no authorization."""
288-
resp = self._send_request(request, timeout)
288+
resp = self._perform_request(request, timeout)
289289
return resp
290290

291291
def _send_with_credentials(self, request: requests.Request, timeout) \
292292
-> requests.Response:
293293
"""Sends the given request with HTTP Basic Authorization."""
294294

295295
request.auth = self._credentials
296-
resp = self._send_request(request, timeout)
296+
resp = self._perform_request(request, timeout)
297297

298298
if "session_id" in resp.cookies:
299299
# Update the session ID if we don't already have one
@@ -306,7 +306,7 @@ def _send_with_session_id(self, request: requests.Request, timeout) \
306306
"""Sends the given request with the session ID."""
307307
request.cookies = {"session_id": self._session_id}
308308
try:
309-
resp = self._send_request(request, timeout)
309+
resp = self._perform_request(request, timeout)
310310
except bf_errors.InvalidSessionError:
311311
# The session likely expired. Try again with the username and
312312
# password to fetch a new session
@@ -316,19 +316,13 @@ def _send_with_session_id(self, request: requests.Request, timeout) \
316316
return resp
317317

318318
@staticmethod
319-
def _send_request(request: requests.Request, timeout: int) \
320-
-> requests.Response:
321-
"""Sends a request to the server. This method is mocked out in unit
322-
tests.
319+
def _perform_request(*args, **kwargs):
320+
"""Sends a request and handles any errors in the result
323321
324-
:param request: The request to send
325-
:return: The response data
322+
All arguments are passed to BaseStub._send_request
326323
"""
327-
prepared = request.prepare()
328-
329-
session = requests.Session()
330324
try:
331-
resp = session.send(prepared, stream=True, timeout=timeout)
325+
resp = BaseStub._send_request(*args, **kwargs)
332326
except requests.exceptions.RequestException as exc:
333327
raise _make_api_error(exception=exc)
334328

@@ -337,6 +331,18 @@ def _send_request(request: requests.Request, timeout: int) \
337331

338332
return resp
339333

334+
@staticmethod
335+
def _send_request(request: requests.Request, timeout: int) \
336+
-> requests.Response:
337+
"""Sends a request to the server. This method is mocked out in unit
338+
tests.
339+
340+
:param request: The request to send
341+
:return: The response data
342+
"""
343+
prepared = request.prepare()
344+
return requests.Session().send(prepared, stream=True, timeout=timeout)
345+
340346

341347
@typing.overload
342348
def _make_api_error(exception: Exception) -> bf_errors.BaseAPIError:

0 commit comments

Comments
 (0)