# Rent a device

# Checking available devices

Make an API request to check the devices that are available for rent.

def check_avaible_devices():
    response = requests.get(url=f'{OSAI_URL}/Devices/AvailableToRent', headers=HEADERS)
    return response.json()

# Checking a particular device

A custom function to check if a device of the given type and with the given hardware is available.

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

# Device rental

Rent a device by making an API request to the specified endpoint, providing the necessary information such as the device name, type, hardware IDs, and optionally a webhook URL for receiving notifications related to the rented device.

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()

# Notification on your own site

Data that you may receive when a notification is sent to your own website.

Webhook
{
  "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
}

# Example

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()
----------------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
        }
    ]
}