Skip to content

Commit c9044a8

Browse files
committed
vo-added-wrapper-generator-with-okhttp3
1 parent b695dab commit c9044a8

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/main/java/io/github/venkat1701/ApiWrapperGenerator.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
package io.github.venkat1701;
22

3-
import com.squareup.javapoet.JavaFile;
4-
import com.squareup.javapoet.MethodSpec;
5-
import com.squareup.javapoet.TypeSpec;
3+
import com.squareup.javapoet.*;
64
import io.swagger.v3.oas.models.OpenAPI;
75
import io.swagger.v3.oas.models.Operation;
86
import io.swagger.v3.oas.models.PathItem;
97
import io.swagger.v3.parser.OpenAPIV3Parser;
10-
118
import javax.lang.model.element.Modifier;
129
import java.io.IOException;
1310
import java.nio.file.Paths;
1411
import java.util.Map;
1512

1613
public class ApiWrapperGenerator {
1714
public static void generateApiWrapper(OpenAPI openAPI, String outputDir) throws IOException {
18-
TypeSpec.Builder apiWrapperClassBuilder = TypeSpec.classBuilder("ApiWrapperGenerator")
15+
// yeh voh class hai jismei saare code jayenge and last mei flush hoga.
16+
TypeSpec.Builder apiWrapperClassBuilder = TypeSpec.classBuilder("ApiWrapper")
1917
.addModifiers(Modifier.PUBLIC);
20-
2118
openAPI.getPaths().forEach((path, pathItem) -> {
2219
Map<PathItem.HttpMethod, Operation> operations = pathItem.readOperationsMap();
2320
operations.forEach((httpMethod, operation) -> {
2421
String methodName = operation.getOperationId();
2522
if (methodName == null || methodName.isEmpty()) {
2623
methodName = httpMethod.name().toLowerCase() + "_" + path;
2724
}
28-
MethodSpec methodSpec = MethodSpec.methodBuilder(sanitizeMethodName(sanitizePath(methodName)))
29-
.addModifiers(Modifier.PUBLIC)
30-
.returns(void.class)
31-
.addParameter(String.class, "request")
32-
.addStatement("$T.out.println($S)", System.class,
33-
"Executing " + httpMethod.name() + " on " + path)
34-
.build();
25+
// basically this will create a separate method for each openapi spec path
26+
// and then handle request-response gracefully with exc handling
3527

36-
apiWrapperClassBuilder.addMethod(methodSpec);
28+
//using code block was important and I just made use of jep 378 to write efficient and more readable code.
29+
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(sanitizeMethodName(methodName))
30+
.addModifiers(Modifier.PUBLIC)
31+
.returns(String.class)
32+
.addParameter(String.class, "jsonRequestBody")
33+
.addCode("""
34+
OkHttpClient client = new OkHttpClient();
35+
RequestBody body = RequestBody.create(jsonRequestBody, MediaType.get("application/json"));
36+
Request request = new Request.Builder()
37+
.url("$L")
38+
.method("$L", body)
39+
.build();
40+
try (Response response = client.newCall(request).execute()) {
41+
return response.body() != null ? response.body().string() : null;
42+
} catch (IOException e) {
43+
System.err.println("Network error occurred: " + e.getMessage());
44+
return "Network error";
45+
} catch (Exception e) {
46+
System.err.println("Unexpected error: " + e.getMessage());
47+
return "Unexpected error";
48+
}
49+
""", path, httpMethod.name())
50+
.addException(IOException.class);
51+
apiWrapperClassBuilder.addMethod(methodBuilder.build());
3752
});
3853
});
39-
4054
TypeSpec apiWrapperClass = apiWrapperClassBuilder.build();
41-
JavaFile javaFile = JavaFile.builder("io.demotesting.api", apiWrapperClass)
55+
JavaFile javaFile = JavaFile.builder("io.github.venkat1701.api", apiWrapperClass)
4256
.build();
4357
javaFile.writeTo(Paths.get(outputDir));
4458
}
4559

46-
private static String sanitizePath(String path) {
47-
String[] sanitized = path.replaceAll("^/+", "").replaceAll("[^a-zA-Z0-9]", "_").split("_+");
48-
String methodName = sanitized[0];
49-
for(int i=1; i<sanitized.length; i++) {
50-
methodName += Character.toUpperCase(sanitized[i].charAt(0))+sanitized[i].substring(1);
51-
}
52-
return methodName;
53-
}
54-
5560
private static String sanitizeMethodName(String methodName) {
56-
String[] methodNameArray = methodName.split("_");
57-
String sanitizedMethodName = methodNameArray[0];
58-
for(int i=1; i<methodNameArray.length; i++) {
59-
sanitizedMethodName += Character.toUpperCase(methodNameArray[i].charAt(0))+methodNameArray[i].substring(1);
60-
}
61-
return sanitizedMethodName;
61+
return methodName.replaceAll("[^a-zA-Z0-9]", "_");
6262
}
6363

6464
public static void main(String[] args) {

0 commit comments

Comments
 (0)