mirror of
https://github.com/foss42/apidash.git
synced 2025-06-17 11:54:51 +08:00
refactoring and fixes
This commit is contained in:
@ -58,8 +58,7 @@ class Codegen {
|
|||||||
case CodegenLanguage.nodejsFetch:
|
case CodegenLanguage.nodejsFetch:
|
||||||
return FetchCodeGen(isNodeJs: true).getCode(rM);
|
return FetchCodeGen(isNodeJs: true).getCode(rM);
|
||||||
case CodegenLanguage.javaAsyncHttpClient:
|
case CodegenLanguage.javaAsyncHttpClient:
|
||||||
return JavaAsyncHttpClientGen()
|
return JavaAsyncHttpClientGen().getCode(rM);
|
||||||
.getCode(rM, boundary: boundary ?? getNewUuid());
|
|
||||||
case CodegenLanguage.javaHttpClient:
|
case CodegenLanguage.javaHttpClient:
|
||||||
return JavaHttpClientCodeGen().getCode(rM);
|
return JavaHttpClientCodeGen().getCode(rM);
|
||||||
case CodegenLanguage.javaOkHttp:
|
case CodegenLanguage.javaOkHttp:
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
import 'dart:convert';
|
|
||||||
import 'package:apidash/utils/har_utils.dart';
|
|
||||||
import 'package:apidash/utils/http_utils.dart';
|
|
||||||
import 'package:jinja/jinja.dart' as jj;
|
import 'package:jinja/jinja.dart' as jj;
|
||||||
|
import 'package:apidash/utils/utils.dart'
|
||||||
|
show getValidRequestUri, stripUriParams;
|
||||||
import 'package:apidash/models/models.dart' show RequestModel;
|
import 'package:apidash/models/models.dart' show RequestModel;
|
||||||
import 'package:apidash/consts.dart';
|
import 'package:apidash/consts.dart';
|
||||||
|
|
||||||
class JavaAsyncHttpClientGen {
|
class JavaAsyncHttpClientGen {
|
||||||
final String kTemplateStart = '''
|
final String kStringStart = '''
|
||||||
import org.asynchttpclient.*;
|
import org.asynchttpclient.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -20,17 +19,18 @@ import java.util.stream.Collectors;
|
|||||||
final String kTemplateMultipartImport = '''
|
final String kTemplateMultipartImport = '''
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import org.asynchttpclient.request.body.multipart.FilePart;
|
|
||||||
import org.asynchttpclient.request.body.multipart.StringPart;
|
import org.asynchttpclient.request.body.multipart.StringPart;
|
||||||
|
{% if hasFileInFormData %}import org.asynchttpclient.request.body.multipart.FilePart;
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
''';
|
''';
|
||||||
|
|
||||||
final String kTemplateMainClassMainMethodStart = '''
|
final String kStringMainClassMainMethodStart = '''
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
''';
|
''';
|
||||||
|
|
||||||
final String kTemplateAsyncHttpClientTryBlockStart = '''
|
final String kStringAsyncHttpClientTryBlockStart = '''
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
''';
|
''';
|
||||||
|
|
||||||
@ -51,10 +51,6 @@ public class Main {
|
|||||||
requestBuilder{% for name, value in headers %}
|
requestBuilder{% for name, value in headers %}
|
||||||
.addHeader("{{ name }}", "{{ value }}"){% endfor %};\n
|
.addHeader("{{ name }}", "{{ value }}"){% endfor %};\n
|
||||||
''';
|
''';
|
||||||
final String kTemplateSimpleTextFormData = '''
|
|
||||||
requestBuilder{% for param in params if param.type == "text" %}
|
|
||||||
.addFormParam("{{ param.name }}", "{{ param.value }}"){% endfor %};\n
|
|
||||||
''';
|
|
||||||
|
|
||||||
final String kTemplateMultipartTextFormData = '''
|
final String kTemplateMultipartTextFormData = '''
|
||||||
|
|
||||||
@ -95,13 +91,14 @@ public class Main {
|
|||||||
''';
|
''';
|
||||||
|
|
||||||
String kTemplateRequestBodyContent = '''
|
String kTemplateRequestBodyContent = '''
|
||||||
String bodyContent = "{{body}}";\n
|
String bodyContent = """
|
||||||
|
{{body}}""";\n
|
||||||
''';
|
''';
|
||||||
String kTemplateRequestBodySetup = '''
|
String kStringRequestBodySetup = '''
|
||||||
requestBuilder.setBody(bodyContent);\n
|
requestBuilder.setBody(bodyContent);
|
||||||
''';
|
''';
|
||||||
|
|
||||||
final String kTemplateRequestEnd = '''
|
final String kStringRequestEnd = '''
|
||||||
Future<Response> whenResponse = requestBuilder.execute();
|
Future<Response> whenResponse = requestBuilder.execute();
|
||||||
Response response = whenResponse.get();
|
Response response = whenResponse.get();
|
||||||
InputStream is = response.getResponseBodyAsStream();
|
InputStream is = response.getResponseBodyAsStream();
|
||||||
@ -113,13 +110,12 @@ public class Main {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}\n
|
}
|
||||||
''';
|
''';
|
||||||
|
|
||||||
String? getCode(
|
String? getCode(
|
||||||
RequestModel requestModel, {
|
RequestModel requestModel,
|
||||||
String? boundary,
|
) {
|
||||||
}) {
|
|
||||||
try {
|
try {
|
||||||
String result = '';
|
String result = '';
|
||||||
bool hasBody = false;
|
bool hasBody = false;
|
||||||
@ -134,46 +130,33 @@ public class Main {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
result += kTemplateStart;
|
result += kStringStart;
|
||||||
if (requestModel.hasBody && requestModel.hasFileInFormData) {
|
if (requestModel.hasFormData) {
|
||||||
result += kTemplateMultipartImport;
|
var templateMultipartImport = jj.Template(kTemplateMultipartImport);
|
||||||
|
result += templateMultipartImport
|
||||||
|
.render({"hasFileInFormData": requestModel.hasFileInFormData});
|
||||||
|
;
|
||||||
}
|
}
|
||||||
result += kTemplateMainClassMainMethodStart;
|
result += kStringMainClassMainMethodStart;
|
||||||
result += kTemplateAsyncHttpClientTryBlockStart;
|
result += kStringAsyncHttpClientTryBlockStart;
|
||||||
|
|
||||||
var url = stripUriParams(uri);
|
var url = stripUriParams(uri);
|
||||||
|
|
||||||
// contains the HTTP method associated with the request
|
// contains the HTTP method associated with the request
|
||||||
var method = requestModel.method;
|
var method = requestModel.method;
|
||||||
|
|
||||||
// contains the entire request body as a string if body is present
|
|
||||||
var requestBody = requestModel.requestBody;
|
|
||||||
|
|
||||||
// generating the URL to which the request has to be submitted
|
// generating the URL to which the request has to be submitted
|
||||||
var templateUrl = jj.Template(kTemplateUrl);
|
var templateUrl = jj.Template(kTemplateUrl);
|
||||||
result += templateUrl.render({"url": url});
|
result += templateUrl.render({"url": url});
|
||||||
|
|
||||||
// creating request body if available
|
|
||||||
var rM = requestModel.copyWith(url: url);
|
|
||||||
var harJson = requestModelToHARJsonRequest(rM, useEnabled: true);
|
|
||||||
|
|
||||||
// if request type is not form data, the request method can include
|
// if request type is not form data, the request method can include
|
||||||
// a body, and the body of the request is not null, in that case
|
// a body, and the body of the request is not null, in that case
|
||||||
// we need to parse the body as it is, and write it to the body
|
// we need to parse the body as it is, and write it to the body
|
||||||
if (!requestModel.hasFormData &&
|
if (requestModel.hasTextData || requestModel.hasJsonData) {
|
||||||
kMethodsWithBody.contains(method) &&
|
|
||||||
requestBody != null) {
|
|
||||||
var contentLength = utf8.encode(requestBody).length;
|
|
||||||
if (contentLength > 0) {
|
|
||||||
var templateBodyContent = jj.Template(kTemplateRequestBodyContent);
|
var templateBodyContent = jj.Template(kTemplateRequestBodyContent);
|
||||||
|
result +=
|
||||||
|
templateBodyContent.render({"body": requestModel.requestBody});
|
||||||
hasBody = true;
|
hasBody = true;
|
||||||
if (harJson["postData"]?["text"] != null) {
|
|
||||||
result += templateBodyContent.render({
|
|
||||||
"body": kEncoder.convert(harJson["postData"]["text"]).substring(
|
|
||||||
1, kEncoder.convert(harJson["postData"]["text"]).length - 1)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var templateRequestCreation = jj.Template(kTemplateRequestCreation);
|
var templateRequestCreation = jj.Template(kTemplateRequestCreation);
|
||||||
@ -186,44 +169,16 @@ public class Main {
|
|||||||
result += templateUrlQueryParam.render({"queryParams": params});
|
result += templateUrlQueryParam.render({"queryParams": params});
|
||||||
}
|
}
|
||||||
|
|
||||||
var headers = <String, String>{};
|
var headers = requestModel.enabledHeadersMap;
|
||||||
for (var i in harJson["headers"]) {
|
if (hasBody && !requestModel.hasContentTypeHeader) {
|
||||||
headers[i["name"]] = i["value"];
|
headers[kHeaderContentType] =
|
||||||
}
|
requestModel.requestBodyContentType.header;
|
||||||
|
|
||||||
// especially sets up Content-Type header if the request has a body
|
|
||||||
// and Content-Type is not explicitely set by the developer
|
|
||||||
if (requestModel.hasBody && !headers.containsKey("Content-Type")) {
|
|
||||||
headers["Content-Type"] = requestModel.requestBodyContentType.header;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (requestModel.hasBody &&
|
|
||||||
requestModel.hasFormData &&
|
|
||||||
!requestModel.hasFileInFormData) {
|
|
||||||
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
||||||
}
|
|
||||||
|
|
||||||
// we will use this request boundary to set boundary if multipart formdata is used
|
|
||||||
// String requestBoundary = "";
|
|
||||||
String multipartTypePrefix = "multipart/form-data; boundary=";
|
|
||||||
if (headers.containsKey("Content-Type") &&
|
|
||||||
headers["Content-Type"]!.startsWith(RegExp(multipartTypePrefix))) {
|
|
||||||
// String tmp = headers["Content-Type"]!;
|
|
||||||
// requestBoundary = tmp.substring(multipartTypePrefix.length);
|
|
||||||
|
|
||||||
// if a boundary is provided, we will use that as the default boundary
|
|
||||||
if (boundary != null) {
|
|
||||||
// requestBoundary = boundary;
|
|
||||||
headers["Content-Type"] = multipartTypePrefix + boundary;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setting up rest of the request headers
|
// setting up rest of the request headers
|
||||||
if (headers.isNotEmpty) {
|
if (headers.isNotEmpty) {
|
||||||
var templateRequestHeader = jj.Template(kTemplateRequestHeader);
|
var templateRequestHeader = jj.Template(kTemplateRequestHeader);
|
||||||
result += templateRequestHeader.render({
|
result += templateRequestHeader.render({"headers": headers});
|
||||||
"headers": headers //
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handling form data
|
// handling form data
|
||||||
@ -240,13 +195,11 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (textCount > 0) {
|
if (textCount > 0) {
|
||||||
var templateRequestFormData = jj.Template(
|
var templateRequestFormData =
|
||||||
(requestModel.hasFileInFormData)
|
jj.Template(kTemplateMultipartTextFormData);
|
||||||
? kTemplateMultipartTextFormData
|
|
||||||
: kTemplateSimpleTextFormData);
|
|
||||||
|
|
||||||
result += templateRequestFormData.render({
|
result += templateRequestFormData.render({
|
||||||
"params": formDataList, //
|
"params": formDataList,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,15 +212,11 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var templateRequestBodySetup = jj.Template(kTemplateRequestBodySetup);
|
if (hasBody) {
|
||||||
if (kMethodsWithBody.contains(method) &&
|
result += kStringRequestBodySetup;
|
||||||
hasBody &&
|
|
||||||
!requestModel.hasFormData) {
|
|
||||||
result += templateRequestBodySetup.render();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var templateRequestBodyEnd = jj.Template(kTemplateRequestEnd);
|
result += kStringRequestEnd;
|
||||||
result += templateRequestBodyEnd.render();
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -516,7 +516,10 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://api.apidash.dev/case/lower";
|
String url = "https://api.apidash.dev/case/lower";
|
||||||
String bodyContent = "{\n\"text\": \"I LOVE Flutter\"\n}";
|
String bodyContent = """
|
||||||
|
{
|
||||||
|
"text": "I LOVE Flutter"
|
||||||
|
}""";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "text/plain");
|
.addHeader("Content-Type", "text/plain");
|
||||||
@ -553,7 +556,15 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://api.apidash.dev/case/lower";
|
String url = "https://api.apidash.dev/case/lower";
|
||||||
String bodyContent = "{\n\"text\": \"I LOVE Flutter\",\n\"flag\": null,\n\"male\": true,\n\"female\": false,\n\"no\": 1.2,\n\"arr\": [\"null\", \"true\", \"false\", null]\n}";
|
String bodyContent = """
|
||||||
|
{
|
||||||
|
"text": "I LOVE Flutter",
|
||||||
|
"flag": null,
|
||||||
|
"male": true,
|
||||||
|
"female": false,
|
||||||
|
"no": 1.2,
|
||||||
|
"arr": ["null", "true", "false", null]
|
||||||
|
}""";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "application/json");
|
.addHeader("Content-Type", "application/json");
|
||||||
@ -590,11 +601,14 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://api.apidash.dev/case/lower";
|
String url = "https://api.apidash.dev/case/lower";
|
||||||
String bodyContent = "{\n\"text\": \"I LOVE Flutter\"\n}";
|
String bodyContent = """
|
||||||
|
{
|
||||||
|
"text": "I LOVE Flutter"
|
||||||
|
}""";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "application/json")
|
.addHeader("User-Agent", "Test Agent")
|
||||||
.addHeader("User-Agent", "Test Agent");
|
.addHeader("Content-Type", "application/json");
|
||||||
requestBuilder.setBody(bodyContent);
|
requestBuilder.setBody(bodyContent);
|
||||||
Future<Response> whenResponse = requestBuilder.execute();
|
Future<Response> whenResponse = requestBuilder.execute();
|
||||||
Response response = whenResponse.get();
|
Response response = whenResponse.get();
|
||||||
@ -624,17 +638,30 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import org.asynchttpclient.request.body.multipart.StringPart;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://api.apidash.dev/io/form";
|
String url = "https://api.apidash.dev/io/form";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
||||||
requestBuilder
|
|
||||||
.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
Map<String, String> params = new HashMap<>() {
|
||||||
requestBuilder
|
{
|
||||||
.addFormParam("text", "API")
|
put("text", "API");
|
||||||
.addFormParam("sep", "|")
|
put("sep", "|");
|
||||||
.addFormParam("times", "3");
|
put("times", "3");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String paramName : params.keySet()) {
|
||||||
|
requestBuilder.addBodyPart(new StringPart(
|
||||||
|
paramName, params.get(paramName)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
Future<Response> whenResponse = requestBuilder.execute();
|
Future<Response> whenResponse = requestBuilder.execute();
|
||||||
Response response = whenResponse.get();
|
Response response = whenResponse.get();
|
||||||
InputStream is = response.getResponseBodyAsStream();
|
InputStream is = response.getResponseBodyAsStream();
|
||||||
@ -663,18 +690,32 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import org.asynchttpclient.request.body.multipart.StringPart;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://api.apidash.dev/io/form";
|
String url = "https://api.apidash.dev/io/form";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
.addHeader("User-Agent", "Test Agent");
|
.addHeader("User-Agent", "Test Agent");
|
||||||
requestBuilder
|
|
||||||
.addFormParam("text", "API")
|
Map<String, String> params = new HashMap<>() {
|
||||||
.addFormParam("sep", "|")
|
{
|
||||||
.addFormParam("times", "3");
|
put("text", "API");
|
||||||
|
put("sep", "|");
|
||||||
|
put("times", "3");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String paramName : params.keySet()) {
|
||||||
|
requestBuilder.addBodyPart(new StringPart(
|
||||||
|
paramName, params.get(paramName)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
Future<Response> whenResponse = requestBuilder.execute();
|
Future<Response> whenResponse = requestBuilder.execute();
|
||||||
Response response = whenResponse.get();
|
Response response = whenResponse.get();
|
||||||
InputStream is = response.getResponseBodyAsStream();
|
InputStream is = response.getResponseBodyAsStream();
|
||||||
@ -705,16 +746,14 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import org.asynchttpclient.request.body.multipart.FilePart;
|
|
||||||
import org.asynchttpclient.request.body.multipart.StringPart;
|
import org.asynchttpclient.request.body.multipart.StringPart;
|
||||||
|
import org.asynchttpclient.request.body.multipart.FilePart;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://api.apidash.dev/io/img";
|
String url = "https://api.apidash.dev/io/img";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
||||||
requestBuilder
|
|
||||||
.addHeader("Content-Type", "multipart/form-data; boundary=12b3ea10-3711-1f2f-ae52-0d40a793d870");
|
|
||||||
|
|
||||||
Map<String, String> params = new HashMap<>() {
|
Map<String, String> params = new HashMap<>() {
|
||||||
{
|
{
|
||||||
@ -760,8 +799,7 @@ public class Main {
|
|||||||
''';
|
''';
|
||||||
expect(
|
expect(
|
||||||
codeGen.getCode(
|
codeGen.getCode(
|
||||||
CodegenLanguage.javaAsyncHttpClient, requestModelPost6, "https",
|
CodegenLanguage.javaAsyncHttpClient, requestModelPost6, "https"),
|
||||||
boundary: "12b3ea10-3711-1f2f-ae52-0d40a793d870"),
|
|
||||||
expectedCode);
|
expectedCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -776,16 +814,14 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import org.asynchttpclient.request.body.multipart.FilePart;
|
|
||||||
import org.asynchttpclient.request.body.multipart.StringPart;
|
import org.asynchttpclient.request.body.multipart.StringPart;
|
||||||
|
import org.asynchttpclient.request.body.multipart.FilePart;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://api.apidash.dev/io/img";
|
String url = "https://api.apidash.dev/io/img";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("POST", url);
|
||||||
requestBuilder
|
|
||||||
.addHeader("Content-Type", "multipart/form-data; boundary=12b3ea10-3711-1f2f-ae52-0d40a793d870");
|
|
||||||
|
|
||||||
Map<String, String> params = new HashMap<>() {
|
Map<String, String> params = new HashMap<>() {
|
||||||
{
|
{
|
||||||
@ -831,8 +867,7 @@ public class Main {
|
|||||||
''';
|
''';
|
||||||
expect(
|
expect(
|
||||||
codeGen.getCode(
|
codeGen.getCode(
|
||||||
CodegenLanguage.javaAsyncHttpClient, requestModelPost7, "https",
|
CodegenLanguage.javaAsyncHttpClient, requestModelPost7, "https"),
|
||||||
boundary: "12b3ea10-3711-1f2f-ae52-0d40a793d870"),
|
|
||||||
expectedCode);
|
expectedCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -845,6 +880,10 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import org.asynchttpclient.request.body.multipart.StringPart;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
@ -853,12 +892,21 @@ public class Main {
|
|||||||
requestBuilder
|
requestBuilder
|
||||||
.addQueryParam("size", "2")
|
.addQueryParam("size", "2")
|
||||||
.addQueryParam("len", "3");
|
.addQueryParam("len", "3");
|
||||||
requestBuilder
|
|
||||||
.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
Map<String, String> params = new HashMap<>() {
|
||||||
requestBuilder
|
{
|
||||||
.addFormParam("text", "API")
|
put("text", "API");
|
||||||
.addFormParam("sep", "|")
|
put("sep", "|");
|
||||||
.addFormParam("times", "3");
|
put("times", "3");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String paramName : params.keySet()) {
|
||||||
|
requestBuilder.addBodyPart(new StringPart(
|
||||||
|
paramName, params.get(paramName)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
Future<Response> whenResponse = requestBuilder.execute();
|
Future<Response> whenResponse = requestBuilder.execute();
|
||||||
Response response = whenResponse.get();
|
Response response = whenResponse.get();
|
||||||
InputStream is = response.getResponseBodyAsStream();
|
InputStream is = response.getResponseBodyAsStream();
|
||||||
@ -889,8 +937,8 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import org.asynchttpclient.request.body.multipart.FilePart;
|
|
||||||
import org.asynchttpclient.request.body.multipart.StringPart;
|
import org.asynchttpclient.request.body.multipart.StringPart;
|
||||||
|
import org.asynchttpclient.request.body.multipart.FilePart;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -901,7 +949,6 @@ public class Main {
|
|||||||
.addQueryParam("size", "2")
|
.addQueryParam("size", "2")
|
||||||
.addQueryParam("len", "3");
|
.addQueryParam("len", "3");
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "multipart/form-data; boundary=1556b1e0-1406-1f2f-ae52-0d40a793d870")
|
|
||||||
.addHeader("User-Agent", "Test Agent")
|
.addHeader("User-Agent", "Test Agent")
|
||||||
.addHeader("Keep-Alive", "true");
|
.addHeader("Keep-Alive", "true");
|
||||||
|
|
||||||
@ -949,8 +996,7 @@ public class Main {
|
|||||||
''';
|
''';
|
||||||
expect(
|
expect(
|
||||||
codeGen.getCode(
|
codeGen.getCode(
|
||||||
CodegenLanguage.javaAsyncHttpClient, requestModelPost9, "https",
|
CodegenLanguage.javaAsyncHttpClient, requestModelPost9, "https"),
|
||||||
boundary: "1556b1e0-1406-1f2f-ae52-0d40a793d870"),
|
|
||||||
expectedCode);
|
expectedCode);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -969,7 +1015,11 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://reqres.in/api/users/2";
|
String url = "https://reqres.in/api/users/2";
|
||||||
String bodyContent = "{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}";
|
String bodyContent = """
|
||||||
|
{
|
||||||
|
"name": "morpheus",
|
||||||
|
"job": "zion resident"
|
||||||
|
}""";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("PUT", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("PUT", url);
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "application/json");
|
.addHeader("Content-Type", "application/json");
|
||||||
@ -1008,7 +1058,11 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://reqres.in/api/users/2";
|
String url = "https://reqres.in/api/users/2";
|
||||||
String bodyContent = "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}";
|
String bodyContent = """
|
||||||
|
{
|
||||||
|
"name": "marfeus",
|
||||||
|
"job": "accountant"
|
||||||
|
}""";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("PATCH", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("PATCH", url);
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "application/json");
|
.addHeader("Content-Type", "application/json");
|
||||||
@ -1080,7 +1134,11 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||||
String url = "https://reqres.in/api/users/2";
|
String url = "https://reqres.in/api/users/2";
|
||||||
String bodyContent = "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}";
|
String bodyContent = """
|
||||||
|
{
|
||||||
|
"name": "marfeus",
|
||||||
|
"job": "accountant"
|
||||||
|
}""";
|
||||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("DELETE", url);
|
BoundRequestBuilder requestBuilder = asyncHttpClient.prepare("DELETE", url);
|
||||||
requestBuilder
|
requestBuilder
|
||||||
.addHeader("Content-Type", "application/json");
|
.addHeader("Content-Type", "application/json");
|
||||||
|
Reference in New Issue
Block a user