def __init__(self, host, key, eid): self.eid = eid self.configuration = esperclient.Configuration() self.configuration.host = f"https://{host}-api.esper.cloud/api" self.configuration.api_key['Authorization'] = key self.configuration.api_key_prefix['Authorization'] = 'Bearer' self.app_api_instance = esperclient.ApplicationApi( esperclient.ApiClient(self.configuration)) self.cmd_api_instance = esperclient.CommandsV2Api( esperclient.ApiClient(self.configuration)) self.dev_api_instance = esperclient.DeviceApi( esperclient.ApiClient(self.configuration))
def test_application_upload_delete(): # upload api_instance = esperclient.ApplicationApi( esperclient.ApiClient(configuration)) enterprise = enterprise_id try: api_response = api_instance.upload(enterprise_id, enterprise, app_file) print(api_response) except ApiException as e: print("Exception when calling ApplicationApi->upload: %s\n" % e) assert api_response.application.is_active, "application not successful, app not active" assert len( api_response.application.versions) > 0, "No version present for app" # delete application_id = api_response.application.id try: # Delete an application api_instance.delete_application(application_id, enterprise_id) except ApiException as e: print( "Exception when calling ApplicationApi->delete_application: %s\n" % e)
def getAllApplicationsForHost(config, enterprise_id, maxAttempt=Globals.MAX_RETRY): """ Make a API call to get all Applications belonging to the Enterprise """ try: api_instance = esperclient.ApplicationApi( esperclient.ApiClient(config)) api_response = None for attempt in range(maxAttempt): try: api_response = api_instance.get_all_applications( enterprise_id, limit=Globals.limit, offset=0, is_hidden=False, ) break except Exception as e: if attempt == maxAttempt - 1: ApiToolLog().LogError(e) raise e time.sleep(Globals.RETRY_SLEEP) return api_response except Exception as e: raise Exception( "Exception when calling ApplicationApi->get_all_applications: %s\n" % e)
def validateConfiguration(host, entId, key, prefix="Bearer", maxAttempt=Globals.MAX_RETRY): configuration = esperclient.Configuration() configuration.host = host configuration.api_key["Authorization"] = key configuration.api_key_prefix["Authorization"] = prefix api_instance = esperclient.EnterpriseApi( esperclient.ApiClient(configuration)) enterprise_id = entId try: api_response = None for attempt in range(maxAttempt): try: api_response = api_instance.get_enterprise(enterprise_id) break except Exception as e: if attempt == maxAttempt - 1: ApiToolLog().LogError(e) raise e time.sleep(Globals.RETRY_SLEEP) if hasattr(api_response, "id"): return True except ApiException as e: print("Exception when calling EnterpriseApi->get_enterprise: %s\n" % e) ApiToolLog().LogError(e) return False
def getAllApplications(maxAttempt=Globals.MAX_RETRY): """ Make a API call to get all Applications belonging to the Enterprise """ try: api_instance = esperclient.ApplicationApi( esperclient.ApiClient(Globals.configuration)) api_response = None for attempt in range(maxAttempt): try: api_response = api_instance.get_all_applications( Globals.enterprise_id, limit=Globals.limit, offset=Globals.offset, is_hidden=False, ) break except Exception as e: if attempt == maxAttempt - 1: ApiToolLog().LogError(e) raise e time.sleep(Globals.RETRY_SLEEP) postEventToFrame(wxThread.myEVT_LOG, "---> App API Request Finished") return api_response except ApiException as e: raise Exception( "Exception when calling ApplicationApi->get_all_applications: %s\n" % e)
def getAppVersions(application_id, version_code="", build_number="", maxAttempt=Globals.MAX_RETRY): api_instance = esperclient.ApplicationApi( esperclient.ApiClient(Globals.configuration)) enterprise_id = Globals.enterprise_id for attempt in range(maxAttempt): try: api_response = api_instance.get_app_versions( application_id, enterprise_id, version_code=version_code, build_number=build_number, limit=Globals.limit, offset=Globals.offset, ) return api_response except Exception as e: if attempt == maxAttempt - 1: ApiToolLog().LogError(e) print( "Exception when calling ApplicationApi->get_app_versions: %s\n" % e) raise e time.sleep(1)
def getInstallDevices(version_id, application_id, maxAttempt=Globals.MAX_RETRY): api_instance = esperclient.ApplicationApi( esperclient.ApiClient(Globals.configuration)) enterprise_id = Globals.enterprise_id for attempt in range(maxAttempt): try: # List install devices api_response = api_instance.get_install_devices( version_id, application_id, enterprise_id, limit=Globals.limit, offset=Globals.offset, ) return api_response except ApiException as e: if attempt == maxAttempt - 1: ApiToolLog().LogError(e) print( "Exception when calling ApplicationApi->get_install_devices: %s\n" % e) raise e time.sleep(1)
def test_add_device_in_group(): # create group api_instance = esperclient.DeviceGroupApi( esperclient.ApiClient(configuration)) data = esperclient.DeviceGroup(name='TestBotGroup') # DeviceGroup | try: api_response = api_instance.create_group(enterprise_id, data) print(api_response) except ApiException as e: print("Exception when calling DeviceGroupApi->create_group: %s\n" % e) assert api_response.id is not None, "Id cannot be None" assert api_response.device_count == 0, "Invalid device count" group_id = api_response.id devices = [ '63325b67-1564-472a-9bd8-10728984231a', 'cd2d9e3b-953c-482f-abbc-168910851bad' ] #Add valid uuid data = esperclient.DeviceGroupUpdate( device_ids=devices) # DeviceGroupUpdate try: # Partial update group api_response = api_instance.partial_update_group( group_id, enterprise_id, data) print(api_response) except ApiException as e: print( "Exception when calling DeviceGroupApi->partial_update_group: %s\n" % e) assert api_response.device_count == 2, "Invalid device count"
def get_version_for_app(application_id, enterprise_id): api_instance = esperclient.ApplicationApi(esperclient.ApiClient(configuration)) try: api_response = api_instance.get_app_versions(application_id, enterprise_id) except ApiException as e: print("Exception when calling ApplicationApi->get_app_versions: %s\n" % e) return api_response.results[0].id
def get_group_for_enterprise(enterprise_id): api_instance = esperclient.DeviceGroupApi(esperclient.ApiClient(configuration)) try: api_response = api_instance.get_all_groups(enterprise_id) except ApiException as e: print("Exception when calling DeviceGroupApi->get_all_groups: %s\n" % e) return api_response.results[0].id
def get_enterprise_for_env(): api_instance = esperclient.EnterpriseApi(esperclient.ApiClient(configuration)) try: api_response = api_instance.get_all_enterprises() except ApiException as e: print("Exception when calling EnterpriseApi->get_all_enterprises: %s\n" % e) return api_response.results[0].id
def get_package_id(device_id, package_name): api_instance = esperclient.DeviceApi(esperclient.ApiClient(CONFIGURATION)) response = api_instance.get_app_installs(ENTERPRISE_ID, device_id, package_name=package_name) if response and response.count > 0: print("getting package id for package_name") app_id = response.results[0].application.version.app_version_id if app_id: return response.results[0].application.version.app_version_id return None
def test_device_command_reboot(): api_instance = esperclient.CommandsApi(esperclient.ApiClient(configuration)) command = esperclient.CommandRequest(command='REBOOT') try: api_response = api_instance.run_command(enterprise_id, device_id, command) #print(api_response) except ApiException as e: print("Exception when calling CommandsApi->run_command: %s\n" % e) assert api_response.id is not None, "Id cannot be None" assert api_response.state == 'Command Initiated', "Command could not be initiated"
def test_device_detail(): api_instance = esperclient.DeviceApi(esperclient.ApiClient(configuration)) try: # Fetch device details by ID api_response = api_instance.get_device_by_id(enterprise_id, device_id) # print(api_response) except ApiException as e: print("Exception when calling DeviceApi->get_device_by_id: %s\n" % e) assert api_response.id is not None, "Id cannot be null"
def test_device_app_deploy(): api_instance = esperclient.CommandsApi(esperclient.ApiClient(configuration)) command_args = esperclient.CommandArgs(app_version='2f81b955-4a30-4b1b-a99f-b4016ba24854') # replace with valid uuid command = esperclient.CommandRequest(command='INSTALL', command_args=command_args) try: api_response = api_instance.run_command(enterprise_id, device_id, command) #print(api_response) except ApiException as e: print("Exception when calling CommandsApi->run_command: %s\n" % e) assert api_response.id is not None, "Id cannot be None" assert api_response.state == 'Command Initiated', "Command could not be initiated"
def test_device_command_settings_change(): api_instance = esperclient.CommandsApi(esperclient.ApiClient(configuration)) command_args = esperclient.CommandArgs(brightness_value=100) command = esperclient.CommandRequest(command='SET_BRIGHTNESS_SCALE', command_args=command_args) try: api_response = api_instance.run_command(enterprise_id, device_id, command) # print(api_response) except ApiException as e: print("Exception when calling CommandsApi->run_command: %s\n" % e) assert api_response.id is not None, "Id cannot be None" assert api_response.state == 'Command Initiated', "Command could not be initiated"
def test_device_command_status_get(): api_instance = esperclient.CommandsApi(esperclient.ApiClient(configuration)) command_id = '0fd65c8a-a015-4695-a7b6-6f79c75887df' # replace with valid uuid device_id = 'cd2d9e4b-953c-482f-abbc-168910851bad' # replace with valid uuid try: api_response = api_instance.get_command(command_id, device_id, enterprise_id) #print(api_response) except ApiException as e: print("Exception when calling CommandsApi->get_command: %s\n" % e) assert api_response.id is not None, "Id cannot be None"
def getApplication(application_id): api_instance = esperclient.ApplicationApi( esperclient.ApiClient(Globals.configuration)) enterprise_id = Globals.enterprise_id try: # Get application information api_response = api_instance.get_application(application_id, enterprise_id) return api_response except ApiException as e: print("Exception when calling ApplicationApi->get_application: %s\n" % e)
def setAppStateForAllAppsListed(state, maxAttempt=Globals.MAX_RETRY): api_instance = esperclient.DeviceApi( esperclient.ApiClient(Globals.configuration)) api_response = None for attempt in range(maxAttempt): try: api_response = api_instance.get_all_devices( Globals.enterprise_id, limit=Globals.limit, offset=Globals.offset, ) break except Exception as e: if attempt == maxAttempt - 1: postEventToFrame( wxThread.myEVT_LOG, "---> ERROR: Failed to get devices ids to modify tags and aliases", ) print(e) ApiToolLog().LogError(e) return time.sleep(Globals.RETRY_SLEEP) deviceIdentifers = Globals.frame.gridPanel.getDeviceIdentifersFromGrid() if api_response: tempRes = [] for device in api_response.results: for deviceIds in deviceIdentifers: if (device.device_name in deviceIds or device.hardware_info["serialNumber"] in deviceIds): tempRes.append(device) if tempRes: api_response.results = tempRes threads = [] for device in api_response.results: if (device.device_name in deviceIdentifers or device.hardware_info["serialNumber"] in deviceIdentifers): t = wxThread.GUIThread( Globals.frame, setAllAppsState, args=(Globals.frame, device, state), name="setAllAppsState", ) threads.append(t) t.start() limitActiveThreads(threads) t = wxThread.GUIThread( Globals.frame, waitTillThreadsFinish, args=(tuple(threads), state, -1, 4), name="waitTillThreadsFinish%s" % state, ) t.start()
def test_device_app_list(): api_instance = esperclient.DeviceApi(esperclient.ApiClient(configuration)) #package_name = 'com.android.calculator2' # str | Filter by Package name (optional) #whitelisted = False # str | Whitelist filter (optional) try: # List all device apps api_response = api_instance.get_device_apps(enterprise_id, device_id) #print(api_response) except ApiException as e: print("Exception when calling DeviceApi->get_device_apps: %s\n" % e) assert api_response.count > 0, "No apps present on device"
def test_device_group_detail(): # create an instance of the API class api_instance = esperclient.DeviceGroupApi( esperclient.ApiClient(configuration)) try: # Get device group information api_response = api_instance.get_group_by_id(group_id, enterprise_id) except ApiException as e: print("Exception when calling DeviceGroupApi->get_group_by_id: %s\n" % e) assert api_response.id is not None, "Id cannot be null"
def test_device_status(): api_instance = esperclient.DeviceApi(esperclient.ApiClient(configuration)) latest_event = 1 # float | Flag to get latest event try: # Get latest device event api_response = api_instance.get_device_event(enterprise_id, device_id, latest_event) # print(api_response) except ApiException as e: print("Exception when calling DeviceApi->get_device_event: %s\n" % e) assert api_response.count == 1, "Device not sending status events" assert api_response.results[0].id is not None
def test_app_version_detail(): api_instance = esperclient.ApplicationApi( esperclient.ApiClient(configuration)) try: api_response = api_instance.get_app_version(version_id, application_id, enterprise_id) #print(api_response) except ApiException as e: print("Exception when calling ApplicationApi->get_app_version: %s\n" % e) assert api_response.id is not None, "Id cannot be None"
def test_app_version_delete(): api_instance = esperclient.ApplicationApi( esperclient.ApiClient(configuration)) version_id = '2b233cc3-ec02-4319-8601-43edcd5a3ed7' # replace with valid uui application_id = 'c968e4a2-7e30-49fd-a548-85f11f05e972' # replace with valid uuid try: # Delete app version api_instance.delete_app_version(version_id, application_id, enterprise_id) except ApiException as e: print( "Exception when calling ApplicationApi->delete_app_version: %s\n" % e)
def test_application_list(): api_instance = esperclient.ApplicationApi( esperclient.ApiClient(configuration)) try: api_response = api_instance.get_all_applications(enterprise_id) #print(api_response) except ApiException as e: print( "Exception when calling ApplicationApi->get_all_applications: %s\n" % e) assert api_response.count > 0, "At least one application is needed for testing" assert api_response.results[0].id is not None, "Id cannot be null"
def getAllDevices(groupToUse, maxAttempt=Globals.MAX_RETRY): """ Make a API call to get all Devices belonging to the Enterprise """ if not groupToUse: return None try: api_instance = esperclient.DeviceApi( esperclient.ApiClient(Globals.configuration)) api_response = None if type(groupToUse) == list: for group in groupToUse: for attempt in range(maxAttempt): try: response = api_instance.get_all_devices( Globals.enterprise_id, group=group, limit=Globals.limit, offset=Globals.offset, ) break except Exception as e: if attempt == maxAttempt - 1: ApiToolLog().LogError(e) raise e time.sleep(Globals.RETRY_SLEEP) if not api_response: api_response = response else: api_response.results = api_response.results + response.results else: for attempt in range(maxAttempt): try: api_response = api_instance.get_all_devices( Globals.enterprise_id, group=groupToUse, limit=Globals.limit, offset=Globals.offset, ) break except Exception as e: if attempt == maxAttempt - 1: ApiToolLog().LogError(e) raise e time.sleep(Globals.RETRY_SLEEP) postEventToFrame(wxThread.myEVT_LOG, "---> Device API Request Finished") return api_response except ApiException as e: raise Exception( "Exception when calling DeviceApi->get_all_devices: %s\n" % e)
def test_enterprise_list(): api_instance = esperclient.EnterpriseApi( esperclient.ApiClient(configuration)) # Enterprise list try: # List all enterprises api_response = api_instance.get_all_enterprises() # print(api_response) except ApiException as e: print( "Exception when calling EnterpriseApi->get_all_enterprises: %s\n" % e) assert (api_response.count == 1), "Only one enterprise supported"
def test_list_device_group(): api_instance = esperclient.DeviceGroupApi( esperclient.ApiClient(configuration)) #name = 'name_example' # str | filter by group name (optional) #limit = 20 # int | Number of results to return per page. (optional) (default to 20) #offset = 56 # int | The initial index from which to return the results. (optional) try: api_response = api_instance.get_all_groups(enterprise_id) except ApiException as e: print("Exception when calling DeviceGroupApi->get_all_groups: %s\n" % e) assert api_response.count > 0, "Atleast one device group is needed for testing" assert api_response.results[0].id is not None, "Id cannot be null"
def managed_configuration_all_groups(): # create an instance of the API class api_instance = esperclient.DeviceGroupApi(esperclient.ApiClient(configuration)) try: api_response = api_instance.get_all_groups(enterprise_id, limit=configuration.per_page_limit, offset=configuration.per_page_offset) if len(api_response.results): for group in api_response.results: # add all the devices in this group to global list of devices run_managed_configuration(group.id) #print(allgroups) except ApiException as e: print("Exception when calling DeviceGroupApi->get_all_groups: %s\n" % e)
def run_whitelist_command(device, package_name): api_instance = esperclient.CommandsApi( esperclient.ApiClient(CONFIGURATION)) command_args = esperclient.CommandArgs(package_name=package_name) command = esperclient.CommandRequest(command='ADD_TO_WHITELIST', command_args=command_args) try: api_response = api_instance.run_command(ENTERPRISE_ID, device.id, command) if api_response.id is not None: print('Whitelist command fired successfully on {} for {}'.format( device.device_name, package_name)) except ApiException as api_exception: print("Exception when calling CommandsApi->run_command: %s\n" % api_exception)