def repeat_name_verification(request, project_id): if request.method != CommonData.Method.GET.value: return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json') if ('path' not in request.GET.keys()) or \ ('method' not in request.GET.keys()): response = Response(MissingParametersError, 'Missing Parameters', {}) return HttpResponse(response.toJson(), content_type='application/json') path = request.GET['path'] method = request.GET['method'] if path is None: return api = ApiDao.get_api(project_id, path, method) response = Response(Success, 'Success', {}) if api is None: response.data['repeatability'] = False else: response.data['api'] = api.as_dict() if 'api_id' in request.GET.keys(): api_id = request.GET['api_id'] if (str(api.api_id) == str(api_id)): response.data['repeatability'] = False else: response.data['repeatability'] = True else: response.data['repeatability'] = True return HttpResponse(response.toJson(), content_type='application/json')
def mock(request, project_id, path): method = str(request.method) api = ApiDao.get_api(project_id, path, method) if api is None: response = Response(APINotExist, '该 Method 的 API 不存在', {}) return HttpResponse(response.toJson(), content_type='application/json') if api.status != Api.Status.Mock.value: response = Response(APINotOpenMock, '该 API 没有开启 Mock', {}) return HttpResponse(response.toJson(), content_type='application/json') data = json.loads(api.resTemplate.responseJson) return HttpResponse(json.dumps(data), content_type='application/json')
def mock(request, project_id, path): method = str(request.method) api = ApiDao.get_api(path, method) if api is None: data = CommonData.response_data(APINotExist, "该 Method 的 API 不存在") return HttpResponse(json.dumps(data), content_type="application/json") if api.status != Api.Status.Mock.value: data = CommonData.response_data(APINotOpenMock, "该 API 没有开启 Mock") return HttpResponse(json.dumps(data), content_type="application/json") data = json.loads(api.responseJson) return HttpResponse(json.dumps(data), content_type="application/json")
def copy(request, api_id): if request.method != CommonData.Method.GET.value: return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json') if ('target_project_id' not in request.GET.keys()): response = Response(MissingParametersError, 'Has no param "status"', {}) return HttpResponse(response.toJson(), content_type='application/json') target_project_id = request.GET['target_project_id'] api = ApiDao.get_api_with_id(api_id) result = ApiDao.get_api(target_project_id, api.path, api.method) if result is not None: response = Response(APIAlreadyExist, '该项目下已存在相同 Method 和 Path 的 API', {}) return HttpResponse(response.toJson(), content_type='application/json') project = ProjectDao.get_project_with_id(target_project_id) new_api = Api() new_api.path = api.path new_api.method = api.method new_api.name = api.name new_api.note = api.note new_api.status = api.status original_res = ResTemplateDao.get_private_res_template_with_api_id(api_id) res = ResTemplate() res.type = original_res.type res.name = uuid.uuid4() res.mimeType = original_res.mimeType res.responseJson = original_res.responseJson res.project = project result = ResTemplateDao.create(res) new_api.resTemplate = result result_api = ApiDao.create(new_api) project.apis.add(result_api) project.save() if result_api is None: response = Response(DaoOperationError, 'API create failed', {}) return HttpResponse(response.toJson(), content_type='application/json') response = Response(Success, 'API copy success', {}) return HttpResponse(response.toJson(), content_type='application/json')