From d95d0cefbc846af4d0775a99ba2d9356377cdf06 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 27 Oct 2019 14:42:26 +0100 Subject: [PATCH] Start of the api connection --- api.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gui.py | 0 2 files changed, 100 insertions(+) create mode 100644 api.py create mode 100644 gui.py diff --git a/api.py b/api.py new file mode 100644 index 0000000..c695ff6 --- /dev/null +++ b/api.py @@ -0,0 +1,100 @@ +import requests +import json + + +class API: + def __init__(self, url: str, username: str, password: str, port: int = 5000): + self.url = url + self.username = username + self.password = password + self.port = port + self.token = None + self._auth() + + def _auth(self): + try: + resp = requests.post(self.url+"/auth", json={"username": self.username, "password": self.password}) + except (json.JSONDecodeError, KeyError): + raise Exception("InvalidLogin") + except requests.exceptions.ConnectionError: + raise Exception("Connection Error") + else: + self.token = {"Authorization": f"jwt {resp.json()['access_token']}"} + + def _request(self, args: str, body=None, method: str = "get"): + if body is None: + body = dict() + + if method == "get": + resp = requests.get(self.url+args, headers=self.token, json=body) + elif method == "put": + resp = requests.put(self.url + args, headers=self.token, json=body) + else: + raise Exception("Invalid method") + + if resp.status_code == 401: + self._auth() + self._request(args, body, method) + else: + return resp + + def code_400(self, text: str): + if "not running" in text: + msg = "Server is not running" + elif "running" in text: + msg = "Server is running" + elif "respond" in text: + msg = "Server did not respond" + else: + msg = text + raise Exception(msg) + + def status(self): + resp = self._request("/") + if resp.status_code == 400 or "offline" in resp.text: + return False + else: + ps = resp.text.find("has") + pe = resp.text.find("players", ps) + p = int(resp.text[ps+3:pe]) + ls = resp.text.find("in", pe) + le = resp.text.find("ms", ls) + lt = float(resp.text[ls+2:le]) + pls = resp.text.find("players: ", le) + pl = resp.text[pls+9:].split(", ") + return p, lt, pl + + def start(self): + resp = self._request("/start") + if resp.status_code == 400: + self.code_400(resp.text) + else: + return True + + def stop(self): + resp = self._request("/stop") + if resp.status_code == 400: + self.code_400(resp.text) + else: + return resp.text + + def kill(self): + resp = self._request("/kill") + if resp.status_code == 400: + self.code_400(resp.text) + else: + return True + + def cmd(self, cmd): + resp = self._request("/cmd/"+cmd) + if resp.status_code == 400: + self.code_400(resp.text) + else: + return resp.text + + def logs(self): + resp = self._request("/logs") + if resp.status_code == 400: + self.code_400(resp.text) + else: + return resp.text diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..e69de29