1818import org .apache .commons .lang .StringUtils ;
1919import org .apache .maven .model .Model ;
2020import org .apache .maven .model .io .xpp3 .MavenXpp3Reader ;
21- import org .jetbrains .annotations .NotNull ;
2221import site .forgus .plugins .apigenerator .config .ApiGeneratorConfig ;
2322import site .forgus .plugins .apigenerator .constant .TypeEnum ;
2423import site .forgus .plugins .apigenerator .constant .WebAnnotation ;
3231import site .forgus .plugins .apigenerator .yapi .model .*;
3332import site .forgus .plugins .apigenerator .yapi .sdk .YApiSdk ;
3433
35- import javax .swing .*;
3634import java .io .*;
3735import java .nio .charset .StandardCharsets ;
3836import java .util .*;
@@ -122,19 +120,13 @@ private void uploadHttpMethodsToYApi(Project project, PsiClass psiClass) throws
122120 }
123121 config .getState ().projectId = projectId ;
124122 }
123+ Map <String , YApiCat > catNameMap = getCatNameMap ();
125124 PsiMethod [] methods = psiClass .getMethods ();
126- boolean uploadSuccess = false ;
127125 for (PsiMethod method : methods ) {
128126 if (hasMappingAnnotation (method )) {
129- uploadToYApi (project , method );
130- uploadSuccess = true ;
127+ uploadToYApi (project , method ,catNameMap );
131128 }
132129 }
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 );
138130 }
139131
140132 private void generateMarkdownForInterface (Project project , PsiElement referenceAt , PsiClass selectedClass ) {
@@ -253,6 +245,29 @@ private void uploadToYApi(Project project, PsiMethod psiMethod) throws IOExcepti
253245 }
254246 }
255247
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+
256271 private YApiInterfaceWrapper buildYApiInterface (PsiMethod psiMethod ) {
257272 try {
258273 PsiClass containingClass = psiMethod .getContainingClass ();
@@ -285,11 +300,68 @@ private YApiInterfaceWrapper buildYApiInterface(PsiMethod psiMethod) {
285300 }
286301 }
287302 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 ()));
292363 yApiInterface .setPath (buildPath (classRequestMapping , methodMapping ));
364+ yApiInterface .setTitle (StringUtils .isEmpty (methodInfo .getDesc ()) ? yApiInterface .getPath () : methodInfo .getDesc ());
293365 if (containResponseBodyAnnotation (psiMethod .getAnnotations ()) || controller .getText ().contains ("Rest" )) {
294366 yApiInterface .setReq_headers (Collections .singletonList (YApiHeader .json ()));
295367 yApiInterface .setRes_body (JsonUtil .buildJson5 (methodInfo .getResponse ()));
@@ -304,7 +376,7 @@ private YApiInterfaceWrapper buildYApiInterface(PsiMethod psiMethod) {
304376 }catch (Exception e ) {
305377 Map <String ,Object > errorInfo = new HashMap <>();
306378 //TODO errorInfo
307- errorInfo .put ("plugin_version" ,"2021.02.10 " );
379+ errorInfo .put ("plugin_version" ,"2021.02.15 " );
308380 errorInfo .put ("_cause" ,e .getMessage ());
309381 errorInfo .put ("_trace" ,buildTraceStr (e ));
310382 errorInfo .put ("method_text" ,buildMethodSnapshot (psiMethod ));
0 commit comments