|
1 | 1 | package io.github.venkat1701; |
2 | 2 |
|
3 | | -import com.squareup.javapoet.JavaFile; |
4 | | -import com.squareup.javapoet.MethodSpec; |
5 | | -import com.squareup.javapoet.TypeSpec; |
| 3 | +import com.squareup.javapoet.*; |
6 | 4 | import io.swagger.v3.oas.models.OpenAPI; |
7 | 5 | import io.swagger.v3.oas.models.Operation; |
8 | 6 | import io.swagger.v3.oas.models.PathItem; |
9 | 7 | import io.swagger.v3.parser.OpenAPIV3Parser; |
10 | | - |
11 | 8 | import javax.lang.model.element.Modifier; |
12 | 9 | import java.io.IOException; |
13 | 10 | import java.nio.file.Paths; |
14 | 11 | import java.util.Map; |
15 | 12 |
|
16 | 13 | public class ApiWrapperGenerator { |
17 | 14 | 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") |
19 | 17 | .addModifiers(Modifier.PUBLIC); |
20 | | - |
21 | 18 | openAPI.getPaths().forEach((path, pathItem) -> { |
22 | 19 | Map<PathItem.HttpMethod, Operation> operations = pathItem.readOperationsMap(); |
23 | 20 | operations.forEach((httpMethod, operation) -> { |
24 | 21 | String methodName = operation.getOperationId(); |
25 | 22 | if (methodName == null || methodName.isEmpty()) { |
26 | 23 | methodName = httpMethod.name().toLowerCase() + "_" + path; |
27 | 24 | } |
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 |
35 | 27 |
|
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()); |
37 | 52 | }); |
38 | 53 | }); |
39 | | - |
40 | 54 | TypeSpec apiWrapperClass = apiWrapperClassBuilder.build(); |
41 | | - JavaFile javaFile = JavaFile.builder("io.demotesting.api", apiWrapperClass) |
| 55 | + JavaFile javaFile = JavaFile.builder("io.github.venkat1701.api", apiWrapperClass) |
42 | 56 | .build(); |
43 | 57 | javaFile.writeTo(Paths.get(outputDir)); |
44 | 58 | } |
45 | 59 |
|
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 | | - |
55 | 60 | 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]", "_"); |
62 | 62 | } |
63 | 63 |
|
64 | 64 | public static void main(String[] args) { |
|
0 commit comments