Agent: Add handle_island_errors() decorator to http_island_api_client.py

This commit is contained in:
Shreya Malviya 2022-09-19 18:37:31 +05:30 committed by Mike Salvatore
parent d188b06980
commit fa9225370e
1 changed files with 7 additions and 12 deletions

View File

@ -1,3 +1,4 @@
import functools
import logging import logging
import requests import requests
@ -9,19 +10,11 @@ from . import IIslandAPIClient, IslandAPIConnectionError, IslandAPIError, Island
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class HTTPIslandAPIClient(IIslandAPIClient): def handle_island_errors(fn):
""" @functools.wraps(fn)
A client for the Island's HTTP API def decorated(*args, **kwargs):
"""
def __init__(self, island_server: str):
try: try:
requests.get( # noqa: DUO123 return fn(*args, **kwargs)
f"https://{island_server}/api?action=is-up",
verify=False,
timeout=MEDIUM_REQUEST_TIMEOUT,
)
self._island_server = island_server
except requests.exceptions.ConnectionError as err: except requests.exceptions.ConnectionError as err:
raise IslandAPIConnectionError(err) raise IslandAPIConnectionError(err)
except TimeoutError as err: except TimeoutError as err:
@ -29,6 +22,8 @@ class HTTPIslandAPIClient(IIslandAPIClient):
except Exception as err: except Exception as err:
raise IslandAPIError(err) raise IslandAPIError(err)
return decorated
# TODO: set server address as object property when init is called in find_server and pass # TODO: set server address as object property when init is called in find_server and pass
# object around? won't need to pass island server and create object in every function # object around? won't need to pass island server and create object in every function
def send_log(self, data: str): def send_log(self, data: str):