Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(device): execute api to accept query parameters #101

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions rapyuta_io/clients/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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]]
Expand Down
8 changes: 4 additions & 4 deletions tests/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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):
Expand Down