some optimisations
This commit is contained in:
33
SatiAPI.py
33
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:
|
||||
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)
|
||||
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.
|
||||
@@ -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())
|
||||
# print(sati.get_token())
|
||||
# print(sati.token)
|
||||
# print(sati.get_status())
|
||||
print(sati.claim_server('SDGame01', 'Admin123!'))
|
||||
|
||||
# print(sati.get_status())
|
||||
Reference in New Issue
Block a user