From bc5f7853ed377a7004ed53317578ec664c700ec9 Mon Sep 17 00:00:00 2001
From: anima
Date: Sat, 21 Sep 2024 20:48:22 +0200
Subject: [PATCH] some optimisations
---
SatiAPI.py | 41 +++++++++++++++++++++++++++++++----------
1 file changed, 31 insertions(+), 10 deletions(-)
diff --git a/SatiAPI.py b/SatiAPI.py
index 21c3ff4..5d79813 100644
--- a/SatiAPI.py
+++ b/SatiAPI.py
@@ -16,7 +16,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class SatiAPI:
"""A API wrapper for Satisfactory dedicated server"""
__AUTHOR__ = 'anima'
- __VERSION__ = '0.4.2'
+ __VERSION__ = '0.4.3'
def __init__(self, host: str = None, port: int = 7777, token: str = None, conffile: str = 'conf.yml', logfile: str = 'SatiAPI.log', loglevel: int = 20) -> None:
"""create a wrapper for satisfactory dedicated server
@@ -142,13 +142,15 @@ class SatiAPI:
if 'token' in self._config['auth'] and self.token is None:
self.token = self._config['auth']['token']
- def __query(self, query: str, data: dict = None) -> dict | bool | None:
+ def __query(self, query: str, data: dict = None, auth: bool = False) -> dict | bool | None:
"""run a query to satisfacoty dedicated server
:param query: name of querytype
:type query: string
:param data: (maybe optional) data to be send as dict
:type data: dict
+ :param auth: set true if authentication requiered
+ :type auth: bool
:return: response from server
:rtype: dict | None | bool
"""
@@ -160,8 +162,17 @@ class SatiAPI:
headers = dict()
headers['Content-Type'] = 'application/json'
- if self.token is not None:
- headers['Authorization'] = f'Bearer {self.token}'
+ if auth:
+ if self.token is None:
+ print(self.token)
+ self.get_token()
+
+ if self.token is None:
+ print(self.token)
+ headers['Authorization'] = f'Bearer {self.token}'
+ else:
+ self._log.error('no data to authencitate')
+ return False
response = requests.post(f'https://{self.host}:{self.port}/api/v1', headers=headers, json=payload, verify=False)
if response.status_code == 200:
@@ -169,6 +180,10 @@ class SatiAPI:
if 'data' in json_response:
return json_response
else:
+ #or if 403
+ # if json_response['errorMessage'] == '':
+ # self._log.debug(f'token expired')
+ # self.get_token()
self._log.error(f'wrong response data {json_response=}')
else:
@@ -192,7 +207,6 @@ class SatiAPI:
"""
if self.token is not None:
self._log.warning('you have already a token')
- return True
if isinstance(password, str):
self._log.debug('password used from parameter')
else:
@@ -216,8 +230,11 @@ class SatiAPI:
data['MinimumPrivilegeLevel'] = privilegeLevel
response = self.__query('PasswordLogin', data)
- self.token = response['authenticationToken']
- return True
+ if response:
+ self.token = response['authenticationToken']
+ return True
+ else:
+ return response
def get_status(self) -> dict:
"""Retrieves the current state of the Dedicated Server. Does not require any input parameters.
@@ -256,7 +273,7 @@ class SatiAPI:
"""
return self.__query('GetAdvancedGameSettings')
-
+
@@ -283,5 +300,9 @@ class SatiAPI:
if __name__ == "__main__":
sati = SatiAPI(loglevel=10)
# print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers'])
- print(sati.get_status())
- # print(sati.get_advanced_game_settings())
\ No newline at end of file
+ # print(sati.get_token())
+ # print(sati.token)
+ # print(sati.get_status())
+ print(sati.claim_server('SDGame01', 'Admin123!'))
+
+ # print(sati.get_status())
\ No newline at end of file