KlippyRest/Websocket simplify methods into comprehensions

This commit is contained in:
alfrix 2024-03-09 06:32:38 -03:00
parent 292dda7416
commit 7cd6107998
2 changed files with 6 additions and 15 deletions

View File

@ -12,19 +12,14 @@ class KlippyRest:
@property
def endpoint(self):
protocol = "http"
if int(self.port) in {443, 7130}:
protocol = "https"
return f"{protocol}://{self.ip}:{self.port}"
return f"{'https' if int(self.port) in {443, 7130} else 'http'}://{self.ip}:{self.port}"
def get_server_info(self):
return self.send_request("server/info")
def get_oneshot_token(self):
r = self.send_request("access/oneshot_token")
if r is False or 'result' not in r:
return False
return r['result']
res = self.send_request("access/oneshot_token")
return res['result'] if 'result' in res else False
def get_printer_info(self):
return self.send_request("printer/info")

View File

@ -38,9 +38,7 @@ class KlippyWebsocket(threading.Thread):
@property
def ws_proto(self):
if int(self.port) in {443, 7130}:
return "wss"
return "ws"
return "wss" if int(self.port) in {443, 7130} else "ws"
def retry(self):
self.reconnect_count = 0
@ -78,8 +76,7 @@ class KlippyWebsocket(threading.Thread):
return True
token = self._screen.apiclient.get_oneshot_token()
except Exception as e:
logging.critical(e, exc_info=True)
logging.debug("Unable to get oneshot token")
logging.debug(f"Unable to get oneshot token {e}")
return True
self.ws_url = f"{self.ws_proto}://{self._url}/websocket?token={token}"
@ -92,8 +89,7 @@ class KlippyWebsocket(threading.Thread):
logging.debug("Starting websocket thread")
self._wst.start()
except Exception as e:
logging.critical(e, exc_info=True)
logging.debug("Error starting web socket")
logging.debug(f"Error starting web socket {e}")
return True
return False