mirror of
https://github.com/foss42/apidash.git
synced 2025-07-08 18:33:37 +08:00
Merge pull request #343 from AcousticDeveloper/resolve-issue-135
Added Code Generation For Java (Unirest)
This commit is contained in:
@ -22,6 +22,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/unirest.dart';
|
||||||
import 'java/okhttp.dart';
|
import 'java/okhttp.dart';
|
||||||
import 'java/async_http_client.dart';
|
import 'java/async_http_client.dart';
|
||||||
import 'java/httpclient.dart';
|
import 'java/httpclient.dart';
|
||||||
@ -68,6 +69,8 @@ class Codegen {
|
|||||||
return JavaHttpClientCodeGen().getCode(rM, boundary: boundary);
|
return JavaHttpClientCodeGen().getCode(rM, boundary: boundary);
|
||||||
case CodegenLanguage.javaOkHttp:
|
case CodegenLanguage.javaOkHttp:
|
||||||
return JavaOkHttpCodeGen().getCode(rM);
|
return JavaOkHttpCodeGen().getCode(rM);
|
||||||
|
case CodegenLanguage.javaUnirest:
|
||||||
|
return JavaUnirestGen().getCode(rM);
|
||||||
case CodegenLanguage.juliaHttp:
|
case CodegenLanguage.juliaHttp:
|
||||||
return JuliaHttpClientCodeGen().getCode(rM);
|
return JuliaHttpClientCodeGen().getCode(rM);
|
||||||
case CodegenLanguage.kotlinOkHttp:
|
case CodegenLanguage.kotlinOkHttp:
|
||||||
|
172
lib/codegen/java/unirest.dart
Normal file
172
lib/codegen/java/unirest.dart
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
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:apidash/models/models.dart' show RequestModel;
|
||||||
|
import 'package:apidash/consts.dart';
|
||||||
|
|
||||||
|
class JavaUnirestGen {
|
||||||
|
final String kStringUnirestImports = '''
|
||||||
|
import kong.unirest.core.*;
|
||||||
|
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kStringFileIoImports = '''
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
''';
|
||||||
|
final String kStringStart = '''
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateUrl = '''
|
||||||
|
final String requestURL = "{{url}}";\n
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateRequestBodyContent = '''
|
||||||
|
final String requestBody = """
|
||||||
|
{{body}}""";
|
||||||
|
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateRequestCreation = '''
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.{{method}}(requestURL)\n
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateRequestHeader = '''
|
||||||
|
.header("{{name}}", "{{value}}")\n
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateUrlQueryParam = '''
|
||||||
|
.queryString("{{name}}", "{{value}}")\n
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateRequestTextFormData = '''
|
||||||
|
.field("{{name}}", "{{value}}")\n
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kTemplateRequestFileFormData = '''
|
||||||
|
.field("{{name}}", new File("{{value}}"))\n
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kStringRequestBodySetup = '''
|
||||||
|
.body(requestBody)
|
||||||
|
''';
|
||||||
|
|
||||||
|
final String kStringRequestEnd = """
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
String? getCode(RequestModel requestModel) {
|
||||||
|
try {
|
||||||
|
String result = '';
|
||||||
|
bool hasBody = false;
|
||||||
|
|
||||||
|
var rec = getValidRequestUri(
|
||||||
|
requestModel.url,
|
||||||
|
requestModel.enabledRequestParams,
|
||||||
|
);
|
||||||
|
|
||||||
|
// uri is already generated based on url and enabled request params
|
||||||
|
Uri? uri = rec.$1;
|
||||||
|
|
||||||
|
if (uri == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is the common import and will be imported for every generated code snippet
|
||||||
|
result += kStringUnirestImports;
|
||||||
|
|
||||||
|
// java file io packages are to be imported only when there is a form with file present
|
||||||
|
if (requestModel.hasFormData && requestModel.hasFileInFormData) {
|
||||||
|
result += kStringFileIoImports;
|
||||||
|
}
|
||||||
|
|
||||||
|
// adding the main method under Main class
|
||||||
|
result += kStringStart;
|
||||||
|
|
||||||
|
var url = stripUriParams(uri);
|
||||||
|
|
||||||
|
// generating the URL to which the request has to be submitted
|
||||||
|
var templateUrl = jj.Template(kTemplateUrl);
|
||||||
|
result += templateUrl.render({"url": url});
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// we need to parse the body as it is, and write it to the body
|
||||||
|
if (requestModel.hasTextData || requestModel.hasJsonData) {
|
||||||
|
var templateBodyContent = jj.Template(kTemplateRequestBodyContent);
|
||||||
|
result += templateBodyContent.render({
|
||||||
|
"body": requestModel.requestBody,
|
||||||
|
});
|
||||||
|
hasBody = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateRequestCreation = jj.Template(kTemplateRequestCreation);
|
||||||
|
result += templateRequestCreation
|
||||||
|
.render({"method": requestModel.method.name.toLowerCase()});
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~~ request header start ~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
var headers = requestModel.enabledHeadersMap;
|
||||||
|
if (hasBody && !requestModel.hasContentTypeHeader) {
|
||||||
|
headers[kHeaderContentType] =
|
||||||
|
requestModel.requestBodyContentType.header;
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateRequestHeader = jj.Template(kTemplateRequestHeader);
|
||||||
|
// setting up rest of the request headers
|
||||||
|
headers.forEach((name, value) {
|
||||||
|
result += templateRequestHeader.render({"name": name, "value": value});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~~ request header ends ~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~~ query parameters start ~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
if (uri.hasQuery) {
|
||||||
|
var params = uri.queryParameters;
|
||||||
|
var templateUrlQueryParam = jj.Template(kTemplateUrlQueryParam);
|
||||||
|
params.forEach((name, value) {
|
||||||
|
result +=
|
||||||
|
templateUrlQueryParam.render({"name": name, "value": value});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~~ query parameters end ~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
// handling form data
|
||||||
|
if (requestModel.hasFormData) {
|
||||||
|
// including form data into the request
|
||||||
|
var templateRequestTextFormData =
|
||||||
|
jj.Template(kTemplateRequestTextFormData);
|
||||||
|
var templateRequestFileFormData =
|
||||||
|
jj.Template(kTemplateRequestFileFormData);
|
||||||
|
for (var field in requestModel.formDataList) {
|
||||||
|
if (field.type == FormDataType.text) {
|
||||||
|
result += templateRequestTextFormData
|
||||||
|
.render({"name": field.name, "value": field.value});
|
||||||
|
} else if (field.type == FormDataType.file) {
|
||||||
|
result += templateRequestFileFormData
|
||||||
|
.render({"name": field.name, "value": field.value});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasBody) {
|
||||||
|
result += kStringRequestBodySetup;
|
||||||
|
}
|
||||||
|
|
||||||
|
result += kStringRequestEnd;
|
||||||
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -293,6 +293,7 @@ enum CodegenLanguage {
|
|||||||
javaOkHttp("Java (okhttp3)", "java", 'java'),
|
javaOkHttp("Java (okhttp3)", "java", 'java'),
|
||||||
javaAsyncHttpClient("Java (asynchttpclient)", "java", "java"),
|
javaAsyncHttpClient("Java (asynchttpclient)", "java", "java"),
|
||||||
javaHttpClient("Java (HttpClient)", "java", "java"),
|
javaHttpClient("Java (HttpClient)", "java", "java"),
|
||||||
|
javaUnirest("Java (Unirest)", "java", "java"),
|
||||||
juliaHttp("Julia (HTTP)", "julia", "jl"),
|
juliaHttp("Julia (HTTP)", "julia", "jl"),
|
||||||
phpCurl("PHP (cURL)", "php", "php"),
|
phpCurl("PHP (cURL)", "php", "php"),
|
||||||
phpGuzzle("PHP (guzzle)", "php", "php");
|
phpGuzzle("PHP (guzzle)", "php", "php");
|
||||||
@ -318,6 +319,8 @@ const kSubTypeXml = 'xml';
|
|||||||
const kSubTypeYaml = 'yaml';
|
const kSubTypeYaml = 'yaml';
|
||||||
const kSubTypeXYaml = 'x-yaml';
|
const kSubTypeXYaml = 'x-yaml';
|
||||||
const kSubTypeYml = 'x-yml';
|
const kSubTypeYml = 'x-yml';
|
||||||
|
// in future need to add support for url-encoded form data
|
||||||
|
const kSubTypeXWwwFormUrlencoded = 'x-www-form-urlencoded';
|
||||||
|
|
||||||
const kTypeText = 'text';
|
const kTypeText = 'text';
|
||||||
// text
|
// text
|
||||||
|
658
test/codegen/java_unirest_codegen_test.dart
Normal file
658
test/codegen/java_unirest_codegen_test.dart
Normal file
@ -0,0 +1,658 @@
|
|||||||
|
import 'package:apidash/codegen/codegen.dart';
|
||||||
|
import 'package:apidash/consts.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import '../request_models.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final codeGen = Codegen();
|
||||||
|
|
||||||
|
group('GET Request', () {
|
||||||
|
test('GET 1', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet1, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 2', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/country/data";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.queryString("code", "US")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet2, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 3', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/country/data";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.queryString("code", "IND")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet3, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 4', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/humanize/social";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.queryString("num", "8700000")
|
||||||
|
.queryString("digits", "3")
|
||||||
|
.queryString("system", "SS")
|
||||||
|
.queryString("add_space", "true")
|
||||||
|
.queryString("trailing_zeros", "true")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet4, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 5', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.github.com/repos/foss42/apidash";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet5, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 6', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.github.com/repos/foss42/apidash";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.queryString("raw", "true")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet6, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 7', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet7, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 8', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.github.com/repos/foss42/apidash";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.queryString("raw", "true")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet8, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 9', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/humanize/social";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.queryString("num", "8700000")
|
||||||
|
.queryString("add_space", "true")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet9, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 10', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/humanize/social";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest,
|
||||||
|
requestModelGet10,
|
||||||
|
"https",
|
||||||
|
),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 11', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/humanize/social";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.queryString("num", "8700000")
|
||||||
|
.queryString("digits", "3")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet11, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET 12', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/humanize/social";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.get(requestURL)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelGet12, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('HEAD Request', () {
|
||||||
|
test('HEAD 1', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.head(requestURL)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelHead1, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('HEAD 2', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "http://api.apidash.dev";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.head(requestURL)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelHead2, "http"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('POST Request', () {
|
||||||
|
test('POST 1', () {
|
||||||
|
const expectedCode = r'''import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/case/lower";
|
||||||
|
final String requestBody = """
|
||||||
|
{
|
||||||
|
"text": "I LOVE Flutter"
|
||||||
|
}""";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.header("Content-Type", "text/plain")
|
||||||
|
.body(requestBody)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPost1, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST 2', () {
|
||||||
|
const expectedCode = r'''import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/case/lower";
|
||||||
|
final String requestBody = """
|
||||||
|
{
|
||||||
|
"text": "I LOVE Flutter",
|
||||||
|
"flag": null,
|
||||||
|
"male": true,
|
||||||
|
"female": false,
|
||||||
|
"no": 1.2,
|
||||||
|
"arr": ["null", "true", "false", null]
|
||||||
|
}""";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(requestBody)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPost2, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST 3', () {
|
||||||
|
const expectedCode = r'''import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/case/lower";
|
||||||
|
final String requestBody = """
|
||||||
|
{
|
||||||
|
"text": "I LOVE Flutter"
|
||||||
|
}""";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(requestBody)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPost3, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 4', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/io/form";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.field("text", "API")
|
||||||
|
.field("sep", "|")
|
||||||
|
.field("times", "3")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest,
|
||||||
|
requestModelPost4,
|
||||||
|
"https",
|
||||||
|
),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST 5', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/io/form";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.field("text", "API")
|
||||||
|
.field("sep", "|")
|
||||||
|
.field("times", "3")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest,
|
||||||
|
requestModelPost5,
|
||||||
|
"https",
|
||||||
|
),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 6', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/io/img";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.field("token", "xyz")
|
||||||
|
.field("imfile", new File("/Documents/up/1.png"))
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPost6, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 7', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/io/img";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.field("token", "xyz")
|
||||||
|
.field("imfile", new File("/Documents/up/1.png"))
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPost7, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 8', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/io/form";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.queryString("size", "2")
|
||||||
|
.queryString("len", "3")
|
||||||
|
.field("text", "API")
|
||||||
|
.field("sep", "|")
|
||||||
|
.field("times", "3")
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest,
|
||||||
|
requestModelPost8,
|
||||||
|
"https",
|
||||||
|
),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
test('POST 9', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://api.apidash.dev/io/img";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.post(requestURL)
|
||||||
|
.header("User-Agent", "Test Agent")
|
||||||
|
.header("Keep-Alive", "true")
|
||||||
|
.queryString("size", "2")
|
||||||
|
.queryString("len", "3")
|
||||||
|
.field("token", "xyz")
|
||||||
|
.field("imfile", new File("/Documents/up/1.png"))
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPost9, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('PUT Request', () {
|
||||||
|
test('PUT 1', () {
|
||||||
|
const expectedCode = r'''import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://reqres.in/api/users/2";
|
||||||
|
final String requestBody = """
|
||||||
|
{
|
||||||
|
"name": "morpheus",
|
||||||
|
"job": "zion resident"
|
||||||
|
}""";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.put(requestURL)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(requestBody)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPut1, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('PATCH Request', () {
|
||||||
|
test('PATCH 1', () {
|
||||||
|
const expectedCode = r'''import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://reqres.in/api/users/2";
|
||||||
|
final String requestBody = """
|
||||||
|
{
|
||||||
|
"name": "marfeus",
|
||||||
|
"job": "accountant"
|
||||||
|
}""";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.patch(requestURL)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(requestBody)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelPatch1, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('DELETE Request', () {
|
||||||
|
test('DELETE 1', () {
|
||||||
|
const expectedCode = r"""import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://reqres.in/api/users/2";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.delete(requestURL)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelDelete1, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('DELETE 2', () {
|
||||||
|
const expectedCode = r'''import kong.unirest.core.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String requestURL = "https://reqres.in/api/users/2";
|
||||||
|
final String requestBody = """
|
||||||
|
{
|
||||||
|
"name": "marfeus",
|
||||||
|
"job": "accountant"
|
||||||
|
}""";
|
||||||
|
HttpResponse<JsonNode> response = Unirest
|
||||||
|
.delete(requestURL)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(requestBody)
|
||||||
|
.asJson();
|
||||||
|
System.out.println(response.getStatus());
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(
|
||||||
|
CodegenLanguage.javaUnirest, requestModelDelete2, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Reference in New Issue
Block a user