def get(self, request): """ 获取Api下载文档路径 :param request: :return: """ project_id = request.GET.get("project_id") try: if not project_id.isdecimal(): return JsonResponse(code="999996", msg="参数有误!") except AttributeError: return JsonResponse(code="999996", msg="参数有误!") try: obj = Project.objects.get(id=project_id) except ObjectDoesNotExist: return JsonResponse(code="999995", msg="项目不存在!") pro_data = ProjectSerializer(obj) if not pro_data.data["status"]: return JsonResponse(code="999985", msg="该项目已禁用") obi = ApiGroupLevelFirst.objects.filter(project=project_id) data = ApiInfoDocSerializer(obi, many=True).data obn = ApiInfoSerializer(ApiInfo.objects.filter(project=project_id), many=True).data url = Write().write_api(str(obj), group_data=data, data=obn) return JsonResponse(code="999999", msg="成功!", data=url)
def download(request): """ 获取Api下载文档路径 project_id 项目ID :param request: :return: """ project_id = request.GET.get("project_id") if not project_id.isdecimal(): return JsonResponse(code_msg=GlobalStatusCode.parameter_wrong()) obj = Project.objects.filter(id=project_id) if obj: obi = ApiGroupLevelFirst.objects.filter(project=project_id) data = ApiInfoDocSerializer(obi, many=True).data obn = ApiInfoSerializer(ApiInfo.objects.filter(project=project_id), many=True).data url = Write().write_api(str(obj[0]), group_data=data, data=obn) return JsonResponse(code_msg=GlobalStatusCode.success(), data=url) else: return JsonResponse(code_msg=GlobalStatusCode.project_not_exist())
def api_info(request): """ 获取接口详情 project_id 项目ID api_id 接口ID :return: """ project_id = request.GET.get("project_id") api_id = request.GET.get("api_id") if not project_id.isdecimal() or not api_id.isdecimal(): return JsonResponse(code_msg=GlobalStatusCode.parameter_wrong()) obj = Project.objects.filter(id=project_id) if obj: try: obi = ApiInfo.objects.get(id=api_id, project=project_id) serialize = ApiInfoSerializer(obi) return JsonResponse(data=serialize.data, code_msg=GlobalStatusCode.success()) except ObjectDoesNotExist: return JsonResponse(code_msg=GlobalStatusCode.api_not_exist()) else: return JsonResponse(code_msg=GlobalStatusCode.project_not_exist())
def get(self, request): """ 获取接口详情 :return: """ project_id = request.GET.get("project_id") api_id = request.GET.get("api_id") if not project_id or not api_id: return JsonResponse(code="999996", msg="参数有误!") if not project_id.isdecimal() or not api_id.isdecimal(): return JsonResponse(code="999996", msg="参数有误!") try: pro_data = Project.objects.get(id=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="该项目已禁用") try: obi = ApiInfo.objects.get(id=api_id, project=project_id) serialize = ApiInfoSerializer(obi) return JsonResponse(data=serialize.data, code="999999", msg="成功!") except ObjectDoesNotExist: return JsonResponse(code="999990", msg="接口不存在!")
def run_api_by_id(id, value, cookies): """ 根据接口ID,接口参数调用接口 :param request: :return: 接口返回数据 """ result = "" try: serialize = ApiInfoSerializer(ApiInfo.objects.get(id=id)) api = serialize.data headers = {} for header in api["headers"]: headers[header["name"]] = header["value"] parameters = {} valueMap = json.loads(value) for parameter in api["requestParameter"]: if parameter["name"] == "postData" and len( api["requestParameter"]) == 1: if parameter["name"] in valueMap: parameters = json.loads2(valueMap[ parameter["name"]]) if parameter["_type"] in ( "Object", "Array") else valueMap[parameter["name"]] else: parameters = json.loads2( parameter["value"]) if parameter["_type"] in ( "Object", "Array") else parameter["value"] else: if parameter["name"] in valueMap: parameterValue = valueMap[parameter["name"]] else: parameterValue = json.loads2( parameter["value"]) if parameter["_type"] in ( "Object", "Array") else parameter["value"] if parameterValue != "NULL": parameters[parameter["name"]] = parameterValue api["apiAddress"] = ParamUtil.replaceParam(api["apiAddress"], json.loads(value)) code, response_data, header_data = ApiService.run_api( api["type"], api["apiAddress"], api["requestType"], headers, api["requestParameterType"], parameters, env=value, cookies=cookies) assertMessage = [] if len(api["response"]) > 0: for response in api["response"]: responseValues = json.get_values(response_data, response["name"]) if responseValues: for value in responseValues: if response["value"] and not match( response["value"], value): assertMessage.append( "接口返回数据参数%s实际值%s,与预期值%s不一致!" % (response["name"], value, response["value"])) actualType = json.get_type(value) if actualType and actualType != response["_type"]: assertMessage.append( "接口返回数据参数%s实际类型%s,与预期类型%s不一致!" % (response["name"], actualType, response["_type"])) elif response["required"]: assertMessage.append("接口返回数据不存在必含参数%s!" % response["name"]) result = { "url": api["apiAddress"], "method": api["requestType"], "data": parameters, "response": response_data, "responseCode": code } if len(assertMessage) > 0: result["assertMessage"] = str(assertMessage) if len( assertMessage) > 1 else assertMessage[0] return result except Exception as e: logging.error(traceback.format_exc()) raise e