update curl create

This commit is contained in:
oppofind 2020-12-22 00:32:23 +08:00
parent 77a1417b3f
commit ad191bc160
3 changed files with 175 additions and 17 deletions

View File

@ -0,0 +1,92 @@
/*
* smart-doc https://github.com/shalousun/smart-doc
*
* Copyright (C) 2018-2021 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.model.request;
import com.power.doc.model.ApiReqHeader;
import java.util.List;
/**
* @author yu 2020/12/21.
*/
public class CurlRequest {
private String type;
private List<ApiReqHeader> reqHeaders;
private String url;
private String body;
private String contentType;
public static CurlRequest builder() {
return new CurlRequest();
}
public String getType() {
return type;
}
public CurlRequest setType(String type) {
this.type = type;
return this;
}
public List<ApiReqHeader> getReqHeaders() {
return reqHeaders;
}
public CurlRequest setReqHeaders(List<ApiReqHeader> reqHeaders) {
this.reqHeaders = reqHeaders;
return this;
}
public String getUrl() {
return url;
}
public CurlRequest setUrl(String url) {
this.url = url;
return this;
}
public String getBody() {
return body;
}
public CurlRequest setBody(String body) {
this.body = body;
return this;
}
public String getContentType() {
return contentType;
}
public CurlRequest setContentType(String contentType) {
this.contentType = contentType;
return this;
}
}

View File

@ -32,6 +32,7 @@ import com.power.doc.helper.JsonBuildHelper;
import com.power.doc.helper.ParamsBuildHelper;
import com.power.doc.model.*;
import com.power.doc.model.request.ApiRequestExample;
import com.power.doc.model.request.CurlRequest;
import com.power.doc.model.request.RequestMapping;
import com.power.doc.utils.*;
import com.thoughtworks.qdox.model.*;
@ -249,14 +250,13 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
List<JavaParameter> parameterList = method.getParameters();
List<ApiReqHeader> reqHeaderList = apiMethodDoc.getRequestHeaders();
StringBuilder header = new StringBuilder(reqHeaderList.size());
for (ApiReqHeader reqHeader : reqHeaderList) {
header.append(" -H ").append("'").append(reqHeader.getName())
.append(":").append(reqHeader.getValue()).append("'");
}
if (parameterList.size() < 1) {
String format = String.format(DocGlobalConstants.CURL_REQUEST_TYPE, methodType,
header.toString(), apiMethodDoc.getUrl());
CurlRequest curlRequest = CurlRequest.builder()
.setContentType(apiMethodDoc.getContentType())
.setType(methodType)
.setReqHeaders(reqHeaderList)
.setUrl(apiMethodDoc.getUrl());
String format = CurlUtil.toCurl(curlRequest);
return ApiRequestExample.builder().setUrl(apiMethodDoc.getUrl()).setExampleBody(format);
}
Map<String, JavaType> actualTypesMap = javaMethod.getActualTypesMap();
@ -432,22 +432,36 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
body = StringUtil.removeQuotes(body);
url = apiMethodDoc.getServerUrl() + "/" + path;
url = UrlUtil.simplifyUrl(url);
String format = String.format(DocGlobalConstants.CURL_REQUEST_TYPE, methodType, header.toString(), url);
if (requestExample.isJson()) {
if (StringUtil.isNotEmpty(requestExample.getJsonBody())) {
String tempUrl = url + "?" + body;
exampleBody = String.format(DocGlobalConstants.CURL_POST_PUT_JSON, methodType, header.toString(),
tempUrl, requestExample.getJsonBody());
} else {
exampleBody = format;
url = url + "?" + body;
}
CurlRequest curlRequest = CurlRequest.builder()
.setBody(requestExample.getJsonBody())
.setContentType(apiMethodDoc.getContentType())
.setType(methodType)
.setReqHeaders(reqHeaderList)
.setUrl(url);
exampleBody = CurlUtil.toCurl(curlRequest);
} else {
CurlRequest curlRequest;
if (StringUtil.isNotEmpty(body)) {
exampleBody = String.format(DocGlobalConstants.CURL_REQUEST_TYPE_DATA, methodType,
header.toString(), url, body);
curlRequest = CurlRequest.builder()
.setBody(body)
.setContentType(apiMethodDoc.getContentType())
.setType(methodType)
.setReqHeaders(reqHeaderList)
.setUrl(url);
} else {
exampleBody = format;
curlRequest = CurlRequest.builder()
.setBody(requestExample.getJsonBody())
.setContentType(apiMethodDoc.getContentType())
.setType(methodType)
.setReqHeaders(reqHeaderList)
.setUrl(url);
}
exampleBody = CurlUtil.toCurl(curlRequest);
}
requestExample.setExampleBody(exampleBody).setUrl(url);
} else {
@ -458,7 +472,13 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
url = StringUtil.removeQuotes(url);
url = apiMethodDoc.getServerUrl() + "/" + url;
url = UrlUtil.simplifyUrl(url);
exampleBody = String.format(DocGlobalConstants.CURL_REQUEST_TYPE, methodType, header.toString(), url);
CurlRequest curlRequest = CurlRequest.builder()
.setBody(requestExample.getJsonBody())
.setContentType(apiMethodDoc.getContentType())
.setType(methodType)
.setReqHeaders(reqHeaderList)
.setUrl(url);
exampleBody = CurlUtil.toCurl(curlRequest);
requestExample.setExampleBody(exampleBody)
.setJsonBody(DocGlobalConstants.EMPTY)
.setUrl(url);

View File

@ -0,0 +1,46 @@
package com.power.doc.utils;
import com.power.common.util.CollectionUtil;
import com.power.common.util.StringUtil;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.model.ApiReqHeader;
import com.power.doc.model.request.CurlRequest;
/**
* @author yu 2020/12/21.
*/
public class CurlUtil {
public static String toCurl(CurlRequest request) {
StringBuilder sb = new StringBuilder();
sb.append("curl");
sb.append(" -X");
sb.append(" ").append(request.getType());
if (request.getUrl().indexOf("https") == 0) {
sb.append(" -k");
}
if (StringUtil.isNotEmpty(request.getContentType())&&
!DocGlobalConstants.URL_CONTENT_TYPE.equals(request.getContentType())) {
sb.append(" -H");
sb.append(" 'Content-Type: ").append(request.getContentType()).append("'");
}
if (CollectionUtil.isNotEmpty(request.getReqHeaders())) {
for (ApiReqHeader reqHeader : request.getReqHeaders()) {
sb.append(" -H");
if (StringUtil.isEmpty(reqHeader.getValue())) {
sb.append(" '" + reqHeader.getName() + "'");
} else {
sb.append(" '" + reqHeader.getName() + ':' + reqHeader.getValue() + "'");
}
}
}
sb.append(" -i");
// append request url
sb.append(" ").append(request.getUrl());
if (StringUtil.isNotEmpty(request.getBody())) {
sb.append(" --data");
sb.append(" '" + request.getBody() + "'");
}
return sb.toString();
}
}