def send(url, data, appid, sign, accesstoken=''): utctime = datetime.datetime.utcnow() timestamp = utctime.strftime('%Y-%m-%d %H:%M:%S') headers = { "X-PPD-APPID": appid, "X-PPD-SIGN": sign, "X-PPD-TIMESTAMP": timestamp, "X-PPD-TIMESTAMP-SIGN": rsa.sign("%s%s" % (appid, timestamp)), "Accept": "application/json;charset=UTF-8" } if accesstoken.strip(): headers["X-PPD-ACCESSTOKEN"] = accesstoken status_code, response_str = http_client.http_post(url, data.encode('utf-8'), headers=headers) retry_count = 0 while status_code != 200: logging.warning("Service unavailable for a while.") retry_count = retry_count + 1 time.sleep(1) if retry_count > 3: logging.error( "Service failed. Please wait a moment and retry.") # catch this at user code to wait for a reasonable long time raise RuntimeError("Wow, requests are sent too frequently...") status_code, response_str = http_client.http_post( url, data.encode('utf-8'), headers=headers) return response_str
def authorize(appid, code): data = "{\"AppID\":\"%s\",\"Code\":\"%s\"}" % (appid, code) data = data.encode("utf-8") result = http_client.http_post(openapi_client.AUTHORIZE_URL, data) #result = gzip.GzipFile(fileobj=StringIO.StringIO(result),mode="r") #result = result.read().decode("gbk").encode("utf-8") # print("authorize_data:%s" % (result)) return result
def trigger_ifttt(event, value1, value2, value3): logging.info("ready to trigger ifttt") trigger_url = "https://maker.ifttt.com/trigger/%s/with/key/dYb4ZEIKS2NZKWhWI5TMww" % event data = {"value1": value1, "value2": value2, "value3": value3} status_code, response = http_client.http_post( trigger_url, json.dumps(data).encode("utf-8")) if status_code == 200: logging.info("ifttt trigger event sent.") return 0 else: logging.warning("ifttt trigger failed with status code %s", status_code) return -1
def send(url, data, appid, sign, accesstoken=''): utctime = datetime.datetime.utcnow() timestamp = utctime.strftime('%Y-%m-%d %H:%M:%S') headers = { "X-PPD-APPID": appid, "X-PPD-SIGN": sign, "X-PPD-TIMESTAMP": timestamp, "X-PPD-TIMESTAMP-SIGN": rsa.sign("%s%s" % (appid, timestamp)) } if accesstoken.strip(): headers["X-PPD-ACCESSTOKEN"] = accesstoken data = data.lower() result = http_client.http_post(url, data, headers=headers) print("send_data:%s" % (result)) return result
def send(url, data, appid, sign, accesstoken=''): utctime = datetime.datetime.utcnow() timestamp = utctime.strftime('%Y-%m-%d %H:%M:%S') headers = { "X-PPD-APPID": appid, "X-PPD-SIGN": sign, "X-PPD-TIMESTAMP": timestamp, "X-PPD-TIMESTAMP-SIGN": rsa.sign("%s%s" % (appid, timestamp)), "Accept": "application/json;charset=UTF-8" } if accesstoken.strip(): headers["X-PPD-ACCESSTOKEN"] = accesstoken result = http_client.http_post(url, data, headers=headers) #print("send_data:\n%s" % (result)) return json.loads(result)
def refresh_token(appid, openid, refreshtoken): data = "{\"AppID\":\"%s\",\"OpenId\":\"%s\",\"RefreshToken\":\"%s\"}" % ( appid, openid, refreshtoken) result = http_client.http_post(openapi_client.REFRESHTOKEN_URL, data) print("refresh_token_data:%s" % (result)) return result