From ee501f2594f54481f961d59b673c53c7a3435184 Mon Sep 17 00:00:00 2001 From: alfrix Date: Sat, 9 Mar 2024 08:07:10 -0300 Subject: [PATCH] rest: cleanup requests increase timeout to slightly larger than a multiple of 3, which is the default TCP packet retransmission window. --- ks_includes/KlippyRest.py | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/ks_includes/KlippyRest.py b/ks_includes/KlippyRest.py index 3513bd14..2a3e4c5b 100644 --- a/ks_includes/KlippyRest.py +++ b/ks_includes/KlippyRest.py @@ -32,39 +32,23 @@ class KlippyRest: def _do_request(self, method, request_method, data=None, json=None, json_response=True, timeout=3): url = f"{self.endpoint}/{method}" - headers = {} if self.api_key is False else {"x-api-key": self.api_key} - response_data = False + headers = {"x-api-key": self.api_key} if self.api_key else {} + logging.debug(f"Sending {request_method} to {url}") try: callee = getattr(requests, request_method) response = callee(url, json=json, data=data, headers=headers, timeout=timeout) response.raise_for_status() - if json_response: - logging.debug(f"Sending request to {url}") - response_data = response.json() - else: - response_data = response.content - except requests.exceptions.HTTPError as h: - self.status = self.format_status(h) - except requests.exceptions.ConnectionError as c: - self.status = self.format_status(c) - except requests.exceptions.Timeout as t: - self.status = self.format_status(t) - except requests.exceptions.JSONDecodeError as j: - self.status = self.format_status(j) - except requests.exceptions.RequestException as r: - self.status = self.format_status(r) + self.status = '' + return response.json() if json_response else response.content except Exception as e: self.status = self.format_status(e) - if response_data: - self.status = '' - else: logging.error(self.status.replace('\n', '>>')) - return response_data + return False def post_request(self, method, data=None, json=None, json_response=True): return self._do_request(method, "post", data, json, json_response) - def send_request(self, method, json=True, timeout=3): + def send_request(self, method, json=True, timeout=4): return self._do_request(method, "get", json_response=json, timeout=timeout) @staticmethod