From ad191bc160ceb36f236fe27e14747c6e58639953 Mon Sep 17 00:00:00 2001 From: oppofind <836575280@qq.com> Date: Tue, 22 Dec 2020 00:32:23 +0800 Subject: [PATCH] update curl create --- .../power/doc/model/request/CurlRequest.java | 92 +++++++++++++++++++ .../template/SpringBootDocBuildTemplate.java | 54 +++++++---- .../java/com/power/doc/utils/CurlUtil.java | 46 ++++++++++ 3 files changed, 175 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/power/doc/model/request/CurlRequest.java create mode 100644 src/main/java/com/power/doc/utils/CurlUtil.java diff --git a/src/main/java/com/power/doc/model/request/CurlRequest.java b/src/main/java/com/power/doc/model/request/CurlRequest.java new file mode 100644 index 0000000..fd75cc9 --- /dev/null +++ b/src/main/java/com/power/doc/model/request/CurlRequest.java @@ -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 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 getReqHeaders() { + return reqHeaders; + } + + public CurlRequest setReqHeaders(List 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; + } +} diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index 9e877b8..b684136 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -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 { List parameterList = method.getParameters(); List 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 actualTypesMap = javaMethod.getActualTypesMap(); @@ -432,22 +432,36 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { 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 { 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); diff --git a/src/main/java/com/power/doc/utils/CurlUtil.java b/src/main/java/com/power/doc/utils/CurlUtil.java new file mode 100644 index 0000000..44e928c --- /dev/null +++ b/src/main/java/com/power/doc/utils/CurlUtil.java @@ -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(); + } +}