def refreshAuthTokenInfo(self): arInDTO = AuthRefreshInDTO() arInDTO.appId = (Constant().readConfFile())[2] arInDTO.secret = (Constant().readConfFile())[3] authOutDTO = AuthOutDTO() result = Authentication().getAuthToken(Constant().clientInfo()) authOutDTO.setRefreshToken(json.loads(result)['refreshToken']) arInDTO.refreshToken = authOutDTO.getRefreshToken() return arInDTO
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 subscribe(notifyType, callbackUrl): sb = Subscription(notifyType, callbackUrl) authentication = Authentication() subscriptionManagement = SubscriptionManagement() # get accessToken at first result = authentication.getAuthToken(Constant().clientInfo()) authOutDTO = AuthOutDTO() authOutDTO.setAccessToken(json.loads(result)['accessToken']) accessToken = authOutDTO.getAccessToken() # sub deviceDataChanged notification ss = subscriptionManagement.subDeviceBusinessData(sb.subDeviceBusinessData(), accessToken) print("====== subscribe to device business data notification ======") print("result:", ss + "\n") return ss
def postCommand(method, paras): sdTest = PostCommand() sdTest.method = method sdTest.paras = paras authentication = Authentication() signalDelivery = SignalDelivery() # get accessToken at first result = authentication.getAuthToken(Constant().clientInfo()) authOutDTO = AuthOutDTO() authOutDTO.setAccessToken(json.loads(result)['accessToken']) accessToken = authOutDTO.getAccessToken() # post an NB-IoT device command sp = signalDelivery.postDeviceCommand(sdTest.postDeviceCommandInfo(), None, accessToken) # print("====== post an NB-IoT device command ======") # print("result:", sp + "\n") return sp
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")
from huawei_sdk.client.invokeapi.Authentication import Authentication from huawei_sdk.client.dto.AuthOutDTO import AuthOutDTO from huawei_sdk.client.dto.AuthRefreshInDTO import AuthRefreshInDTO from huawei_sdk.constant.Constant import Constant class AuthenticationTest(object): def refreshAuthTokenInfo(self): arInDTO = AuthRefreshInDTO() arInDTO.appId = (Constant().readConfFile())[2] arInDTO.secret = (Constant().readConfFile())[3] authOutDTO = AuthOutDTO() result = Authentication().getAuthToken(Constant().clientInfo()) authOutDTO.setRefreshToken(json.loads(result)['refreshToken']) arInDTO.refreshToken = authOutDTO.getRefreshToken() return arInDTO if __name__ == "__main__": auTest = AuthenticationTest() authentication = Authentication() ag = authentication.getAuthToken(Constant().clientInfo()) print("====== get access token ======") print("result:", ag + "\n") # ar = authentication.refreshAuthToken(auTest.refreshAuthTokenInfo()) # print("====== refresh token ======") # print("result:", ar + "\n")