Ejemplo n.º 1
0
 def __init__(self):
     self.developClient = Constant().clientInfoEx("CLIENT_INFO")
     self.businessClient = Constant().clientInfoEx("CLIENTBUSINESS_INFO")
     print(self.developClient)
     print(self.businessClient)
Ejemplo n.º 2
0
class NorthApiClient(object):
    requests.packages.urllib3.disable_warnings()
    cert = (Constant().readCertificate())

    def invokeAPI(httpMethod, authUrl, payload, accessToken):
        nc = NorthApiClient()

        clientInfo = Constant().clientInfo()
        url = RestConstant.BASE_URL + clientInfo['platformIp'] + ":" + clientInfo[
            'platformPort'] + authUrl
        if accessToken == None:
            headers = {'Content-Type': 'application/json'}
        else:
            headers = {
                "app_key": clientInfo['appId'],
                "Authorization": "Bearer " + accessToken,
                "Content-Type": "application/json"
            }
        try:
            request = requests.Session()
            request.mount('https://', HTTPAdapter(pool_connections=10, pool_maxsize=10))

            global response
            if httpMethod == "POST":
                response = requests.post(url, headers=headers, data=payload, cert=nc.cert, verify=False)
            elif httpMethod == "PUT":
                response = request.put(url, headers=headers, data=payload, cert=nc.cert, verify=False)
            elif httpMethod == "DELETE":
                response = request.delete(url, headers=headers, data=payload, cert=nc.cert, verify=False)
            elif httpMethod == "GET":
                response = request.get(url, headers=headers, params=payload, cert=nc.cert, verify=False)
            logging.info(url), logging.info(headers), logging.info(payload), logging.info(response.text)
            return response.text

        except ReadTimeout as e:
            logging.error(e)
            raise ReadTimeout(e)
        except ConnectionError as e:
            logging.error(e)
            raise ConnectionError(e)
        except RequestException as e:
            logging.error(e)
            raise RequestException(e)
        except Exception as e:
            logging.error(e)
            raise Exception(e)

    def invokeAPI2(httpMethod, url, headers, payload):
        nc = NorthApiClient()

        try:
            request = requests.Session()
            request.mount('https://', HTTPAdapter(pool_connections=11, pool_maxsize=11))

            global response
            if httpMethod == "POST":
                response = request.post(url, headers=headers, data=payload, cert=nc.cert, verify=False, timeout=10)
            logging.info(url), logging.info(headers), logging.info(payload), logging.info(response.text)
            return response.text

        except ReadTimeout as e:
            logging.error(e)
            raise ReadTimeout(e)
        except ConnectionError as e:
            logging.error(e)
            raise ConnectionError(e)
        except RequestException as e:
            logging.error(e)
            raise RequestException(e)
        except Exception as e:
            logging.error(e)
            raise Exception(e)
Ejemplo n.º 3
0
    def refreshDeviceKeyInfo(self):
        rdkInDTO = RefreshDeviceKeyInDTO()
        rdkInDTO.deviceId = deviceId
        rdkInDTO.nodeId = "SSSDAWSQAS"
        rdkInDTO.verifyCode = "SSSDAWSQAS11"
        return rdkInDTO


if __name__ == "__main__":
    dmTest = DeviceManagementTest()
    authentication = Authentication()
    deviceManagement = DeviceManagement()

    # get accessToken at first
    result = Authentication().getAuthToken(Constant().clientInfo())
    authOutDTO = AuthOutDTO()
    authOutDTO.setAccessToken(json.loads(result)['accessToken'])
    accessToken = authOutDTO.getAccessToken()

    # register a new device
    dr = deviceManagement.regDirectDevice(dmTest.regDirectDeviceInfo(), None,
                                          accessToken)
    print("====== register a new device ======")
    print("result:", dr + "\n")

    # get deviceId
    rddod = RegDirectDeviceOutDTO()
    rddod.setDeviceId(json.loads(dr)['deviceId'])
    deviceId = rddod.getDeviceId()
    print("deviceId==", deviceId + "\n")