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 requests
@ -9,19 +10,11 @@ from . import IIslandAPIClient, IslandAPIConnectionError, IslandAPIError, Island
logger = logging.getLogger(__name__)
class HTTPIslandAPIClient(IIslandAPIClient):
"""
A client for the Island's HTTP API
"""
def __init__(self, island_server: str):
def handle_island_errors(fn):
@functools.wraps(fn)
def decorated(*args, **kwargs):
try:
requests.get( # noqa: DUO123
f"https://{island_server}/api?action=is-up",
verify=False,
timeout=MEDIUM_REQUEST_TIMEOUT,
)
self._island_server = island_server
return fn(*args, **kwargs)
except requests.exceptions.ConnectionError as err:
raise IslandAPIConnectionError(err)
except TimeoutError as err:
@ -29,6 +22,8 @@ class HTTPIslandAPIClient(IIslandAPIClient):
except Exception as err:
raise IslandAPIError(err)
return decorated
# 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
def send_log(self, data: str):