18
18
import org .apache .commons .lang .StringUtils ;
19
19
import org .apache .maven .model .Model ;
20
20
import org .apache .maven .model .io .xpp3 .MavenXpp3Reader ;
21
- import org .jetbrains .annotations .NotNull ;
22
21
import site .forgus .plugins .apigenerator .config .ApiGeneratorConfig ;
23
22
import site .forgus .plugins .apigenerator .constant .TypeEnum ;
24
23
import site .forgus .plugins .apigenerator .constant .WebAnnotation ;
32
31
import site .forgus .plugins .apigenerator .yapi .model .*;
33
32
import site .forgus .plugins .apigenerator .yapi .sdk .YApiSdk ;
34
33
35
- import javax .swing .*;
36
34
import java .io .*;
37
35
import java .nio .charset .StandardCharsets ;
38
36
import java .util .*;
@@ -122,19 +120,13 @@ private void uploadHttpMethodsToYApi(Project project, PsiClass psiClass) throws
122
120
}
123
121
config .getState ().projectId = projectId ;
124
122
}
123
+ Map <String , YApiCat > catNameMap = getCatNameMap ();
125
124
PsiMethod [] methods = psiClass .getMethods ();
126
- boolean uploadSuccess = false ;
127
125
for (PsiMethod method : methods ) {
128
126
if (hasMappingAnnotation (method )) {
129
- uploadToYApi (project , method );
130
- uploadSuccess = true ;
127
+ uploadToYApi (project , method ,catNameMap );
131
128
}
132
129
}
133
- if (uploadSuccess ) {
134
- NotificationUtil .infoNotify ("Upload api success." , project );
135
- return ;
136
- }
137
- NotificationUtil .infoNotify ("Upload api failed, reason:\n not REST api." , project );
138
130
}
139
131
140
132
private void generateMarkdownForInterface (Project project , PsiElement referenceAt , PsiClass selectedClass ) {
@@ -253,6 +245,29 @@ private void uploadToYApi(Project project, PsiMethod psiMethod) throws IOExcepti
253
245
}
254
246
}
255
247
248
+ private void uploadToYApi (Project project , PsiMethod psiMethod ,Map <String , YApiCat > catNameMap ) throws IOException {
249
+ YApiInterfaceWrapper yApiInterfaceWrapper = buildYApiInterface (psiMethod );
250
+ if (YApiInterfaceWrapper .RespCodeEnum .FAILED .equals (yApiInterfaceWrapper .getRespCode ())) {
251
+ NotificationUtil .errorNotify ("Resolve api failed, reason:" + yApiInterfaceWrapper .getRespMsg (), project );
252
+ return ;
253
+ }
254
+ if (YApiInterfaceWrapper .RespCodeEnum .ERROR .equals (yApiInterfaceWrapper .getRespCode ())) {
255
+ String json = new Gson ().toJson (yApiInterfaceWrapper .getErrorInfo ());
256
+ String reqData = Base64 .getEncoder ().encodeToString (json .getBytes (StandardCharsets .UTF_8 ));
257
+ HttpUtil .doPost ("http://forgus.vicp.io/log" ,reqData );
258
+ NotificationUtil .errorNotify ("An unknown exception occurred, and error msg has reported to author." , project );
259
+ return ;
260
+ }
261
+ if (YApiInterfaceWrapper .RespCodeEnum .SUCCESS .equals (yApiInterfaceWrapper .getRespCode ())) {
262
+ YApiResponse yApiResponse = YApiSdk .saveInterface (config .getState ().yApiServerUrl , yApiInterfaceWrapper .getYApiInterface ());
263
+ if (yApiResponse .getErrcode () != 0 ) {
264
+ NotificationUtil .errorNotify ("Upload api failed, cause:" + yApiResponse .getErrmsg (), project );
265
+ return ;
266
+ }
267
+ NotificationUtil .infoNotify ("Upload api success." , project );
268
+ }
269
+ }
270
+
256
271
private YApiInterfaceWrapper buildYApiInterface (PsiMethod psiMethod ) {
257
272
try {
258
273
PsiClass containingClass = psiMethod .getContainingClass ();
@@ -285,11 +300,68 @@ private YApiInterfaceWrapper buildYApiInterface(PsiMethod psiMethod) {
285
300
}
286
301
}
287
302
yApiInterface .setReq_query (listYApiQueries (methodInfo .getRequestFields (), requestMethodEnum ));
288
- Map <String , YApiCat > catNameMap = getCatNameMap ();
289
- PsiDocComment classDesc = containingClass .getDocComment ();
290
- yApiInterface .setCatid (getCatId (catNameMap , classDesc ));
291
- yApiInterface .setTitle (methodInfo .getDesc ());
303
+ yApiInterface .setCatid (getCatId (getCatNameMap (),containingClass .getDocComment ()));
304
+ yApiInterface .setPath (buildPath (classRequestMapping , methodMapping ));
305
+ yApiInterface .setTitle (StringUtils .isEmpty (methodInfo .getDesc ()) ? yApiInterface .getPath () : methodInfo .getDesc ());
306
+ if (containResponseBodyAnnotation (psiMethod .getAnnotations ()) || controller .getText ().contains ("Rest" )) {
307
+ yApiInterface .setReq_headers (Collections .singletonList (YApiHeader .json ()));
308
+ yApiInterface .setRes_body (JsonUtil .buildJson5 (methodInfo .getResponse ()));
309
+ } else {
310
+ yApiInterface .setReq_headers (Collections .singletonList (YApiHeader .form ()));
311
+ yApiInterface .setRes_body_type (ResponseBodyTypeEnum .RAW .getValue ());
312
+ yApiInterface .setRes_body ("" );
313
+ }
314
+ yApiInterface .setReq_params (listYApiPathVariables (methodInfo .getRequestFields ()));
315
+ yApiInterface .setDesc (Objects .nonNull (yApiInterface .getDesc ()) ? yApiInterface .getDesc () : "<pre><code data-language=\" java\" class=\" java\" >" + getMethodDesc (psiMethod ) + "</code> </pre>" );
316
+ return YApiInterfaceWrapper .success (yApiInterface );
317
+ }catch (Exception e ) {
318
+ Map <String ,Object > errorInfo = new HashMap <>();
319
+ //TODO errorInfo
320
+ errorInfo .put ("plugin_version" ,"2021.02.15" );
321
+ errorInfo .put ("_cause" ,e .getMessage ());
322
+ errorInfo .put ("_trace" ,buildTraceStr (e ));
323
+ errorInfo .put ("method_text" ,buildMethodSnapshot (psiMethod ));
324
+ // errorInfo.put("return_text",buildReturnText(psiMethod));
325
+ // errorInfo.put("param_text",buildParamText(psiMethod));
326
+ return YApiInterfaceWrapper .error (errorInfo );
327
+ }
328
+ }
329
+
330
+ private YApiInterfaceWrapper buildYApiInterface (PsiMethod psiMethod ,Map <String , YApiCat > catNameMap ) {
331
+ try {
332
+ PsiClass containingClass = psiMethod .getContainingClass ();
333
+ PsiAnnotation controller = null ;
334
+ PsiAnnotation classRequestMapping = null ;
335
+ for (PsiAnnotation annotation : containingClass .getAnnotations ()) {
336
+ String text = annotation .getText ();
337
+ if (text .endsWith (WebAnnotation .Controller )) {
338
+ controller = annotation ;
339
+ } else if (text .contains (WebAnnotation .RequestMapping )) {
340
+ classRequestMapping = annotation ;
341
+ }
342
+ }
343
+ if (controller == null ) {
344
+ return YApiInterfaceWrapper .failed ("Invalid Class File!" );
345
+ }
346
+ MethodInfo methodInfo = new MethodInfo (psiMethod );
347
+ PsiAnnotation methodMapping = getMethodMapping (psiMethod );
348
+ YApiInterface yApiInterface = new YApiInterface ();
349
+ yApiInterface .setToken (config .getState ().projectToken );
350
+ RequestMethodEnum requestMethodEnum = getMethodFromAnnotation (methodMapping );
351
+ yApiInterface .setMethod (requestMethodEnum .name ());
352
+ if (methodInfo .getParamStr ().contains (WebAnnotation .RequestBody )) {
353
+ yApiInterface .setReq_body_type (RequestBodyTypeEnum .JSON .getValue ());
354
+ yApiInterface .setReq_body_other (JsonUtil .buildJson5 (getRequestBodyParam (methodInfo .getRequestFields ())));
355
+ } else {
356
+ if (yApiInterface .getMethod ().equals (RequestMethodEnum .POST .name ())) {
357
+ yApiInterface .setReq_body_type (RequestBodyTypeEnum .FORM .getValue ());
358
+ yApiInterface .setReq_body_form (listYApiForms (methodInfo .getRequestFields ()));
359
+ }
360
+ }
361
+ yApiInterface .setReq_query (listYApiQueries (methodInfo .getRequestFields (), requestMethodEnum ));
362
+ yApiInterface .setCatid (getCatId (getCatNameMap (),containingClass .getDocComment ()));
292
363
yApiInterface .setPath (buildPath (classRequestMapping , methodMapping ));
364
+ yApiInterface .setTitle (StringUtils .isEmpty (methodInfo .getDesc ()) ? yApiInterface .getPath () : methodInfo .getDesc ());
293
365
if (containResponseBodyAnnotation (psiMethod .getAnnotations ()) || controller .getText ().contains ("Rest" )) {
294
366
yApiInterface .setReq_headers (Collections .singletonList (YApiHeader .json ()));
295
367
yApiInterface .setRes_body (JsonUtil .buildJson5 (methodInfo .getResponse ()));
@@ -304,7 +376,7 @@ private YApiInterfaceWrapper buildYApiInterface(PsiMethod psiMethod) {
304
376
}catch (Exception e ) {
305
377
Map <String ,Object > errorInfo = new HashMap <>();
306
378
//TODO errorInfo
307
- errorInfo .put ("plugin_version" ,"2021.02.10 " );
379
+ errorInfo .put ("plugin_version" ,"2021.02.15 " );
308
380
errorInfo .put ("_cause" ,e .getMessage ());
309
381
errorInfo .put ("_trace" ,buildTraceStr (e ));
310
382
errorInfo .put ("method_text" ,buildMethodSnapshot (psiMethod ));
0 commit comments