diff --git a/lib/codegen/codegen.dart b/lib/codegen/codegen.dart index 5e093532..5100f7aa 100644 --- a/lib/codegen/codegen.dart +++ b/lib/codegen/codegen.dart @@ -17,6 +17,7 @@ import 'others/curl.dart'; import 'julia/http.dart'; import 'java/okhttp.dart'; import 'java/async_http_client.dart'; +import 'java/httpclient.dart'; class Codegen { String? getCode( @@ -73,6 +74,8 @@ class Codegen { return JuliaHttpClientCodeGen().getCode(rM); case CodegenLanguage.javaAsyncHttpClient: return JavaAsyncHttpClientGen().getCode(rM); + case CodegenLanguage.javaHttpClient: + return JavaHttpClientCodeGen().getCode(rM); } } } diff --git a/lib/codegen/java/httpclient.dart b/lib/codegen/java/httpclient.dart new file mode 100644 index 00000000..037a9a87 --- /dev/null +++ b/lib/codegen/java/httpclient.dart @@ -0,0 +1,183 @@ +import 'dart:convert'; +import 'package:jinja/jinja.dart' as jj; +import 'package:apidash/utils/utils.dart' + show getValidRequestUri, requestModelToHARJsonRequest, stripUriParams; +import '../../models/request_model.dart'; +import 'package:apidash/consts.dart'; + +class JavaHttpClientCodeGen { + final String kTemplateStart = """ +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpHeaders; +import java.net.http.HttpRequest.BodyPublishers; +import java.net.http.HttpResponse.BodyHandlers; + +public class JavaHttpClientExample { + + public static void main(String[] args) throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); +"""; + final String kTemplateUrl = ''' + + String url = "{{url}}"; + +'''; + + final String kTemplateUrlQuery = ''' + + String url = "{{url}}"; + try { + URI uri = new URI(url); + url = uri.resolve("{{params}}").toString(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + +'''; + + String kTemplateRequestBody = ''' + + String body = "{{body}}"; + +'''; + + final String kStringRequestStart = """ + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(url)) +"""; + + final String kTemplateRequestEnd = """ + .{{method}}({{body}}) + .build(); + + HttpResponse response = client.send(request, BodyHandlers.ofString()); + System.out.println(response.statusCode()); + System.out.println(response.body()); + } +} +\n +"""; + + String? getCode( + RequestModel requestModel, + ) { + try { + String result = ""; + bool hasQuery = false; + bool hasBody = false; + bool hasJsonBody = false; + + var rec = getValidRequestUri( + requestModel.url, + requestModel.enabledRequestParams, + ); + Uri? uri = rec.$1; + + if (uri != null) { + String url = stripUriParams(uri); + + if (uri.hasQuery) { + var params = uri.queryParameters; + if (params.isNotEmpty) { + hasQuery = true; + var templateParams = jj.Template(kTemplateUrlQuery); + result += templateParams.render({"url": url, "params": uri.query}); + } + } + + if (!hasQuery) { + var templateUrl = jj.Template(kTemplateUrl); + result += templateUrl.render({"url": url}); + } + var rM = requestModel.copyWith(url: url); + + var harJson = requestModelToHARJsonRequest(rM, useEnabled: true); + + var method = requestModel.method; + var requestBody = requestModel.requestBody; + if (requestModel.hasFormData && + requestModel.formDataMapList.isNotEmpty && + kMethodsWithBody.contains(method)) { + var formDataList = requestModel.formDataMapList; + result += """ + StringBuilder formData = new StringBuilder(); + formData.append("""; + + for (var formDataMap in formDataList) { + result += '"""${formDataMap['name']}=${formDataMap['value']}&""",'; + } + + result = result.substring(0, result.length - 1); + result += ");\n"; + hasBody = true; + } else if (kMethodsWithBody.contains(method) && requestBody != null) { + var contentLength = utf8.encode(requestBody).length; + if (contentLength > 0) { + var templateBody = jj.Template(kTemplateRequestBody); + hasBody = true; + hasJsonBody = + requestBody.startsWith("{") && requestBody.endsWith("}"); + if (harJson["postData"]?["text"] != null) { + result += templateBody.render({ + "body": kEncoder.convert(harJson["postData"]["text"]).substring( + 1, kEncoder.convert(harJson["postData"]["text"]).length - 1) + }); + } + } + } + + result = kTemplateStart + result; + result += kStringRequestStart; + + var headersList = requestModel.enabledRequestHeaders; + var contentType = requestModel.requestBodyContentType.header; + if (hasBody && + !requestModel.enabledHeadersMap.containsKey('Content-Type')) { + result = + """$result .header("Content-Type", "$contentType")\n"""; + } + if (headersList != null) { + var headers = requestModel.enabledHeadersMap; + if (headers.isNotEmpty) { + result += getHeaders(headers, hasJsonBody); + } + } + + var templateRequestEnd = jj.Template(kTemplateRequestEnd); + + if (kMethodsWithBody.contains(method)) { + result += templateRequestEnd.render({ + "method": method.name.toUpperCase(), + "body": hasBody + ? "BodyPublishers.ofString(body)" + : "BodyPublishers.noBody()" + }); + } else { + result += templateRequestEnd + .render({"method": method.name.toUpperCase(), "body": ""}); + } + } + return result; + } catch (e) { + return null; + } + } + + String getHeaders(Map headers, hasJsonBody) { + String result = ""; + for (final k in headers.keys) { + if (k.toLowerCase() == 'authorization') { + result = """$result .header("$k", "${headers[k]}")\n"""; + } else { + result = """$result .header("$k", "${headers[k]}")\n"""; + } + } + return result; + } +} diff --git a/lib/consts.dart b/lib/consts.dart index 093c1a8c..33c0190b 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -279,6 +279,7 @@ enum CodegenLanguage { rustUreq("Rust (ureq)", "rust", "rs"), javaOkHttp("Java (okhttp3)", "java", 'java'), javaAsyncHttpClient("Java (async-http-client)", "java", "java"), + javaHttpClient("Java (HttpClient)", "java", "java"), juliaHttp("Julia (HTTP)", "julia", "jl"); const CodegenLanguage(this.label, this.codeHighlightLang, this.ext);