1- package io .github .venkat1701 ;
1+ package io .github .venkat1701 . build . utils . okhttp ;
22
33import com .squareup .javapoet .*;
44import io .swagger .v3 .oas .models .OpenAPI ;
55import io .swagger .v3 .oas .models .Operation ;
66import io .swagger .v3 .oas .models .PathItem ;
7- import io . swagger . v3 . parser . OpenAPIV3Parser ;
7+
88import javax .lang .model .element .Modifier ;
99import java .io .IOException ;
1010import java .nio .file .Paths ;
1111import java .util .Map ;
1212
13- public class ApiWrapperGenerator {
13+ /**
14+ * This ApiWrapperGenerator is responsible for generating the Client SDK with Okhttp.
15+ * @author Venkat
16+ */
17+ public class OkHttpApiClientGenerator {
1418 public static void generateApiWrapper (OpenAPI openAPI , String outputDir ) throws IOException {
15- // yeh voh class hai jismei saare code jayenge and last mei flush hoga.
1619 TypeSpec .Builder apiWrapperClassBuilder = TypeSpec .classBuilder ("ApiWrapper" )
1720 .addModifiers (Modifier .PUBLIC );
1821 openAPI .getPaths ().forEach ((path , pathItem ) -> {
@@ -22,10 +25,6 @@ public static void generateApiWrapper(OpenAPI openAPI, String outputDir) throws
2225 if (methodName == null || methodName .isEmpty ()) {
2326 methodName = httpMethod .name ().toLowerCase () + "_" + path ;
2427 }
25- // basically this will create a separate method for each openapi spec path
26- // and then handle request-response gracefully with exc handling
27-
28- //using code block was important and I just made use of jep 378 to write efficient and more readable code.
2928 MethodSpec .Builder methodBuilder = MethodSpec .methodBuilder (sanitizeMethodName (methodName ))
3029 .addModifiers (Modifier .PUBLIC )
3130 .returns (String .class )
@@ -61,18 +60,4 @@ private static String sanitizeMethodName(String methodName) {
6160 return methodName .replaceAll ("[^a-zA-Z0-9]" , "_" );
6261 }
6362
64- public static void main (String [] args ) {
65- try {
66- String specPath = "src/main/resources/openapi.yaml" ;
67- OpenAPI openAPI = new OpenAPIV3Parser ().read (specPath );
68- if (openAPI == null ) {
69- System .err .println ("Failed to parse the OpenAPI specification." );
70- return ;
71- }
72- generateApiWrapper (openAPI , "./generated-sources" );
73- System .out .println ("API wrapper generated successfully." );
74- } catch (IOException e ) {
75- e .printStackTrace ();
76- }
77- }
7863}
0 commit comments