fix(系统设置): 代码片段生成代码时解析路径中的rest参数

--bug=1013664 --user=李玉号
【系统设置】github#14102,从API定义导入CASE的python代码REST参数不会填写
https://www.tapd.cn/55049933/s/1179853

Closes #14102
This commit is contained in:
shiziyuan9527 2022-06-13 15:24:41 +08:00 committed by shiziyuan9527
parent dd6d7c770a
commit 0efa692599
3 changed files with 51 additions and 7 deletions

View File

@ -107,6 +107,7 @@ export default {
_parseRequestObj(data) {
let requestHeaders = new Map();
let requestArguments = new Map();
let requestRest = new Map();
let requestMethod = "";
let requestBody = "";
let requestPath = "";
@ -115,6 +116,14 @@ export default {
requestPath = request.path;
requestMethod = request.method;
let headers = request.headers;
let rest = request.rest;
if (rest && rest.length > 0) {
rest.forEach(r => {
if (r.enable) {
requestRest.set(r.name, r.value);
}
})
}
if (headers && headers.length > 0) {
headers.forEach(header => {
if (header.name) {
@ -134,7 +143,7 @@ export default {
if (body.json) {
requestBody = body.raw;
}
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments}
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments, requestRest}
},
apiClose() {

View File

@ -114,6 +114,7 @@ export default {
_parseRequestObj(data) {
let requestHeaders = new Map();
let requestArguments = new Map();
let requestRest = new Map();
let requestMethod = "";
let requestBody = "";
let requestPath = "";
@ -122,12 +123,20 @@ export default {
requestPath = request.path;
requestMethod = request.method;
let headers = request.headers;
let rest = request.rest;
if (rest && rest.length > 0) {
rest.forEach(r => {
if (r.enable) {
requestRest.set(r.name, r.value);
}
})
}
if (headers && headers.length > 0) {
headers.forEach(header => {
if (header.name) {
requestHeaders.set(header.name, header.value);
}
})
});
}
let args = request.arguments;
if (args && args.length) {
@ -141,7 +150,7 @@ export default {
if (body.json) {
requestBody = body.raw;
}
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments}
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments, requestRest}
},
apiClose() {

View File

@ -26,9 +26,10 @@ export function getCodeTemplate(language, requestObj) {
function groovyCode(requestObj) {
let {requestHeaders = new Map(), requestBody = "", requestPath = "",
requestMethod = "", host = "", protocol = "", requestArguments = new Map()} = requestObj;
requestMethod = "", host = "", protocol = "", requestArguments = new Map(), requestRest = new Map()} = requestObj;
let requestUrl = "";
requestPath = getRequestPath(requestArguments, requestPath);
requestPath = replaceRestParams(requestPath, requestRest);
if (protocol && host && requestPath) {
requestUrl = protocol + "://" + host + requestPath;
}
@ -40,7 +41,7 @@ function groovyCode(requestObj) {
function pythonCode(requestObj) {
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
requestMethod = "", host = "", protocol = "http", requestArguments = new Map()} = requestObj;
requestMethod = "", host = "", protocol = "http", requestArguments = new Map(), requestRest = new Map()} = requestObj;
let connType = "HTTPConnection";
if (protocol === 'https') {
connType = "HTTPSConnection";
@ -48,6 +49,7 @@ function pythonCode(requestObj) {
let headers = getHeaders(requestHeaders);
requestBody = requestBody ? JSON.stringify(requestBody) : "{}";
requestPath = getRequestPath(requestArguments, requestPath);
requestPath = replaceRestParams(requestPath, requestRest);
let obj = {requestBody, headers, host, requestPath, requestMethod, connType};
return _pythonCodeTemplate(obj);
}
@ -142,7 +144,8 @@ log.info(conn.content.text)
function _beanshellTemplate(obj) {
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = ""} = obj;
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj;
requestPath = replaceRestParams(requestPath, requestRest);
let uri = `new URIBuilder()
.setScheme("${protocol}")
.setHost("${domain}")
@ -211,8 +214,9 @@ if (response.getStatusLine().getStatusCode() == 200) {
function _jsTemplate(obj) {
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = ""} = obj;
requestMethod = "GET", protocol = "http", requestArguments = new Map(), domain = "", port = "", requestRest = new Map()} = obj;
let url = "";
requestPath = replaceRestParams(requestPath, requestRest);
if (protocol && domain && port) {
url = protocol + "://" + domain + ":" + port + requestPath;
}
@ -251,3 +255,25 @@ while((lines = reader.readLine()) !== null) {
log.info(res);
`;
}
function replaceRestParams(path, restMap) {
if (!path) {
return path;
}
let arr = path.match(/{([\w]+)}/g);
if (Array.isArray(arr) && arr.length > 0) {
arr = Array.from(new Set(arr));
for (let str of arr) {
try {
let temp = str.substr(1);
let param = temp.substring(0, temp.length - 1);
if (str && restMap.has(param)) {
path = path.replace(new RegExp(str, 'g'), restMap.get(param));
}
} catch (e) {
// nothing
}
}
}
return path;
}