mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Added exception handler if the initial request fails
This commit is contained in:
parent
cf61c3c931
commit
8bba57d950
@ -25,14 +25,20 @@ except ModuleNotFoundError as e:
|
|||||||
|
|
||||||
class ServerJars:
|
class ServerJars:
|
||||||
|
|
||||||
def _get_api_result(self, call_url: str):
|
def __init__(self):
|
||||||
base_url = "https://serverjars.com"
|
self.base_url = "https://serverjars.com"
|
||||||
full_url = "{base}{call_url}".format(base=base_url, call_url=call_url)
|
|
||||||
|
|
||||||
|
def _get_api_result(self, call_url: str):
|
||||||
|
full_url = "{base}{call_url}".format(base=self.base_url, call_url=call_url)
|
||||||
|
|
||||||
|
try:
|
||||||
r = requests.get(full_url, timeout=2)
|
r = requests.get(full_url, timeout=2)
|
||||||
|
|
||||||
if r.status_code not in [200, 201]:
|
if r.status_code not in [200, 201]:
|
||||||
return {}
|
return {}
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Unable to connect to serverjar.com api due to error: {}".format(e))
|
||||||
|
return {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
api_data = json.loads(r.content)
|
api_data = json.loads(r.content)
|
||||||
@ -66,18 +72,21 @@ class ServerJars:
|
|||||||
data = self._read_cache()
|
data = self._read_cache()
|
||||||
return data.get('servers')
|
return data.get('servers')
|
||||||
|
|
||||||
@staticmethod
|
def _check_api_alive(self):
|
||||||
def _check_api_alive():
|
|
||||||
logger.info("Checking serverjars.com API status")
|
logger.info("Checking serverjars.com API status")
|
||||||
|
|
||||||
check_url = "https://serverjars.com/api/fetchTypes"
|
check_url = "{base}/api/fetchTypes".format(base=self.base_url)
|
||||||
|
try:
|
||||||
r = requests.get(check_url, timeout=2)
|
r = requests.get(check_url, timeout=2)
|
||||||
|
|
||||||
if r.status_code in [200, 201]:
|
if r.status_code in [200, 201]:
|
||||||
logger.info("Serverjars.com API is alive")
|
logger.info("Serverjars.com API is alive")
|
||||||
return True
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Unable to connect to serverjar.com api due to error: {}".format(e))
|
||||||
|
return {}
|
||||||
|
|
||||||
logger.error("unable to contact Serverjars.com api")
|
logger.error("unable to contact serverjars.com api")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def refresh_cache(self):
|
def refresh_cache(self):
|
||||||
@ -141,12 +150,11 @@ class ServerJars:
|
|||||||
response = self._get_api_result(url)
|
response = self._get_api_result(url)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@staticmethod
|
def download_jar(self, server, version, path):
|
||||||
def download_jar(server, version, path):
|
fetch_url = "{base}/api/fetchJar/{server}/{version}".format(base=self.base_url, server=server, version=version)
|
||||||
base_url = "https://serverjars.com/api/fetchJar/{server}/{version}".format(server=server, version=version)
|
|
||||||
|
|
||||||
# open a file stream
|
# open a file stream
|
||||||
with requests.get(base_url, timeout=2, stream=True) as r:
|
with requests.get(fetch_url, timeout=2, stream=True) as r:
|
||||||
try:
|
try:
|
||||||
with open(path, 'wb') as output:
|
with open(path, 'wb') as output:
|
||||||
shutil.copyfileobj(r.raw, output)
|
shutil.copyfileobj(r.raw, output)
|
||||||
|
Loading…
Reference in New Issue
Block a user