Agent: Implement send_events in HTTPIslandAPIClient

This commit is contained in:
Ilija Lazoroski 2022-09-20 12:29:29 +02:00
parent 9000a01d1d
commit f39007b0ce
2 changed files with 49 additions and 0 deletions

View File

@ -1,8 +1,10 @@
import functools
import logging
from typing import Sequence
import requests
from common.agent_event_serializers import JSONSerializable
from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT, MEDIUM_REQUEST_TIMEOUT
from . import (
@ -76,3 +78,14 @@ class HTTPIslandAPIClient(IIslandAPIClient):
response.raise_for_status()
return response.content
@handle_island_errors
def send_events(self, events: Sequence[JSONSerializable]):
response = requests.post( # noqa: DUO123
f"{self._api_url}/agent-events",
json=events,
verify=False,
timeout=MEDIUM_REQUEST_TIMEOUT,
)
response.raise_for_status()

View File

@ -17,6 +17,7 @@ PBA_FILE = "dummy.pba"
ISLAND_URI = f"https://{SERVER}/api?action=is-up"
ISLAND_SEND_LOG_URI = f"https://{SERVER}/api/log"
ISLAND_GET_PBA_FILE_URI = f"https://{SERVER}/api/pba/download/{PBA_FILE}"
ISLAND_SEND_EVENTS_URI = f"https://{SERVER}/api/agent-events"
@pytest.mark.parametrize(
@ -118,3 +119,38 @@ def test_island_api_client_get_pba_file__status_code(status_code, expected_error
with pytest.raises(expected_error):
m.get(ISLAND_GET_PBA_FILE_URI, status_code=status_code)
island_api_client.get_pba_file(filename=PBA_FILE)
@pytest.mark.parametrize(
"actual_error, expected_error",
[
(requests.exceptions.ConnectionError, IslandAPIConnectionError),
(TimeoutError, IslandAPITimeoutError),
(Exception, IslandAPIError),
],
)
def test_island_api_client__send_events(actual_error, expected_error):
with requests_mock.Mocker() as m:
m.get(ISLAND_URI)
island_api_client = HTTPIslandAPIClient(SERVER)
with pytest.raises(expected_error):
m.post(ISLAND_SEND_EVENTS_URI, exc=actual_error)
island_api_client.send_events(events="some_data")
@pytest.mark.parametrize(
"status_code, expected_error",
[
(401, IslandAPIRequestError),
(501, IslandAPIRequestFailedError),
],
)
def test_island_api_client_send_events__status_code(status_code, expected_error):
with requests_mock.Mocker() as m:
m.get(ISLAND_URI)
island_api_client = HTTPIslandAPIClient(SERVER)
with pytest.raises(expected_error):
m.post(ISLAND_SEND_EVENTS_URI, status_code=status_code)
island_api_client.send_events(events="some_data")