Skip to content

Commit 928147d

Browse files
fix(device): update execute to accept query parameters
1 parent db34d48 commit 928147d

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

rapyuta_io/clients/device.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ def validate(name, runtime, runtime_docker, runtime_preinstalled, ros_distro, ro
306306
if description is not None and not isinstance(description, six.string_types):
307307
raise InvalidParameterException('description must be of type string')
308308

309-
def _execute_api(self, url, request_method=HttpMethod.GET, payload=None, retry_limit=0):
309+
def _execute_api(self, url, request_method=HttpMethod.GET, payload=None, retry_limit=0, query=None):
310310
headers = create_auth_header(self._auth_token, self._project)
311311
headers['Content-Type'] = 'application/json'
312-
rest_client = RestClient(url).method(request_method).headers(headers)
312+
rest_client = RestClient(url).method(request_method).headers(headers).query_param(query)
313313
response = rest_client.retry(retry_limit).execute(payload=payload)
314314
return response
315315

@@ -575,13 +575,14 @@ def execute_command(self, command, retry_limit=0):
575575
raise ValueError("Job ID not found in the response")
576576
return self.fetch_command_result(jid, [self.uuid], timeout=command.timeout)
577577

578-
def fetch_command_result(self, jid: str, deviceids: list, timeout: int):
578+
def fetch_command_result(self, jid: str, deviceids: list, timeout: int, interval: int = 10):
579579
"""
580580
Fetch the result of the command execution using the job ID (jid) and the first device ID from the list.
581581
Args:
582-
jobid (str): The job ID of the executed command.
582+
jid (str): The job ID of the executed command.
583583
deviceids (list): A list of device IDs on which the command was executed.
584584
timeout (int): The maximum time to wait for the result (in seconds). Default is 300 seconds.
585+
interval (int): time interval for retry
585586
Returns:
586587
dict: The result of the command execution.
587588
Raises:
@@ -591,15 +592,15 @@ def fetch_command_result(self, jid: str, deviceids: list, timeout: int):
591592

592593
if not deviceids or not isinstance(deviceids, list):
593594
raise ValueError("Device IDs must be provided as a non-empty list.")
594-
url = self._device_api_host + DEVICE_COMMAND_API_PATH + "jobid"
595-
payload = {
595+
url = self._device_api_host + DEVICE_COMMAND_API_PATH + jid
596+
query = {
596597
"jid": jid,
597598
"device_id": deviceids[0]
598599
}
599-
total_time_waited = 0
600-
wait_interval = 10
601-
while total_time_waited < timeout:
602-
response = self._execute_api(url, HttpMethod.POST, payload)
600+
time_elapsed = 0
601+
wait_interval = interval
602+
while time_elapsed < timeout:
603+
response = self._execute_api(url, HttpMethod.GET, query=query)
603604
if response.status_code == requests.codes.OK:
604605
result = get_api_response_data(response)
605606
return result[deviceids[0]]

tests/device_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def test_onboard_script_dockercompose_success(self, mock_request):
733733
temp_header['Content-Type'] = 'application/json'
734734

735735
mock_request.assert_called_once_with(
736-
url=expected_onboard_script_url, method='GET', headers=temp_header, params={}, json=None)
736+
url=expected_onboard_script_url, method='GET', headers=temp_header, params=None, json=None)
737737
self.assertEqual(onboard_script.url, 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/start')
738738
self.assertEqual(onboard_script.command, 'sudo bash start -r dockercompose -d melodic -b test/path')
739739
self.assertEqual(onboard_script.token, 'sample-token')
@@ -762,7 +762,7 @@ def test_onboard_script_preinstalled_success(self, mock_request):
762762
temp_header['Content-Type'] = 'application/json'
763763

764764
mock_request.assert_called_once_with(
765-
url=expected_onboard_script_url, method='GET', headers=temp_header, params={}, json=None)
765+
url=expected_onboard_script_url, method='GET', headers=temp_header, params=None, json=None)
766766
self.assertEqual(onboard_script.url, 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/start')
767767
self.assertEqual(onboard_script.command, 'sudo bash start -r preinstalled -w test/path')
768768
self.assertEqual(onboard_script.token, 'sample-token')
@@ -792,7 +792,7 @@ def test_onboard_script_both_runtimes_success(self, mock_request):
792792
temp_header['Content-Type'] = 'application/json'
793793

794794
mock_request.assert_called_once_with(
795-
url=expected_onboard_script_url, method='GET', headers=temp_header, params={}, json=None)
795+
url=expected_onboard_script_url, method='GET', headers=temp_header, params=None, json=None)
796796
self.assertEqual(onboard_script.url, 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io/start')
797797
self.assertEqual(onboard_script.command, 'sudo bash start -r dockercompose -d melodic -b test/path -r '
798798
'preinstalled')
@@ -869,7 +869,7 @@ def test_upgrade_device_dockercompose_success(self, mock_request):
869869
temp_header['Content-Type'] = 'application/json'
870870

871871
mock_request.assert_called_once_with(
872-
url=expected_upgrade_device_url, method='PUT', headers=temp_header, params={}, json=None)
872+
url=expected_upgrade_device_url, method='PUT', headers=temp_header, params=None, json=None)
873873

874874
@patch('requests.request')
875875
def test_upgrade_device_not_found(self, mock_request):

0 commit comments

Comments
 (0)