def post(self, request): """ 修改接口 :param request: :return: """ data=request.data result = self.parameter_check(data) if result: return result data["userUpdate"] = request.user.pk data["params"]="{}" if not "params" in data or not data["params"] else data["params"] try: pro_data = Project.objects.get(id=data["project_id"]) except ObjectDoesNotExist: return JsonResponse(code="999995", msg="项目不存在!") pro_data = ProjectSerializer(pro_data) if not pro_data.data["status"]: return JsonResponse(code="999985", msg="该项目已禁用") api_name = ApiInfo.objects.filter(name=data["name"], project=data["project_id"]).exclude(id=data["id"]) if len(api_name): return JsonResponse(code="999997", msg="存在相同名称!") # data["userUpdate"] = request.user result=ApiService.updateApi(data) if result: record_dynamic(project=data["project_id"], _type="修改", operationObject="接口", user=request.user.pk, data="修改接口“%s”" % data["name"]) api_record = ApiOperationHistory(api=ApiInfo.objects.get(id=data['id']), user=User.objects.get(id=request.user.pk), description="修改接口\"%s\"" % data["name"]) api_record.save() return JsonResponse(code="999999", msg="执行成功!") return JsonResponse(code="999996", msg="执行失败!")
def post(self, request): """ 新增接口 :param request: :return: """ data=request.data result = self.parameter_check(data) if result: return result data["userUpdate"] = request.user.pk data["params"]="{}" if not "params" in data or not data["params"] else data["params"] api_name = ApiInfo.objects.filter(name=data["name"], project=data["project_id"]) if len(api_name): return JsonResponse(code="999997", msg="存在相同名称!") else: result=ApiService.addApi(data) if result: record_dynamic(project=data["project_id"], _type="新增", operationObject="接口", user=request.user.pk, data="新增接口“%s”" % data["name"]) api_record = ApiOperationHistory(api=ApiInfo.objects.get(id=result["id"]), user=User.objects.get(id=request.user.pk), description="新增接口“%s”" % data["name"]) api_record.save() return JsonResponse(code="999999", msg="执行成功!", data={"api_id": result["id"]}) else: return JsonResponse(code="999996", msg="执行失败!")
def get_command(line,projectId=None): api_pattern="执行接口(?P<name>.*),使用参数(?P<params>.*)" match=re.match(api_pattern,line) try: if match: api_name=match.group("name") api_params=match.group("params") api=ApiService.get_api_by_name(projectId,api_name) api_command=None if api: api_command={"name":api_name,"type":"api","actionId":api.id,"alias":"","value":api_params,"desc":api.description} else: api_command={"name":api_name,"type":"api","actionId":0,"alias":"","value":api_params,"desc":""} return api_command command_list=[] command_list.extend(CommandService.get_commands(ControlCommand,"control")) command_list.extend(CommandService.get_commands(CommonCommand,"common")) for command in command_list: if "pattern" in command: match=re.match(command["pattern"],line) if match: valueMap=json.loads(command["value"]) for key in valueMap: valueMap[key]=match.group(key) command["value"]=json.dumps(valueMap) return command except Exception as e: # logging.error(traceback.format_exc()) raise e
def run(self): try: start = time.clock() for apiInfo in self.apiInfoList: if "id" in apiInfo: ApiService.updateApi(apiInfo) else: ApiService.addApi(apiInfo) duration = int(time.clock() - start) print("导入%s分组共%s个接口,耗时%s秒!" % (self.toGroupName, len(self.apiInfoList), duration)) record_dynamic(project=self.projectId, _type="导入", operationObject="接口", user=self.userId, data="导入%s分组共%s个接口,耗时%s秒!" % (self.toGroupName, len(self.apiInfoList), duration)) except: logging.error(traceback.format_exc())
def post(self,request): try: data=request.data projectId=data["project_id"] groupId=data["group_id"] fileName=data["fileName"] result=ApiService.importFromPostman(projectId,groupId,fileName,request.user.pk) except: logging.error(traceback.format_exc()) return JsonResponse(code="999995", msg="执行失败") return JsonResponse(data=result["errorList"], code="999999", msg="共处理{totalCount}条记录,{passCount}条导入成功,{existCount}条已存在,{failCount}条导入失败!".format(totalCount=result["totalCount"],passCount=result["passCount"],existCount=result["existCount"],failCount=result["failCount"]))
def post(self, request): """ 执行接口 :param request: :return: """ data=request.data try: if "api_id" in data.keys(): api=ApiInfo.objects.get(id=data["api_id"]) type=api.type method=api.requestType contentType=api.requestParameterType else: type=data["type"] method=data["method"] contentType=data["contentType"] except ObjectDoesNotExist: return JsonResponse(code="999990", msg="接口不存在!") try: env=data["env"] if "env" in data.keys() else None if env and not json.is_json(env): obi = ProjectConfig.objects.filter(project=data["project_id"],name=env,type="env").order_by("-id") env=obi[0].value envMap=json.loads(env) testtime=time.time() result=ApiService.run_api(type,data["url"],method,data["headers"],contentType,data["data"],env) duration=int((time.time() - testtime)*1000) if "api_id" in data.keys(): AutomationResult(name=api.name,value=data["data"],result="PASS" if result[0]==200 else "FAIL", project_id=data["project_id"],api=api,testTime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(testtime)),duration=duration,env=envMap["env"] if "env" in envMap else "",user_id=request.user.pk).save() record_dynamic(project=data["project_id"], _type="执行", operationObject="接口", user=request.user.pk, data="执行接口[%s]%s" % (api.name if "api_id" in data.keys() else data["url"],"成功" if result[0]==200 else "失败")) except Exception: logging.error(traceback.format_exc()) return JsonResponse(code="999998", msg="失败!") return JsonResponse(data={ "result": result }, code="999999", msg="成功!")
def run_api(id, value, context): api_status = "RUNNING" api_result = {} try: for i in range(3): api_result = ApiService.run_api_by_id(id, value, context["cookies"]) if api_result["responseCode"] == 200: if "assertMessage" in api_result: api_status = "FAIL" else: api_status = "PASS" break if api_status != "FAIL": api_status = "PASS" except Exception as e: logging.error(traceback.format_exc()) raise ExecutionFailException(str(e)) finally: context["result"]["result"] = api_result["response"] context["result"]["responseCode"] = api_result["responseCode"] return api_result, api_status