|
20 | 20 | */ |
21 | 21 | package com.ly.doc.utils; |
22 | 22 |
|
23 | | -import java.util.ArrayList; |
24 | 23 | import java.util.List; |
25 | | -import java.util.Map; |
26 | 24 | import java.util.Objects; |
27 | | -import java.util.stream.Collectors; |
28 | 25 |
|
29 | 26 | import com.ly.doc.constants.DocGlobalConstants; |
30 | | -import com.ly.doc.constants.Methods; |
31 | | -import com.ly.doc.model.ApiMethodDoc; |
32 | 27 | import com.ly.doc.model.ApiReqParam; |
33 | 28 | import com.ly.doc.model.FormData; |
34 | | -import com.ly.doc.model.request.ApiRequestExample; |
35 | 29 | import com.power.common.util.CollectionUtil; |
36 | 30 | import com.power.common.util.StringUtil; |
37 | 31 | import com.ly.doc.model.request.CurlRequest; |
38 | | -import com.power.common.util.UrlUtil; |
39 | 32 |
|
40 | 33 | /** |
41 | 34 | * @author yu 2020/12/21. |
@@ -89,80 +82,4 @@ public static String toCurl(CurlRequest request) { |
89 | 82 | } |
90 | 83 | return sb.toString(); |
91 | 84 | } |
92 | | - |
93 | | - public void setExampleBody(ApiMethodDoc apiMethodDoc, ApiRequestExample requestExample, |
94 | | - List<FormData> formDataList, |
95 | | - Map<String, String> pathParamsMap, Map<String, String> queryParamsMap) { |
96 | | - String methodType = apiMethodDoc.getType(); |
97 | | - String[] paths = apiMethodDoc.getPath().split(";"); |
98 | | - String path = paths[0]; |
99 | | - String body; |
100 | | - String exampleBody; |
101 | | - String url; |
102 | | - List<ApiReqParam> reqHeaderList = apiMethodDoc.getRequestHeaders(); |
103 | | - Map<Boolean, List<FormData>> formDataGroupMap = formDataList.stream() |
104 | | - .collect(Collectors.groupingBy(e -> Objects.equals(e.getType(), DocGlobalConstants.PARAM_TYPE_FILE))); |
105 | | - List<FormData> fileFormDataList = formDataGroupMap.getOrDefault(Boolean.TRUE, new ArrayList<>()); |
106 | | - // curl send file to convert |
107 | | - final Map<String, String> formDataToMap = DocUtil.formDataToMap(formDataList); |
108 | | - // formData add to params '--data' |
109 | | - queryParamsMap.putAll(formDataToMap); |
110 | | - if (Methods.POST.getValue().equals(methodType) || Methods.PUT.getValue().equals(methodType)) { |
111 | | - // for post put |
112 | | - path = DocUtil.formatAndRemove(path, pathParamsMap); |
113 | | - body = UrlUtil.urlJoin(DocGlobalConstants.EMPTY, queryParamsMap) |
114 | | - .replace("?", DocGlobalConstants.EMPTY); |
115 | | - url = apiMethodDoc.getServerUrl() + "/" + path; |
116 | | - url = UrlUtil.simplifyUrl(url); |
117 | | - |
118 | | - if (requestExample.isJson()) { |
119 | | - if (StringUtil.isNotEmpty(body)) { |
120 | | - url = url + "?" + body; |
121 | | - } |
122 | | - CurlRequest curlRequest = CurlRequest.builder() |
123 | | - .setBody(requestExample.getJsonBody()) |
124 | | - .setContentType(apiMethodDoc.getContentType()) |
125 | | - .setType(methodType) |
126 | | - .setReqHeaders(reqHeaderList) |
127 | | - .setUrl(url); |
128 | | - exampleBody = CurlUtil.toCurl(curlRequest); |
129 | | - } else { |
130 | | - CurlRequest curlRequest; |
131 | | - if (StringUtil.isNotEmpty(body)) { |
132 | | - curlRequest = CurlRequest.builder() |
133 | | - .setBody(body) |
134 | | - .setContentType(apiMethodDoc.getContentType()) |
135 | | - .setFileFormDataList(fileFormDataList) |
136 | | - .setType(methodType) |
137 | | - .setReqHeaders(reqHeaderList) |
138 | | - .setUrl(url); |
139 | | - } else { |
140 | | - curlRequest = CurlRequest.builder() |
141 | | - .setBody(requestExample.getJsonBody()) |
142 | | - .setContentType(apiMethodDoc.getContentType()) |
143 | | - .setFileFormDataList(fileFormDataList) |
144 | | - .setType(methodType) |
145 | | - .setReqHeaders(reqHeaderList) |
146 | | - .setUrl(url); |
147 | | - } |
148 | | - exampleBody = CurlUtil.toCurl(curlRequest); |
149 | | - } |
150 | | - requestExample.setExampleBody(exampleBody).setUrl(url); |
151 | | - } else { |
152 | | - // for get delete |
153 | | - url = ""; |
154 | | - //formatRequestUrl(pathParamsMap, queryParamsMap, apiMethodDoc.getServerUrl(), path); |
155 | | - CurlRequest curlRequest = CurlRequest.builder() |
156 | | - .setBody(requestExample.getJsonBody()) |
157 | | - .setContentType(apiMethodDoc.getContentType()) |
158 | | - .setType(methodType) |
159 | | - .setReqHeaders(reqHeaderList) |
160 | | - .setUrl(url); |
161 | | - exampleBody = CurlUtil.toCurl(curlRequest); |
162 | | - |
163 | | - requestExample.setExampleBody(exampleBody) |
164 | | - .setJsonBody(requestExample.isJson() ? requestExample.getJsonBody() : DocGlobalConstants.EMPTY) |
165 | | - .setUrl(url); |
166 | | - } |
167 | | - } |
168 | 85 | } |
0 commit comments