From 928147d7d638f9047575a68d79dc649d8bbd0d22 Mon Sep 17 00:00:00 2001 From: KUMAR SHIKHAR Date: Wed, 11 Dec 2024 07:46:54 +0530 Subject: [PATCH] fix(device): update execute to accept query parameters --- rapyuta_io/clients/device.py | 21 +++++++++++---------- tests/device_test.py | 8 ++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/rapyuta_io/clients/device.py b/rapyuta_io/clients/device.py index e6f2fcb1..a008f135 100644 --- a/rapyuta_io/clients/device.py +++ b/rapyuta_io/clients/device.py @@ -306,10 +306,10 @@ def validate(name, runtime, runtime_docker, runtime_preinstalled, ros_distro, ro if description is not None and not isinstance(description, six.string_types): raise InvalidParameterException('description must be of type string') - def _execute_api(self, url, request_method=HttpMethod.GET, payload=None, retry_limit=0): + def _execute_api(self, url, request_method=HttpMethod.GET, payload=None, retry_limit=0, query=None): headers = create_auth_header(self._auth_token, self._project) headers['Content-Type'] = 'application/json' - rest_client = RestClient(url).method(request_method).headers(headers) + rest_client = RestClient(url).method(request_method).headers(headers).query_param(query) response = rest_client.retry(retry_limit).execute(payload=payload) return response @@ -575,13 +575,14 @@ def execute_command(self, command, retry_limit=0): raise ValueError("Job ID not found in the response") return self.fetch_command_result(jid, [self.uuid], timeout=command.timeout) - def fetch_command_result(self, jid: str, deviceids: list, timeout: int): + def fetch_command_result(self, jid: str, deviceids: list, timeout: int, interval: int = 10): """ Fetch the result of the command execution using the job ID (jid) and the first device ID from the list. Args: - jobid (str): The job ID of the executed command. + jid (str): The job ID of the executed command. deviceids (list): A list of device IDs on which the command was executed. timeout (int): The maximum time to wait for the result (in seconds). Default is 300 seconds. + interval (int): time interval for retry Returns: dict: The result of the command execution. Raises: @@ -591,15 +592,15 @@ def fetch_command_result(self, jid: str, deviceids: list, timeout: int): if not deviceids or not isinstance(deviceids, list): raise ValueError("Device IDs must be provided as a non-empty list.") - url = self._device_api_host + DEVICE_COMMAND_API_PATH + "jobid" - payload = { + url = self._device_api_host + DEVICE_COMMAND_API_PATH + jid + query = { "jid": jid, "device_id": deviceids[0] } - total_time_waited = 0 - wait_interval = 10 - while total_time_waited < timeout: - response = self._execute_api(url, HttpMethod.POST, payload) + time_elapsed = 0 + wait_interval = interval + while time_elapsed < timeout: + response = self._execute_api(url, HttpMethod.GET, query=query) if response.status_code == requests.codes.OK: result = get_api_response_data(response) return result[deviceids[0]] diff --git a/tests/device_test.py b/tests/device_test.py index bdc96b97..7fb7f5bd 100644 --- a/tests/device_test.py +++ b/tests/device_test.py @@ -733,7 +733,7 @@ def test_onboard_script_dockercompose_success(self, mock_request): temp_header['Content-Type'] = 'application/json' mock_request.assert_called_once_with( - url=expected_onboard_script_url, method='GET', headers=temp_header, params={}, json=None) + url=expected_onboard_script_url, method='GET', headers=temp_header, params=None, json=None) self.assertEqual(onboard_script.url, 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/start') self.assertEqual(onboard_script.command, 'sudo bash start -r dockercompose -d melodic -b test/path') self.assertEqual(onboard_script.token, 'sample-token') @@ -762,7 +762,7 @@ def test_onboard_script_preinstalled_success(self, mock_request): temp_header['Content-Type'] = 'application/json' mock_request.assert_called_once_with( - url=expected_onboard_script_url, method='GET', headers=temp_header, params={}, json=None) + url=expected_onboard_script_url, method='GET', headers=temp_header, params=None, json=None) self.assertEqual(onboard_script.url, 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/start') self.assertEqual(onboard_script.command, 'sudo bash start -r preinstalled -w test/path') self.assertEqual(onboard_script.token, 'sample-token') @@ -792,7 +792,7 @@ def test_onboard_script_both_runtimes_success(self, mock_request): temp_header['Content-Type'] = 'application/json' mock_request.assert_called_once_with( - url=expected_onboard_script_url, method='GET', headers=temp_header, params={}, json=None) + url=expected_onboard_script_url, method='GET', headers=temp_header, params=None, json=None) self.assertEqual(onboard_script.url, 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/start') self.assertEqual(onboard_script.command, 'sudo bash start -r dockercompose -d melodic -b test/path -r ' 'preinstalled') @@ -869,7 +869,7 @@ def test_upgrade_device_dockercompose_success(self, mock_request): temp_header['Content-Type'] = 'application/json' mock_request.assert_called_once_with( - url=expected_upgrade_device_url, method='PUT', headers=temp_header, params={}, json=None) + url=expected_upgrade_device_url, method='PUT', headers=temp_header, params=None, json=None) @patch('requests.request') def test_upgrade_device_not_found(self, mock_request):