fix: 拼接请求参数、请求header
This commit is contained in:
parent
97ecfbd698
commit
387577cd02
|
@ -67,12 +67,26 @@ export default {
|
|||
}
|
||||
let code = "";
|
||||
if (data.length > 0) {
|
||||
data.forEach(dt => {
|
||||
for (let dt of data) {
|
||||
// 过滤非HTTP接口API
|
||||
if (dt.protocol !== "HTTP") {
|
||||
if (!dt.request) {
|
||||
continue;
|
||||
} else {
|
||||
// 是否是HTTP接口CASE
|
||||
if (dt.request) {
|
||||
let req = JSON.parse(dt.request);
|
||||
if (req.protocol !== "HTTP") {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let param = this._parseRequestObj(dt);
|
||||
param['host'] = host;
|
||||
param['protocol'] = protocol;
|
||||
code += '\n' + getCodeTemplate(this.language, param);
|
||||
})
|
||||
}
|
||||
}
|
||||
this.handleCodeTemplate(code);
|
||||
this.$refs.apiFuncRelevance.close();
|
||||
|
@ -82,6 +96,7 @@ export default {
|
|||
},
|
||||
_parseRequestObj(data) {
|
||||
let requestHeaders = new Map();
|
||||
let requestArguments = new Map();
|
||||
let requestMethod = "";
|
||||
let requestBody = "";
|
||||
let requestPath = "";
|
||||
|
@ -97,11 +112,19 @@ export default {
|
|||
}
|
||||
})
|
||||
}
|
||||
let args = request.arguments;
|
||||
if (args && args.length) {
|
||||
args.forEach(arg => {
|
||||
if (arg.name) {
|
||||
requestArguments.set(arg.name, arg.value);
|
||||
}
|
||||
})
|
||||
}
|
||||
let body = request.body;
|
||||
if (body.json) {
|
||||
requestBody = body.raw;
|
||||
}
|
||||
return {requestPath, requestHeaders, requestMethod, requestBody}
|
||||
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments}
|
||||
},
|
||||
apiClose() {
|
||||
|
||||
|
|
|
@ -24,17 +24,15 @@ export function getCodeTemplate(language, requestObj) {
|
|||
}
|
||||
|
||||
function groovyCode(requestObj) {
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "", requestMethod = "", host = "", protocol = ""} = requestObj;
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "",
|
||||
requestMethod = "", host = "", protocol = "", requestArguments = new Map()} = requestObj;
|
||||
let requestUrl = "";
|
||||
requestPath = getRequestPath(requestArguments, requestPath);
|
||||
if (protocol && host && requestPath) {
|
||||
requestUrl = protocol + "://" + host + requestPath;
|
||||
}
|
||||
let headers = "", body = JSON.stringify(requestBody);
|
||||
for (let [k, v] of requestHeaders) {
|
||||
// 拼装映射
|
||||
headers += `['${k}':'${v}']`;
|
||||
}
|
||||
headers = headers === "" ? "[]" : headers;
|
||||
let body = JSON.stringify(requestBody);
|
||||
let headers = getHeaders(requestHeaders);
|
||||
let params = "";
|
||||
params += `[
|
||||
'url': '${requestUrl}',
|
||||
|
@ -45,9 +43,7 @@ function groovyCode(requestObj) {
|
|||
return `import groovy.json.JsonOutput
|
||||
import groovy.json.JsonSlurper
|
||||
|
||||
def http_post() {
|
||||
def params = ${params}
|
||||
|
||||
def headers = params['headers']
|
||||
// json数据
|
||||
def data = params['data']
|
||||
|
@ -66,24 +62,19 @@ def http_post() {
|
|||
writer.close()
|
||||
}
|
||||
log.info(conn.content.text)
|
||||
}
|
||||
http_post()
|
||||
`;
|
||||
}
|
||||
|
||||
function pythonCode(requestObj) {
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "", requestMethod = "", host = "", protocol = "http"} = requestObj;
|
||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
||||
requestMethod = "", host = "", protocol = "http", requestArguments = new Map()} = requestObj;
|
||||
let connType = "HTTPConnection";
|
||||
if (protocol === 'https') {
|
||||
connType = "HTTPSConnection";
|
||||
}
|
||||
let headers = "";
|
||||
for (let [k, v] of requestHeaders) {
|
||||
// 拼装
|
||||
headers += `{'${k}':'${v}'}`;
|
||||
}
|
||||
headers = headers === "" ? "{}" : headers;
|
||||
let headers = getHeaders(requestHeaders);
|
||||
requestBody = requestBody ? requestBody : "{}";
|
||||
requestPath = getRequestPath(requestArguments, requestPath);
|
||||
return `import httplib
|
||||
params = ${requestBody} #例 {'username':'test'}
|
||||
headers = ${headers} #例 {'Content-Type':'application/json'}
|
||||
|
@ -96,7 +87,7 @@ conn.request(method, path, params, headers)
|
|||
res = conn.getresponse()
|
||||
data = unicode(res.read(), 'utf-8')
|
||||
log.info(data)
|
||||
`
|
||||
`;
|
||||
}
|
||||
|
||||
function javaCode(requestObj) {
|
||||
|
@ -106,3 +97,33 @@ function javaCode(requestObj) {
|
|||
function jsCode(requestObj) {
|
||||
return ``;
|
||||
}
|
||||
|
||||
function getRequestPath(requestArgs, requestPath) {
|
||||
if (requestArgs.size > 0) {
|
||||
requestPath = requestPath + "?"
|
||||
let index = 1;
|
||||
for (let [k, v] of requestArgs) {
|
||||
if (index !== 1) {
|
||||
requestPath = requestPath + "&";
|
||||
}
|
||||
requestPath = requestPath + k + "=" + v;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return requestPath;
|
||||
}
|
||||
|
||||
function getHeaders(requestHeaders) {
|
||||
let headers = "{";
|
||||
let index = 1;
|
||||
for (let [k, v] of requestHeaders) {
|
||||
if (index !== 1) {
|
||||
headers += ",";
|
||||
}
|
||||
// 拼装
|
||||
headers += `'${k}':'${v}'`;
|
||||
index ++;
|
||||
}
|
||||
headers = headers + "}"
|
||||
return headers;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue