forked from p34709852/monkey
Agent: Add handle_island_errors() decorator to http_island_api_client.py
This commit is contained in:
parent
d188b06980
commit
fa9225370e
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue