@@ -25,33 +25,46 @@ public static void generateApiWrapper(OpenAPI openAPI, String outputDir) throws
2525 if (methodName == null || methodName .isEmpty ()) {
2626 methodName = httpMethod .name ().toLowerCase () + "_" + path ;
2727 }
28+
2829 MethodSpec .Builder methodBuilder = MethodSpec .methodBuilder (sanitizeMethodName (methodName ))
2930 .addModifiers (Modifier .PUBLIC )
3031 .returns (String .class )
3132 .addParameter (String .class , "jsonRequestBody" )
32- .addCode ("""
33- OkHttpClient client = new OkHttpClient();
34- RequestBody body = RequestBody.create(jsonRequestBody, MediaType.get("application/json"));
35- Request request = new Request.Builder()
36- .url("$L")
37- .method("$L", body)
38- .build();
39- try (Response response = client.newCall(request).execute()) {
40- return response.body() != null ? response.body().string() : null;
41- } catch (IOException e) {
42- System.err.println("Network error occurred: " + e.getMessage());
43- return "Network error";
44- } catch (Exception e) {
45- System.err.println("Unexpected error: " + e.getMessage());
46- return "Unexpected error";
47- }
48- """ , path , httpMethod .name ())
4933 .addException (IOException .class );
34+ CodeBlock methodBody = CodeBlock .builder ()
35+ .addStatement ("$T client = new $T()" , ClassName .get ("okhttp3" , "OkHttpClient" ), ClassName .get ("okhttp3" , "OkHttpClient" ))
36+ .addStatement ("$T body = $T.create(jsonRequestBody, $T.get(\" application/json\" ))" ,
37+ ClassName .get ("okhttp3" , "RequestBody" ),
38+ ClassName .get ("okhttp3" , "RequestBody" ),
39+ ClassName .get ("okhttp3" , "MediaType" ))
40+ .addStatement ("$T request = new $T.Builder()"
41+ + ".url($S)"
42+ + ".method($S, body)"
43+ + ".build()" ,
44+ ClassName .get ("okhttp3" , "Request" ),
45+ ClassName .get ("okhttp3" , "Request" ),
46+ path ,
47+ httpMethod .name ())
48+ .beginControlFlow ("try ($T response = client.newCall(request).execute())" ,
49+ ClassName .get ("okhttp3" , "Response" ))
50+ .addStatement ("return response.body() != null ? response.body().string() : null" )
51+ .endControlFlow ()
52+ .beginControlFlow ("catch ($T e)" , IOException .class )
53+ .addStatement ("$T.err.println(\" Network error occurred: \" + e.getMessage())" , System .class )
54+ .addStatement ("return \" Network error\" " )
55+ .endControlFlow ()
56+ .beginControlFlow ("catch ($T e)" , Exception .class )
57+ .addStatement ("$T.err.println(\" Unexpected error: \" + e.getMessage())" , System .class )
58+ .addStatement ("return \" Unexpected error\" " )
59+ .endControlFlow ()
60+ .build ();
61+
62+ methodBuilder .addCode (methodBody );
5063 apiWrapperClassBuilder .addMethod (methodBuilder .build ());
5164 });
5265 });
5366 TypeSpec apiWrapperClass = apiWrapperClassBuilder .build ();
54- JavaFile javaFile = JavaFile .builder ("io.github.venkat1701 .api" , apiWrapperClass )
67+ JavaFile javaFile = JavaFile .builder ("com.example .api" , apiWrapperClass )
5568 .build ();
5669 javaFile .writeTo (Paths .get (outputDir ));
5770 }
0 commit comments