def postEsperCommand(command_data, useV0=True):
    json_resp = None
    resp = None
    try:
        headers = getHeader()
        url = ""
        if useV0:
            url = "https://%s-api.esper.cloud/api/v0/enterprise/%s/command/" % (
                Globals.configuration.host.split("-api")[0].replace(
                    "https://", ""),
                Globals.enterprise_id,
            )
        else:
            url = "https://%s-api.esper.cloud/api/enterprise/%s/command/" % (
                Globals.configuration.host.split("-api")[0].replace(
                    "https://", ""),
                Globals.enterprise_id,
            )
        resp = performPostRequestWithRetry(url,
                                           headers=headers,
                                           json=command_data)
        json_resp = resp.json()
        logBadResponse(url, resp, json_resp)
    except Exception as e:
        ApiToolLog().LogError(e)
    return resp, json_resp
def toggleKioskMode(
    frame,
    deviceid,
    appToUse,
    isKiosk,
    timeout=Globals.COMMAND_TIMEOUT,
    maxAttempt=Globals.MAX_RETRY,
):
    """Toggles Kiosk Mode On/Off"""
    api_instance = getCommandsApiInstance()
    if isKiosk:
        command_args = esperclient.V0CommandArgs(package_name=appToUse)
    else:
        command_args = {}
    command = esperclient.V0CommandRequest(
        enterprise=Globals.enterprise_id,
        command_type="DEVICE",
        device_type=Globals.CMD_DEVICE_TYPE,
        devices=[deviceid],
        command="SET_KIOSK_APP",
        command_args=command_args,
    )
    api_response = None
    for attempt in range(maxAttempt):
        try:
            api_response = api_instance.create_command(Globals.enterprise_id,
                                                       command)
            break
        except Exception as e:
            if hasattr(e, "body") and "invalid device id" in e.body:
                logBadResponse("create command api", api_response, None)
                return None
            if attempt == maxAttempt - 1:
                ApiToolLog().LogError(e)
                raise e
            time.sleep(Globals.RETRY_SLEEP)
    response = None
    for attempt in range(maxAttempt):
        try:
            response = api_instance.get_command_request_status(
                Globals.enterprise_id, api_response.id)
            break
        except Exception as e:
            if attempt == maxAttempt - 1:
                ApiToolLog().LogError(e)
                raise e
            time.sleep(Globals.RETRY_SLEEP)
    status = response.results[0].state
    ignoreQueued = False if Globals.REACH_QUEUED_ONLY else True
    status = waitForCommandToFinish(frame,
                                    api_response.id,
                                    ignoreQueue=ignoreQueued,
                                    timeout=timeout)
    return status
def getInfo(request_extension, deviceid):
    """Sends Request For Device Info JSON"""
    headers = getHeader()
    url = (Globals.BASE_DEVICE_URL.format(
        configuration_host=Globals.configuration.host,
        enterprise_id=Globals.enterprise_id,
        device_id=deviceid,
    ) + request_extension)
    resp = performGetRequestWithRetry(url, headers=headers)
    json_resp = resp.json()
    logBadResponse(url, resp, json_resp)

    return json_resp
def patchInfo(request_extension, deviceid, tags):
    """Pushes Data To Device Info JSON"""
    headers = getHeader()
    url = (Globals.BASE_DEVICE_URL.format(
        configuration_host=Globals.configuration.host,
        enterprise_id=Globals.enterprise_id,
        device_id=deviceid,
    ) + request_extension)
    resp = performPatchRequestWithRetry(url,
                                        headers=headers,
                                        data=json.dumps({"tags": tags}))
    json_resp = resp.json()
    logBadResponse(url, resp, json_resp)
    return json_resp
def fetchGroupName(groupURL):
    headers = getHeader()
    resp = performGetRequestWithRetry(groupURL, headers=headers)
    try:
        if resp.status_code < 300:
            json_resp = resp.json()
            logBadResponse(groupURL, resp, json_resp)

            if "name" in json_resp:
                return json_resp["name"]
    except Exception as e:
        ApiToolLog().LogError(e)
        logBadResponse(groupURL, resp, None)
    return None
def clearAppData(frame, device):
    json_resp = None
    try:
        appToUse = frame.sidePanel.appChoice.GetClientData(
            frame.sidePanel.appChoice.GetSelection())
        _, apps = getdeviceapps(device.id,
                                createAppList=False,
                                useEnterprise=Globals.USE_ENTERPRISE_APP)
        cmdArgs = {}
        for app in apps["results"]:
            if app["package_name"] == appToUse:
                cmdArgs["package_name"] = app["package_name"]
                cmdArgs["application_name"] = app["app_name"]
                cmdArgs["version_code"] = app["version_code"]
                cmdArgs["version_name"] = app["version_name"]
                if app["app_type"] == "GOOGLE":
                    cmdArgs["is_g_play"] = True
                else:
                    cmdArgs["is_g_play"] = False
                break

        if cmdArgs:
            reqData = {
                "command_type": "DEVICE",
                "command_args": cmdArgs,
                "devices": [device.id],
                "groups": [],
                "device_type": Globals.CMD_DEVICE_TYPE,
                "command": "CLEAR_APP_DATA",
            }
            resp, json_resp = postEsperCommand(reqData)
            logBadResponse(resp.request.url, resp, json_resp)
            if resp.status_code > 300:
                postEventToFrame(wxThread.myEVT_ON_FAILED, device)
            if resp.status_code < 300:
                frame.Logging(
                    "---> Clear %s App Data Command has been sent to %s" %
                    (cmdArgs["application_name"], device.device_name))
        else:
            frame.Logging(
                "ERROR: Failed to send Clear %s App Data Command to %s" %
                (frame.sidePanel.appChoice.GetValue(), device.device_name))
    except Exception as e:
        ApiToolLog().LogError(e)
        frame.Logging("ERROR: Failed to send Clear App Data Command to %s" %
                      (device.device_name))
        postEventToFrame(wxThread.myEVT_ON_FAILED, device)
    return json_resp
def getDeviceApplicationById(device_id, application_id):
    try:
        headers = getHeader()
        url = "https://%s-api.esper.cloud/api/enterprise/%s/device/%s/app/%s" % (
            Globals.configuration.host.split("-api")[0].replace(
                "https://", ""),
            Globals.enterprise_id,
            device_id,
            application_id,
        )
        resp = performGetRequestWithRetry(url, headers=headers)
        json_resp = resp.json()
        logBadResponse(url, resp, json_resp)
    except Exception as e:
        ApiToolLog().LogError(e)
    return resp, json_resp