def getStrategy(strategyConf, pair, cretenExecDetlId, exchangeClient, marketDataManager, marketRulesManager, portfolioManager, orderManager): modulePath = "strategy_repository." + strategyConf['strategy_name'] if "module_path" in strategyConf: modulePath = strategyConf['module_path'] try: strategyModule = importlib.import_module(modulePath) strategyClass = getattr(strategyModule, strategyConf['strategy_name']) s = strategyClass(cretenExecDetlId, pair, strategyConf, exchangeClient, marketDataManager, marketRulesManager, portfolioManager, orderManager) except: log = Logger() log.error("Cannot create instance of strategy [" + strategyConf['strategy_name'] + "]! Module path [" + modulePath + "].") raise return s
def updatepwd(self, uid, pwd): req = self.password.reset_password(uid, pwd, app_id=2901) if not req.ok: Logger.error( "reset user password failed!user_code=%s,pwd=%s, msg=%s", uid, pwd, req.content) return
class single: def __init__(self): self.logger = Logger(logger="single").getlog() def get_singleURL(self, baseurl,url, lang, timeStamp, clientVersionInfo,access_token): ''' :param url: :param lang: :param timeStamp: :param clientVersionInfo: :return: ''' singleURL = baseurl+ url + "?lang=%s&timeStamp=%s&clientVersionInfo=%s&access_token=%s" % (lang, timeStamp, clientVersionInfo,access_token) self.logger.info("url为:%s" %singleURL) return singleURL def send_request_single(self,url,targetid,filename,filepath): ''' :param url: :param targetid: :param filename: :param filepath: :return: ''' proportion = False files = {"file":(filename,open(filepath,"rb"),"multipart/form-data",{})} if targetid == "社区": data = {"targetid":"48d1cd1ef8d846d98897cf68f12dba01", "sizes":"240x360", "proportion":proportion} elif targetid == "头像": data = {"targetid":"329ad03f9c6c4db59caffbbe3b02e1e6", "sizes":"240x360", "proportion":proportion} elif targetid =="甲面": data = {"targetid":"6e9800a33d364c298a9e515ac3a2a9bc", "sizes":"240x360", "proportion":proportion} elif targetid == "DIY": data = {"targetid":"cb8b8835e5f84249958f01d1b2f47b07", "sizes":"240x360", "proportion":proportion} elif targetid == "系统": data = {"targetid":"4305685b17aa11e9b53f005056ad4128", "sizes":"240x360", "proportion":proportion} else: self.logger.error("targetid不存在,请检查配置!") self.logger.info("请求的参数为:%s" % data) r = requests.request("post",url,data = data,files=files,timeout=30) re = r.text josnre = json.loads(re) self.logger.info("返回的参数为:%s" % json.loads(r.text)) pictureDict= { "galleryName" :josnre["data"]["pictureName"], "galleryThumbnailUrl" :josnre["data"]["thumbnailPictureUrl"], "galleryUrl" :josnre["data"]["pictureUrl"] } return pictureDict
class BasicURL: def __init__(self): self.logger = Logger(logger="BasicRUL").getlog() def basicURL(self,env): param = { "mi":"https://mi-api.nailtutu.com", "usami":"https://usa-mi-api.nailtutu.com", "env":"https://env-proxy.nailtutu.com/api" } if env == "prod": return param elif env == "uat": param["mi"]="https://mi-api-uat.nailtutu.com" param["usami"] = "https://usa-mi-api-uat.nailtutu.com" param["env"] = "https://env-proxy-uat.nailtutu.com/api" return param elif env == "dev": param["mi"]="https://mi-api-dev.nailtutu.com" param["usami"] = "https://usa-mi-api-dev.nailtutu.com" param["env"] = "https://env-proxy-dev.nailtutu.com/api" return param else: self.logger.error("您传入的参数有误,请检查!")
def add(self, mobile, pwd): # 注册用户 req = self.user.register(mobile, pwd, "", "", app_id="2001") if not req.ok: Logger.error("register user failed!user_code=%s,pwd=%s, msg=%s", mobile, pwd, req.content) return
def login_test(self, mobile, pwd): # 尝试登录 rtn = self.login(mobile, pwd, app_id=1001) if rtn is None: Logger.error("login failed!user=%s, pwd=%s", mobile, pwd) return login_user = rtn Logger.info(rtn)
class ocweb: def __init__(self): self.logger = Logger(logger="ocweb").getlog() def get_ocweb_url(self, env): ''' :param env: :return: ''' if env == "prod": url = "https://oc-api.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) elif env == "uat": url = "https://oc-api-uat.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) elif env == "dev": url = "https://oc-api-dev.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) else: self.logger.error("配置有误,请检查!") return url def get_ocweb_logged_in(self, email, password): ''' :param email: :param password: :return: ''' ocweb_logged_in = { "client_id": "d6165bbf3d804c6196c0c6d7201de970", "client_secret": "25f9e794323b453885f5181f1b624d0b", "scope": "all", "grant_type": "password", "email": email, "password": base.MD5(password), "auth_type": "email_password" } self.logger.info("账号【%s】开始登录oc_web......" % email) return ocweb_logged_in def send_request_ocweb_login(self, url, data): ''' :param url: :param data: :return: ''' headers = {"Content-Type": "application/json"} self.logger.info("请求的参数为:%s" % data) r = requests.post(url, data=json.dumps(data), headers=headers) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text)
class GetURL: def __init__(self): self.logger = Logger(logger="GetURL").getlog() def get_url(self,type): ''' :param type: :return: ''' if type == "Album": url = "/oc/tOcAlbumsInfo/addAlbumsInfo" self.logger.info("Album的url为:%s" %url) elif type == "uat": url = "https://oc-api-uat.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) elif type == "dev": url = "https://oc-api-dev.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) else: self.logger.error("传值有误,请检查!") return url
class Arduino(object): """docstring for ClassName""" def __init__(self): self.log = Logger('main').logger() config = Config(main_cfg_path) config.cfg_load() port = config.cfg.get('Arduino', 'port_name') baudrate = config.cfg.get('Arduino', 'baudrate') self.project = config.cfg.get('Project', 'name') self.tmp_data = config.cfg.get('Data', 'tmp') self.led1 = config.cfg.get('Data', 'led1') self.led2 = config.cfg.get('Data', 'led2') self.sound = config.cfg.get('Data', 'sound') self.data_backup = config.cfg.get('Log', 'data_backup') self.ser = SerialHelper() port_name = config.cfg.get('Arduino', 'port_name') port = self.ser.serial_port(port_name) if port != '': self.ser.port = port self.ser.baudrate = baudrate self.ser.start() if not self.ser.alive: self.log.error('Cannot open port {}!'.format(port)) sys.exit() else: self.log.error('Cannot find port {}!'.format(port_name)) sys.exit() # here must add sleep >= 2, will be cannot receive if not time.sleep(2) @staticmethod def write_file(path, type, text): with open(path, type) as f: f.write(text) @staticmethod def move_file(src, _dir): if not os.path.exists(_dir): os.mkdir(_dir) a, b = os.path.basename(src).rsplit('.') dst = _dir + a + \ datetime.datetime.now().strftime('_%m%d%H%M%S') + '.' + b copyfile(src, dst) def relay(self, num, t): self.ser.write('{},{},{}'.format('relay', num, t).encode()) def detect(self, name, num, t): self.log.info('Detecting sound and led data') self.write_file(self.tmp_data.format(name), 'w', '') self.ser.write('{},{},{}'.format(name, num, t).encode()) # start = time.time() read_val = '' with open(self.tmp_data.format(name), 'a') as f: while True: # time.time() - start < t try: read_val = self.ser.readline().decode().replace("\r\n", "") except Exception as e: self.log.error(e) if read_val != '': value = datetime.datetime.now().strftime( '%m-%d %H:%M:%S.%f')[:-2] + ": " + read_val # print(value) f.write(value + "\n") else: break # print(time.time() - start) # print(read_val) def run_detect(self, name, num, t): self.detect(name, num, t) if name == 'all': self.log.info('Processing data') self.write_file(self.led1, 'w', '') self.write_file(self.led2, 'w', '') self.write_file(self.sound, 'w', '') with open(self.tmp_data.format(name), 'r') as f: lines = f.readlines() for line in lines: if '1L' in line: line = line.replace('1L', '') self.write_file(self.led1, 'a', line) elif '2L' in line: line = line.replace('2L', '') self.write_file(self.led2, 'a', line) elif 'S' in line: line = line.replace('S', '') self.write_file(self.sound, 'a', line) self.move_file(self.led1, self.data_backup) self.move_file(self.led2, self.data_backup) self.move_file(self.sound, self.data_backup) def Thread_detect_data(self, num, t): try: _thread.start_new_thread(self.run_detect, ('all', num, t)) except Exception as e: self.log.info('Error when start Thread_detect_data: {0}'.format(e))
def createLicenseIndex(*args): outdir = args[0] ext_path = args[1] #if product.lower() == "modeler": #repos_set_uri = RAW_REPOS_SET_URI.format('modeler','modeler') #index_key = RAW_INDEX_KEY.format('modeler') #elif product.lower() == 'stats': # wrong spell of statistics #repos_set_uri = RAW_REPOS_SET_URI.format('statisitcs','stats') #index_key = RAW_INDEX_KEY.format('stats') index_key = RAW_INDEX_KEY licenseLogger = None try: license_obj_list = [] lic_path = os.path.join(outdir,LICENSE_DIR) os.mkdir(lic_path) root_log_dir = os.path.join(outdir, LOG_DIR_NAME) licenseLogger = Logger(os.path.join(root_log_dir,LOG_INFO),'licenseLogger') licenseLogger.info("CreateLicenseIndex script start ...") try: licenseLogger.info("Get extension list ...") repos_set_json = loadExtJSONContent(ext_path) repos_set_json_index = repos_set_json[index_key] except Exception as e: raise e repo_index = 0 licenseLogger.info("Start to get license content ...") for repo in repos_set_json_index: try: repo_name = repo["repository"] except Exception: raise Exception("At least one repository in index file does not have repo name. Please check!") repo_license_uri = RAW_LICENSE_URI.format(repo_name) try: repo_license_content = URILoader.loadURI(repo_license_uri, "license file") except Exception as e: raise e isExistedLicense = False repo_index += 1 for item in license_obj_list: if repo_license_content == item.getLicenseContent(): isExistedLicense = True item.addRepoName(repo_name) break if not isExistedLicense: addObj(repo_name, repo_license_content,license_obj_list) lic_index = 0 index_content = "{\n"+INDENT+"\"license_index\": [\n"; for obj in license_obj_list: index_item_str = LicenseIndexItemStr.getItemStr(obj) index_content += index_item_str lic_index += 1 licenseLogger.info(str(lic_index)+" license: save in file '"+obj.getLicenseName()+"'.") licenseLogger.info("Repos use this license: "+LicenseIndexItemStr.convertListToString(obj.getRepoNameList())) license_fp = open(os.path.join(lic_path,obj.getLicenseName()),'w',encoding = "utf-8") license_fp.write(obj.getLicenseContent()) license_fp.close() index_content = index_content[0:-2] index_content += '\n' + INDENT + "]\n}" index_fp = open(os.path.join(lic_path,LIC_INDEX_FILE),'w',encoding='utf-8') index_fp.write(index_content) licenseLogger.info("CreateLicenseIndex action succeeded!") except Exception as e: if licenseLogger!=None: licenseLogger.error(str(e),e) licenseLogger.info("CreateLicenseIndex action failed!") raise e finally: if licenseLogger!=None: licenseLogger.info("Totally get "+str(len(license_obj_list))+" type(s) of license from "+str(repo_index)+" repos!") licenseLogger.close()
def CreateIndex(*args): print("size" + str(len(args))) outdir = args[0] product = args[1] if product == "modeler": repos_set_uri = RAW_REPOS_SET_URI.format('modeler', 'modeler') index_key = RAW_INDEX_KEY.format('modeler') elif product == 'stats': # wrong spell of statistics repos_set_uri = RAW_REPOS_SET_URI.format('statisitcs', 'stats') index_key = RAW_INDEX_KEY.format('stats') try: lic_path = os.path.join(outdir, LICENSE_DIR) os.mkdir(lic_path) logger = Logger(os.path.join(lic_path, LOG_INFO)) logger.info("Script start ...") except IOError as e: raise IOError("IOError: Need permission to write in " + outdir) try: try: repos_set_json = json.loads( URILoader.loadURI(repos_set_uri, "index file")) except ValueError as e: raise Exception( "ValueError: The {0} has an illegal format. Please check!\n\n" .format("index file")) except Exception as e: raise e try: repos_set_json_index = repos_set_json[index_key] except Exception as e: raise e license_obj_list = [] for repo in repos_set_json_index: try: repo_name = repo["repository"] except Exception: raise Exception( "At least one repository in index file does not have repo name. Please check!\n\n" ) repo_license_uri = RAW_LICENSE_URI.format(repo_name) try: repo_license_content = URILoader.loadURI( repo_license_uri, "license file") except Exception as e: raise e isExistedLicense = False for item in license_obj_list: if repo_license_content == item.getLicenseContent(): isExistedLicense = True item.addRepoName(repo_name) break if not isExistedLicense: addObj(repo_name, repo_license_content, license_obj_list) print("Start to read license...") index_content = "{\n" + INDENT + "\"license_index\": [\n" for obj in license_obj_list: index_item_str = LicenseIndexItemStr.getItemStr(obj) index_content += index_item_str license_fp = open(os.path.join(lic_path, obj.getLicenseName()), 'w') license_fp.write(obj.getLicenseContent()) license_fp.close() index_content = index_content[0:-2] index_content += '\n' + INDENT + "]\n}" print("Start to write license to text...") index_fp = open(os.path.join(lic_path, 'license_index.json'), 'w') index_fp.write(index_content) except Exception as e: logger.error(str(e))
def createIndex(*args): indexdir = args[0] product = args[1] START_WORDS = "{\n\"productname_extension_index\":[\n" cur_time = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) root_spe_dir = os.path.join(indexdir,"spe"+cur_time) try: os.mkdir(root_spe_dir) logger = Logger(os.path.join(indexdir,LOG_INFO)) logger.info("Script start ...") except IOError as e: raise IOError("IOError: Need permission to write in "+indexdir) index_for_extension = re.sub('productname', product, START_WORDS) whole_product_name = getWholeProductName(product) print("start to get repo data from github ...") logger.info("start to get repo data from github ...") i=0 ok_repo_num = 0 try: githubApiInfo_obj = GithubApiInfoObj() for item in githubApiInfo_obj.item_list: i+=1 print(i) index_for_extension_item = INDENT+"{\n" index_for_extension_item += generateJSONStr(item) repo_name = item[githubApiInfo_obj.__class__.REPOSITORY].val logger.info(str(i)+"th repo: "+repo_name) print(repo_name) try: info_json = InfoJSONObj(repo_name) except ValueError as e: raise e except Exception as e: print(str(e)) logger.warning(str(e)) continue index_for_extension_item += generateJSONStr(info_json.item_list) repo_software = info_json.item_list[info_json.__class__.SOFTWARE].val index_for_extension_item += INDENT*2 + "\"download_link\":" +"\"" + re.sub('repos_name', repo_name, SPE_DOWNLOAD_URL) +"\",\n" index_for_extension_item += INDENT*2 + "\"image_link\":" +"\"" + re.sub('repos_name', repo_name, IMG_DOWNLOAD_URL) +"\",\n" if repo_software != whole_product_name: print("This is not a " + whole_product_name + " repo.\nSwitch to next repo.\n\n\n") logger.info("This is not a " + whole_product_name + " repo.\nSwitch to next repo.") continue repo_spe_url = re.sub('repos_name', repo_name, SPE_DOWNLOAD_URL) spe_name = repo_name+".spe" spe_saving_path = os.path.join(root_spe_dir,repo_name) os.mkdir(spe_saving_path) try: urllib.request.urlretrieve(repo_spe_url, os.path.join(spe_saving_path,spe_name)) srcZip = zipfile.ZipFile(os.path.join(spe_saving_path,spe_name), "r", zipfile.ZIP_DEFLATED) except: print("This repo '"+repo_name+"' does not have spe package. Please check!"+"\nSwitch to next repo.\n\n\n") logger.warning("This repo '"+repo_name+"' does not have spe package. Please check!"+"\nSwitch to next repo.") continue for file in srcZip.namelist(): if not os.path.isdir(spe_saving_path): os.mkdir(spe_saving_path) if FILE_NAME in file: srcZip.extract(file, spe_saving_path) srcZip.close() meta_path = os.path.join(spe_saving_path, META_DIR, FILE_NAME) metaObj = MetaObj(meta_path) index_for_extension_item += metaObj.generateExtensionJSON() index_for_extension_item += INDENT + "},\n" index_for_extension += index_for_extension_item print("Successfully get data!\n\n") ok_repo_num += 1 logger.info("Successfully get data!\n") index_for_extension = index_for_extension[0:-2] index_for_extension += '\n]\n}' index_for_extension_fp = open(os.path.join(indexdir, INDEX_FILE),'w') index_for_extension_fp.write(index_for_extension) index_for_extension_fp.close() except Exception as e: print(str(e)) logger.error(str(e)) finally: print("Totally get "+str(ok_repo_num)+" repo data successfully!\n\n") logger.info("Totally get "+str(ok_repo_num)+" repo data successfully!") clear(root_spe_dir)
class albumsShare4Front: def __init__(self): self.logger = Logger(logger="albumsShare4Front").getlog() def get_albumsShare4FrontURL(self,baseURL,URL,lang,timeStamp,clientVersionInfo,access_token): ''' :param baseURL: :param lang: :param timeStamp: :param clientVersionInfo: :param access_token: :return:特辑-专辑分享 ''' albumsShare4FrontURL = baseURL + URL + "?lang=%s&timeStamp=%s&clientVersionInfo=%s&access_token=%s" % (lang, timeStamp, clientVersionInfo,access_token) self.logger.info("url为:%s" %albumsShare4FrontURL) return albumsShare4FrontURL @retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000, retry_on_result=_result) def send_request_albumsShare4Front(self,url,shareType,shareWay,sharedId): ''' :param url: :param shareType: :param shareWay: :param sharedId: :return: ''' headers = {"Content-Type": "application/json"} if shareType == "专辑" and shareWay == "微信": parameters = { "shareType":"1", "shareWay":"1", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "专辑" and shareWay == "微信朋友圈": parameters = { "shareType":"1", "shareWay":"0", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "专辑" and shareWay == "微博": parameters = { "shareType":"1", "shareWay":"2", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "专辑" and shareWay == "QQ": parameters = { "shareType":"1", "shareWay":"3", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "社区" and shareWay == "微信": parameters = { "shareType":"2", "shareWay":"1", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "社区" and shareWay == "微博": parameters = { "shareType":"2", "shareWay":"2", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "社区" and shareWay == "QQ": parameters = { "shareType":"2", "shareWay":"3", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "系统" and shareWay == "微信": parameters = { "shareType":"3", "shareWay":"1", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "系统" and shareWay == "微博": parameters = { "shareType":"3", "shareWay":"2", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif shareType == "系统" and shareWay == "QQ": parameters = { "shareType":"3", "shareWay":"3", "sharedId":sharedId } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) else: self.logger.error("传的参数有误,请检查!")
class singelPicCollectionForMi: def __init__(self): self.logger = Logger(logger="singelPicCollectionForMi").getlog() def get_singelPicCollectionForMiURL(self, baseURL, URL, lang, timeStamp, clientVersionInfo, access_token): ''' :param baseURL: :param lang: :param timeStamp: :param clientVersionInfo: :param access_token: :return:图片收藏与取消收藏(diy、系统、社区图) ''' singelPicCollectionForMiURL = baseURL + URL + "?lang=%s&timeStamp=%s&clientVersionInfo=%s&access_token=%s" % ( lang, timeStamp, clientVersionInfo, access_token) self.logger.info("url为:%s" % singelPicCollectionForMiURL) return singelPicCollectionForMiURL @retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000, retry_on_result=_result) def send_request_singelPicCollectionForMi(self, url, galleryId, collectionType, collectStatus): ''' :param url: :param galleryId: :param collectionType: :param collectStatus: :return: ''' headers = {"Content-Type": "application/json"} if collectionType == "系统图库" and collectStatus == "收藏": parameters = {"galleryId": galleryId, "collectionType": "1"} pictureList = [] pictureList.append(parameters) data = {"collectStatus": "1"} data["picList"] = pictureList self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(data), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif collectionType == "系统图库" and collectStatus == "取消收藏": parameters = {"galleryId": galleryId, "collectionType": "1"} pictureList = [] pictureList.append(parameters) data = {"collectStatus": "0"} data["picList"] = pictureList self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(data), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif collectionType == "社区图库" and collectStatus == "收藏": parameters = {"galleryId": galleryId, "collectionType": "2"} pictureList = [] pictureList.append(parameters) data = {"collectStatus": "1"} data["picList"] = pictureList self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(data), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif collectionType == "社区图库" and collectStatus == "取消收藏": parameters = {"galleryId": galleryId, "collectionType": "2"} pictureList = [] pictureList.append(parameters) data = {"collectStatus": "0"} data["picList"] = pictureList self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(data), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif collectionType == "DIY图库" and collectStatus == "收藏": parameters = {"galleryId": galleryId, "collectionType": "3"} pictureList = [] pictureList.append(parameters) data = {"collectStatus": "1"} data["picList"] = pictureList self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(data), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif collectionType == "社区图库" and collectStatus == "取消收藏": parameters = {"galleryId": galleryId, "collectionType": "3"} pictureList = [] pictureList.append(parameters) data = {"collectStatus": "0"} data["picList"] = pictureList self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(data), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) else: self.logger.error("传值有误,请检查!") @retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000, retry_on_result=_result) def send_request_singelPicCollectionForMi_new(self, url, collectStatus, collectionType, galleryId): ''' :param url: :param galleryId: :param collectionType: :param collectStatus: :return: ''' headers = {"Content-Type": "application/json"} parameters = { "collectStatus": collectStatus, "collectVersion": "2.0", "picList": [{ "collectionType": collectionType, "galleryId": galleryId }] } self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text)
class albumsUnCollection4Front: def __init__(self): self.logger = Logger(logger="albumsUnCollection4Front").getlog() def get_albumsUnCollection4FrontURL(self, baseURL, URL, lang, timeStamp, clientVersionInfo, access_token): ''' :param baseURL: :param lang: :param timeStamp: :param clientVersionInfo: :param access_token: :return:特辑-专辑图片取消收藏(分为,一键收藏和单独收藏) ''' albumsUnCollection4FrontURL = baseURL + URL + "?lang=%s&timeStamp=%s&clientVersionInfo=%s&access_token=%s" % ( lang, timeStamp, clientVersionInfo, access_token) self.logger.info("url为:%s" % albumsUnCollection4FrontURL) return albumsUnCollection4FrontURL @retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000, retry_on_result=_result) def send_request_albumsUnCollection4Front(self, url, albumsId, isallConnectionRemove, galleryId): ''' :param url: :param albumsId: :param allConnection: :param galleryId: :return: ''' headers = {"Content-Type": "application/json"} allConnectionRemove = True NotallConnectionRemove = False if isallConnectionRemove == "一键取消收藏": parameters = { "albumsId": albumsId, "allConnectionRemove": allConnectionRemove } self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif isallConnectionRemove == "取消收藏": parameters = { "albumsId": albumsId, "allConnectionRemove": NotallConnectionRemove, "galleryId": galleryId } self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) else: self.logger.error("传的参数有误,请检查!") @retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000, retry_on_result=_result) def send_request_albumsUnCollection4Front_new(self, url, albumsId): ''' :param url: :param albumsId: :param allConnection: :return: ''' headers = {"Content-Type": "application/json"} parameters = { "albumsId": albumsId, "allConnectionRemove": True, "collectVersion": "2.0" } self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text)
def test_single(self): user_code = "13911223344" user_code2 = "13911223345" pwd = "123456" # 注册用户 req = self.user.register(user_code, pwd, "", "") if not req.ok: Logger.error("register user failed!user_code=%s,pwd=%s, msg=%s", user_code, pwd, req.content) return tmp_user = req.json() self.context.add_register_user(tmp_user) req = self.user.register(user_code2, pwd, "", "") if not req.ok: Logger.error("register user2 failed!user_code=%s,pwd=%s, msg=%s", user_code2, pwd, req.content) return tmp_user = req.json() self.context.add_register_user(tmp_user) # 尝试登录 rtn = self.login(user_code, pwd) if rtn is None: Logger.error("login failed!user=%s, pwd=%s", user_code, pwd) return login_user = rtn # 重复登录 rtn = self.login(user_code, pwd) if rtn is None: Logger.error("login failed!user=%s, pwd=%s", user_code, pwd) return if rtn["accessToken"] != login_user["accessToken"] or rtn["refreshToken"] != login_user["refreshToken"]: Logger.error("repeat login failed!newUser=%s, oldUser=%s", rtn, login_user) return # 尝试验证获取到的授权码 rtn = self.verify_token(login_user["accessToken"]) if not rtn: Logger.error("verify token failed!user=%s", login_user) return # 尝试验证查询登录用户信息 rtn = self.verify_user(login_user["uid"], login_user["accessToken"]) if rtn is None: Logger.error("verify user failed!user=%s", login_user) return if len(rtn) != 1: Logger.error("query user token failed!expect=%s, actual=%s", 1, len(rtn)) print("query_user failed!%s" % rtn) return # 更新授权码 rtn = self.update(login_user["uid"], login_user["accessToken"], login_user["refreshToken"]) if rtn is None: Logger.error("update token failed!user=%s", login_user) return old_access_token = login_user["accessToken"] old_refresh_token = login_user["refreshToken"] login_user = rtn new_access_token = login_user["accessToken"] new_refresh_token = login_user["refreshToken"] if old_access_token == new_access_token or old_refresh_token == new_refresh_token: Logger.error("update token failed!token not refresh.") print("update_token failed!%s" % (req.json())) return # 验证旧token rtn = self.verify_token(old_access_token) if rtn: Logger.error("verify old token failed!old_token=%s, new_token=%s", old_access_token, new_access_token) return # 验证新token rtn = self.verify_token(new_access_token) if not rtn: Logger.error("verify new token failed!user=%s", login_user) return # 更新用户密码,不会影响当次登录 new_pwd = "new_pass_word_123456" # print(login_user) req = self.password.update_password(login_user["uid"], pwd, new_pwd, login_user["accessToken"], login_user["appId"]) if not req.ok: Logger.error("update user password failed!user_code=%s,pwd=%s, msg=%s", user_code, pwd, req.content) return # print("update result:status code=%s,ok=%s, content=%s" % (req.status_code, req.ok, req.json())) # 验证新token rtn = self.verify_token(new_access_token) if not rtn: Logger.error("verify new token failed!user=%s", login_user) return # 注销 rtn = self.logout(login_user["uid"], login_user["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s", login_user) return # 再次注销应该找不到了 rtn = self.logout(login_user["uid"], login_user["accessToken"]) if rtn is not None: Logger.error("logout check failed!user=%s", login_user) # 尝试登录 rtn = self.login(user_code, pwd) if rtn is not None: Logger.error("login failed, password update, but login success of the old password!user=%s, pwd=%s", user_code, pwd) return rtn = self.login(user_code, new_pwd) if rtn is None: Logger.error("login failed of the new password!user=%s, pwd=%s", user_code, new_pwd) return login_user = rtn # 验证新token rtn = self.verify_token(login_user["accessToken"]) if not rtn: Logger.error("verify new token failed!user=%s", login_user) return # 更新用户密码为旧密码 # print(login_user) req = self.password.update_password(login_user["uid"], new_pwd, pwd, login_user["accessToken"], login_user["appId"]) if not req.ok: Logger.error("update user password failed!user_code=%s,pwd=%s, msg=%s", user_code, pwd, req.content) return # 注销 rtn = self.logout(login_user["uid"], login_user["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s", login_user) return # 尝试登录 rtn = self.login(user_code, pwd) if rtn is None: Logger.error("login old password failed!user=%s, pwd=%s", user_code, pwd) return login_user = rtn rtn = self.login(user_code2, pwd) if rtn is None: Logger.error("login old password failed!user=%s, pwd=%s", user_code2, pwd) return login_user2 = rtn # 修改用户的状态为禁用 rtn = self.userStatus.disable_user(login_user2["uid"], login_user["uid"], login_user2["accessToken"], login_user2["appId"]) if not rtn.ok: Logger.error("update user status failed!user_code=%s,pwd=%s, msg=%s", user_code2, pwd, rtn.json()) return # 再验证用户的Token应该会失败 rtn = self.verify_token(login_user["accessToken"]) if rtn: Logger.error("verify update user status token failed!user=%s", login_user) return # 尝试登录也会失败 rtn = self.login(user_code, pwd) if rtn is not None: Logger.error("login disable user is success!user=%s, pwd=%s", user_code, pwd) return
class OrderManager(object): def __init__(self, exchangeClient, marketRulesManager, strategyManager): self.exchangeClient = exchangeClient self.marketRulesManager = marketRulesManager self.strategyManager = strategyManager self.shapeNewOrders = True self.validateOrders = True self.preventImmediateLimitOrder = True self.liveOrderCache = {} self.liveTradeCache = {} self.log = Logger() def setShapeNewOrders(self, shapeNewOrders): self.shapeNewOrders = shapeNewOrders def setValidateOrders(self, validateOrders): self.validateOrders = validateOrders def setPreventImmediateLimitOrder(self, preventImmediateLimitOrder): self.preventImmediateLimitOrder = preventImmediateLimitOrder def getLiveOrderCache(self): return self.liveOrderCache def getLiveTradeCache(self): return self.liveTradeCache def reset(self): self.liveTradeCache = {} self.liveOrderCache = {} def storeOrder(self, order): Db.Session().add(order) # flush in order to generate db maintained sequence Db.Session().flush() # update internal reference only when we got new order_id if not order.int_order_ref: order.int_order_ref = Settings.ORDER_REFERENCE_PREFIX + str(order.order_id) # track order updates in the cache. o = Order() o.setFromEntity(order) self.liveOrderCache[order.order_id] = o def _storeTrade(self, trade): Db.Session().add(trade) # flush in order to generate db maintained sequence Db.Session().flush() # track trade updates in the cache. if trade reached a final state, remove it from the cache t = orders.Trade.Trade() t.setFromEntity(trade) if not trade.trade_id in self.liveTradeCache: self.liveTradeCache[trade.trade_id] = t else: if trade.trade_state in TradeStateMapper.getDbValue([TradeState.CLOSED, TradeState.CLOSE_FAILED, TradeState.OPEN_FAILED]): self.liveTradeCache.pop(trade.trade_id, None) # once trade is removed, clean also cache of corresponding trade orders for orderKey in list(self.liveOrderCache.keys()): if self.liveOrderCache[orderKey].getTradeId() == t.getTradeId(): self.liveOrderCache.pop(orderKey) else: self.liveTradeCache[trade.trade_id] = t def closeTrade(self, trade, closeTmstmp): self.log.info('Trade ' + str(trade.trade_id) + ' CLOSED.') trade.trade_state = TradeStateMapper.getDbValue(TradeState.CLOSED) trade.close_tmstmp = closeTmstmp # notify strategy about the closed trade self.strategyManager.getStrategy(trade.strategy_exec_id).tradeClosed(trade.trade_id) self._calcTradePerf(trade) def _calcTradePerf(self, trade): orders = TradeManager.getAllOrders(tradeId = trade.trade_id, orderState = OrderStateMapper.getDbValue(OrderState.FILLED)) buyPrice = Decimal(0.0) sellPrice = Decimal(0.0) for order in orders: if order.order_side == OrderSideMapper.getDbValue(OrderSide.BUY): buyPrice += order.qty * Decimal(order.price) else: sellPrice += order.qty * Decimal(order.price) diff = sellPrice - buyPrice rules = self.marketRulesManager.getSymbolRules(trade.base_asset, trade.quote_asset) if diff < 0: color = Style.BRIGHT + Fore.LIGHTWHITE_EX + Back.RED else: color = Style.BRIGHT + Fore.LIGHTWHITE_EX + Back.GREEN self.log.info(color + 'Gain: ' + str(('{:.' + str(rules.quoteAssetPrecision) + 'f}').format(diff)) + Style.RESET_ALL) def processOrderUpdate(self, orderResponse): # update order in the database based on the update response order = TradeManager.getOrder(intOrderRef = orderResponse.getClientOrderId()) order.order_state = OrderStateMapper.getDbValue(orderResponse.getOrderState()) order.lst_upd_tmstmp = datetime.now() if orderResponse.getOrderState() == OrderState.OPENED: order.open_tmstmp = orderResponse.getOrderTmstmp() if orderResponse.getOrderState() == OrderState.FILLED: self.log.debug('Order ' + str(order.order_id) + ' FILLED.') order.filled_tmstmp = orderResponse.getOrderTmstmp() if orderResponse.getOrderState() == OrderState.CANCELED: self.log.debug('Order ' + str(order.order_id) + ' CANCELLED.') # update trade in the database based on the state of orders trade = TradeManager.getTrade(tradeId = order.trade_id) if trade.trade_type == TradeTypeMapper.getDbValue(TradeType.LONG): # open trade when first confirmed BUY is received if orderResponse.getOrderSide() == OrderSide.BUY and \ orderResponse.getOrderState() == OrderState.OPENED and \ trade.trade_state == TradeStateMapper.getDbValue(TradeState.OPEN_PENDING): trade.trade_state = TradeStateMapper.getDbValue(TradeState.OPENED) trade.open_tmstmp = orderResponse.getOrderTmstmp() # close trade when no pending order is left elif orderResponse.getOrderState() in [OrderState.CANCELED, OrderState.REJECTED, OrderState.EXPIRED]: openOrders = TradeManager.getPendOrders(trade.trade_id) if len(openOrders) == 0: self.closeTrade(trade, orderResponse.getOrderTmstmp()) # handle closing of the trade based on the defined strategy elif orderResponse.getOrderState() == OrderState.FILLED: tradeCloseType = self.strategyManager.getStrategy(trade.strategy_exec_id).getTradeCloseType() TradeCloseStrategy.evalTradeClose(tradeCloseType, trade, orderResponse, self) else: raise Exception('Short trades not supported!') self.storeOrder(order) self._storeTrade(trade) def _validateOrders(self, baseAsset, quoteAsset, orders): rules = self.marketRulesManager.getSymbolRules(baseAsset, quoteAsset) for order in orders: qty = order.getQty() if qty < rules.minQty: raise Exception('Quantity is less than minimum quantity! Qty [' + str(qty) + '], minQty [' + str(rules.minQty) + ']') if qty > rules.maxQty: raise Exception('Quantity is greater than maximum quantity! Qty [' + str(qty) + '], maxQty [' + str(rules.maxQty) + ']') if (qty - rules.minQty) % rules.minQtyDenom != 0: raise Exception('Quantity is not multiply of denomination! qty [' + str(qty) + '], minQty [' + str(rules.minQty) + '], minDenom [' + str(rules.minQtyDenom) + ']') price = order.getPrice() if price: if price < rules.minPrice: raise Exception('Price is less than minimum price! Price [' + str(price) + '], minPrice [' + str(rules.minPrice) + ']') if price > rules.maxPrice: raise Exception('Price is greater than maximum price! Price [' + str(price) + '], maxPrice [' + str(rules.maxPrice) + ']') if (price - rules.minPrice) % rules.minPriceDenom != 0: raise Exception('Price is not multiply of denomination! Price [' + str(price) + '], minPrice [' + str(rules.minPrice) + '], minDenom [' + str(rules.minPriceDenom) + ']') if price * qty < rules.minNotional: raise Exception('Quantity and price is less than minimum notional value! Qty [' + str(qty) + '], price [' + str(price) + '], minNotional [' + str(rules.minNotional) + ']') stopPrice = order.getStopPrice() if stopPrice: if stopPrice < rules.minPrice: raise Exception('Stop price is less than minimum price! Stop price [' + str(stopPrice) + '], minPrice [' + str(rules.minPrice) + ']') if stopPrice > rules.maxPrice: raise Exception('Stop price is greater than maximum price! Stop price [' + str(stopPrice) + '], maxPrice [' + str(rules.maxPrice) + ']') if (stopPrice - rules.minPrice) % rules.minPriceDenom != 0: raise Exception('Stop price is not multiply of denomination! Stop price [' + str(stopPrice) + '], minPrice [' + str(rules.minPrice) + '], minDenom [' + str(rules.minPriceDenom) + ']') if stopPrice * qty < rules.minNotional: raise Exception('Quantity and stop price is less than minimum notional value! Qty [' + str(qty) + '], stop price [' + str(stopPrice) + '], minNotional [' + str(rules.minNotional) + ']') return True def _validateImmediateLimitOrder(self, candle, orders): for order in orders: if order.getOrderSide() == OrderSide.BUY: if order.getOrderType() == OrderType.LIMIT and order.getPrice() >= candle.getClose(): raise Exception("LIMIT BUY order will be executed immediately! Order price [" + str(order.getPrice()) + "], market price [" + str(candle.getClose()) + "]") if order.getOrderType() in [OrderType.STOP_LOSS_LIMIT, OrderType.STOP_LOSS_MARKET] and order.getStopPrice() <= candle.getClose(): raise Exception("STOP LOSS BUY order will be executed immediately! Order stop price [" + str(order.getStopPrice()) + "], market price [" + str(candle.getClose()) + "]") if order.getOrderType() in [OrderType.TAKE_PROFIT_LIMIT, OrderType.TAKE_PROFIT_MARKET] and order.getStopPrice() >= candle.getClose(): raise Exception("TAKE PROFIT BUY order will be executed immediately! Order stop price [" + str(order.getStopPrice()) + "], market price [" + str(candle.getClose()) + "]") if order.getOrderSide() == OrderSide.SELL: if order.getOrderType() == OrderType.LIMIT and order.getPrice() <= candle.getClose(): raise Exception("LIMIT SELL order will be executed immediately! Order price [" + str(order.getPrice()) + "], market price [" + str(candle.getClose()) + "]") if order.getOrderType() in [OrderType.STOP_LOSS_LIMIT, OrderType.STOP_LOSS_MARKET] and order.getStopPrice() >= candle.getClose(): raise Exception("STOP LOSS SELL order will be executed immediately! Order stop price [" + str(order.getStopPrice()) + "], market price [" + str(candle.getClose()) + "]") if order.getOrderType() in [OrderType.TAKE_PROFIT_LIMIT, OrderType.TAKE_PROFIT_MARKET] and order.getStopPrice() <= candle.getClose(): raise Exception("TAKE PROFIT SELL order will be executed immediately! Order stop price [" + str(order.getStopPrice()) + "], market price [" + str(candle.getClose()) + "]") def _shapeValue(self, value, minVal, minDenom, precision): self.log.debug('Shaping value [' + str(value) + '], minVal [' + str(minVal) + '], minDenom [' + str(minDenom) + '], precision [' + str(precision) + ']') ret = Decimal(minVal) + Decimal(minDenom) * Decimal((Decimal(value) - Decimal(minVal)) / Decimal(minDenom)).quantize(Decimal('1'), rounding = ROUND_DOWN) #ret = Decimal(ret).quantize(Decimal('10') ** -precision) self.log.debug('Shaped value [' + str(ret) + ']') return ret def _shapeOrders(self, baseAsset, quoteAsset, orders): rules = self.marketRulesManager.getSymbolRules(baseAsset, quoteAsset) for order in orders: if order.getQty(): self.log.debug('Shaping quantity') order.setQty(self._shapeValue(order.getQty(), rules.minQty, rules.minQtyDenom, rules.baseAssetPrecision)) if order.getPrice(): self.log.debug('Shaping price') order.setPrice(self._shapeValue(order.getPrice(), rules.minPrice, rules.minPriceDenom, rules.quoteAssetPrecision)) if order.getStopPrice(): self.log.debug('Shaping stop price') order.setStopPrice(self._shapeValue(order.getStopPrice(), rules.minPrice, rules.minPriceDenom, rules.quoteAssetPrecision)) def openTrade(self, strategyExecId, tradeType, candle): trade = Trade() trade.strategy_exec_id = strategyExecId trade.base_asset = candle.getBaseAsset() trade.quote_asset = candle.getQuoteAsset() trade.init_tmstmp = candle.getCloseTime() trade.trade_state = TradeStateMapper.getDbValue(TradeState.OPEN_PENDING) trade.trade_type = TradeTypeMapper.getDbValue(tradeType) self._storeTrade(trade) return trade def openOrder(self, trade, candle, orders): self.log.debug('Orders to be created: ' + str(len(orders))) # adjust quantity and price such that it meets market rules if self.shapeNewOrders: self._shapeOrders(candle.getBaseAsset(), candle.getQuoteAsset(), orders) # validate orders before sending them to the exchange if self.validateOrders: self._validateOrders(candle.getBaseAsset(), candle.getQuoteAsset(), orders) # make sure that limit orders are not executed immediately if self.preventImmediateLimitOrder: self._validateImmediateLimitOrder(candle, orders) for order in orders: try: dbOrder = Orders() dbOrder.trade_id = trade.trade_id dbOrder.qty = order.getQty() dbOrder.stop_price = order.getStopPrice() if order.getOrderType() == OrderType.MARKET: dbOrder.price = candle.getClose() else: dbOrder.price = order.getPrice() dbOrder.order_side = OrderSideMapper.getDbValue(order.getOrderSide()) dbOrder.order_type = OrderTypeMapper.getDbValue(order.getOrderType()) dbOrder.order_state = OrderStateMapper.getDbValue(OrderState.OPEN_PENDING_INT) dbOrder.init_tmstmp = candle.getCloseTime() self.storeOrder(dbOrder) # save the generated order id order.setOrderId(dbOrder.order_id) # save the order state (for print) order.setOrderState(OrderState.OPEN_PENDING_INT) self.log.info('Order: ' + str(order)) except: self.log.error('Could not create order ' + str(order)) raise def sendOrders(self, cretenExecDetlId): ordersToBeSent = TradeManager.getAllOrders(cretenExecDetlId = cretenExecDetlId, orderState = OrderStateMapper.getDbValue([OrderState.OPEN_PENDING_INT, OrderState.CANCEL_PENDING_INT])) for dbOrder in ordersToBeSent: if dbOrder.order_state == OrderStateMapper.getDbValue(OrderState.OPEN_PENDING_INT): try: order = Order() order.setFromEntity(dbOrder) self.log.debug('Processing order [' + str(order) + ']') trade = TradeManager.getTrade(tradeId = dbOrder.trade_id) rules = self.marketRulesManager.getSymbolRules(trade.base_asset, trade.quote_asset) qty = ('{:.' + str(rules.baseAssetPrecision) + 'f}').format(order.getQty()) price = ('{:.' + str(rules.quoteAssetPrecision) + 'f}').format(order.getPrice()) if order.getPrice() else None stopPrice = ('{:.' + str(rules.quoteAssetPrecision) + 'f}').format(order.getStopPrice()) if order.getStopPrice() else None self.log.debug('Values to be sent: qty [' + str(qty) + '], price [' + str(price) + '], stop price [' + str(stopPrice) + ']') response = self.exchangeClient.createOrder(order.getOrderSide(), order.getOrderType(), trade.base_asset, trade.quote_asset, qty, stopPrice, price, order.getIntOrderRef()) self.log.debug('Response: ' + str(response.getRawData())) if not response.getOrderState() in [OrderState.OPENED, OrderState.FILLED]: raise Exception('Unexpected response received for order [' + str(order) + ']! Expected state [' + str([OrderState.OPENED, OrderState.FILLED]) + '], received [' + str(response.getOrderState()) + ']') if order.getOrderType() == OrderType.MARKET: dbOrder.price = response.getPrice() dbOrder.ext_order_ref = response.getExtOrderRef() dbOrder.order_state = OrderStateMapper.getDbValue(OrderState.OPEN_PENDING_EXT) self.storeOrder(dbOrder) except: self.log.error('Error while creating orders!') dbOrder.order_state = OrderStateMapper.getDbValue(OrderState.OPEN_FAILED) self.storeOrder(dbOrder) raise elif dbOrder.order_state == OrderStateMapper.getDbValue(OrderState.CANCEL_PENDING_INT): try: order = Order() order.setFromEntity(dbOrder) self.log.debug('Cancelling order [' + str(order) + ']') trade = TradeManager.getTrade(tradeId = dbOrder.trade_id) response = self.exchangeClient.cancelOrder(baseAsset = trade.base_asset, quoteAsset = trade.quote_asset, clientOrderId = order.getIntOrderRef()) self.log.debug('Response: ' + str(response.getRawData())) if not response.getOrderState() in [OrderState.OPENED]: raise Exception('Unexpected response received for order [' + str(order) + ']! Expected state [' + str([OrderState.OPENED, OrderState.FILLED]) + '], received [' + str(response.getOrderState()) + ']') dbOrder.ext_order_ref = response.getExtOrderRef() dbOrder.order_state = OrderStateMapper.getDbValue(OrderState.CANCEL_PENDING_EXT) self.storeOrder(dbOrder) except: self.log.error('Error while cancelling orders!') dbOrder.order_state = OrderStateMapper.getDbValue(OrderState.CANCEL_FAILED) self.storeOrder(dbOrder) raise else: raise Exception('Creating orders for unsupporterd order state!') # TODO def init(self): pass
class getPicDetailForMi: def __init__(self): self.logger = Logger(logger="getPicDetailForMi").getlog() def get_getPicDetailForMiURL(self,baseURL,URL,lang,timeStamp,clientVersionInfo,access_token): ''' :param baseURL: :param lang: :param timeStamp: :param clientVersionInfo: :param access_token: :return:取单张图详情 ''' getPicDetailForMiURL = baseURL + URL + "?lang=%s&timeStamp=%s&clientVersionInfo=%s&access_token=%s" % (lang, timeStamp, clientVersionInfo,access_token) self.logger.info("url为:%s" %getPicDetailForMiURL) return getPicDetailForMiURL @retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000, retry_on_result=_result) def send_request_getPicDetailForMi(self,url,id,collectionType): ''' :param galleryName: :param galleryUrl: :param galleryThumbnailUrl: :param collectionType: :return: ''' headers = {"Content-Type": "application/json"} if collectionType == "系统图库": parameters = { "id":id, "collectionType":"1" } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif collectionType == "社区图库": parameters = { "id":id, "collectionType":"2" } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) elif collectionType == "DIY图库": parameters = { "id":id, "collectionType":"3" } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) elif collectionType == "专辑图库": parameters = { "id":id, "collectionType":"4" } self.logger.info("请求的参数为:%s" %parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers,timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) else: self.logger.error("collectionType不存在,请检查!")
class modifyDeviceModel: def __init__(self): self.logger = Logger(logger="modifyDeviceModel").getlog() def get_modifyDeviceModel_url(self, env, access_token, lang, timeStamp): ''' :param access_token: :param lang: :param timeStamp: :return: ''' if env == "prod": modifyDeviceModelURL = "https://oc-api.nailtutu.com" + "/api/deviceModel/edit?access_token=%s&lang=%s&timeStamp=%s" % ( access_token, lang, timeStamp) self.logger.info("修改设备模式的URL为:%s" % modifyDeviceModelURL) return modifyDeviceModelURL elif env == "uat": modifyDeviceModelURL = "https://oc-api-uat.nailtutu.com" + "/api/deviceModel/edit?access_token=%s&lang=%s&timeStamp=%s" % ( access_token, lang, timeStamp) self.logger.info("修改设备模式的URL为:%s" % modifyDeviceModelURL) return modifyDeviceModelURL elif env == "dev": modifyDeviceModelURL = "https://oc-api-dev.nailtutu.com" + "/api/deviceModel/edit?access_token=%s&lang=%s&timeStamp=%s" % ( access_token, lang, timeStamp) self.logger.info("修改设备模式的URL为:%s" % modifyDeviceModelURL) return modifyDeviceModelURL def get_data_modifyDeviceModel(self, sn, deviceModel): ''' :param sn: :param deviceModel: :return: ''' para = { "id": "488bd8f4744048ea9c69dd350fbe014a", "sn": sn, "deviceModel": 0, "sku": "49-03000-007-b" } #线上的id #488bd8f4744048ea9c69dd350fbe014a #uat #fde1248e6a524ff9a956b90acdc7d1f4 if deviceModel == "出租": para["deviceModel"] = 0 self.logger.info("%s的设备模式修改为%s" % (sn, deviceModel)) return para elif deviceModel == "共享": para["deviceModel"] = 1 para["sku"] = "49-03000-007-a" self.logger.info("%s的设备模式修改为%s" % (sn, deviceModel)) return para elif deviceModel == "购买": para["deviceModel"] = 2 para["sku"] = "49-03000-007" self.logger.info("%s的设备模式修改为%s" % (sn, deviceModel)) return para else: self.logger.error("参数错误,请检查配置!") def send_request_modifyDeviceModel(self, url, data): ''' :param url: :param data: :return: ''' headers = {"Content-Type": "application/json"} self.logger.info("请求的参数为:%s" % data) r = requests.post(url, data=json.dumps(data), headers=headers) self.logger.info("返回的数据为:%s" % json.loads(r.text)) return json.loads(r.text)
class Authentication: def __init__(self): self.logger = Logger(logger="Authentication").getlog() def get_Android_CN_Not_logged_in(self): ''' :return:ok ''' Android_CN_Not_logged_in = { "client_id": "72e4664b6055411284eb3f9dc14fb280", "client_secret": "27cb32fd6113b86ab989d6f08618c202", "device_name": "ANDROID", "scope": "all", "grant_type": "client_credentials", "imei": "5155515454444" } self.logger.info("Android_CN鉴权的参数为%s" % Android_CN_Not_logged_in) return Android_CN_Not_logged_in def get_test_IOS_client_Not_logged_in(self): ''' :return:ok ''' IOS_CN_Not_logged_in = { "client_id": "a62d47e603b64b059755fa84c500753d", "client_secret": "add30a6f69614900a73a5d6814950954", "device_name": "ios", "scope": "all", "grant_type": "client_credentials", "imei": "5155515454444" } self.logger.info("鉴权的参数为%s" % IOS_CN_Not_logged_in) return IOS_CN_Not_logged_in def get_Android_CN_logged_in(self, mobile, password, countryCode=None): ''' :param mobile:ok :return: ''' Android_CN_logged_in = { "auth_type": "mobile_password", "client_id": "72e4664b6055411284eb3f9dc14fb280", "client_secret": "27cb32fd6113b86ab989d6f08618c202", "device_name": "ANDROID", "grant_type": "password", "imei": "560689945444355", "mobile": mobile, "password": base.MD5(password), "scope": "all" } if countryCode: self.logger("美甲涂涂移动Android端的鉴权参数为%s" % Android_CN_logged_in) Android_CN_logged_in["countryCode"] = countryCode self.logger.info("账号:%s开始登录......" % mobile) return Android_CN_logged_in else: self.logger.info("账号:%s开始登录......" % mobile) return Android_CN_logged_in def get_Android_CN_sms_logged_in(self, mobile, verify_code): ''' :param mobile:ok :return: ''' Android_CN_logged_in = { "auth_type": "mobile_verify_code", "client_id": "72e4664b6055411284eb3f9dc14fb280", "client_secret": "27cb32fd6113b86ab989d6f08618c202", "device_name": "anjou_android_app", "grant_type": "password", "imei": "a000006d937c5f", "mobile": mobile, "scope": "all", "verify_code": verify_code } self.logger.info("账号:%s开始登录......" % mobile) return Android_CN_logged_in def get_Android_EN_Not_logged_in(self): ''' :return: ''' Android_EN_Not_logged_in = { "client_id": "1ee50af9e9194d999583bf64224074bc", "client_secret": "7c172a92fd38428a57902f77c05e2bf3", "scope": "all", "grant_type": "client_credentials" } self.logger.info("Android_EN_Not_logged_in参数的值为:%s" % Android_EN_Not_logged_in) return Android_EN_Not_logged_in def get_Android_EN_logged_in(self, email, password): ''' :param email: :param password: :return: ''' Android_EN_logged_in = { "auth_type": "email_password", "client_id": "1ee50af9e9194d999583bf64224074bc", "client_secret": "7c172a92fd38428a57902f77c05e2bf3", "device_name": "ANDROID", "email": email, "grant_type": "password", "imei": "864588030495393", "password": base.MD5(password), "scope": "all" } self.logger("anjou移动Android端的鉴权参数为%s" % Android_EN_logged_in) self.logger.info("账号:%s开始登录......" % email) return Android_EN_logged_in def get_Applets_Merchant_logged_in(self, phone, password): ''' :param phone: :param password: :return: ''' SmallProgram_ZH_logged_in = { "client_id": "e230de4a1a64452c89919b1341f7229a", "client_secret": "c3d72eb29df7fd295b734c24f1e8aa43", "grant_type": "password", "mobile": phone, "password": base.MD5(password), "auth_type": "mobile_password", "scope": "all", "product_line_id": "46981435671117824", "imei": "IMEI_1568613543758", "device_name": "EVA-AL10" } self.logger.info("美甲涂涂小程序商户端鉴权的参数为%s" % SmallProgram_ZH_logged_in) return SmallProgram_ZH_logged_in def get_Applets_ordinary_logged_in(self, phone, password): ''' :param phone: :param password: :return: ''' SmallProgram_ZH_logged_in = { "client_id": "f9c8218ff78a4e8daa6f014b20c61be2", "client_secret": "c3d72eb29df7fd295b734c24f1e8aa43", "grant_type": "password", "mobile": phone, "password": base.MD5(password), "auth_type": "mobile_password", "scope": "all", "product_line_id": "46981435671117824", "imei": "IMEI_1568613543758", "device_name": "EVA-AL10" } self.logger.info("美甲涂涂小程序普通端鉴权的参数为%s" % SmallProgram_ZH_logged_in) return SmallProgram_ZH_logged_in def get_IOS_CN_Not_logged_in(self): ''' :return:ok ''' IOS_CN_Not_logged_in = { "client_id": "e79661c27154473eaf5f3136b7587518", "client_secret": "78163c4521ed8490a967db3bd4dae48a", "scope": "all", "grant_type": "client_credentials" } self.logger.info("IOS_CN_Not_logged_in参数的值为:%s" % IOS_CN_Not_logged_in) return IOS_CN_Not_logged_in def get_IOS_CN_logged_in(self, mobile, password): ''' :param mobile: :return:ok ''' IOS_CN_logged_in = { "mobile": mobile, "password": base.MD5(password), "device_name": "anjou_ios_app", "imei": "2A0E951B-CABD-42D8-AC72-B00D1B0C46EA", "scope": "all", "grant_type": "password", "client_secret": "78163c4521ed8490a967db3bd4dae48a", "client_id": "e79661c27154473eaf5f3136b7587518", "auth_type": "mobile_password" } self.logger.info("美甲涂涂移动IOS端鉴权的参数为%s" % IOS_CN_logged_in) self.logger.info("账号:%s开始登录......" % mobile) return IOS_CN_logged_in def get_IOS_EN_Not_logged_in(self): ''' :return: ''' IOS_EN_Not_logged_in = { "client_id": "4b61e3634c364384ab26e25ac856058d", "client_secret": "dfc7098421aed0c1348d063ddf134a83", "scope": "all", "grant_type": "client_credentials" } self.logger.info("IOS_EN_Not_logged_in参数的值为:%s" % IOS_EN_Not_logged_in) return IOS_EN_Not_logged_in def get_IOS_EN_logged_in(self, email, password): ''' :param mobile: :return: ''' IOS_EN_logged_in = { "auth_type": "email_password", "client_id": "4b61e3634c364384ab26e25ac856058d", "client_secret": "dfc7098421aed0c1348d063ddf134a83", "device_name": "anjou_IOS_app", "grant_type": "password", "imei": "317813771187541", "email": email, "password": base.MD5(password), "scope": "all" } self.logger.info("anjou移动IOS端鉴权的参数为%s" % IOS_EN_logged_in) self.logger.info("账号:%s开始登录......" % email) return IOS_EN_logged_in def get_PAD_Not_logged_in(self): ''' :return: ''' PAD__Not_logged_in = { "client_id": "d4909d4e5c40467ebdef591e7e161a0a", "client_secret": "cdec037307bb368dd4da842e8829b2e1", "device_channel_id": "ClientId_Pad_App_CN_e4937a14f91c05a3", "device_name": "PAD", "grant_type": "client_credentials", "imei": "e4937a14f91c05a3", "scope": "all" } self.logger.info("PAD_Not_logged_in参数的值为:%s" % PAD__Not_logged_in) return PAD__Not_logged_in def get_PAD_Not_logged_in_25_30(self): ''' :return: ''' PAD_Not_logged_in = { "client_id": "d4909d4e5c40467ebdef591e7e161a0a", "client_secret": "cdec037307bb368dd4da842e8829b2e1", "device_name": "Anjou_Pad", "grant_type": "client_credentials", "imei": "521912129226407", "scope": "all" } self.logger.info("PAD_Not_logged_in参数的值为:%s" % PAD_Not_logged_in) return PAD_Not_logged_in def get_PAD_CN_logged_in(self, mobile, password, countryCode=None, version=None): ''' :param mobile: :return: ''' PAD_CN_logged_in = { "auth_type": "mobile_password", "client_id": "d4909d4e5c40467ebdef591e7e161a0a", "client_secret": "cdec037307bb368dd4da842e8829b2e1", "device_name": "pad", "grant_type": "password", "imei": "e4937a14f91c05a3", "mobile": mobile, "password": base.MD5(password), "scope": "all" } # if countryCode: # PAD_CN_logged_in["countryCode"] = countryCode # logger.info("请求的参数为:%s" % PAD_CN_logged_in) # logger.info("账号:%s开始登录......" % mobile) # return PAD_CN_logged_in if version == "01.00.25": PAD_CN_logged_in["imei"] = "986151127149663" self.logger.info("请求的参数为:%s" % PAD_CN_logged_in) self.logger.info("账号:%s开始登录......" % mobile) return PAD_CN_logged_in elif version == "01.00.18": PAD_CN_logged_in["imei"] = "808678559873015" self.logger.info("请求的参数为:%s" % PAD_CN_logged_in) self.logger.info("账号:%s开始登录......" % mobile) return PAD_CN_logged_in else: self.logger.info("请求的参数为:%s" % PAD_CN_logged_in) self.logger.info("账号:%s开始登录......" % mobile) return PAD_CN_logged_in def get_PAD_CN_logged_in_countryCode(self, mobile, password, countryCode=None): ''' :param mobile: :return: ''' PAD_CN_logged_in = { "auth_type": "mobile_password", "client_id": "d4909d4e5c40467ebdef591e7e161a0a", "client_secret": "cdec037307bb368dd4da842e8829b2e1", "device_name": "pad", "grant_type": "password", "imei": "e4937a14f91c05a3", "mobile": mobile, "password": base.MD5(password), "scope": "all" } if countryCode: PAD_CN_logged_in["countryCode"] = countryCode self.logger.info("请求的参数为:%s" % PAD_CN_logged_in) self.logger.info("账号:%s开始登录......" % mobile) return PAD_CN_logged_in else: self.logger.info("请求的参数为:%s" % PAD_CN_logged_in) self.logger.info("账号:%s开始登录......" % mobile) return PAD_CN_logged_in def get_PAD_CN_logged_in_email(self, email, password): ''' :param email: :return: ''' PAD_CN_logged_in = { "auth_type": "email_password", "client_id": "d4909d4e5c40467ebdef591e7e161a0a", "client_secret": "cdec037307bb368dd4da842e8829b2e1", "device_name": "PAD", "grant_type": "password", "email": email, "imei": "e4937a14f91c05a3", "password": base.MD5(password), "scope": "all" } self.logger.info("请求的参数为:%s" % PAD_CN_logged_in) self.logger.info("账号:%s开始登录......" % email) def get_PAD_CN_scan_code_logged_in(self, qr_code): ''' :param qr_code: :return: ''' PAD_CN_logged_in = { "auth_type": "scan_code", "client_id": "d4909d4e5c40467ebdef591e7e161a0a", "client_secret": "cdec037307bb368dd4da842e8829b2e1", "device_name": "PAD", "grant_type": "password", "imei": "e4937a14f91c05a3", "qr_code": qr_code, "scope": "all" } self.logger.info("pad端扫码登录请求的参数为%s" % PAD_CN_logged_in) return PAD_CN_logged_in def get_PAD_EN_Not_logged_in(self): ''' :return: ''' PAD_EN_Not_logged_in = { "client_id": "dfd558847f184e8e844971764e510661", "client_secret": "902457d20226393bda22a95124ea6620", "scope": "all", "grant_type": "client_credentials" } self.logger.info("PAD_EN_Not_logged_in参数的值为:%s" % PAD_EN_Not_logged_in) return PAD_EN_Not_logged_in def get_PAD_EN_logged_in(self, email, password, version=None): ''' :param email: :param password: :return: ''' PAD_EN_logged_in = { "auth_type": "email_password", "client_id": "dfd558847f184e8e844971764e510661", "client_secret": "902457d20226393bda22a95124ea6620", "device_name": "pad", "email": email, "grant_type": "password", "imei": "e4937a14f91c05a3", "password": base.MD5(password), "scope": "all" } if version == "01.00.34": PAD_EN_logged_in["device_name"] = "PAD" self.logger.info("PAD_EN_logged_in参数的值为:%s" % PAD_EN_logged_in) return PAD_EN_logged_in else: self.logger.info("PAD_EN_logged_in参数的值为:%s" % PAD_EN_logged_in) return PAD_EN_logged_in def get_firmware(self, sn): ''' :return: ''' firmware = { "auth_type": "sn_password", "client_id": "d00a9260baff4ad386939a7baf22b799", "client_secret": "8236dc7b20ed484c830aabc5643717e9", "grant_type": "password", "scope": "all", "sn": sn } self.logger.info("sn:%s鉴权成功" % sn) return firmware def get_oc_web(self, userName, password): password = base.MD5(password) oc_web = { "client_id": "d6165bbf3d804c6196c0c6d7201de970", "client_secret": "25f9e794323b453885f5181f1b624d0b", "scope": "all", "grant_type": "password", "email": userName, "password": password, "auth_type": "email_password" } self.logger.info("用户名:%s开始登录!" % userName) return oc_web def get_AuthenticationURL_OC(self, baseURL, loginURL, lang, timeStamp, clientVersionInfo): ''' :param url: :param lang: :param timeStamp: :param clientVersionInfo: :return: ''' AuthenticationURL = baseURL + loginURL + "?lang=%s&timeStamp=%s&clientVersionInfo=%s" % ( lang, timeStamp, clientVersionInfo) self.logger.info("AuthenticationURL_OC的值为:%s" % AuthenticationURL) return AuthenticationURL def get_AuthenticationURL(self, baseURL, loginURL, lang, timeStamp, clientVersionInfo): ''' :param url: :param lang: :param timeStamp: :param clientVersionInfo: :return: ''' AuthenticationURL = baseURL + loginURL + "?lang=%s&timeStamp=%s&clientVersionInfo=%s" % ( lang, timeStamp, clientVersionInfo) self.logger.info("AuthenticationURL的值为:%s" % AuthenticationURL) return AuthenticationURL def get_AuthenticationURL_SmallProgram(self, baseURL, loginURL, lang, timeStamp): ''' :param url: :param lang: :param timeStamp: :param clientVersionInfo: :return: ''' AuthenticationURL = baseURL + loginURL + "?lang=%s&timeStamp=%s" % ( lang, timeStamp) self.logger.info("AuthenticationURL的值为:%s" % AuthenticationURL) return AuthenticationURL def get_response(self, url, dictPara): ''' :param url: :param dictPara: :return: 获取accesstoken ''' headers = {"Content-Type": "application/json"} try: r = requests.post(url, data=json.dumps(dictPara), headers=headers) result = json.loads(r.text) except: self.logger.info("返回的参数为:%s" % result) self.logger.error("登录失败,请检查您的配置!") else: self.logger.info("返回的参数为:%s" % result) self.logger.info("登录成功") return result def get_Access_token(self, url, dictPara): ''' :param url: :param dictPara: :return: 获取accesstoken ''' headers = {"Content-Type": "application/json"} result = "" try: r = requests.post(url, data=json.dumps(dictPara), headers=headers, timeout=30) result = json.loads(r.text) except: self.logger.info("返回的参数为:%s" % result) self.logger.error("登录失败,请检查您的配置!") else: self.logger.info("返回的参数为:%s" % result) access_token = json.loads(r.text)["data"]["access_token"] self.logger.info("登录成功") return access_token def get_Access_token_not_login(self, url, dictPara): ''' :param url: :param dictPara: :return: 获取accesstoken ''' headers = {"Content-Type": "application/json"} result = "" try: r = requests.post(url, data=json.dumps(dictPara), headers=headers, timeout=30) result = json.loads(r.text) except: self.logger.info("返回的参数为:%s" % result) self.logger.error("鉴权失败,请检查您的配置!") else: self.logger.info("返回的参数为:%s" % result) access_token = json.loads(r.text)["data"]["access_token"] self.logger.info("鉴权成功") return access_token def get_logout_url(self, env): url = "" if env == "prod": url = "http://mi-api.nailtutu.com/oauth/logout" elif env == "uat": url = "http://mi-api-uat.nailtutu.com/oauth/logout" elif env == "dev": url = "http://mi-api-dev.nailtutu.com/oauth/logout" else: self.logger.error("配置有误,请检查!") return url def get_ocweb_really_logout_url(self, url, access_token): ReallyURL = url + "?timeStamp=%s&access_token=%s&lang=zh" % ( base.getTimeStamp(), access_token) self.logger.info("退出登录的URL为:%s" % ReallyURL) return ReallyURL def send_request_logout(self, url): headers = { "Content-Type": "application/json", "User-Agent": "User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" } parameters = {} self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) def logout(self, env, access_token): url = self.get_ocweb_really_logout_url(self.get_logout_url(env), access_token) self.send_request_logout(url)
def run(self, user_code, pwd): # 尝试登录 rtn = self.login(user_code, pwd, app_id=1001) if rtn is None: Logger.error("login failed!user=%s, pwd=%s", user_code, pwd) return login_user = rtn # 重复登录 rtn = self.login(user_code, pwd) if rtn is None: Logger.error("login failed!user=%s, pwd=%s", user_code, pwd) return if rtn["accessToken"] != login_user["accessToken"] or rtn[ "refreshToken"] != login_user["refreshToken"]: Logger.error("repeat login failed!newUser=%s, oldUser=%s", rtn, login_user) return # 尝试验证获取到的授权码 rtn = self.verify_token(login_user["accessToken"]) if not rtn: Logger.error("verify token failed!user=%s", login_user) return print("verify token:", rtn) # 尝试验证查询登录用户信息 rtn = self.verify_user(login_user["uid"], login_user["accessToken"]) if rtn is None: Logger.error("verify user failed!user=%s", login_user) return print("verify user:"******"query user token failed!expect=%s, actual=%s", 1, len(rtn)) print("query_user failed!%s" % rtn) return # 更新授权码 rtn = self.update(login_user["uid"], login_user["accessToken"], login_user["refreshToken"]) if rtn is None: Logger.error("update token failed!user=%s", login_user) return old_access_token = login_user["accessToken"] old_refresh_token = login_user["refreshToken"] login_user = rtn new_access_token = login_user["accessToken"] new_refresh_token = login_user["refreshToken"] if old_access_token == new_access_token or old_refresh_token == new_refresh_token: Logger.error("update token failed!token not refresh.") print("update_token failed!%s" % (rtn.json())) return # 验证旧token rtn = self.verify_token(old_access_token) if rtn: Logger.error("verify old token failed!old_token=%s, new_token=%s", old_access_token, new_access_token) return print("verify old token:", rtn) # 验证新token rtn = self.verify_token(new_access_token) if not rtn: Logger.error("verify new token failed!user=%s", login_user) return print("verify new token:", rtn) # 更新用户密码,不会影响当次登录 new_pwd = "new_pass_word_123456" # print(login_user) req = self.password.update_password(login_user["uid"], pwd, new_pwd, login_user["accessToken"], login_user["appId"]) if not req.ok: Logger.error( "update user password failed!user_code=%s,pwd=%s, msg=%s", user_code, pwd, req.content) return # 更新用户密码为旧密码 # print(login_user) req = self.password.update_password(login_user["uid"], new_pwd, pwd, login_user["accessToken"], login_user["appId"]) if not req.ok: Logger.error( "update user password failed!user_code=%s,pwd=%s, msg=%s", user_code, pwd, req.content) return
class ocweb: def __init__(self): self.logger = Logger(logger="ocweb").getlog() self.base = baseUtils() def get_ocweb_login_url(self, env): ''' :param env: :return: ''' if env == "prod": url = "https://oc-api.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) elif env == "uat": url = "https://oc-api-uat.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) elif env == "dev": url = "https://oc-api-dev.nailtutu.com/oauth/login" self.logger.info("oc_web登录的url为:%s" % url) else: self.logger.error("配置有误,请检查!") return url def get_ocweb_logged_in_URL(self, email, password): ''' :param email: :param password: :return: ''' ocweb_logged_in = { "client_id": "d6165bbf3d804c6196c0c6d7201de970", "client_secret": "25f9e794323b453885f5181f1b624d0b", "scope": "all", "grant_type": "password", "email": email, "password": self.base.MD5(password), "auth_type": "email_password" } self.logger.info("账号【%s】开始登录oc_web......" % email) return ocweb_logged_in def send_request_ocweb_login(self, url, data): ''' :param url: :param data: :return: ''' headers = {"Content-Type": "application/json"} self.logger.info("请求的参数为:%s" % data) r = requests.post(url, data=json.dumps(data), headers=headers) self.logger.info("返回的参数为:%s" % json.loads(r.text)) re = json.loads(r.text) access_token = re["data"]["access_token"] return re, access_token def get_ocweb_logout_url(self, env): if env == "prod": url = "https://oc-api.nailtutu.com/oauth/logout" elif env == "uat": url = "https://oc-api-uat.nailtutu.com/oauth/logout" elif env == "dev": url = "https://oc-api-dev.nailtutu.com/oauth/logout" else: self.logger.error("配置有误,请检查!") return url def get_ocweb_really_logout_url(self, url, access_token): ReallyURL = url + "?timeStamp=%s&access_token=%s&lang=zh" % ( self.base.getTimeStamp(), access_token) self.logger.info("ocweb退出登录的URL为:%s" % ReallyURL) return ReallyURL def send_request_logout(self, url): headers = {"Content-Type": "application/json"} parameters = {} self.logger.info("请求的参数为:%s" % parameters) r = requests.post(url, data=json.dumps(parameters), headers=headers, timeout=30) self.logger.info("返回的参数为:%s" % json.loads(r.text)) return json.loads(r.text) def logout(self, env, access_token): url = self.get_ocweb_really_logout_url(self.get_ocweb_logout_url(env), access_token) self.send_request_logout(url) def login(self, env, email, password): re = self.send_request_ocweb_login( self.get_ocweb_login_url(env), self.get_ocweb_logged_in_URL(email, password)) return re
def createExtensionIndex(*args): socket.setdefaulttimeout(TIMEOUT) indexdir = args[0] product = args[1] START_WORDS = "{\n\"extension_index\":[\n" cur_time = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) root_spe_dir = os.path.join(indexdir,"spe"+cur_time) root_log_dir = os.path.join(indexdir, LOG_DIR_NAME) try: os.mkdir(root_spe_dir) extLogger = Logger(os.path.join(root_log_dir,LOG_INFO),'extLogger') extLogger.info("CreateExtensionIndex script start ...") except IOError as e: raise IOError("IOError: Need permission to write in "+indexdir) index_for_extension = START_WORDS whole_product_name = getWholeProductName(product) extLogger.info("start to get repo data from github ...") ext_output_path = os.path.join(indexdir, RAW_INDEX_FILE) i=0 ok_repo_num = 0 try: githubApiInfo_obj = GithubApiInfoObj() for item in githubApiInfo_obj.item_list: i+=1 index_for_extension_item = INDENT+"{\n" index_for_extension_item += generateJSONStr(item) repo_name = item[githubApiInfo_obj.__class__.REPOSITORY].val extLogger.info(str(i)+" repo: "+repo_name) try: info_json = InfoJSONObj(repo_name) except ValueError as e: raise e except Exception as e: extLogger.warning(str(e)) continue index_for_extension_item += generateJSONStr(info_json.item_list) repo_software = info_json.item_list[info_json.__class__.SOFTWARE].val index_for_extension_item += INDENT*2 + "\"download_link\":" +"\"" + re.sub('repos_name', repo_name, SPE_DOWNLOAD_URL) +"\",\n" index_for_extension_item += INDENT*2 + "\"image_link\":" +"\"" + re.sub('repos_name', repo_name, IMG_DOWNLOAD_URL) +"\",\n" if repo_software != whole_product_name: extLogger.info("This is not a " + whole_product_name + " repo. Switch to next repo.") continue repo_spe_url = re.sub('repos_name', repo_name, SPE_DOWNLOAD_URL) spe_name = repo_name+".spe" spe_saving_path = os.path.join(root_spe_dir,repo_name) os.mkdir(spe_saving_path) try: urllib.request.urlretrieve(repo_spe_url, os.path.join(spe_saving_path,spe_name)) srcZip = zipfile.ZipFile(os.path.join(spe_saving_path,spe_name), "r", zipfile.ZIP_DEFLATED) except: extLogger.warning("This repo '"+repo_name+" does not have spe package. Please check! Switch to next repo.") continue for file in srcZip.namelist(): if not os.path.isdir(spe_saving_path): os.mkdir(spe_saving_path) if FILE_NAME in file: srcZip.extract(file, spe_saving_path) srcZip.close() meta_path = os.path.join(spe_saving_path, META_DIR, FILE_NAME) metaObj = MetaObj(meta_path) index_for_extension_item += metaObj.generateExtensionJSON() index_for_extension_item += INDENT + "},\n" index_for_extension += index_for_extension_item ok_repo_num += 1 extLogger.info("Successfully get data!") index_for_extension = index_for_extension[0:-2] index_for_extension += '\n]\n}' index_for_extension_fp = open(ext_output_path,'w', encoding='utf-8') index_for_extension_fp.write(index_for_extension) index_for_extension_fp.close() extLogger.info("CreateIndexForDownloadExtensiosn action succeeded!") extLogger.info("Extension index file has been saved in "+ext_output_path) except Exception as e: extLogger.error(str(e), e) extLogger.info("CreateIndexForDownloadExtensiosn action failed!") raise e finally: extLogger.info("Totally get "+str(ok_repo_num)+" repo data.") clear(root_spe_dir) if not os.path.exists(ext_output_path): raise Exception("Fail to create extension index file! Please contact github administrator!") return ext_output_path
class Speaker(object): """docstring for Speaker""" def __init__(self): self.log = Logger("main").logger() self.ard = Arduino() self.process = DataProcess() self.r = Relay() self.keyword = ParseCSV('../testfile/testcase/{}_keyword.csv'.format( self.ard.project)) config = Config('../config/{}.ini'.format(self.ard.project)) config.cfg_load() self.led_num = config.cfg.getint('config', 'led') self.power_button = config.cfg.get('config', 'power_button') m_cfg = Config(main_cfg_path) m_cfg.cfg_load() self.led1_data = m_cfg.cfg.get('Data', 'led1') self.sound_data = m_cfg.cfg.get('Data', 'sound') if not self.r.init_relay(): self.log.error('Something is wrong with your relay!') sys.exit() def behave(self, name): row = self.keyword.get_row(name) self.log.info(name) button = row.get('power_button_press') time_wait = row.get('time_wait') audio_cue = row.get('audio_cue') tmp = re.findall(r'[^()]+', row.get('led1')) exp_led = tmp[1].split('-') if 'n/a' not in button.lower(): if 'data' != tmp[0]: self.ard.Thread_detect_data(self.led_num, time_wait) self.r.press(self.power_button, button) else: self.r.press(self.power_button, button) time.sleep(time_wait) self.ard.Thread_detect_data(self.led_num, time_wait) time.sleep(time_wait * 2) assert self.process.verify_data(self.led1_data, tmp[0], exp_led) if 'y' in audio_cue.lower(): exp = re.findall(r'[^()]+', audio_cue)[1].split('-') assert self.process.verify_data(self.sound_data, 'audio_cue', exp) else: time.sleep(time_wait - 25) self.ard.Thread_detect_data(self.led_num, 25) time.sleep(25 * 1.5) if 'y' in audio_cue.lower(): exp = re.findall(r'[^()]+', audio_cue)[1].split('-') assert self.process.verify_data(self.sound_data, 'audio_cue', exp) time.sleep(2) self.ard.Thread_detect_data(self.led_num, 3) time.sleep(3 * 2) assert self.process.verify_data(self.led1_data, tmp[0], exp_led) def power_on(self): self.behave('power on') def power_off(self): self.behave('power off') def bt_pairing(self): self.behave('BT pairing') def bt_cancel_pairing(self): self.behave('BT cancel pairing') def bt_pairing_timeout(self): self.behave('BT pairing timeout')
def run(self): # db = MongoProcess("uiac") # db.init() # # Logger.init() base_url = self.baseUrl cache = Cache(base_url) cache.clean() u = User(base_url) u_status = UserStatus(base_url) a_auth = Authorization(base_url) user_code = "13400000001" user_code_2 = "13400000002" user_code_3 = "13400000003" password = "******" user_info = AccountUtils.get_user_info() extend_info = AccountUtils.get_extend_info() token_err = "token" token_1 = "" app_id_err = -1 app_id_1 = 1001 # 注册三个测试账号, 其中uid_1为有操作权限的管理员账号 response = u.register(user_code, password, user_info, extend_info) uid_1 = response.json()["uid"] response = a_auth.login(user_code, password, app_id_1) token_1 = response.json()["accessToken"] response = u.register(user_code_2, password, user_info, extend_info) uid_2 = response.json()["uid"] response = u.register(user_code_3, password, user_info, extend_info) uid_3 = response.json()["uid"] # 禁用账号 ************************************************************************************ Logger.info("# 禁用账号 ************************************************************************************") # uid不合法 response = u_status.disable_user(0, uid_2, token_1, app_id_1) if response.json()["code"] != "10000023": Logger.error("disable user, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u_status.disable_user(uid_1, uid_2, token_err, app_id_1) # if response.json()["code"] != "10000002": # Logger.error("disable user, expected code is 10000002") # Logger.error(str(response.json())) # app_id不合法 response = u_status.disable_user(uid_1, uid_2, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("disable user, expected code is 10000003") Logger.error(str(response.json())) # 禁用成功 response = u_status.disable_user(uid_1, uid_2, token_1, app_id_1) if str(response.json()["result"]) != "True": Logger.error("disable user, expected result is \"True\"") Logger.error(str(response.json())) # 用户被禁用后,不允许登录 response = a_auth.login(user_code_2, password, app_id_1) if response.json()["code"] != "10000005": Logger.error("disable user, expected code is 10000005") Logger.error(str(response.json())) # 查看禁用状态 ************************************************************************************ Logger.info("# 查看禁用状态 ************************************************************************************") # uid不合法 response = u_status.get_status(0, uid_2, token_1, app_id_1) if response.json()["code"] != "10000023": Logger.error("get user_status, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u_status.get_status(uid_1, uid_2, token_err, app_id_1) # if response.json()["code"] != "10000002": # Logger.error("get user_status, expected code is 10000002") # Logger.error(str(response.json())) # app_id不合法 response = u_status.get_status(uid_1, uid_2, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("get user_status, expected code is 10000003") Logger.error(str(response.json())) # 获取成功 response = u_status.get_status(uid_1, uid_2, token_1, app_id_1) if str(response.json()["result"]) != "False": Logger.error("get user_status, expected result is \"False\"") Logger.error(str(response.json())) response = u_status.get_status(uid_1, uid_3, token_1, app_id_1) if str(response.json()["result"]) != "True": Logger.error("enable user, expected result is \"True\"") Logger.error(str(response.json())) # 用户解禁 ************************************************************************************ Logger.info("# 用户解禁 ************************************************************************************") # uid不合法 response = u_status.enable_user(0, uid_2, token_1, app_id_1) if response.json()["code"] != "10000023": Logger.error("enable user, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u_status.enable_user(uid_1, uid_2, token_err, app_id_1) # if response.json()["code"] != "10000002": # Logger.error("enable user, expected code is 10000002") # Logger.error(str(response.json())) # app_id不合法 response = u_status.enable_user(uid_1, uid_2, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("enable user, expected code is 10000003") Logger.error(str(response.json())) # 解禁成功 response = u_status.enable_user(uid_1, uid_2, token_1, app_id_1) if str(response.json()["result"]) != "True": Logger.error("enable user, expected result is \"True\"") Logger.error(str(response.json())) # 解禁成功后,能正常登陆 response = a_auth.login(user_code_2, password, app_id_1) if response.json()["uid"] != uid_2: Logger.error("enable user, fail to login") Logger.error(str(response.json()))
def test_single_mul_app(self): appid1 = "1" appid2 = "2" user_code = "13311223355" pwd = "123456" # 注册用户 req = self.user.register(user_code, pwd, "", "") if not req.ok: Logger.error("register user failed!user_code=%s,pwd=%s, msg=%s", user_code, pwd, req.content) return tmp_user = req.json() self.context.add_register_user(tmp_user) # 尝试登录 appId1 rtn = self.login(user_code, pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code, pwd, appid1) return login_user1 = rtn # 尝试登录 appId2 rtn = self.login(user_code, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code, pwd, appid2) return login_user2 = rtn app1_token = login_user1["accessToken"] app2_token = login_user2["accessToken"] if app1_token == app2_token: Logger.error("login multi app failed!app1User=%s, app2User=%s", login_user1, login_user2) return # 尝试验证查询登录用户信息 rtn = self.verify_user(login_user1["uid"], app1_token) if rtn is None: Logger.error("verify user failed!user=%s", login_user1) return if len(rtn) != 2: Logger.error("query user token failed!expect=%s, actual=%s", 2, len(rtn)) return rtn2 = self.verify_user(login_user2["uid"], app2_token) if rtn2 is None: Logger.error("verify user failed!user=%s", login_user2) return if len(rtn2) != 2: Logger.error("query user token failed!expect=%s, actual=%s", 2, len(rtn2)) return # 注销用户 rtn = self.logout(login_user1["uid"], app1_token) if rtn is None: Logger.error("logout failed!user=%s", login_user1) return rtn = self.verify_user(login_user2["uid"], app2_token) if rtn is None: Logger.error("verify user failed!user=%s", login_user2) return if len(rtn) != 1: Logger.error("query user token failed!expect=%s, actual=%s", 1, len(rtn)) return rtn = self.logout(login_user2["uid"], app2_token) if rtn is None: Logger.error("logout failed!user=%s", login_user2) return
def CreateIndex(*args): print("size"+str(len(args))) outdir = args[0] product = args[1] if product == "modeler": repos_set_uri = RAW_REPOS_SET_URI.format('modeler','modeler') index_key = RAW_INDEX_KEY.format('modeler') elif product == 'stats': # wrong spell of statistics repos_set_uri = RAW_REPOS_SET_URI.format('statisitcs','stats') index_key = RAW_INDEX_KEY.format('stats') try: lic_path = os.path.join(outdir,LICENSE_DIR) os.mkdir(lic_path) logger = Logger(os.path.join(lic_path,LOG_INFO)) logger.info("Script start ...") except IOError as e: raise IOError("IOError: Need permission to write in "+outdir) try: try: repos_set_json = json.loads(URILoader.loadURI(repos_set_uri, "index file")) except ValueError as e: raise Exception("ValueError: The {0} has an illegal format. Please check!\n\n".format("index file")) except Exception as e: raise e try: repos_set_json_index = repos_set_json[index_key] except Exception as e: raise e license_obj_list = [] for repo in repos_set_json_index: try: repo_name = repo["repository"] except Exception: raise Exception("At least one repository in index file does not have repo name. Please check!\n\n") repo_license_uri = RAW_LICENSE_URI.format(repo_name) try: repo_license_content = URILoader.loadURI(repo_license_uri, "license file") except Exception as e: raise e isExistedLicense = False for item in license_obj_list: if repo_license_content == item.getLicenseContent(): isExistedLicense = True item.addRepoName(repo_name) break if not isExistedLicense: addObj(repo_name, repo_license_content,license_obj_list) print("Start to read license...") index_content = "{\n"+INDENT+"\"license_index\": [\n"; for obj in license_obj_list: index_item_str = LicenseIndexItemStr.getItemStr(obj) index_content += index_item_str license_fp = open(os.path.join(lic_path,obj.getLicenseName()),'w') license_fp.write(obj.getLicenseContent()) license_fp.close() index_content = index_content[0:-2] index_content += '\n' + INDENT + "]\n}" print("Start to write license to text...") index_fp = open(os.path.join(lic_path,'license_index.json'),'w') index_fp.write(index_content) except Exception as e: logger.error(str(e))
def test(self): appid1 = "1001" appid2 = "1002" user_code1 = "13311224400" user_code2 = "13311225500" pwd = "123456" # 注册用户 req = self.user.register(user_code1, pwd, "", "") if not req.ok: Logger.error("register user failed!user_code=%s,pwd=%s, msg=%s", user_code1, pwd, req.content) return req = self.user.register(user_code2, pwd, "", "") if not req.ok: Logger.error("register user failed!user_code=%s,pwd=%s, msg=%s", user_code1, pwd, req.content) return # 先登录一次 rtn = self.login(user_code1, pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code1, pwd, appid1) return login_user1 = rtn rtn = self.login(user_code2, pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code2, pwd, appid1) return login_user2 = rtn # 休眠30秒 time.sleep(30) # 再次登录app2 rtn = self.login(user_code1, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code1, pwd, appid2) return login_user1_1 = rtn rtn = self.login(user_code2, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code2, pwd, appid2) return login_user2_1 = rtn # 休眠35秒,等待第一次登录过期 time.sleep(35) # 尝试验证查询登录用户信息 rtn = self.verify_user(login_user1_1["uid"], login_user1_1["accessToken"]) if rtn is None: Logger.error("verify user failed!user=%s", login_user1_1) return if len(rtn) != 1: Logger.error( "query user token failed!expect=%s, actual=%s, rtn=%s", 1, len(rtn), rtn) return # 验证用户的Token应该会失败 rtn = self.verify_token(login_user1["accessToken"]) if rtn: Logger.error("verify update user1 status token failed!user=%s", login_user1) return rtn = self.verify_token(login_user2["accessToken"]) if rtn: Logger.error("verify update user2 status token failed!user=%s", login_user2) return rtn = self.verify_token(login_user1_1["accessToken"]) if not rtn: Logger.error("verify update user1_1 status token failed!user=%s", login_user1_1) return rtn = self.verify_token(login_user2_1["accessToken"]) if not rtn: Logger.error("verify update user2_1 status token failed!user=%s", login_user2_1) return # 休眠35秒,等待第二次登录过期 time.sleep(30) # 验证用户的Token应该会失败 rtn = self.verify_token(login_user1_1["accessToken"]) if rtn: Logger.error("verify update user1_1 status token failed!user=%s", login_user1_1) return rtn = self.verify_token(login_user2_1["accessToken"]) if rtn: Logger.error("verify update user2_1 status token failed!user=%s", login_user2_1) return
def test(self): appid1 = "1" appid2 = "2" user_code1 = "13311224400" user_code2 = "13311225500" pwd = "123456" port1 = 9527 port2 = 9528 local_url1 = "http://%s:%s" % (self.get_lan_ip(), port1) local_url2 = "http://%s:%s" % (self.get_lan_ip(), port1) self.http_svc1 = PushObserverClientServer("", port1, appid1) self.http_svc2 = PushObserverClientServer("", port2, appid2) # 注册用户 print("register user!") req = self.user.register(user_code1, pwd, "", "") if not req.ok: Logger.error("register user failed!user_code=%s,pwd=%s, msg=%s", user_code1, pwd, req.content) return req = self.user.register(user_code2, pwd, "", "") if not req.ok: Logger.error("register user failed!user_code=%s,pwd=%s, msg=%s", user_code1, pwd, req.content) return # 未注册监听请求前,先发起一次登录请求 print("user login for app1!") rtn = self.login(user_code1, pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code1, pwd, appid1) return login_user1 = rtn rtn = self.login(user_code2, pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code2, pwd, appid1) return login_user2 = rtn time.sleep(8) app1_login_count = 0 app1_update_count = 0 app1_logout_count = 0 app2_login_count = 0 app2_update_count = 0 app2_logout_count = 0 # 开始客户端的HTTP服务 print("start local http server!") self.http_svc1.start() self.http_svc2.start() #注册监听 print("register lcoal observer for app1 and app2!") req = self.push_obj.register(local_url1, appid1) if not req.ok: Logger.error( "register observer failed!callback_url=%s,appid=%s, msg=%s", local_url1, appid1, req.content) return req = self.push_obj.register(local_url2, appid2) if not req.ok: Logger.error( "register observer failed!callback_url=%s,appid=%s, msg=%s", local_url2, appid2, req.content) return # 登录app2 print("login user for app2") rtn = self.login(user_code1, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code1, pwd, appid2) return login_user1_1 = rtn app2_login_count += 1 rtn = self.login(user_code2, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code2, pwd, appid2) return login_user2_1 = rtn app2_login_count += 1 print("waiting message callback!") time.sleep(8) msg1 = self.http_svc1.get_message() tmpMsg = msg1.get("Authroize_login") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app1_login_count: Logger.error( "check Authroize_login message count failed!except=%s, actual=%s, appId=%s, msg=%s", app1_login_count, len(msg1), appid1, msg1) return msg2 = self.http_svc2.get_message() tmpMsg = msg2.get("Authroize_login") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app2_login_count: Logger.error( "check Authroize_login message count failed!except=%s, actual=%s, appId=%s, msg=%s", app2_login_count, len(msg2), appid2, msg2) return # 用户注销 print("logout for app1") rtn = self.logout(login_user1["uid"], login_user1["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid1, login_user1) return app1_logout_count += 1 rtn = self.logout(login_user2["uid"], login_user2["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid1, login_user2) return app1_logout_count += 1 # 重复注销,通知应该是不会累加1的 rtn = self.logout(login_user2["uid"], login_user2["accessToken"]) if rtn is not None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid1, login_user2) return rtn = self.logout(login_user1["uid"], login_user1["accessToken"]) if rtn is not None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid1, login_user1) return print("waiting message receive!") time.sleep(8) # 判断注销请求数量 msg1 = self.http_svc1.get_message() tmpMsg = msg1.get("Authroize_Logout") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app1_logout_count: Logger.error( "check Authroize_Logout message count failed!except=%s, actual=%s, appId=%s, msg=%s", app1_logout_count, len(msg1), appid1, msg1) return msg2 = self.http_svc2.get_message() tmpMsg = msg2.get("Authroize_Logout") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app2_logout_count: Logger.error( "check Authroize_Logout message count failed!except=%s, actual=%s, appId=%s, msg=%s", app2_logout_count, len(msg2), appid2, msg2) return # 注销App2登录 print("logout for app2") rtn = self.logout(login_user1_1["uid"], login_user1_1["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid2, login_user1_1) return app2_logout_count += 1 rtn = self.logout(login_user2_1["uid"], login_user2_1["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid2, login_user2_1) return app2_logout_count += 1 print("waiting app2 logout message!") time.sleep(8) # 判断注销请求数量 msg1 = self.http_svc1.get_message() tmpMsg = msg1.get("Authroize_Logout") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app1_logout_count: Logger.error( "check Authroize_Logout message count failed!except=%s, actual=%s, appId=%s, msg=%s", app1_logout_count, len(msg1), appid1, msg1) return msg2 = self.http_svc2.get_message() tmpMsg = msg2.get("Authroize_Logout") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app2_logout_count: Logger.error( "check Authroize_Logout message count failed!except=%s, actual=%s, appId=%s, msg=%s", app2_logout_count, len(msg2), appid2, msg2) return # 重新登录 print("user login for app1 and app2!") rtn = self.login(user_code1, pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code1, pwd, appid1) return print("user1 app1 login:%s" % rtn) login_user1 = rtn app1_login_count += 1 rtn = self.login(user_code2, pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code2, pwd, appid1) return print("user2 app1 login:%s" % rtn) login_user2 = rtn app1_login_count += 1 rtn = self.login(user_code1, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code1, pwd, appid2) return print("user1 app2 login:%s" % rtn) login_user1_1 = rtn app2_login_count += 1 rtn = self.login(user_code2, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code2, pwd, appid2) return print("user2 app2 login:%s" % rtn) login_user2_1 = rtn app2_login_count += 1 time.sleep(6) rtn = self.login(user_code2, pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", user_code2, pwd, appid2) return login_user2_1 = rtn app2_login_count += 1 print("waiting login message callback!") time.sleep(8) msg1 = self.http_svc1.get_message() tmpMsg = msg1.get("Authroize_login") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app1_login_count: Logger.error( "check Authroize_login message count failed!except=%s, actual=%s, appId=%s, msg=%s", app1_login_count, len(msg1), appid1, msg1) return msg2 = self.http_svc2.get_message() tmpMsg = msg2.get("Authroize_login") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app2_login_count: Logger.error( "check Authroize_login message count failed!except=%s, actual=%s, appId=%s, msg=%s", app2_login_count, len(msg2), appid2, msg2) return # 更新授权码 print("update token message test for app1 and app2") rtn = self.update(login_user1["uid"], login_user1["accessToken"], login_user1["refreshToken"], login_user1["appId"]) if rtn is None: Logger.error("update user1 app1 token failed!user=%s", login_user1) return login_user1 = rtn app1_update_count += 1 rtn = self.update(login_user2["uid"], login_user2["accessToken"], login_user2["refreshToken"], login_user2["appId"]) if rtn is None: Logger.error("update user2 app1 token failed!user=%s", login_user2) return login_user2 = rtn app1_update_count += 1 rtn = self.update(login_user1_1["uid"], login_user1_1["accessToken"], login_user1_1["refreshToken"], login_user1_1["appId"]) if rtn is None: Logger.error("update user1 app2 token failed!user=%s", login_user1_1) return login_user1_1 = rtn app2_update_count += 1 rtn = self.update(login_user2_1["uid"], login_user2_1["accessToken"], login_user2_1["refreshToken"], login_user2_1["appId"]) if rtn is None: Logger.error("update user2 app2 token failed!user=%s", login_user2_1) return login_user2_1 = rtn app2_update_count += 1 print("waiting update message callback!") time.sleep(8) msg1 = self.http_svc1.get_message() tmpMsg = msg1.get("Authorize_Update") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app1_update_count: Logger.error( "check Authorize_Update message count failed!except=%s, actual=%s, appId=%s, msg=%s", app1_update_count, len(msg1), appid1, msg1) return msg2 = self.http_svc2.get_message() tmpMsg = msg2.get("Authorize_Update") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app2_update_count: Logger.error( "check Authorize_Update message count failed!except=%s, actual=%s, appId=%s, msg=%s", app2_update_count, len(msg2), appid2, msg2) return # 删除监听 print("unregister observer!") req = self.push_obj.un_register(appid1) print("logout for app1 and app2") rtn = self.logout(login_user1["uid"], login_user1["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid1, login_user1) return rtn = self.logout(login_user2["uid"], login_user2["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid1, login_user2) return rtn = self.logout(login_user1_1["uid"], login_user1_1["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid2, login_user1_1) return app2_logout_count += 1 rtn = self.logout(login_user2_1["uid"], login_user2_1["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s, appId=%s, user=%s", user_code1, appid2, login_user2_1) return app2_logout_count += 1 print("waiting app2 logout message callback!") time.sleep(8) # 判断注销请求数量 msg1 = self.http_svc1.get_message() tmpMsg = msg1.get("Authroize_Logout") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app1_logout_count: Logger.error( "check Authroize_Logout message count failed!except=%s, actual=%s, appId=%s, msg=%s", app1_logout_count, len(msg1), appid1, msg1) return msg2 = self.http_svc2.get_message() tmpMsg = msg2.get("Authroize_Logout") if tmpMsg is None: tmpMsg = [] if len(tmpMsg) != app2_logout_count: Logger.error( "check Authroize_Logout message count failed!except=%s, actual=%s, appId=%s, msg=%s", app2_logout_count, len(msg2), appid2, msg2) return req = self.push_obj.un_register(appid2) # 停止HTTP服务 self.http_svc1.stop() self.http_svc2.stop() self.http_svc1 = None self.http_svc2 = None
def createIndex(*args): indexdir = args[0] product = args[1] START_WORDS = '{\n"productname_extension_index":[\n' cur_time = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) root_spe_dir = os.path.join(indexdir, "spe" + cur_time) try: os.mkdir(root_spe_dir) logger = Logger(os.path.join(indexdir, LOG_INFO)) logger.info("Script start ...") except IOError as e: raise IOError("IOError: Need permission to write in " + indexdir) index_for_extension = re.sub("productname", product, START_WORDS) whole_product_name = getWholeProductName(product) print("start to get repo data from github ...") logger.info("start to get repo data from github ...") i = 0 ok_repo_num = 0 try: githubApiInfo_obj = GithubApiInfoObj() for item in githubApiInfo_obj.item_list: i += 1 print(i) index_for_extension_item = INDENT + "{\n" index_for_extension_item += generateJSONStr(item) repo_name = item[githubApiInfo_obj.__class__.REPOSITORY].val logger.info(str(i) + "th repo: " + repo_name) print(repo_name) try: info_json = InfoJSONObj(repo_name) except ValueError as e: raise e except Exception as e: print(str(e)) logger.warning(str(e)) continue index_for_extension_item += generateJSONStr(info_json.item_list) repo_software = info_json.item_list[info_json.__class__.SOFTWARE].val index_for_extension_item += ( INDENT * 2 + '"download_link":' + '"' + re.sub("repos_name", repo_name, SPE_DOWNLOAD_URL) + '",\n' ) index_for_extension_item += ( INDENT * 2 + '"image_link":' + '"' + re.sub("repos_name", repo_name, IMG_DOWNLOAD_URL) + '",\n' ) if repo_software != whole_product_name: print("This is not a " + whole_product_name + " repo.\nSwitch to next repo.\n\n\n") logger.info("This is not a " + whole_product_name + " repo.\nSwitch to next repo.") continue repo_spe_url = re.sub("repos_name", repo_name, SPE_DOWNLOAD_URL) spe_name = repo_name + ".spe" spe_saving_path = os.path.join(root_spe_dir, repo_name) os.mkdir(spe_saving_path) try: urllib.request.urlretrieve(repo_spe_url, os.path.join(spe_saving_path, spe_name)) srcZip = zipfile.ZipFile(os.path.join(spe_saving_path, spe_name), "r", zipfile.ZIP_DEFLATED) except: print( "This repo '" + repo_name + "' does not have spe package. Please check!" + "\nSwitch to next repo.\n\n\n" ) logger.warning( "This repo '" + repo_name + "' does not have spe package. Please check!" + "\nSwitch to next repo." ) continue for file in srcZip.namelist(): if not os.path.isdir(spe_saving_path): os.mkdir(spe_saving_path) if FILE_NAME in file: srcZip.extract(file, spe_saving_path) srcZip.close() meta_path = os.path.join(spe_saving_path, META_DIR, FILE_NAME) metaObj = MetaObj(meta_path) index_for_extension_item += metaObj.generateExtensionJSON() index_for_extension_item += INDENT + "},\n" index_for_extension += index_for_extension_item print("Successfully get data!\n\n") ok_repo_num += 1 logger.info("Successfully get data!\n") index_for_extension = index_for_extension[0:-2] index_for_extension += "\n]\n}" index_for_extension_fp = open(os.path.join(indexdir, INDEX_FILE), "w") index_for_extension_fp.write(index_for_extension) index_for_extension_fp.close() except Exception as e: print(str(e)) logger.error(str(e)) finally: print("Totally get " + str(ok_repo_num) + " repo data successfully!\n\n") logger.info("Totally get " + str(ok_repo_num) + " repo data successfully!") clear(root_spe_dir)
class single: def __init__(self): self.logger = Logger(logger="single").getlog() warnings.simplefilter("ignore", ResourceWarning) def get_singleURL(self, baseurl, url, lang, timeStamp, clientVersionInfo, access_token): ''' :param url: :param lang: :param timeStamp: :param clientVersionInfo: :return: ''' singleURL = baseurl + url + "?lang=%s&timeStamp=%s&clientVersionInfo=%s&access_token=%s" % ( lang, timeStamp, clientVersionInfo, access_token) self.logger.info("单图片上传接口的URL为:%s" % singleURL) return singleURL @retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000, retry_on_result=_result) def send_request_single(self, url, targetid, filename, filepath): ''' :param url: :param targetid: :param filename: :param filepath: :return: ''' proportion = False files = { "file": (filename, open(filepath, "rb"), "multipart/form-data", {}) } if targetid == "社区": data = { "targetid": "48d1cd1ef8d846d98897cf68f12dba01", "sizes": "240x360", "proportion": proportion } elif targetid == "头像": data = { "targetid": "329ad03f9c6c4db59caffbbe3b02e1e6", "sizes": "240x360", "proportion": proportion } elif targetid == "甲面": data = { "targetid": "6e9800a33d364c298a9e515ac3a2a9bc", "sizes": "240x360", "proportion": proportion } elif targetid == "DIY": data = { "targetid": "cb8b8835e5f84249958f01d1b2f47b07", "sizes": "240x360", "proportion": proportion } elif targetid == "系统": data = { "targetid": "4305685b17aa11e9b53f005056ad4128", "sizes": "240x360", "proportion": proportion } else: self.logger.error("targetid不存在,请检查配置!") self.logger.info("单图片上传的请求参数为%s" % data) r = requests.request("post", url, data=data, files=files, timeout=30) re = r.text josnre = json.loads(re) self.logger.info("单图片上传接口的返回值为:%s" % josnre) return json.loads(re)
def test(self): appid1 = "1001" appid2 = "1002" user_code = 13311224000 user_code_end = 13311225000 pwd = "123456" # 注册用户 print("register user call!") for num in range(user_code, user_code_end): req = self.user.register(str(num), pwd, "", "") if not req.ok: Logger.error( "register user failed!user_code=%s,pwd=%s, msg=%s", user_code, pwd, req.content) return # 多用户登录 print("test user login call!") login_user = [] uids = [] for num in range(user_code, user_code_end): rtn = self.login(str(num), pwd, appid1) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", num, pwd, appid1) return login_user.append(rtn) rtn = self.login(str(num), pwd, appid2) if rtn is None: Logger.error("login failed!user=%s, pwd=%s, appId=%s", num, pwd, appid2) return login_user.append(rtn) uids.append(rtn) # 验证多用户的授权码是否有效 print("test access token call!") for login_obj in login_user: rtn = self.verify_token(login_obj["accessToken"]) if not rtn: Logger.error("verify new token failed!user=%s", login_obj) return # 验证登录数量 print("test user login number call!") for login_obj in uids: rtn = self.verify_user(login_obj["uid"], login_obj["accessToken"]) if rtn is None: Logger.error("verify user failed!user=%s", login_obj) return if len(rtn) != 2: Logger.error("query user token failed!expect=%s, actual=%s", 2, len(rtn)) return # 注销 print("test user logout call!") for login_obj in login_user: rtn = self.logout(login_obj["uid"], login_obj["accessToken"]) if rtn is None: Logger.error("logout failed!user=%s", login_obj)
def createLangIndex(*args): outdir = args[0] ext_path = args[1] # if product == "modeler": # index_key = RAW_INDEX_KEY.format('modeler') # elif product == 'stats': # index_key = RAW_INDEX_KEY.format('stats') index_key = RAW_INDEX_KEY langLogger = None try: lang_path = os.path.join(outdir, LANG_DIR) os.mkdir(lang_path) root_log_dir = os.path.join(outdir, LOG_DIR_NAME) langLogger = Logger(os.path.join(root_log_dir, LOG_INFO), "langLogger") langLogger.info("CreateLicenseIndex script start ...") langLogger.info("Get extension index ...") repos_set_json = loadExtJSONContent(ext_path) repos_set_json_index = repos_set_json[index_key] for lang_item in LANG_LIST: fp_content = LANG_INDEX_PRE langLogger.info("Start to get '" + lang_item + "' file") i = 0 for repo in repos_set_json_index: try: repo_name = repo["repository"] i += 1 langLogger.info(lang_item + ": " + str(i) + " repo " + repo_name) except Exception: raise Exception("At least one repository in index file does not have repo name. Please check!") repo_lang_uri = RAW_REPO_LANG_URI.format(repo_name, lang_item) try: repo_lang_content = URILoader.loadURI(repo_lang_uri, "language index file") lang_json_str = LangPropObj.convertToJSONStr(repo_name, repo_lang_content, lang_item) except Exception as e: if "HTTPError" in str(e): # some repositories do not have lang file, by default use en file in index for extension file lang_json_str = LangPropObj.generateJSONStr( repo_name, repo[EXT_KEY]["Summary"], repo[EXT_KEY]["Description"] ) else: raise e fp_content += lang_json_str try: fp_content = fp_content[0:-2] + "\n" + INDENT + "]\n}" fp = open(os.path.join(lang_path, lang_item + ".json"), "w", encoding="utf-8") fp.write(fp_content) fp.close() except Exception as e: raise e except Exception as e: if langLogger != None: langLogger.error(str(e), e) langLogger.info("CreatelangIndex action failed!") raise e finally: if langLogger != None: langLogger.close()
class ExchangeDataListener: __metaclass__ = ABCMeta def __init__(self, exchangeClient, marketDataManager, marketRulesManager, portfolioManager, orderManager): self.exchangeClient = exchangeClient self.marketDataManager = marketDataManager self.marketRulesManager = marketRulesManager self.portfolioManager = portfolioManager self.orderManager = orderManager self.candleSubscriptions = [] self.candleListenerCallbackMap = {} self.callbackPortfolio = None self.callbackOrders = None self.cretenExecDetlId = None self.lock = RLock() self.log = Logger() @abstractmethod def start(self): pass @abstractmethod def parseCandleUpdate(self, msg): pass @abstractmethod def parseOrderUpdate(self, msg): pass @abstractmethod def parsePortfolioUpdate(self, msg): pass def setCretenExecDetlId(self, id): self.cretenExecDetlId = id def resetCandleListener(self): self.candleSubscriptions = [] self.candleListenerCallbackMap = {} def resetUserDataListener(self): self.callbackPortfolio = None self.callbackOrders = None def registerCandleListener(self, pair, cretenInterval, callback): self.candleSubscriptions.append( CandleSubscriptionKey(pair, cretenInterval)) self.candleListenerCallbackMap[CandleSubscriptionKey( pair, cretenInterval)] = makeList(callback) def registerUserDataListener(self, callbackPortfolio=None, callbackOrders=None): self.callbackPortfolio = makeList(callbackPortfolio) self.callbackOrders = makeList(callbackOrders) def processCandleUpdate(self, msg): with self.lock: self.log.debug('Candle message received: ' + str(msg)) try: # 1. parse incoming message candle = self.parseCandleUpdate(msg) # 2. update market data manager self.marketDataManager.processCandle(candle) # 3. invoke custom callbacks for callback in self.candleListenerCallbackMap[ CandleSubscriptionKey( Pair(candle.getBaseAsset(), candle.getQuoteAsset()), candle.getInterval())]: callback(candle) Db.Session().commit() except: self.log.error('Candle processing failed! Msg [' + str(msg) + ']') Db.Session().rollback() raise finally: Db.Session.remove() try: # 4. create outbound orders self.orderManager.sendOrders(self.cretenExecDetlId) Db.Session().commit() except: self.log.error( 'Candle processing failed while generating outbound orders! Msg [' + str(msg) + ']') Db.Session().rollback() raise finally: Db.Session.remove() def processOrderUpdate(self, msg): with self.lock: try: orderResponse = self.parseOrderUpdate(msg) # TODO verify that the order is incoming from this instance of creten # process only orders originating from creten if orderResponse.getClientOrderId( )[:len(Settings.ORDER_REFERENCE_PREFIX )] == Settings.ORDER_REFERENCE_PREFIX: self.orderManager.processOrderUpdate(orderResponse) if self.callbackOrders: for callback in self.callbackOrders: callback(orderResponse) else: self.log.info( 'Order message received for an order not originating from Creten. Msg [' + str(msg) + ']') Db.Session().commit() except: self.log.error('Order update processing failed! Msg [' + str(msg) + ']') Db.Session().rollback() raise finally: Db.Session.remove() try: # create outbound orders self.orderManager.sendOrders(self.cretenExecDetlId) Db.Session().commit() except: self.log.error( 'Order update processing failed while generating outbound orders! Msg [' + str(msg) + ']') Db.Session().rollback() raise finally: Db.Session.remove() def processPortfolioUpdate(self, msg): with self.lock: try: positions = self.parsePortfolioUpdate(msg) for position in positions: self.portfolioManager.addPosition(position) if self.callbackPortfolio: for callback in self.callbackPortfolio: callback(position) Db.Session().commit() except: self.log.error('Portfolio update processing failed! Msg [' + str(msg) + ']') Db.Session().rollback() raise finally: Db.Session.remove()
runCreateLicenseIndex.join() runCreateLangIndex.join() if runCreateLicenseIndex.exitcode!=0: raise Exception('Exception in ' + runCreateLicenseIndex.getName()+'\n'+runCreateLicenseIndex.exc_traceback) if runCreateLangIndex.exitcode!=0: raise Exception('Exception in ' + runCreateLangIndex.getName()+'\n'+runCreateLangIndex.exc_traceback) mainLogger.info("'CreateLicenseIndex' thread complete...") mainLogger.info("'CreateLangIndex' thread complete...") # start to compress files to ZIP if not runCreateLangIndex.isAlive() and not runCreateLicenseIndex.isAlive(): mainLogger.info("Start to compress files into package ...") zippath = os.path.join(savePath,ZIP_NAME+'.zip') zip_dir(savePath, zippath) mainLogger.info("Compression complete ...") except Exception as e: mainLogger.error(str(e),e) raise e finally: mainLogger.close() except Exception as e: print(str(traceback.format_exc())) except IOError: print(str(traceback.format_exc())) print("Need permission to create folder in "+options.outdir+" or the destination already contains a folder named '"+PACAKAGE_NAME+"'")
def createLangIndex(*args): outdir = args[0] ext_path = args[1] index_key = RAW_INDEX_KEY langLogger = None try: lang_path = os.path.join(outdir,LANG_DIR) os.mkdir(lang_path) root_log_dir = os.path.join(outdir, LOG_DIR_NAME) langLogger = Logger(os.path.join(root_log_dir,LOG_INFO),'langLogger') langLogger.info("CreateLicenseIndex script start ...") langLogger.info("Get extension index ...") repos_set_json = loadExtJSONContent(ext_path) repos_set_json_index = repos_set_json[index_key] for lang_item in LANG_LIST: fp_content = LANG_INDEX_PRE langLogger.info("Start to get '"+lang_item+"' file") i=0 for repo in repos_set_json_index: try: repo_name = repo["repository"] i+=1 except Exception: raise Exception("At least one repository in index file does not have repo name. Please check!") repo_lang_uri = RAW_REPO_LANG_URI.format(repo_name, lang_item) try: repo_lang_content = URILoader.loadURI(repo_lang_uri, "language index file") lang_json_str = LangPropObj.convertToJSONStr(repo_name, repo_lang_content, lang_item) except Exception as e: if 'HTTPError' in str(e): # some repositories do not have lang file, by default use en file in index for extension file summary = "" if 'Summary' in repo[EXT_KEY].keys(): summary = repo[EXT_KEY]['Summary'] desc = "" if 'Description' in repo[EXT_KEY].keys(): desc = repo[EXT_KEY]['Description'] lang_json_str = LangPropObj.generateJSONStr(repo_name,summary,desc) else: raise e fp_content += lang_json_str try: fp_content = fp_content[0:-2]+'\n'+INDENT+']\n}' fp = open(os.path.join(lang_path, lang_item+'.json'), 'w', encoding='utf-8') fp.write(fp_content) fp.close() except Exception as e: raise e except Exception as e: if langLogger!=None: langLogger.error(str(e),e) langLogger.info("CreatelangIndex action failed!") raise e finally: if langLogger!=None: langLogger.close()
def run(self): # db = MongoProcess("uiac") # db.init() # # Logger.init() base_url = self.baseUrl cache = Cache(base_url) cache.clean() u = User(base_url) u_bind = UserBinding(base_url) u_captcha = UserCaptcha(base_url) u_password = UserPassword(base_url) a_auth = Authorization(base_url) user_code = "15400000001" user_code_2 = "15400000002" user_code_3 = "15400000003" user_code_err = "1540000" mobile_1 = "15400000003" mobile_2 = "15400000004" mobile_err = "1340000" qq_err = "384328d" postcode_err = "3432" sex_err = "5" email_err = "yexiaoxiao.xiangrikui.f**k" password = "******" password_new = "11111111" password_err = "" user_info = AccountUtils.get_user_info() user_info_err = "\"userName\":\"wrongName" user_info_up = AccountUtils.get_up_user_info() extend_info = AccountUtils.get_extend_info() extend_info_err = "\"a\":\"av" extend_info_up = AccountUtils.get_up_extend_info() uid_1 = 0 uid_err = 100 token_err = "token" token_1 = "" token_2 = "" app_id_err = -1 app_id_1 = 1001 app_id_2 = 1002 app_id_3 = 3 # 注册用户 ************************************************************************************ Logger.info( "# 注册用户 ************************************************************************************" ) # 用户编码出错 response = u.register(user_code_err, password, user_info, extend_info) if response.json()["code"] != "10020400": Logger.error("create user, expected code is 10020400") Logger.error(str(response.json())) # 密码不合法 response = u.register(user_code, password_err, user_info, extend_info) if response.json()["code"] != "10020401": Logger.error("create user, expected code is 10020401") Logger.error(str(response.json())) # 用户基本资料不合法 response = u.register(user_code, password, user_info_err, extend_info) if response.json()["code"] != "10020402": Logger.error("create user, expected code is 10020402") Logger.error(str(response.json())) # 用户扩展资料不合法 response = u.register(user_code, password, user_info, extend_info_err) if response.json()["code"] != "10020403": Logger.error("create user, expected code is 10020403") Logger.error(str(response.json())) # app_id不合法 response = u.register(user_code, password, user_info, extend_info, app_id_err) if response.json()["code"] != "10000003": Logger.error("create user, expected code is 10000003") Logger.error(str(response.json())) # 个人基本信息手机号不合法 response = u.register(user_code, password, "{\"mobile\":" + mobile_err + "}", extend_info) if response.json()["code"] != "10020405": Logger.error("create user, expected code is 10020405") Logger.error(str(response.json())) # 个人信息性别参数不合法 response = u.register(user_code, password, "{\"sex\":" + sex_err + "}", extend_info) if response.json()["code"] != "10020404": Logger.error("create user, expected code is 10020404") Logger.error(str(response.json())) # 个人信息qq参数不合法 response = u.register(user_code, password, "{\"qq\":" + qq_err + "}", extend_info) if response.json()["code"] != "10020407": Logger.error("create user, expected code is 10020407") Logger.error(str(response.json())) # 个人信息邮箱参数不合法 response = u.register(user_code, password, "{\"email\":" + email_err + "}", extend_info) if response.json()["code"] != "10020406": Logger.error("create user, expected code is 10020406") Logger.error(str(response.json())) # 个人信息邮编参数不合法 response = u.register(user_code, password, "{\"postcode\":" + postcode_err + "}", extend_info) if response.json()["code"] != "10020408": Logger.error("create user, expected code is 10020408") Logger.error(str(response.json())) # 正确注册 response = u.register(user_code, password, user_info, extend_info) if response.json()["userCode"] != user_code: Logger.error("create user, expected result is success") Logger.error(str(response.json())) uid_1 = response.json()["uid"] response = u.register(user_code_2, password, user_info, extend_info, app_id_2) uid_2 = response.json()["uid"] # 用户已存在 response = u.register(user_code, password, user_info, extend_info) if response.json()["code"] != "10020499": Logger.error("create user, expected code is 10020499") Logger.error(str(response.json())) # 登录 ************************************************************************************ Logger.info( "# 登录 ************************************************************************************" ) # 未找到用户 response = a_auth.login(user_code_3, password) if response.json()["code"] != "10000005": Logger.error("create user and login, expected code is 10000005") Logger.error(str(response.json())) # 密码错误 response = a_auth.login(user_code, password_new) if response.json()["code"] != "10010101": Logger.error("create user and login, expected code is 10010101") Logger.error(str(response.json())) # 登录成功 response = a_auth.login(user_code, password, app_id_1) token_1 = response.json()["accessToken"] response = a_auth.login(user_code_2, password, app_id_2) token_2 = response.json()["accessToken"] # 查看用户信息 ************************************************************************************ Logger.info( "# 查看用户信息 ************************************************************************************" ) # uid无效 response = u.get_user_info(0, token_1) if response.json()["code"] != "10000023": Logger.error("get user info, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u.get_user_info(uid_1, token_err) # if response.json()["code"] != "10000002": # Logger.error("get user info, expected code is 10000002") # Logger.error(str(response.json())) # 查看成功 response = u.get_user_info(uid_1, token_1) if response.json()["uid"] != uid_1: Logger.error("get user info error") Logger.error(str(response.json())) # 更新用户信息 ************************************************************************************ Logger.info( "# 更新用户信息 ************************************************************************************" ) # uid无效 response = u.update_user_info(0, user_info, extend_info, token_1) if response.json()["code"] != "10000023": Logger.error("update user info, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u.update_user_info(uid_1, user_info, extend_info, token_err) # if response.json()["code"] != "10000002": # Logger.error("update user info, expected code is 10000002") # Logger.error(str(response.json())) # app_id不合法 response = u.update_user_info(uid_1, user_info, extend_info, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("update user info, expected code is 10000003") Logger.error(str(response.json())) # 用户基本信息格式不合法 response = u.update_user_info(uid_1, user_info_err, extend_info, token_1) if response.json()["code"] != "10020601": Logger.error("update user info, expected code is 10020601") Logger.error(str(response.json())) # 用户扩展信息不合法 response = u.update_user_info(uid_1, user_info, extend_info_err, token_1) if response.json()["code"] != "10020602": Logger.error("update user info, expected code is 10020602") Logger.error(str(response.json())) # 用户信息手机参数不合法 response = u.update_user_info(uid_1, "{\"mobile\":" + mobile_err + "}", extend_info_up, token_1) if response.json()["code"] != "10020605": Logger.error("update user info, expected code is 10020605") Logger.error(str(response.json())) # 用户信息性别参数不合法 response = u.update_user_info(uid_1, "{\"sex\":" + sex_err + "}", extend_info_up, token_1) if response.json()["code"] != "10020604": Logger.error("update user info, expected code is 10020605") Logger.error(str(response.json())) # 用户信息qq参数不合法 response = u.update_user_info(uid_1, "{\"qq\":" + qq_err + "}", extend_info_up, token_1) if response.json()["code"] != "10020607": Logger.error("update user info, expected code is 10020607") Logger.error(str(response.json())) # 用户信息邮箱参数不合法 response = u.update_user_info(uid_1, "{\"email\":" + email_err + "}", extend_info_up, token_1) if response.json()["code"] != "10020606": Logger.error("update user info, expected code is 10020606") Logger.error(str(response.json())) # 用户信息邮编参数不合法 response = u.update_user_info(uid_1, "{\"postcode\":" + postcode_err + "}", extend_info_up, token_1) if response.json()["code"] != "10020608": Logger.error("update user info, expected code is 10020608") Logger.error(str(response.json())) # 更新成功 response = u.update_user_info(uid_1, user_info_up, extend_info_up, token_1) if str(response.json()["result"]) != "True": Logger.error("update user info, expected result is \"True\"") Logger.error(str(response.json())) response = u.get_user_info(uid_1, token_1) if response.json()["extendInfo"]["a"] != "newAv": Logger.error("update user info, fail to update") Logger.error(str(response.json())) # 修改用户密码 ************************************************************************************ Logger.info( "# 修改用户密码 ************************************************************************************" ) # uid无效 response = u_password.update_password(0, password, password_new, token_1) if response.json()["code"] != "10000023": Logger.error("update user password, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u_password.update_password(uid_1, password, password_new, token_err) # if response.json()["code"] != "10000002": # Logger.error("update user password, expected code is 10000002") # Logger.error(str(response.json())) # app_id不合法 response = u_password.update_password(uid_1, password, password_new, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("update user password, expected code is 10000003") Logger.error(str(response.json())) # 密码格式错误 response = u_password.update_password(uid_1, "", password_new, token_1) if response.json()["code"] != "10020701": Logger.error("update user password, expected code is 10020701") Logger.error(str(response.json())) # 原始密码错误 response = u_password.update_password(uid_1, password_new, password_new, token_1) if response.json()["code"] != "10020702": Logger.error("update user password, expected code is 10020702") Logger.error(str(response.json())) # 修改成功 response = u_password.update_password(uid_1, password, password_new, token_1) if str(response.json()["result"]) != "True": Logger.error("update user password, expected result is \"True\"") Logger.error(str(response.json())) # 密码修改成功,token不失效 response = u.get_user_info(uid_1, token_1) if response.json()["uid"] != uid_1: Logger.error("update user password, but token is invalid") Logger.error(str(response.json())) # 退出登录 ************************************************************************************ Logger.info( "# 退出登录 ************************************************************************************" ) a_auth.logout(token_1) # 退出登录后不能进行用户操作 # response = u.get_user_info(uid_1, token_1) # if response.json()["code"] != "10000002": # Logger.error("logout but token is valid") # Logger.error(str(response.json())) # 重置用户密码 ************************************************************************************ Logger.info( "# 重置用户密码 ************************************************************************************" ) # uid无效 response = u_password.reset_password(0, password_new) if response.json()["code"] != "10000023": Logger.error("reset user password, expected code is 10000023") Logger.error(str(response.json())) # 用户不存在 response = u_password.reset_password(uid_err, password_new) if response.json()["code"] != "10000005": Logger.error("reset user password, expected code is 10000005") Logger.error(str(response.json())) # 密码格式错误 response = u_password.reset_password(uid_1, "") if response.json()["code"] != "10020801": Logger.error("reset user password, expected code is 10020801") Logger.error(str(response.json())) # app_id不合法 response = u_password.reset_password(uid_1, password_new, app_id_err) if response.json()["code"] != "10000003": Logger.error("reset user password, expected code is 10000003") Logger.error(str(response.json())) # 修改成功 response = u_password.reset_password(uid_1, password_new) if str(response.json()["result"]) != "True": Logger.error("reset user password, expected result is \"True\"") Logger.error(str(response.json())) # 使用新密码登录 ************************************************************************************ Logger.info( "# 使用新密码登录 ************************************************************************************" ) response = a_auth.login(user_code, password_new) token_1 = response.json()["accessToken"] # 确认登录成功,能进行基本操作 response = u.get_user_info(uid_1, token_1) if response.json()["uid"] != uid_1: Logger.error("fail to login with new password") Logger.error(str(response.json())) # 绑定手机 ************************************************************************************ Logger.info( "# 绑定手机 ************************************************************************************" ) # uid无效 response = u_bind.bind_mobile(0, mobile_1, token_1, app_id_1) if response.json()["code"] != "10000023": Logger.error("bind mobile, expected code is 10000023") Logger.error(str(response.json())) # 手机号码格式错误 response = u_bind.bind_mobile(uid_1, mobile_err, token_1, app_id_1) if response.json()["code"] != "10000024": Logger.error("bind mobile, expected code is 10000024") Logger.error(str(response.json())) # token失效 # response = u_bind.bind_mobile(uid_1, mobile_1, token_err, app_id_1) # if response.json()["code"] != "10000002": # Logger.error("bind mobile, expected code is 10000002") # Logger.error(str(response.json())) # app_id不合法 response = u_bind.bind_mobile(uid_1, mobile_1, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("bind mobile, expected code is 10000003") Logger.error(str(response.json())) # 绑定成功 response = u_bind.bind_mobile(uid_1, mobile_2, token_1, app_id_1) if str(response.json()["result"]) != "True": Logger.error("bind mobile, expected result is \"True\"") Logger.error(str(response.json())) # 重复操作 response = u_bind.bind_mobile(uid_1, mobile_2, token_1, app_id_1) if response.json()["code"] != "10021103": Logger.error("bind mobile, expected code is 10021103") Logger.error(str(response.json())) # 手机号已被占用 response = u_bind.bind_mobile(uid_2, mobile_2, token_2, app_id_2) if response.json()["code"] != "10021102": Logger.error("bind mobile, expected code is 10021102") Logger.error(str(response.json())) # 查看手机绑定状态 ************************************************************************************ Logger.info( "# 查看手机绑定状态 ************************************************************************************" ) # uid无效 response = u_bind.get_binding_status(0, token_1, app_id_1) if response.json()["code"] != "10000023": Logger.error("get binding status, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u_bind.get_binding_status(uid_1, token_err, app_id_1) # if response.json()["code"] != "10000002": # Logger.error("get binding status, expected code is 10000002") # Logger.error(str(response.json())) # app_id不合法 response = u_bind.get_binding_status(uid_1, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("get binding status, expected code is 10000003") Logger.error(str(response.json())) # 获取成功 response = u_bind.get_binding_status(uid_1, token_1, app_id_1) if not response.json()["mobile"]: Logger.error( "get uid_1 binding status, expected result is \"True\"") Logger.error(str(response.json())) response = u_bind.get_binding_status(uid_2, token_2, app_id_2) if not response.json()["mobile"]: Logger.error( "get uid_2 binding status, expected result is \"True\"") Logger.error(str(response.json())) # 解绑手机 ************************************************************************************ Logger.info( "# 解绑手机 ************************************************************************************" ) # uid无效 response = u_bind.unbind_mobile(0, token_1, app_id_1) if response.json()["code"] != "10000023": Logger.error("unbind mobile, expected code is 10000023") Logger.error(str(response.json())) # token失效 # response = u_bind.unbind_mobile(uid_1, token_err, app_id_1) # if response.json()["code"] != "10000002": # Logger.error("unbind mobile, expected code is 10000002") # Logger.error(str(response.json())) # app_id失效 response = u_bind.unbind_mobile(uid_1, token_1, app_id_err) if response.json()["code"] != "10000003": Logger.error("unbind mobile, expected code is 10000003") Logger.error(str(response.json())) # 不允许操作 response = u_bind.unbind_mobile(uid_1, token_1, app_id_1) if response.json()["code"] != "10021201": Logger.error("unbind mobile, expected code is 10021101") Logger.error(str(response.json()))