From 1ecb1ea31015493b65a0473481264268586f455f Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum <39048939+jnussbaum@users.noreply.github.com> Date: Tue, 26 Mar 2024 13:53:25 +0100 Subject: [PATCH] fix dsp_client (#78) --- dsp_permissions_scripts/utils/dsp_client.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/dsp_permissions_scripts/utils/dsp_client.py b/dsp_permissions_scripts/utils/dsp_client.py index 4cd0fa64..992b0eb2 100644 --- a/dsp_permissions_scripts/utils/dsp_client.py +++ b/dsp_permissions_scripts/utils/dsp_client.py @@ -30,12 +30,9 @@ class RequestParameters: headers: dict[str, str] | None = None def __post_init__(self) -> None: - self.data_serialized = self._serialize_payload(self.data) - - def _serialize_payload(self, payload: dict[str, Any] | None) -> bytes | None: # If data is not encoded as bytes, issues can occur with non-ASCII characters, # where the content-length of the request will turn out to be different from the actual length. - return json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload else None + self.data_serialized = json.dumps(self.data, ensure_ascii=False).encode("utf-8") if self.data else None def as_kwargs(self) -> dict[str, Any]: return { @@ -228,10 +225,11 @@ def _try_network_action(self, params: RequestParameters) -> Response: self._log_request(params) response = action() except (TimeoutError, ReadTimeout): - self._log_and_sleep(reason="Timeout Error raised", retry_counter=i, exc_info=True) + self._log_and_sleep(reason="TimeoutError/ReadTimeout raised", retry_counter=i, exc_info=True) + continue except (ConnectionError, RequestException): self._renew_session() - self._log_and_sleep(reason="Connection Error raised", retry_counter=i, exc_info=True) + self._log_and_sleep(reason="ConnectionError/RequestException raised", retry_counter=i, exc_info=True) continue self._log_response(response) @@ -267,7 +265,6 @@ def _renew_session(self) -> None: def _log_and_sleep(self, reason: str, retry_counter: int, exc_info: bool) -> None: msg = f"{reason}: Try reconnecting to DSP server, next attempt in {2 ** retry_counter} seconds..." - print(f"{datetime.now()}: {msg}") logger.error(f"{msg} ({retry_counter=:})", exc_info=exc_info) time.sleep(2**retry_counter)