add rename server

This commit is contained in:
2024-09-21 22:39:28 +02:00
parent b38f196528
commit 7d35dd8ae9

View File

@@ -16,7 +16,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class SatiAPI: class SatiAPI:
"""A API wrapper for Satisfactory dedicated server""" """A API wrapper for Satisfactory dedicated server"""
__AUTHOR__ = 'anima' __AUTHOR__ = 'anima'
__VERSION__ = '0.9.0' __VERSION__ = '0.10.1'
def __init__(self, host: str = None, port: int = 7777, token: str = None, conffile: str = 'conf.yml', logfile: str = 'SatiAPI.log', loglevel: int = 20) -> None: 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 """create a wrapper for satisfactory dedicated server
@@ -197,6 +197,10 @@ class SatiAPI:
# self.get_token() # self.get_token()
self._log.error(f'wrong response data {json_response=}') self._log.error(f'wrong response data {json_response=}')
elif response.status_code == 204:
# response from rename server
return True
else: else:
self._log.error(f'non successfull response {response.content} [{response.status_code}]') self._log.error(f'non successfull response {response.content} [{response.status_code}]')
self._log.debug(f'{response.request.url=}') self._log.debug(f'{response.request.url=}')
@@ -263,6 +267,36 @@ class SatiAPI:
self._log.error('server can not be clamed because no authentication possable') self._log.error('server can not be clamed because no authentication possable')
return False return False
def get_token(self, password: str = None, privilegeLevel: str = 'Administrator') -> bool:
"""get token from satisfacory dedicated server, password is needed!
Attempts to log in to the Dedicated Server as a player using either Admin Password or Client Protection Password. This function requires no Authentication.
:param password: password of admin level
:type password: string
:param privilegeLevel: level of authentication
:type privilegeLevel: string
:return: true if successfull get a token
:rtype: bool
"""
if self.token is not None:
self._log.warning('you have already a token')
if isinstance(password, str):
self._log.debug('password used from parameter')
else:
password = self._get_config_password()
if password is not None:
self._log.debug('password used from conf file')
else:
self._log.error(f'no password for auth given!')
return False
data = dict()
data['password'] = password
data['MinimumPrivilegeLevel'] = privilegeLevel
response = self.__query('PasswordLogin', data)
return self._save_token(response)
def set_admin_password(self, password: str = None): def set_admin_password(self, password: str = None):
"""Updates the currently set Admin Password. This will invalidate all previously issued Client and Admin authentication tokens. Requires Admin privileges. Function does not return any data on success. """Updates the currently set Admin Password. This will invalidate all previously issued Client and Admin authentication tokens. Requires Admin privileges. Function does not return any data on success.
@@ -303,36 +337,24 @@ class SatiAPI:
response = self.__query('SetClientPassword', data, True) response = self.__query('SetClientPassword', data, True)
return True return True
def get_token(self, password: str = None, privilegeLevel: str = 'Administrator') -> bool: def set_servername(self, servername: str) -> bool:
"""get token from satisfacory dedicated server, password is needed! """Renames the Dedicated Server once it has been claimed. Requires Admin privileges. Function does not return any data on success.
Attempts to log in to the Dedicated Server as a player using either Admin Password or Client Protection Password. This function requires no Authentication.
:param password: password of admin level :param servername: New name of the Dedicated Server
:type password: string :type servername: string
:param privilegeLevel: level of authentication :return: true if set new name successfull
:type privilegeLevel: string
:return: true if successfull get a token
:rtype: bool :rtype: bool
""" """
if self.token is not None: if isinstance(servername, str):
self._log.warning('you have already a token') # TODO: add check if contain non allowed characters
if isinstance(password, str): data = dict()
self._log.debug('password used from parameter') data['ServerName'] = servername
response = self.__query('RenameServer', data, True)
return response
else: else:
password = self._get_config_password() self._log.error(f'{servername=} is not a valid name')
if password is not None: return False
self._log.debug('password used from conf file')
else:
self._log.error(f'no password for auth given!')
return False
data = dict()
data['password'] = password
data['MinimumPrivilegeLevel'] = privilegeLevel
response = self.__query('PasswordLogin', data)
return self._save_token(response)
def get_status(self) -> dict: def get_status(self) -> dict:
"""Retrieves the current state of the Dedicated Server. Does not require any input parameters. """Retrieves the current state of the Dedicated Server. Does not require any input parameters.
@@ -374,25 +396,26 @@ class SatiAPI:
## todo ## todo
#RenameServer #RunCommand
#SetClientPassword #Shutdown
#SetAutoLoadSessionName #SetAutoLoadSessionName
#ApplyServerOptions #ApplyServerOptions
#ApplyAdvancedGameSettings #ApplyAdvancedGameSettings
#RunCommand
#Shutdown
#CreateNewGame #CreateNewGame
#SaveGame #SaveGame
#LoadGame
#DownloadSaveGame
#UploadSaveGame
#DeleteSaveFile #DeleteSaveFile
#DeleteSaveSession #DeleteSaveSession
#EnumerateSessions #EnumerateSessions
#LoadGame
#UploadSaveGame
#DownloadSaveGame
if __name__ == "__main__": if __name__ == "__main__":
sati = SatiAPI(loglevel=10) sati = SatiAPI(loglevel=10)
# print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers']) # print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers'])
print(sati.get_status()) print(sati.get_status())
print(sati.token) print(sati.set_servername('OtherName'))
print(sati.set_client_password())