Refactored code to be more concise

This commit is contained in:
Firq 2024-07-06 00:16:41 +02:00
parent d79a01cfb1
commit 58aea47921
Signed by: Firq
GPG key ID: 3ACC61C8CEC83C20
13 changed files with 64 additions and 46 deletions
dockge_cli/service

View file

@ -32,7 +32,7 @@ class DockgeConnection:
def _init_events(self):
@self._sio.event
def connect():
self.connect()
self.login()
print("Connected!")
@self._sio.event
@ -70,10 +70,11 @@ class DockgeConnection:
"""
Connect to the websocket
"""
# Dockge uses Socket.io for the websockets, so this URI and params are always the same
self._sio.connect(f"https://{self._host}/socket.io/", transports=['websocket'])
self.connect()
self.login()
def connect(self):
def login(self):
"""
Log into dockge using basicauth
Retries 5 times when timeouts occur
@ -84,6 +85,11 @@ class DockgeConnection:
data = None
retry, count = True, 0
if not storage.exists("username"):
raise ValueError("Missing username")
if not storage.exists("password"):
raise ValueError("Missing password")
while retry and count < 5:
try:
data = self._sio.call(
@ -91,7 +97,7 @@ class DockgeConnection:
{
"username": storage.get("username", encoded=True),
"password": storage.get("password", encoded=True),
"token":""
"token": ""
},
timeout=5
)

View file

@ -16,6 +16,19 @@ def fileexists():
with open(_file, 'a', encoding="utf-8"):
os.utime(_file, None)
def exists(key: str) -> bool:
"""
Checks if a given key exists in the storage file
"""
if not _file.exists():
return False
with open(_file, "r", encoding="utf-8") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader)
return key in content
def put(key: str, value: str, encoded=False):
"""
Puts a given value with a given key into the storage file