|
| 1 | +import 'dart:convert'; |
| 2 | +import 'package:apidash/utils/har_utils.dart'; |
| 3 | +import 'package:apidash/utils/http_utils.dart'; |
| 4 | +import 'package:jinja/jinja.dart' as jj; |
| 5 | +import 'package:apidash/models/models.dart' show RequestModel; |
| 6 | +import 'package:apidash/consts.dart'; |
| 7 | + |
| 8 | +class JavaAsyncHttpClientGen { |
| 9 | + final String kTemplateStart = ''' |
| 10 | +import org.asynchttpclient.*; |
| 11 | +
|
| 12 | +import java.io.BufferedReader; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.concurrent.ExecutionException; |
| 18 | +import java.util.concurrent.Executors; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +
|
| 21 | +public class Main { |
| 22 | + public static void main(String[] args) { |
| 23 | + try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) { |
| 24 | +'''; |
| 25 | + |
| 26 | + final String kTemplateUrl = ''' |
| 27 | + String url = "{{url}}";\n |
| 28 | +'''; |
| 29 | + |
| 30 | + final String kTemplateRequestCreation = ''' |
| 31 | + Request request = asyncHttpClient |
| 32 | + .prepare("{{method}}", url)\n |
| 33 | +'''; |
| 34 | + |
| 35 | + final String kTemplateUrlQueryParam = ''' |
| 36 | + .addQueryParam("{{name}}", "{{value}}")\n |
| 37 | +'''; |
| 38 | + |
| 39 | + final String kTemplateRequestHeader = ''' |
| 40 | + .addHeader("{{name}}", "{{value}}")\n |
| 41 | +'''; |
| 42 | + final String kTemplateRequestFormData = ''' |
| 43 | + .addFormParam("{{name}}", "{{value}}")\n |
| 44 | +'''; |
| 45 | + |
| 46 | + String kTemplateRequestBodyContent = ''' |
| 47 | + String bodyContent = "{{body}}";\n |
| 48 | +'''; |
| 49 | + String kTemplateRequestBodySetup = ''' |
| 50 | + .setBody(bodyContent)\n |
| 51 | +'''; |
| 52 | + |
| 53 | + final String kTemplateRequestEnd = """ |
| 54 | + .build(); |
| 55 | + ListenableFuture<Response> listenableFuture = asyncHttpClient.executeRequest(request); |
| 56 | + listenableFuture.addListener(() -> { |
| 57 | + try { |
| 58 | + Response response = listenableFuture.get(); |
| 59 | + InputStream is = response.getResponseBodyAsStream(); |
| 60 | + BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); |
| 61 | + String respBody = br.lines().collect(Collectors.joining("\\n")); |
| 62 | + System.out.println(response.getStatusCode()); |
| 63 | + System.out.println(respBody); |
| 64 | + } catch (InterruptedException | ExecutionException e) { |
| 65 | + e.printStackTrace(); |
| 66 | + } |
| 67 | + }, Executors.newCachedThreadPool()); |
| 68 | + listenableFuture.get(); |
| 69 | + } catch (InterruptedException | ExecutionException | IOException ignored) { |
| 70 | +
|
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +\n |
| 75 | +"""; |
| 76 | + |
| 77 | + String? getCode( |
| 78 | + RequestModel requestModel, |
| 79 | + ) { |
| 80 | + try { |
| 81 | + String result = ""; |
| 82 | + bool hasBody = false; |
| 83 | + |
| 84 | + var rec = getValidRequestUri( |
| 85 | + requestModel.url, |
| 86 | + requestModel.enabledRequestParams, |
| 87 | + ); |
| 88 | + Uri? uri = rec.$1; |
| 89 | + |
| 90 | + if (uri == null) { |
| 91 | + return ""; |
| 92 | + } |
| 93 | + |
| 94 | + var url = stripUriParams(uri); |
| 95 | + |
| 96 | + // contains the HTTP method associated with the request |
| 97 | + var method = requestModel.method; |
| 98 | + |
| 99 | + // contains the entire request body as a string if body is present |
| 100 | + var requestBody = requestModel.requestBody; |
| 101 | + |
| 102 | + // generating the URL to which the request has to be submitted |
| 103 | + var templateUrl = jj.Template(kTemplateUrl); |
| 104 | + result += templateUrl.render({"url": url}); |
| 105 | + |
| 106 | + // creating request body if available |
| 107 | + var rM = requestModel.copyWith(url: url); |
| 108 | + var harJson = requestModelToHARJsonRequest(rM, useEnabled: true); |
| 109 | + |
| 110 | + // if request type is not form data, the request method can include |
| 111 | + // a body, and the body of the request is not null, in that case |
| 112 | + // we need to parse the body as it is, and write it to the body |
| 113 | + if (!requestModel.hasFormData && |
| 114 | + kMethodsWithBody.contains(method) && |
| 115 | + requestBody != null) { |
| 116 | + var contentLength = utf8.encode(requestBody).length; |
| 117 | + if (contentLength > 0) { |
| 118 | + var templateBodyContent = jj.Template(kTemplateRequestBodyContent); |
| 119 | + hasBody = true; |
| 120 | + if (harJson["postData"]?["text"] != null) { |
| 121 | + result += templateBodyContent.render({ |
| 122 | + "body": kEncoder.convert(harJson["postData"]["text"]).substring( |
| 123 | + 1, kEncoder.convert(harJson["postData"]["text"]).length - 1) |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + var templateRequestCreation = jj.Template(kTemplateRequestCreation); |
| 130 | + result += |
| 131 | + templateRequestCreation.render({"method": method.name.toUpperCase()}); |
| 132 | + |
| 133 | + // setting up query parameters |
| 134 | + if (uri.hasQuery) { |
| 135 | + var params = uri.queryParameters; |
| 136 | + var templateUrlQueryParam = jj.Template(kTemplateUrlQueryParam); |
| 137 | + params.forEach((name, value) { |
| 138 | + result += |
| 139 | + templateUrlQueryParam.render({"name": name, "value": value}); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + result = kTemplateStart + result; |
| 144 | + |
| 145 | + var contentType = requestModel.requestBodyContentType.header; |
| 146 | + var templateRequestHeader = jj.Template(kTemplateRequestHeader); |
| 147 | + |
| 148 | + // especially sets up Content-Type header if the request has a body |
| 149 | + // and Content-Type is not explicitely set by the developer |
| 150 | + if (hasBody && |
| 151 | + !requestModel.enabledHeadersMap.containsKey('Content-Type')) { |
| 152 | + result += templateRequestHeader |
| 153 | + .render({"name": 'Content-Type', "value": contentType}); |
| 154 | + } |
| 155 | + |
| 156 | + // setting up rest of the request headers |
| 157 | + var headers = requestModel.enabledHeadersMap; |
| 158 | + headers.forEach((name, value) { |
| 159 | + result += templateRequestHeader.render({"name": name, "value": value}); |
| 160 | + }); |
| 161 | + |
| 162 | + // handling form data |
| 163 | + if (requestModel.hasFormData && |
| 164 | + requestModel.formDataMapList.isNotEmpty && |
| 165 | + kMethodsWithBody.contains(method)) { |
| 166 | + // including form data into the request |
| 167 | + var formDataList = requestModel.formDataMapList; |
| 168 | + var templateRequestFormData = jj.Template(kTemplateRequestFormData); |
| 169 | + for (var formDataMap in formDataList) { |
| 170 | + result += templateRequestFormData.render( |
| 171 | + {"name": formDataMap['name'], "value": formDataMap['value']}); |
| 172 | + } |
| 173 | + hasBody = true; |
| 174 | + } |
| 175 | + |
| 176 | + var templateRequestBodySetup = jj.Template(kTemplateRequestBodySetup); |
| 177 | + if (kMethodsWithBody.contains(method) && hasBody) { |
| 178 | + result += templateRequestBodySetup.render(); |
| 179 | + } |
| 180 | + |
| 181 | + var templateRequestBodyEnd = jj.Template(kTemplateRequestEnd); |
| 182 | + result += templateRequestBodyEnd.render(); |
| 183 | + |
| 184 | + return result; |
| 185 | + } catch (e) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments