1
1
package com .ztianzeng .apidoc .yapi .upload ;
2
2
3
3
import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .core .type .TypeReference ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
4
6
import com .ztianzeng .apidoc .models .OpenAPI ;
5
7
import com .ztianzeng .apidoc .models .Operation ;
6
8
import com .ztianzeng .apidoc .models .Paths ;
10
12
import com .ztianzeng .apidoc .yapi .utils .HttpClientUtil ;
11
13
import org .apache .commons .lang3 .StringUtils ;
12
14
import org .apache .http .HttpEntity ;
15
+ import org .apache .http .client .methods .HttpGet ;
13
16
import org .apache .http .client .methods .HttpPost ;
14
17
import org .apache .http .entity .StringEntity ;
15
18
24
27
* @date 2019-06-14 14:18
25
28
*/
26
29
public class UploadToYapi {
30
+ public static Map <String , Map <String , Integer >> catMap = new HashMap <>();
31
+ public final ObjectMapper mapper = Json .mapper ();
32
+ private final String projectToken ;
33
+ private final String yapiUrl ;
34
+ private final String projectId ;
35
+
36
+ public UploadToYapi (String projectToken , String yapiUrl , String projectId ) {
37
+ this .projectToken = projectToken ;
38
+ this .yapiUrl = yapiUrl ;
39
+ this .projectId = projectId ;
40
+ }
41
+
27
42
/**
28
43
* 上传OpenAPi
29
44
*
30
45
* @param openAPI
31
46
*/
32
- public void upload (OpenAPI openAPI , String projectToken , String yapiUrl ) {
47
+ public void upload (OpenAPI openAPI ) {
33
48
List <YapiApiDTO > yapiApiDTOS = toYapiDTO (openAPI );
34
49
if (yapiApiDTOS != null ) {
35
50
for (YapiApiDTO yapiApiDTO : yapiApiDTOS ) {
@@ -42,10 +57,11 @@ public void upload(OpenAPI openAPI, String projectToken, String yapiUrl) {
42
57
} else {
43
58
yapiSaveParam .setMenu (YapiConstant .menu );
44
59
}
45
- yapiSaveParam .setCatid ("709" );
60
+
61
+ yapiSaveParam .setCatid (getCatIdOrCreate (yapiApiDTO .getTag ()));
46
62
try {
47
63
// 上传
48
- YapiResponse yapiResponse = new UploadToYapi (). uploadSave (yapiSaveParam );
64
+ YapiResponse yapiResponse = uploadSave (yapiSaveParam );
49
65
if (yapiResponse .getErrcode () != 0 ) {
50
66
51
67
} else {
@@ -87,8 +103,8 @@ public YapiResponse uploadSave(YapiSaveParam yapiSaveParam) throws IOException {
87
103
} else {
88
104
yapiSaveParam .getReq_headers ().add (yapiHeaderDTO );
89
105
}
90
- String response = HttpClientUtil .ObjectToString (HttpClientUtil .getHttpclient ().execute (this .getHttpPost (yapiSaveParam .getYapiUrl () + YapiConstant .yapiSave , Json . mapper () .writeValueAsString (yapiSaveParam ))), "utf-8" );
91
- return Json . mapper () .readValue (response , YapiResponse .class );
106
+ String response = HttpClientUtil .ObjectToString (HttpClientUtil .getHttpclient ().execute (this .getHttpPost (yapiSaveParam .getYapiUrl () + YapiConstant .yapiSave , mapper .writeValueAsString (yapiSaveParam ))), "utf-8" );
107
+ return mapper .readValue (response , YapiResponse .class );
92
108
}
93
109
94
110
/**
@@ -129,10 +145,10 @@ public List<YapiApiDTO> toYapiDTO(OpenAPI openAPI) {
129
145
130
146
131
147
yapiApiDTO .setReq_params (operation .getParameters ());
132
-
148
+ yapiApiDTO . setTag ( operation . getTags (). stream (). findFirst (). orElse ( "" ));
133
149
try {
134
- yapiApiDTO .setRequestBody (Json . mapper () .writeValueAsString (operation .getRequestBody ()));
135
- yapiApiDTO .setResponse (Json . mapper () .writeValueAsString (operation .getResponses ()));
150
+ yapiApiDTO .setRequestBody (mapper .writeValueAsString (operation .getRequestBody ()));
151
+ yapiApiDTO .setResponse (mapper .writeValueAsString (operation .getResponses ()));
136
152
} catch (JsonProcessingException e ) {
137
153
e .printStackTrace ();
138
154
}
@@ -158,58 +174,60 @@ public List<YapiApiDTO> toYapiDTO(OpenAPI openAPI) {
158
174
* @author: chengsheng@qbb6.com
159
175
* @date: 2019/5/15
160
176
*/
161
- // public YapiResponse getCatIdOrCreate(YapiSaveParam yapiSaveParam) {
162
- // Map<String, Integer> catMenuMap = catMap.get(yapiSaveParam.getProjectId().toString());
163
- // if (catMenuMap != null) {
164
- // if (!Strings.isNullOrEmpty(yapiSaveParam.getMenu())) {
165
- // if (Objects.nonNull(catMenuMap.get(yapiSaveParam.getMenu()))) {
166
- // return new YapiResponse(catMenuMap.get(yapiSaveParam.getMenu()));
167
- // }
168
- // } else {
169
- // if (Objects.nonNull(catMenuMap.get(YapiConstant.menu))) {
170
- // return new YapiResponse(catMenuMap.get(YapiConstant.menu));
171
- // }
172
- // yapiSaveParam.setMenu(YapiConstant.menu);
173
- // }
174
- // }
175
- // String response = null;
176
- // try {
177
- // response = HttpClientUtil.ObjectToString(HttpClientUtil.getHttpclient().execute(this.getHttpGet(yapiSaveParam.getYapiUrl() + YapiConstant.yapiCatMenu + "?project_id=" + yapiSaveParam.getProjectId() + "&token=" + yapiSaveParam.getToken())), "utf-8");
178
- // YapiResponse yapiResponse = gson.fromJson(response, YapiResponse.class);
179
- // if (yapiResponse.getErrcode() == 0) {
180
- // List<YapiCatResponse> list = (List<YapiCatResponse>) yapiResponse.getData();
181
- // list = gson.fromJson(gson.toJson(list), new TypeToken<List<YapiCatResponse>>() {
182
- // }.getType());
183
- // for (YapiCatResponse yapiCatResponse : list) {
184
- // if (yapiCatResponse.getName().equals(yapiSaveParam.getMenu())) {
185
- // Map<String, Integer> catMenuMapSub = catMap.get(yapiSaveParam.getProjectId().toString());
186
- // if (catMenuMapSub != null) {
187
- // catMenuMapSub.put(yapiCatResponse.getName(), yapiCatResponse.get_id());
188
- // } else {
189
- // catMenuMapSub = new HashMap<>();
190
- // catMenuMapSub.put(yapiCatResponse.getName(), yapiCatResponse.get_id());
191
- // catMap.put(yapiSaveParam.getProjectId().toString(), catMenuMapSub);
192
- // }
193
- // return new YapiResponse(yapiCatResponse.get_id());
194
- // }
195
- // }
196
- // }
197
- // YapiCatMenuParam yapiCatMenuParam = new YapiCatMenuParam(yapiSaveParam.getMenu(), yapiSaveParam.getProjectId(), yapiSaveParam.getToken());
198
- // String responseCat = HttpClientUtil.ObjectToString(HttpClientUtil.getHttpclient().execute(this.getHttpPost(yapiSaveParam.getYapiUrl() + YapiConstant.yapiAddCat, gson.toJson(yapiCatMenuParam))), "utf-8");
199
- // YapiCatResponse yapiCatResponse = gson.fromJson(gson.fromJson(responseCat, YapiResponse.class).getData().toString(), YapiCatResponse.class);
200
- // Map<String, Integer> catMenuMapSub = catMap.get(yapiSaveParam.getProjectId().toString());
201
- // if (catMenuMapSub != null) {
202
- // catMenuMapSub.put(yapiCatResponse.getName(), yapiCatResponse.get_id());
203
- // } else {
204
- // catMenuMapSub = new HashMap<>();
205
- // catMenuMapSub.put(yapiCatResponse.getName(), yapiCatResponse.get_id());
206
- // catMap.put(yapiSaveParam.getProjectId().toString(), catMenuMapSub);
207
- // }
208
- // return new YapiResponse(yapiCatResponse.get_id());
209
- // } catch (IOException e) {
210
- // e.printStackTrace();
211
- // return new YapiResponse(0, e.toString());
212
- // }
213
- // }
177
+ public Integer getCatIdOrCreate (String tag ) {
178
+ Integer catId = null ;
179
+ String response = null ;
180
+ if (Optional .ofNullable (catMap .get (projectId )).map (a -> a .get (tag )).isPresent ()) {
181
+ return catMap .get (projectId ).get (tag );
182
+ }
183
+ try {
184
+ response = HttpClientUtil .ObjectToString (HttpClientUtil .getHttpclient ().execute (this .getHttpGet (yapiUrl + YapiConstant .yapiCatMenu + "?project_id=" + projectId + "&token=" + projectToken )), "utf-8" );
185
+ YapiResponse yapiResponse = mapper .readValue (response , YapiResponse .class );
186
+ if (yapiResponse .getErrcode () == 0 ) {
187
+ List <YapiCatResponse > list = (List <YapiCatResponse >) yapiResponse .getData ();
188
+ list = mapper .readValue (mapper .writeValueAsString (list ), new TypeReference <List <YapiCatResponse >>() {
189
+ });
190
+ for (YapiCatResponse yapiCatResponse : list ) {
191
+ if (yapiCatResponse .getName ().equals (tag )) {
192
+ Map <String , Integer > catMenuMapSub = catMap .get (projectId );
193
+ if (catMenuMapSub != null ) {
194
+ catMenuMapSub .put (yapiCatResponse .getName (), yapiCatResponse .get_id ());
195
+ } else {
196
+ catMenuMapSub = new HashMap <>();
197
+ catMenuMapSub .put (yapiCatResponse .getName (), yapiCatResponse .get_id ());
198
+ catMap .put (projectId , catMenuMapSub );
199
+ }
200
+ catId = yapiCatResponse .get_id ();
201
+ }
202
+ }
203
+ }
204
+ YapiCatMenuParam yapiCatMenuParam = new YapiCatMenuParam (tag , projectId , projectToken );
205
+ String responseCat = HttpClientUtil .ObjectToString (HttpClientUtil .getHttpclient ().execute (this .getHttpPost (yapiUrl + YapiConstant .yapiAddCat , mapper .writeValueAsString (yapiCatMenuParam ))), "utf-8" );
206
+ catId = (Integer ) ((LinkedHashMap ) mapper .readValue (responseCat , YapiResponse .class ).getData ()).get ("_id" );
207
+ String name = (String ) ((LinkedHashMap ) mapper .readValue (responseCat , YapiResponse .class ).getData ()).get ("name" );
208
+ Map <String , Integer > catMenuMapSub = catMap .get (projectId );
209
+ if (catMenuMapSub != null ) {
210
+ catMenuMapSub .put (name , catId );
211
+ } else {
212
+ catMenuMapSub = new HashMap <>();
213
+ catMenuMapSub .put (name , catId );
214
+ catMap .put (projectId , catMenuMapSub );
215
+ }
216
+
217
+ } catch (IOException e ) {
218
+ e .printStackTrace ();
219
+ }
220
+
221
+ return catMap .get (projectId ).get (tag );
222
+ }
223
+
224
+ private HttpGet getHttpGet (String url ) {
225
+ try {
226
+ return HttpClientUtil .getHttpGet (url , "application/json" , "application/json; charset=utf-8" );
227
+ } catch (IOException e ) {
228
+ e .printStackTrace ();
229
+ }
230
+ return null ;
231
+ }
214
232
215
233
}
0 commit comments