Wypożyczanie urządzenia
Sprawdzanie dostępnych urządzeń
Wykonaj żądanie API, aby sprawdzić urządzenia dostępne do wypożyczenia.
Python
def check_avaible_devices():
response = requests.get(url=f'{OSAI_URL}/Devices/AvailableToRent', headers=HEADERS)
return response.json()
Sprawdzanie określonego urządzenia
Niestandardowa funkcja sprawdzająca, czy dostępne jest urządzenie danego typu i z danym sprzętem.
Python
def check_if_device_with_exact_hardware_exist(available_devices_list, wanted_type_id, wanted_hardware_ids):
for device in available_devices_list:
if device['id'] == wanted_type_id:
existing_hardware_ids = [hardware['id'] for hardware in device['hardware']]
if all((wanted_hardware_id in existing_hardware_ids) for wanted_hardware_id in wanted_hardware_ids):
print('Wanted device exist.')
return True
print('Device with the given parameters does not exist.')
return False
Wypożyczanie urządzenia
Wypożyczenie urządzenia polega na wysłaniu żądania API do określonego punktu końcowego, podając niezbędne informacje, takie jak nazwa urządzenia, typ, identyfikatory sprzętu i opcjonalnie adres URL webhook do otrzymywania powiadomień związanych z wypożyczonym urządzeniem.
Python
def rent_device(name, device_type_id, hardware_ids_list, webhook_url=None):
request = {
'name': name,
'deviceTypeId': device_type_id,
'hardwareIds': hardware_ids_list,
'webhookUrl': webhook_url
}
response = requests.post(url=f'{OSAI_URL}/Devices/Rent', headers=HEADERS, json=request)
return response.json()
Powiadomienie na własnej stronie internetowej
Dane, które użytkownik może otrzymać, gdy powiadomienie zostanie wysłane na jego własną stronę internetową.
Webhook example response
{
"object_id": 1092,
"object_name": "My Rented Jetson",
"task_type": 5,
"creation_time": "2023-06-21T08:55:48.210464Z",
"start_time": "2023-06-21T08:55:52.4821962+00:00",
"end_time": null,
"task_status": 100
}
Przykład
Python
import requests
import json
from enum import IntEnum
OSAI_URL = 'https://app.dev.onestepai.com/api'
API_TOKEN = 'Your Access Token'
HEADERS = {'Authorization': None}
class DeviceTypeEnum(IntEnum):
JETSON_NANO = 1
RASPBERRY_PI = 2
VIRTUAL_MACHINE = 3
class HardwareEnum(IntEnum):
INTEL_XEON_GOLD = 1
TESLA_V100 = 2
NVIDIA_MAXWELL = 3
Cortex_A72 = 4
GOOGLE_CORAL = 5
INTEL_MOVIDIUS = 6
ONNX = 7
def main():
token = sign_in()
global HEADERS
HEADERS = {'Authorization': f'Bearer {token.text}'}
print('----------------Check Avaible Devices--------------------')
avaible_devices = check_avaible_devices()
print_formatted_json(avaible_devices)
print('----------------Check Specyfic Devices--------------------')
wanted_device_type = DeviceTypeEnum.JETSON_NANO
wanted_hardware = [HardwareEnum.NVIDIA_MAXWELL, HardwareEnum.GOOGLE_CORAL]
if check_if_device_with_exact_hardware_exist(avaible_devices, wanted_device_type, wanted_hardware):
print('----------------Rent Device---------------------')
device_name = 'My Rented Jetson'
webhook_url = 'Your Webhook Url'
response = rent_device(device_name, wanted_device_type, wanted_hardware, webhook_url)
print_formatted_json(response)
def sign_in():
response = requests.post(
url=f'{OSAI_URL}/Users/SignIn', json=API_TOKEN)
return response
def check_avaible_devices():
response = requests.get(url=f'{OSAI_URL}/Devices/AvailableToRent', headers=HEADERS)
return response.json()
def check_if_device_with_exact_hardware_exist(available_devices_list, wanted_type_id, wanted_hardware_ids):
for device in available_devices_list:
if device['id'] == wanted_type_id:
existing_hardware_ids = [hardware['id'] for hardware in device['hardware']]
if all((wanted_hardware_id in existing_hardware_ids) for wanted_hardware_id in wanted_hardware_ids):
print('Wanted device exist.')
return True
print('Device with the given parameters does not exist.')
return False
def rent_device(name, device_type_id, hardware_ids_list, webhook_url=None):
request = {
'name': name,
'deviceTypeId': device_type_id,
'hardwareIds': hardware_ids_list,
'webhookUrl': webhook_url
}
response = requests.post(url=f'{OSAI_URL}/Devices/Rent', headers=HEADERS, json=request)
return response.json()
def print_formatted_json(text):
print(json.dumps(text, indent=4))
if __name__ == '__main__':
main()
Example output
----------------Check Avaible Devices--------------------
[
{
"id": 1,
"name": "Jetson Nano",
"deviceTypeId": 1,
"hardware": [
{
"id": 3,
"name": "NVIDIA Maxwell",
"price": 5
},
{
"id": 5,
"name": "Google Coral",
"price": 5
}
]
}
]
----------------Check Specyfic Devices--------------------
Wanted device exist.
----------------Rent Device---------------------
{
"id": 1095,
"name": "My Rented Jetson",
"deviceTypeId": 1,
"url": null,
"rented": true,
"status": 2,
"creationTime": "2023-06-21T10:03:42.7255335Z",
"startTime": "2023-06-21T10:03:42.725909Z",
"endTime": null,
"hardware": [
{
"id": 3,
"name": "NVIDIA Maxwell",
"price": 5
},
{
"id": 5,
"name": "Google Coral",
"price": 5
}
]
}