mirror of
https://github.com/foss42/apidash.git
synced 2025-06-07 03:48:11 +08:00
Add codegen for java okhttp and test cases
This commit is contained in:
@ -135,6 +135,7 @@ API Dash currently supports API integration code generation for the following la
|
|||||||
| Python | `http.client` |
|
| Python | `http.client` |
|
||||||
| Python | `requests` |
|
| Python | `requests` |
|
||||||
| Kotlin | `okhttp3` |
|
| Kotlin | `okhttp3` |
|
||||||
|
| Java | `okhttp3` |
|
||||||
|
|
||||||
We welcome contributions to support other programming languages/libraries/frameworks. Please check out more details [here](https://github.com/foss42/apidash/discussions/80).
|
We welcome contributions to support other programming languages/libraries/frameworks. Please check out more details [here](https://github.com/foss42/apidash/discussions/80).
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ import 'js/fetch.dart';
|
|||||||
import 'others/har.dart';
|
import 'others/har.dart';
|
||||||
import 'others/curl.dart';
|
import 'others/curl.dart';
|
||||||
import 'julia/http.dart';
|
import 'julia/http.dart';
|
||||||
|
import 'java/okhttp.dart';
|
||||||
|
|
||||||
class Codegen {
|
class Codegen {
|
||||||
String? getCode(
|
String? getCode(
|
||||||
@ -52,6 +53,8 @@ class Codegen {
|
|||||||
return FetchCodeGen(isNodeJs: true).getCode(rM);
|
return FetchCodeGen(isNodeJs: true).getCode(rM);
|
||||||
case CodegenLanguage.kotlinOkHttp:
|
case CodegenLanguage.kotlinOkHttp:
|
||||||
return KotlinOkHttpCodeGen().getCode(rM);
|
return KotlinOkHttpCodeGen().getCode(rM);
|
||||||
|
case CodegenLanguage.javaOkHttp:
|
||||||
|
return JavaOkHttpCodeGen().getCode(rM);
|
||||||
case CodegenLanguage.pythonHttpClient:
|
case CodegenLanguage.pythonHttpClient:
|
||||||
return PythonHttpClientCodeGen()
|
return PythonHttpClientCodeGen()
|
||||||
.getCode(rM, boundary: boundary ?? getNewUuid());
|
.getCode(rM, boundary: boundary ?? getNewUuid());
|
||||||
|
190
lib/codegen/java/okhttp.dart
Normal file
190
lib/codegen/java/okhttp.dart
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'package:jinja/jinja.dart' as jj;
|
||||||
|
import 'package:apidash/utils/utils.dart'
|
||||||
|
show getValidRequestUri, stripUriParams;
|
||||||
|
import '../../models/request_model.dart';
|
||||||
|
import 'package:apidash/consts.dart';
|
||||||
|
|
||||||
|
class JavaOkHttpCodeGen {
|
||||||
|
final String kTemplateStart = """
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;{{importForQuery}}{{importForBody}}{{importForFormData}}
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
||||||
|
|
||||||
|
""";
|
||||||
|
|
||||||
|
final String kStringImportForQuery = """
|
||||||
|
|
||||||
|
import okhttp3.HttpUrl;""";
|
||||||
|
|
||||||
|
final String kStringImportForBody = """
|
||||||
|
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.MediaType;""";
|
||||||
|
|
||||||
|
final String kStringImportForFormData = """
|
||||||
|
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.MultipartBody;""";
|
||||||
|
|
||||||
|
final String kTemplateUrl = '''
|
||||||
|
|
||||||
|
String url = "{{url}}";
|
||||||
|
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateUrlQuery = '''
|
||||||
|
|
||||||
|
HttpUrl url = HttpUrl.parse("{{url}}").newBuilder()
|
||||||
|
{{params}}
|
||||||
|
.build();
|
||||||
|
|
||||||
|
''';
|
||||||
|
|
||||||
|
String kTemplateRequestBody = '''
|
||||||
|
|
||||||
|
MediaType mediaType = MediaType.parse("{{contentType}}");
|
||||||
|
|
||||||
|
RequestBody body = RequestBody.create({{body}}, mediaType);
|
||||||
|
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kStringRequestStart = """
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
""";
|
||||||
|
|
||||||
|
final String kTemplateRequestEnd = """
|
||||||
|
.{{method}}({{hasBody}})
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
System.out.println(response.code());
|
||||||
|
if (response.body() != null) {
|
||||||
|
System.out.println(response.body().string());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
""";
|
||||||
|
// Converting list of form data objects to kolin multi part data
|
||||||
|
String kFormDataBody = '''
|
||||||
|
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
|
||||||
|
{%- for item in formDataList -%}
|
||||||
|
{% if item.type == 'file' %}
|
||||||
|
.addFormDataPart("{{ item.name }}",null,RequestBody.create(MediaType.parse("application/octet-stream"),new File("{{ item.value }}")))
|
||||||
|
{%- else %}
|
||||||
|
.addFormDataPart("{{ item.name }}","{{ item.value }}")
|
||||||
|
{%- endif %}
|
||||||
|
{%- endfor %}
|
||||||
|
.build();
|
||||||
|
|
||||||
|
''';
|
||||||
|
|
||||||
|
String? getCode(
|
||||||
|
RequestModel requestModel,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
String result = "";
|
||||||
|
bool hasQuery = false;
|
||||||
|
bool hasBody = false;
|
||||||
|
bool hasFormData = 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": getQueryParams(params)});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!hasQuery) {
|
||||||
|
var templateUrl = jj.Template(kTemplateUrl);
|
||||||
|
result += templateUrl.render({"url": url});
|
||||||
|
}
|
||||||
|
|
||||||
|
var method = requestModel.method;
|
||||||
|
var requestBody = requestModel.requestBody;
|
||||||
|
if (requestModel.hasFormData) {
|
||||||
|
hasFormData = true;
|
||||||
|
var formDataTemplate = jj.Template(kFormDataBody);
|
||||||
|
|
||||||
|
result += formDataTemplate.render({
|
||||||
|
"formDataList": requestModel.formDataMapList,
|
||||||
|
});
|
||||||
|
} else if (kMethodsWithBody.contains(method) && requestBody != null) {
|
||||||
|
var contentLength = utf8.encode(requestBody).length;
|
||||||
|
if (contentLength > 0) {
|
||||||
|
hasBody = true;
|
||||||
|
String contentType = requestModel.requestBodyContentType.header;
|
||||||
|
var templateBody = jj.Template(kTemplateRequestBody);
|
||||||
|
result += templateBody.render({
|
||||||
|
"contentType": contentType,
|
||||||
|
"body": kEncoder.convert(requestBody)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateStart = jj.Template(kTemplateStart);
|
||||||
|
var stringStart = templateStart.render({
|
||||||
|
"importForQuery": hasQuery ? kStringImportForQuery : "",
|
||||||
|
"importForBody": hasBody ? kStringImportForBody : "",
|
||||||
|
"importForFormData": hasFormData ? kStringImportForFormData : ""
|
||||||
|
});
|
||||||
|
|
||||||
|
result = stringStart + result;
|
||||||
|
result += kStringRequestStart;
|
||||||
|
|
||||||
|
var headersList = requestModel.enabledRequestHeaders;
|
||||||
|
if (headersList != null) {
|
||||||
|
var headers = requestModel.enabledHeadersMap;
|
||||||
|
if (headers.isNotEmpty) {
|
||||||
|
result += getHeaders(headers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateRequestEnd = jj.Template(kTemplateRequestEnd);
|
||||||
|
result += templateRequestEnd.render({
|
||||||
|
"method": method.name.toLowerCase(),
|
||||||
|
"hasBody": (hasBody || requestModel.hasFormData) ? "body" : "",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String getQueryParams(Map<String, String> params) {
|
||||||
|
final paramStrings = params.entries.map((entry) => '.addQueryParameter("${entry.key}", "${entry.value}")').toList();
|
||||||
|
return paramStrings.join('\n ');
|
||||||
|
}
|
||||||
|
|
||||||
|
String getHeaders(Map<String, String> headers) {
|
||||||
|
String result = "";
|
||||||
|
for (final k in headers.keys) {
|
||||||
|
result = """$result .addHeader("$k", "${headers[k]}")\n""";
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -272,6 +272,7 @@ enum CodegenLanguage {
|
|||||||
nodejsAxios("node.js (axios)", "javascript", "js"),
|
nodejsAxios("node.js (axios)", "javascript", "js"),
|
||||||
nodejsFetch("node.js (fetch)", "javascript", "js"),
|
nodejsFetch("node.js (fetch)", "javascript", "js"),
|
||||||
kotlinOkHttp("Kotlin (okhttp3)", "java", "kt"),
|
kotlinOkHttp("Kotlin (okhttp3)", "java", "kt"),
|
||||||
|
javaOkHttp("Java (okhttp3)", "java", 'java'),
|
||||||
pythonHttpClient("Python (http.client)", "python", "py"),
|
pythonHttpClient("Python (http.client)", "python", "py"),
|
||||||
pythonRequests("Python (requests)", "python", "py"),
|
pythonRequests("Python (requests)", "python", "py"),
|
||||||
rustActix("Rust (Actix Client)", "rust", "rs"),
|
rustActix("Rust (Actix Client)", "rust", "rs"),
|
||||||
|
1032
test/codegen/java_okhttp_codegen_test.dart
Normal file
1032
test/codegen/java_okhttp_codegen_test.dart
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user