def get_api_data(self, filepath,statusvalue): with open(filepath, encoding='utf-8') as f: # 以字典格式读取yaml文件 deatil = yaml.load(f) data = list(deatil.keys()) for i in range(len(data)): if statusvalue in data[i]: # 获取对应的响应状态 status = data[i] # 拆分获取对应的状态码 #code = re.sub('\D', '', status) # 拆分获取对应的状态描述 #description = re.sub('\d', '', status) break # remove_digits = str.maketrans('', '', digits) # description = status[i].translate(remove_digits) else: status = '200Ok' code = '200' description = 'Ok' log('not found requestdata') requestbody = deatil[status][0] requestbody['verify'] = status log('requestbody is', requestbody) return requestbody
def send_request(): url = 'https://api.ones.team/project/master/auth/login' data = dict( password='******', email='*****@*****.**', ) result = requests.post(url=url, data=json.dumps(data)) log(result.text)
def get_api(self): filepath = self.get_path(apiname='auth', projectname='project', pathname='index',) with open(filepath, encoding='utf-8') as f: deatil = yaml.load(f) log('swagger deatil is',deatil) path = (list(deatil['paths'].keys()))[0] host = (deatil['servers'][0]['url']) url = '{}{}'.format(host, path) methods = list((((deatil['paths'])).get(path, None)).keys())[0] if methods is None: methods = 'get' return url, methods
def get_swagger(self, projectname, apiname, pathname, filelistname): path = swaggeryaml.get_path(projectname, apiname, pathname, filelistname) url, methods = swaggeryaml.get_api(path) if 'get' in methods: httpreq = self.get_data elif 'post' in methods: httpreq = self.post_data else: httpreq = self.get_data log('not found methods') log('httpreq and url is', httpreq, url) return httpreq, url
def send_http(self): httpreq, url = self.get_swagger(filelistname='api', projectname='project', apiname='auth', pathname='login') body, verify = self.get_stage(filelistname='stage', projectname='project', apiname='auth', pathname='login2', statuscode='200Ok') log('body is', body) try: result = httpreq(url, data=json.dumps(body)) except BaseException as msg: daily = InsertLog() daily.error(msg) log('result is', result.json()) return result
def send_request(self): url, methods = self.get_api() body = self.get_api_data('200Ok') verify = body.get('verify', '') body.pop('verify', '') code = re.sub('\D', '', verify) log(code) # 拆分获取对应的状态描述 description = re.sub('\d', '', verify) log(description) result = requests.post(url=url, data=json.dumps(body), verify=False) log(result.status_code)