1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| from enum import Enum
import requests
import json
from sources import commandConsoleResult
class ServerCommand(Enum):
SAVE_ALL = 0
SAVE_ON = 1
SAVE_OFF = 2
BROADCAST = 3
class ConsoleConnection:
statusCode_success = 204
properties_json: str = ''
websockets_connection = None
"""
Returns headers for all connections
"""
def get_headers(self):
return {
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': 'Bearer {0}'.format(self.get_apikey())
}
"""
Returns API key
"""
def get_apikey(self):
return self.properties_json['apikey']
"""
Returns the contents of ./private/properties.json
"""
def get_properties_json(self):
if len(self.properties_json) == 0:
f = open("../private/properties.json", 'r')
self.properties_json = json.loads(f.read())
f.close()
return self.properties_json
"""
Send a command with args
"""
def send_command(self, command, args):
srv_cmd = self.get_command(self, command)
if not args is None and len(args) > 0:
srv_cmd = '{0} {1}'.format(srv_cmd, args)
url = '{0}/servers/{1}/command'.format(self.get_https_url_server_api_client(), self.get_server_id())
response = requests.post(
url,
headers= self.get_headers(),
json = {
'command': srv_cmd
}
)
# 204 = successful
return response.status_code == self.statusCode_success
"""
Try to send a command.
Returns true if success, else an error.
"""
def try_send_command(self, command, message=None):
is_success = self.send_command(command, message)
if not is_success:
raise TypeError('Cannot execute {0}'.format(command))
return is_success
"""
Send a broadcast command with a message
"""
def send_broadcast(self, message):
return self.try_send_command(ServerCommand.BROADCAST, message)
"""
Returns a websockets connection instance
"""
def get_websockets_connection(self):
from sources.webSocketsConnection import WebSocketsConnection
if self.websockets_connection is None:
self.websockets_connection = WebSocketsConnection()
return self.websockets_connection
"""
Stop websockets connection
"""
def stop_websockets_connection(self):
if not self.websockets_connection is None:
self.websockets_connection.stop_ws_client()
"""
Send a save_all command
"""
def send_save_all(self):
ws_connection = self.get_websockets_connection()
ws_connection.start_ws_client()
# get latest "save_all" line in logs
latest_line_from_server = ws_connection.get_latest_line_logs()
if self.try_send_command(ServerCommand.SAVE_ALL):
# check if console show expected messages
result_method = ws_connection.check_logs_to_last_line(
latest_line_from_server,
commandConsoleResult.get_all_message_save_all()
)
return result_method.get_value()
else:
return False
"""
Returns the command string corresponding to the content of the argument
"""
@staticmethod
def get_command(command):
if not isinstance(command, ServerCommand):
raise TypeError('command must be an instance of ServerCommand Enum')
if command == ServerCommand.BROADCAST:
return 'broadcast'
elif command == ServerCommand.SAVE_OFF:
return 'save-off'
elif command == ServerCommand.SAVE_ALL:
return 'save-all'
else:
return 'save-on'
"""
Returns server ID
"""
def get_server_id(self):
self.properties_json = self.get_properties_json()
if len(self.properties_json['server_id']) == 0:
url = self.get_https_url_server_api_client()
response = requests.get(url, headers= self.get_headers())
if response.status_code == 200:
data = response.json()
self.properties_json['server_id'] = data["data"][0]["attributes"]["identifier"]
else:
return None
return self.properties_json['server_id']
"""
Returns the url for accessing the client APIs
"""
def get_https_url_server_api_client(self):
self.properties_json = self.get_properties_json()
return '{0}/api/client'.format(self.get_https_url_server())
"""
Returns the server url
"""
def get_https_url_server(self):
self.properties_json = self.get_properties_json()
return 'https://{0}'.format(self.properties_json['server_url']) |
Partager