mirror of
https://github.com/foss42/apidash.git
synced 2025-06-05 10:20:44 +08:00
Merge branch 'main' into add-feature-mobile-layout
This commit is contained in:
34
README.md
34
README.md
@ -123,18 +123,28 @@ API Dash can be downloaded from the links below:
|
||||
|
||||
API Dash currently supports API integration code generation for the following languages/libraries.
|
||||
|
||||
| Language | Library |
|
||||
| ---------------------- | ------------- |
|
||||
| cURL | |
|
||||
| HAR | |
|
||||
| Dart | `http` |
|
||||
| JavaScript | `axios` |
|
||||
| JavaScript | `fetch` |
|
||||
| JavaScript (`node.js`) | `axios` |
|
||||
| JavaScript (`node.js`) | `fetch` |
|
||||
| Python | `http.client` |
|
||||
| Python | `requests` |
|
||||
| Kotlin | `okhttp3` |
|
||||
| Language | Library | Comment/Issues |
|
||||
| ---------------------- | ------------- | ------- |
|
||||
| cURL | | |
|
||||
| HAR | | |
|
||||
| Dart | `http` | |
|
||||
| Dart | `dio` | |
|
||||
| Go | `net/http` | |
|
||||
| JavaScript | `axios` | |
|
||||
| JavaScript | `fetch` | |
|
||||
| JavaScript (`node.js`) | `axios` | |
|
||||
| JavaScript (`node.js`) | `fetch` | |
|
||||
| Python | `requests` | |
|
||||
| Python | `http.client` | |
|
||||
| Kotlin | `okhttp3` | |
|
||||
| Rust | `reqwest` | |
|
||||
| Rust | `ureq` | |
|
||||
| Rust | `Actix Client` | |
|
||||
| Java | `asynchttpclient` | https://github.com/foss42/apidash/issues/136 |
|
||||
| Java | `HttpClient` | https://github.com/foss42/apidash/issues/137 |
|
||||
| Java | `okhttp3` | |
|
||||
| Julia | `HTTP` | https://github.com/foss42/apidash/issues/154 |
|
||||
| PHP | `guzzle` | https://github.com/foss42/apidash/issues/143 |
|
||||
|
||||
We welcome contributions to support other programming languages/libraries/frameworks. Please check out more details [here](https://github.com/foss42/apidash/discussions/80).
|
||||
|
||||
|
@ -5,6 +5,7 @@ import 'dart/http.dart';
|
||||
import 'dart/dio.dart';
|
||||
import 'go/http.dart';
|
||||
import 'kotlin/okhttp.dart';
|
||||
import 'php/guzzle.dart';
|
||||
import 'python/http_client.dart';
|
||||
import 'python/requests.dart';
|
||||
import 'rust/actix.dart';
|
||||
@ -15,6 +16,9 @@ import 'js/fetch.dart';
|
||||
import 'others/har.dart';
|
||||
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(
|
||||
@ -42,6 +46,8 @@ class Codegen {
|
||||
return DartHttpCodeGen().getCode(rM);
|
||||
case CodegenLanguage.dartDio:
|
||||
return DartDioCodeGen().getCode(rM);
|
||||
case CodegenLanguage.goHttp:
|
||||
return GoHttpCodeGen().getCode(rM);
|
||||
case CodegenLanguage.jsAxios:
|
||||
return AxiosCodeGen().getCode(rM);
|
||||
case CodegenLanguage.jsFetch:
|
||||
@ -50,6 +56,14 @@ class Codegen {
|
||||
return AxiosCodeGen(isNodeJs: true).getCode(rM);
|
||||
case CodegenLanguage.nodejsFetch:
|
||||
return FetchCodeGen(isNodeJs: true).getCode(rM);
|
||||
case CodegenLanguage.javaAsyncHttpClient:
|
||||
return JavaAsyncHttpClientGen().getCode(rM);
|
||||
case CodegenLanguage.javaHttpClient:
|
||||
return JavaHttpClientCodeGen().getCode(rM);
|
||||
case CodegenLanguage.javaOkHttp:
|
||||
return JavaOkHttpCodeGen().getCode(rM);
|
||||
case CodegenLanguage.juliaHttp:
|
||||
return JuliaHttpClientCodeGen().getCode(rM);
|
||||
case CodegenLanguage.kotlinOkHttp:
|
||||
return KotlinOkHttpCodeGen().getCode(rM);
|
||||
case CodegenLanguage.pythonHttpClient:
|
||||
@ -63,10 +77,8 @@ class Codegen {
|
||||
return RustReqwestCodeGen().getCode(rM);
|
||||
case CodegenLanguage.rustUreq:
|
||||
return RustUreqCodeGen().getCode(rM, boundary: boundary);
|
||||
case CodegenLanguage.goHttp:
|
||||
return GoHttpCodeGen().getCode(rM);
|
||||
case CodegenLanguage.juliaHttp:
|
||||
return JuliaHttpClientCodeGen().getCode(rM);
|
||||
case CodegenLanguage.phpGuzzle:
|
||||
return PhpGuzzleCodeGen().getCode(rM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
189
lib/codegen/java/async_http_client.dart
Normal file
189
lib/codegen/java/async_http_client.dart
Normal file
@ -0,0 +1,189 @@
|
||||
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 JavaAsyncHttpClientGen {
|
||||
final String kTemplateStart = '''
|
||||
import org.asynchttpclient.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
try (AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient()) {
|
||||
''';
|
||||
|
||||
final String kTemplateUrl = '''
|
||||
String url = "{{url}}";\n
|
||||
''';
|
||||
|
||||
final String kTemplateRequestCreation = '''
|
||||
Request request = asyncHttpClient
|
||||
.prepare("{{method}}", url)\n
|
||||
''';
|
||||
|
||||
final String kTemplateUrlQueryParam = '''
|
||||
.addQueryParam("{{name}}", "{{value}}")\n
|
||||
''';
|
||||
|
||||
final String kTemplateRequestHeader = '''
|
||||
.addHeader("{{name}}", "{{value}}")\n
|
||||
''';
|
||||
final String kTemplateRequestFormData = '''
|
||||
.addFormParam("{{name}}", "{{value}}")\n
|
||||
''';
|
||||
|
||||
String kTemplateRequestBodyContent = '''
|
||||
String bodyContent = "{{body}}";\n
|
||||
''';
|
||||
String kTemplateRequestBodySetup = '''
|
||||
.setBody(bodyContent)\n
|
||||
''';
|
||||
|
||||
final String kTemplateRequestEnd = """
|
||||
.build();
|
||||
ListenableFuture<Response> listenableFuture = asyncHttpClient.executeRequest(request);
|
||||
listenableFuture.addListener(() -> {
|
||||
try {
|
||||
Response response = listenableFuture.get();
|
||||
InputStream is = response.getResponseBodyAsStream();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
|
||||
String respBody = br.lines().collect(Collectors.joining("\\n"));
|
||||
System.out.println(response.getStatusCode());
|
||||
System.out.println(respBody);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, Executors.newCachedThreadPool());
|
||||
listenableFuture.get();
|
||||
} catch (InterruptedException | ExecutionException | IOException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
\n
|
||||
""";
|
||||
|
||||
String? getCode(
|
||||
RequestModel requestModel,
|
||||
) {
|
||||
try {
|
||||
String result = "";
|
||||
bool hasBody = false;
|
||||
|
||||
var rec = getValidRequestUri(
|
||||
requestModel.url,
|
||||
requestModel.enabledRequestParams,
|
||||
);
|
||||
Uri? uri = rec.$1;
|
||||
|
||||
if (uri == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
var url = stripUriParams(uri);
|
||||
|
||||
// contains the HTTP method associated with the request
|
||||
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
|
||||
var templateUrl = jj.Template(kTemplateUrl);
|
||||
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
|
||||
// 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.hasFormData &&
|
||||
kMethodsWithBody.contains(method) &&
|
||||
requestBody != null) {
|
||||
var contentLength = utf8.encode(requestBody).length;
|
||||
if (contentLength > 0) {
|
||||
var templateBodyContent = jj.Template(kTemplateRequestBodyContent);
|
||||
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);
|
||||
result +=
|
||||
templateRequestCreation.render({"method": method.name.toUpperCase()});
|
||||
|
||||
// setting up query parameters
|
||||
if (uri.hasQuery) {
|
||||
var params = uri.queryParameters;
|
||||
var templateUrlQueryParam = jj.Template(kTemplateUrlQueryParam);
|
||||
params.forEach((name, value) {
|
||||
result +=
|
||||
templateUrlQueryParam.render({"name": name, "value": value});
|
||||
});
|
||||
}
|
||||
|
||||
result = kTemplateStart + result;
|
||||
|
||||
var contentType = requestModel.requestBodyContentType.header;
|
||||
var templateRequestHeader = jj.Template(kTemplateRequestHeader);
|
||||
|
||||
// especially sets up Content-Type header if the request has a body
|
||||
// and Content-Type is not explicitely set by the developer
|
||||
if (hasBody &&
|
||||
!requestModel.enabledHeadersMap.containsKey('Content-Type')) {
|
||||
result += templateRequestHeader
|
||||
.render({"name": 'Content-Type', "value": contentType});
|
||||
}
|
||||
|
||||
// setting up rest of the request headers
|
||||
var headers = requestModel.enabledHeadersMap;
|
||||
headers.forEach((name, value) {
|
||||
result += templateRequestHeader.render({"name": name, "value": value});
|
||||
});
|
||||
|
||||
// handling form data
|
||||
if (requestModel.hasFormData &&
|
||||
requestModel.formDataMapList.isNotEmpty &&
|
||||
kMethodsWithBody.contains(method)) {
|
||||
// including form data into the request
|
||||
var formDataList = requestModel.formDataMapList;
|
||||
var templateRequestFormData = jj.Template(kTemplateRequestFormData);
|
||||
for (var formDataMap in formDataList) {
|
||||
result += templateRequestFormData.render(
|
||||
{"name": formDataMap['name'], "value": formDataMap['value']});
|
||||
}
|
||||
hasBody = true;
|
||||
}
|
||||
|
||||
var templateRequestBodySetup = jj.Template(kTemplateRequestBodySetup);
|
||||
if (kMethodsWithBody.contains(method) && hasBody) {
|
||||
result += templateRequestBodySetup.render();
|
||||
}
|
||||
|
||||
var templateRequestBodyEnd = jj.Template(kTemplateRequestEnd);
|
||||
result += templateRequestBodyEnd.render();
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
183
lib/codegen/java/httpclient.dart
Normal file
183
lib/codegen/java/httpclient.dart
Normal file
@ -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<String> 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<String, String> 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;
|
||||
}
|
||||
}
|
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;
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
import 'dart:convert';
|
||||
import 'package:jinja/jinja.dart' as jj;
|
||||
import 'package:apidash/utils/utils.dart'
|
||||
show padMultilineString, requestModelToHARJsonRequest, stripUrlParams;
|
||||
@ -10,12 +9,14 @@ class AxiosCodeGen {
|
||||
|
||||
final bool isNodeJs;
|
||||
|
||||
String kStringImportNode = """{% if isNodeJs %}import axios from 'axios';
|
||||
String kStringImportNode = """import axios from 'axios';
|
||||
{%if hasFileInFormData -%}
|
||||
import fs from 'fs'
|
||||
{% endif %}
|
||||
|
||||
{% endif %}{% if hasFormData and isNodeJs %}const fs = require('fs');{% endif %}
|
||||
""";
|
||||
|
||||
String kTemplateStart = """let config = {
|
||||
String kTemplateStart = """const config = {
|
||||
url: '{{url}}',
|
||||
method: '{{method}}'
|
||||
""";
|
||||
@ -37,59 +38,28 @@ class AxiosCodeGen {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
String kMultiPartBodyTemplate = r'''
|
||||
|
||||
|
||||
async function buildFormData(fields) {
|
||||
var formdata = new FormData();
|
||||
for (const field of fields) {
|
||||
const name = field.name || '';
|
||||
const value = field.value || '';
|
||||
const type = field.type || 'text';
|
||||
|
||||
if (type === 'text') {
|
||||
formdata.append(name, value);
|
||||
} else if (type === 'file') {
|
||||
formdata.append(name,{% if isNodeJs %} fs.createReadStream(value){% else %} fileInput.files[0],value{% endif %});
|
||||
}
|
||||
}
|
||||
return formdata;
|
||||
}
|
||||
|
||||
|
||||
''';
|
||||
var kGetFormDataTemplate = '''buildFormData({{fields_list}});
|
||||
''';
|
||||
String? getCode(
|
||||
RequestModel requestModel,
|
||||
) {
|
||||
try {
|
||||
jj.Template kNodejsImportTemplate = jj.Template(kStringImportNode);
|
||||
String importsData = kNodejsImportTemplate.render({
|
||||
"hasFormData": requestModel.hasFormData,
|
||||
"isNodeJs": isNodeJs,
|
||||
"hasFileInFormData": requestModel.hasFileInFormData,
|
||||
});
|
||||
|
||||
String result = importsData;
|
||||
if (requestModel.hasFormData && requestModel.formDataMapList.isNotEmpty) {
|
||||
var templateMultiPartBody = jj.Template(kMultiPartBodyTemplate);
|
||||
var renderedMultiPartBody = templateMultiPartBody.render({
|
||||
"isNodeJs": isNodeJs,
|
||||
});
|
||||
result += renderedMultiPartBody;
|
||||
}
|
||||
|
||||
String result = isNodeJs
|
||||
? importsData
|
||||
: requestModel.hasFileInFormData
|
||||
? "// refer https://github.com/foss42/apidash/issues/293#issuecomment-1997568083 for details regarding integration\n\n"
|
||||
: "";
|
||||
var harJson = requestModelToHARJsonRequest(
|
||||
requestModel,
|
||||
useEnabled: true,
|
||||
@ -126,17 +96,22 @@ async function buildFormData(fields) {
|
||||
.render({"headers": padMultilineString(kEncoder.convert(m), 2)});
|
||||
}
|
||||
var templateBody = jj.Template(kTemplateBody);
|
||||
|
||||
if (requestModel.hasFormData && requestModel.formDataMapList.isNotEmpty) {
|
||||
var getFieldDataTemplate = jj.Template(kGetFormDataTemplate);
|
||||
|
||||
result += templateBody.render({
|
||||
"body": getFieldDataTemplate.render({
|
||||
"fields_list": json.encode(requestModel.formDataMapList),
|
||||
})
|
||||
});
|
||||
}
|
||||
if (harJson["postData"]?["text"] != null) {
|
||||
// Manually Create a JS Object
|
||||
Map<String, String> formParams = {};
|
||||
int formFileCounter = 1;
|
||||
for (var element in requestModel.formDataMapList) {
|
||||
formParams["${element["name"]}"] = element["type"] == "text"
|
||||
? "${element["value"]}"
|
||||
: isNodeJs
|
||||
? "fs.createReadStream(${element["value"]})"
|
||||
: "fileInput$formFileCounter.files[0]";
|
||||
if (element["type"] == "file") formFileCounter++;
|
||||
}
|
||||
var sanitizedJSObject = sanitzeJSObject(kEncoder.convert(formParams));
|
||||
result += templateBody
|
||||
.render({"body": padMultilineString(sanitizedJSObject, 2)});
|
||||
} else if (harJson["postData"]?["text"] != null) {
|
||||
result += templateBody
|
||||
.render({"body": kEncoder.convert(harJson["postData"]["text"])});
|
||||
}
|
||||
@ -146,4 +121,18 @@ async function buildFormData(fields) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// escape function and variables in JS Object
|
||||
String sanitzeJSObject(String jsObject) {
|
||||
RegExp pattern = isNodeJs
|
||||
? RegExp(r'"fs\.createReadStream\((.*?)\)"')
|
||||
: RegExp(r'"fileInput(\d+)\.files\[0\]"');
|
||||
|
||||
var sanitizedJSObject = jsObject.replaceAllMapped(pattern, (match) {
|
||||
return isNodeJs
|
||||
? 'fs.createReadStream("${match.group(1)}")'
|
||||
: 'fileInput${match.group(1)}.files[0]';
|
||||
});
|
||||
return sanitizedJSObject;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
import 'dart:convert';
|
||||
import 'package:jinja/jinja.dart' as jj;
|
||||
import 'package:apidash/utils/utils.dart'
|
||||
show padMultilineString, requestModelToHARJsonRequest;
|
||||
@ -11,14 +10,16 @@ class FetchCodeGen {
|
||||
final bool isNodeJs;
|
||||
|
||||
String kStringImportNode = """
|
||||
import fetch from 'node-fetch';
|
||||
{% if hasFormData %}const fs = require('fs');{% endif %}
|
||||
import fetch from 'node-fetch'
|
||||
{% if hasFormData -%}
|
||||
import { {% if hasFileInFormData %}fileFromSync, {% endif %}FormData } from 'node-fetch'
|
||||
{% endif %}
|
||||
|
||||
""";
|
||||
|
||||
String kTemplateStart = """let url = '{{url}}';
|
||||
String kTemplateStart = """const url = '{{url}}';
|
||||
|
||||
let options = {
|
||||
const options = {
|
||||
method: '{{method}}'
|
||||
""";
|
||||
|
||||
@ -27,72 +28,63 @@ let options = {
|
||||
""";
|
||||
|
||||
String kTemplateBody = """,
|
||||
body:
|
||||
{{body}}
|
||||
body: {{body}}
|
||||
""";
|
||||
|
||||
String kMultiPartBodyTemplate = r'''
|
||||
async function buildDataList(fields) {
|
||||
var formdata = new FormData();
|
||||
for (const field of fields) {
|
||||
const name = field.name || '';
|
||||
const value = field.value || '';
|
||||
const type = field.type || 'text';
|
||||
|
||||
if (type === 'text') {
|
||||
formdata.append(name, value);
|
||||
} else if (type === 'file') {
|
||||
formdata.append(name,{% if isNodeJs %} fs.createReadStream(value){% else %} fileInput.files[0],value{% endif %});
|
||||
}
|
||||
}
|
||||
return formdata;
|
||||
}
|
||||
|
||||
const payload = buildDataList({{fields_list}});
|
||||
payload.append("{{name}}", {{value}})
|
||||
|
||||
''';
|
||||
String kStringRequest = """
|
||||
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
return res.text()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`error:\${err}`);
|
||||
});
|
||||
""";
|
||||
|
||||
String? getCode(
|
||||
RequestModel requestModel,
|
||||
) {
|
||||
String? getCode(RequestModel requestModel) {
|
||||
try {
|
||||
jj.Template kNodejsImportTemplate = jj.Template(kStringImportNode);
|
||||
String importsData = kNodejsImportTemplate.render({
|
||||
"hasFormData": requestModel.hasFormData,
|
||||
"hasFileInFormData": requestModel.hasFileInFormData,
|
||||
});
|
||||
|
||||
String result = isNodeJs ? importsData : "";
|
||||
String result = isNodeJs
|
||||
? importsData
|
||||
: requestModel.hasFileInFormData
|
||||
? "// refer https://github.com/foss42/apidash/issues/293#issuecomment-1995208098 for details regarding integration\n\n"
|
||||
: "";
|
||||
if (requestModel.hasFormData) {
|
||||
result += "const payload = new FormData();\n";
|
||||
var templateMultiPartBody = jj.Template(kMultiPartBodyTemplate);
|
||||
result += templateMultiPartBody.render({
|
||||
"isNodeJs": isNodeJs,
|
||||
"fields_list": json.encode(requestModel.formDataMapList),
|
||||
});
|
||||
var formFileCounter = 1;
|
||||
for (var element in requestModel.formDataMapList) {
|
||||
result += templateMultiPartBody.render({
|
||||
"name": element["name"],
|
||||
"value": element["type"] == "text"
|
||||
? "\"${element["value"]}\""
|
||||
: isNodeJs
|
||||
? "fileFromSync(\"${element["value"]}\")"
|
||||
: "fileInput$formFileCounter.files[0]"
|
||||
});
|
||||
if (element["type"] != "text") formFileCounter++;
|
||||
}
|
||||
result += "\n";
|
||||
}
|
||||
|
||||
var harJson = requestModelToHARJsonRequest(
|
||||
requestModel,
|
||||
useEnabled: true,
|
||||
);
|
||||
var harJson =
|
||||
requestModelToHARJsonRequest(requestModel, useEnabled: true);
|
||||
|
||||
var templateStart = jj.Template(kTemplateStart);
|
||||
result += templateStart.render({
|
||||
@ -105,15 +97,18 @@ fetch(url, options)
|
||||
if (headers.isNotEmpty) {
|
||||
var templateHeader = jj.Template(kTemplateHeader);
|
||||
var m = {};
|
||||
if (requestModel.hasFormData) {
|
||||
m[kHeaderContentType] = "multipart/form-data";
|
||||
}
|
||||
for (var i in headers) {
|
||||
// fetch can automatically add the Content-Type header when FormData is passed as body
|
||||
if (i["name"] == "Content-Type" && requestModel.hasFormData) {
|
||||
continue;
|
||||
}
|
||||
m[i["name"]] = i["value"];
|
||||
}
|
||||
result += templateHeader.render({
|
||||
"headers": padMultilineString(kEncoder.convert(m), 2),
|
||||
});
|
||||
if (m.isNotEmpty) {
|
||||
result += templateHeader.render({
|
||||
"headers": padMultilineString(kEncoder.convert(m), 2),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (harJson["postData"]?["text"] != null) {
|
||||
|
@ -7,7 +7,7 @@ import 'package:apidash/consts.dart';
|
||||
|
||||
class KotlinOkHttpCodeGen {
|
||||
final String kTemplateStart = """import okhttp3.OkHttpClient
|
||||
import okhttp3.Request{{importForQuery}}{{importForBody}}{{importForFormData}}
|
||||
import okhttp3.Request{{importForQuery}}{{importForBody}}{{importForFormData}}{{importForFile}}
|
||||
|
||||
fun main() {
|
||||
val client = OkHttpClient()
|
||||
@ -27,6 +27,12 @@ import okhttp3.MediaType.Companion.toMediaType""";
|
||||
|
||||
import okhttp3.MultipartBody""";
|
||||
|
||||
final String kStringImportForFile = """
|
||||
|
||||
import java.io.File
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.MediaType.Companion.toMediaType""";
|
||||
|
||||
final String kTemplateUrl = '''
|
||||
|
||||
val url = "{{url}}"
|
||||
@ -68,7 +74,7 @@ import okhttp3.MultipartBody""";
|
||||
// Converting list of form data objects to kolin multi part data
|
||||
String kFormDataBody = '''
|
||||
val body = MultipartBody.Builder().setType(MultipartBody.FORM){% for item in formDataList %}{% if item.type == 'file' %}
|
||||
.addFormDataPart("{{item.name}}",null,File("{{item.value}}").asRequestBody("application/octet-stream".toMediaType()))
|
||||
.addFormDataPart("{{item.name}}",File("{{item.value}}").name,File("{{item.value}}").asRequestBody("application/octet-stream".toMediaType()))
|
||||
{% else %}.addFormDataPart("{{item.name}}","{{item.value}}")
|
||||
{% endif %}{% endfor %}.build()
|
||||
''';
|
||||
@ -81,6 +87,7 @@ import okhttp3.MultipartBody""";
|
||||
bool hasQuery = false;
|
||||
bool hasBody = false;
|
||||
bool hasFormData = false;
|
||||
bool hasFile = false;
|
||||
|
||||
var rec = getValidRequestUri(
|
||||
requestModel.url,
|
||||
@ -111,8 +118,34 @@ import okhttp3.MultipartBody""";
|
||||
hasFormData = true;
|
||||
var formDataTemplate = jj.Template(kFormDataBody);
|
||||
|
||||
List<Map<String,String>> modifiedFormDataList = [];
|
||||
for (var item in requestModel.formDataList) {
|
||||
if (item.type == FormDataType.file ) {
|
||||
if (item.value[0] == "/") {
|
||||
modifiedFormDataList.add({
|
||||
"name": item.name,
|
||||
"value": item.value.substring(1),
|
||||
"type": "file"
|
||||
});
|
||||
}else{
|
||||
modifiedFormDataList.add({
|
||||
"name": item.name,
|
||||
"value": item.value,
|
||||
"type": "file"
|
||||
});
|
||||
}
|
||||
hasFile = true;
|
||||
}else{
|
||||
modifiedFormDataList.add({
|
||||
"name": item.name,
|
||||
"value": item.value,
|
||||
"type": "text"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
result += formDataTemplate.render({
|
||||
"formDataList": requestModel.formDataMapList,
|
||||
"formDataList": modifiedFormDataList,
|
||||
});
|
||||
} else if (kMethodsWithBody.contains(method) && requestBody != null) {
|
||||
var contentLength = utf8.encode(requestBody).length;
|
||||
@ -129,7 +162,8 @@ import okhttp3.MultipartBody""";
|
||||
var stringStart = templateStart.render({
|
||||
"importForQuery": hasQuery ? kStringImportForQuery : "",
|
||||
"importForBody": hasBody ? kStringImportForBody : "",
|
||||
"importForFormData": hasFormData ? kStringImportForFormData : ""
|
||||
"importForFormData": hasFormData ? kStringImportForFormData : "",
|
||||
"importForFile": hasFile ? kStringImportForFile : "",
|
||||
});
|
||||
|
||||
result = stringStart + result;
|
||||
|
161
lib/codegen/php/guzzle.dart
Normal file
161
lib/codegen/php/guzzle.dart
Normal file
@ -0,0 +1,161 @@
|
||||
import 'package:jinja/jinja.dart' as jj;
|
||||
import 'package:apidash/utils/utils.dart'
|
||||
show requestModelToHARJsonRequest, stripUrlParams;
|
||||
import 'package:apidash/models/models.dart' show RequestModel;
|
||||
import 'package:apidash/consts.dart';
|
||||
|
||||
class PhpGuzzleCodeGen {
|
||||
String kStringImportNode = """use GuzzleHttp\\Client;
|
||||
use GuzzleHttp\\Psr7\\Request;
|
||||
{% if hasFormData %}use GuzzleHttp\\Psr7\\MultipartStream;{% endif %}
|
||||
|
||||
|
||||
""";
|
||||
|
||||
String kMultiPartBodyTemplate = """
|
||||
\$multipart = [
|
||||
{{fields_list}}
|
||||
];
|
||||
|
||||
|
||||
""";
|
||||
|
||||
String kTemplateParams = """
|
||||
\$queryParams = [
|
||||
{{params}}
|
||||
];
|
||||
\$queryParamsStr = '?' . http_build_query(\$queryParams);
|
||||
|
||||
|
||||
""";
|
||||
|
||||
String kTemplateHeader = """
|
||||
\$headers = [
|
||||
{{headers}}
|
||||
];
|
||||
|
||||
|
||||
""";
|
||||
|
||||
String kTemplateBody = """
|
||||
\$body = {{body}};
|
||||
|
||||
|
||||
""";
|
||||
|
||||
String kStringRequest = """
|
||||
\$client = new Client();
|
||||
|
||||
\$request = new Request('{{method}}', '{{url}}'{{queryParams}} {{headers}} {{body}});
|
||||
\$res = \$client->sendAsync(\$request)->wait();
|
||||
echo \$res->getBody();
|
||||
""";
|
||||
|
||||
String? getCode(RequestModel requestModel) {
|
||||
try {
|
||||
jj.Template kNodejsImportTemplate = jj.Template(kStringImportNode);
|
||||
String importsData = kNodejsImportTemplate.render({
|
||||
"hasFormData": requestModel.hasFormData,
|
||||
});
|
||||
|
||||
String result = importsData;
|
||||
|
||||
if (requestModel.hasFormData && requestModel.formDataMapList.isNotEmpty) {
|
||||
var templateMultiPartBody = jj.Template(kMultiPartBodyTemplate);
|
||||
var renderedMultiPartBody = templateMultiPartBody.render({
|
||||
"fields_list": requestModel.formDataMapList.map((field) {
|
||||
return '''
|
||||
[
|
||||
'name' => '${field['name']}',
|
||||
'contents' => '${field['value']}'
|
||||
],''';
|
||||
}).join(),
|
||||
});
|
||||
result += renderedMultiPartBody;
|
||||
}
|
||||
|
||||
var harJson =
|
||||
requestModelToHARJsonRequest(requestModel, useEnabled: true);
|
||||
|
||||
var params = harJson["queryString"];
|
||||
if (params.isNotEmpty) {
|
||||
var templateParams = jj.Template(kTemplateParams);
|
||||
var m = {};
|
||||
for (var i in params) {
|
||||
m[i["name"]] = i["value"];
|
||||
}
|
||||
var jsonString = '';
|
||||
m.forEach((key, value) {
|
||||
jsonString += "\t\t\t\t'$key' => '$value', \n";
|
||||
});
|
||||
jsonString = jsonString.substring(
|
||||
0, jsonString.length - 2); // Removing trailing comma and space
|
||||
result += templateParams.render({
|
||||
"params": jsonString,
|
||||
});
|
||||
}
|
||||
|
||||
var headers = harJson["headers"];
|
||||
if (headers.isNotEmpty || requestModel.hasFormData) {
|
||||
var templateHeader = jj.Template(kTemplateHeader);
|
||||
var m = {};
|
||||
for (var i in headers) {
|
||||
m[i["name"]] = i["value"];
|
||||
}
|
||||
var headersString = '';
|
||||
m.forEach((key, value) {
|
||||
headersString += "\t\t\t\t'$key' => '$value', \n";
|
||||
});
|
||||
if (requestModel.hasFormData) {
|
||||
m['Content-Type'] = 'multipart/form-data';
|
||||
}
|
||||
headersString = headersString.substring(
|
||||
0, headersString.length - 2); // Removing trailing comma and space
|
||||
result += templateHeader.render({
|
||||
"headers": headersString,
|
||||
});
|
||||
}
|
||||
|
||||
var templateBody = jj.Template(kTemplateBody);
|
||||
if (requestModel.hasFormData && requestModel.formDataMapList.isNotEmpty) {
|
||||
result += templateBody.render({
|
||||
"body": "new MultipartStream(\$multipart)",
|
||||
});
|
||||
}
|
||||
|
||||
if (harJson["postData"]?["text"] != null) {
|
||||
result += templateBody
|
||||
.render({"body": kEncoder.convert(harJson["postData"]["text"])});
|
||||
}
|
||||
|
||||
String getRequestBody(Map harJson) {
|
||||
if (harJson.containsKey("postData")) {
|
||||
var postData = harJson["postData"];
|
||||
if (postData.containsKey("mimeType")) {
|
||||
var mimeType = postData["mimeType"];
|
||||
if (mimeType == "text/plain" || mimeType == "application/json") {
|
||||
return " \$body";
|
||||
} else if (mimeType == "multipart/form-data") {
|
||||
return " new MultipartStream(\$multipart)";
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""; // Return empty string if postData or formdata is not present
|
||||
}
|
||||
|
||||
var templateRequest = jj.Template(kStringRequest);
|
||||
result += templateRequest.render({
|
||||
"url": stripUrlParams(requestModel.url),
|
||||
"method": harJson["method"].toLowerCase(),
|
||||
"queryParams":
|
||||
harJson["queryString"].isNotEmpty ? ". \$queryParamsStr," : "",
|
||||
"headers": harJson["headers"].isNotEmpty ? " \$headers," : "",
|
||||
"body": getRequestBody(harJson),
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -273,12 +273,16 @@ enum CodegenLanguage {
|
||||
nodejsAxios("node.js (axios)", "javascript", "js"),
|
||||
nodejsFetch("node.js (fetch)", "javascript", "js"),
|
||||
kotlinOkHttp("Kotlin (okhttp3)", "java", "kt"),
|
||||
pythonHttpClient("Python (http.client)", "python", "py"),
|
||||
pythonRequests("Python (requests)", "python", "py"),
|
||||
pythonHttpClient("Python (http.client)", "python", "py"),
|
||||
rustActix("Rust (Actix Client)", "rust", "rs"),
|
||||
rustReqwest("Rust (reqwest)", "rust", "rs"),
|
||||
rustUreq("Rust (ureq)", "rust", "rs"),
|
||||
juliaHttp("Julia (HTTP)", "julia", "jl");
|
||||
javaOkHttp("Java (okhttp3)", "java", 'java'),
|
||||
javaAsyncHttpClient("Java (asynchttpclient)", "java", "java"),
|
||||
javaHttpClient("Java (HttpClient)", "java", "java"),
|
||||
juliaHttp("Julia (HTTP)", "julia", "jl"),
|
||||
phpGuzzle("PHP (guzzle)", "php", "php");
|
||||
|
||||
const CodegenLanguage(this.label, this.codeHighlightLang, this.ext);
|
||||
final String label;
|
||||
|
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
@ -8,22 +8,19 @@ void main() {
|
||||
|
||||
group('GET Request', () {
|
||||
test('GET 1', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev',
|
||||
method: 'get'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet1, "https"),
|
||||
@ -31,7 +28,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 2', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/country/data',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -40,16 +37,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet2, "https"),
|
||||
@ -57,7 +51,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 3', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/country/data',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -66,16 +60,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet3, "https"),
|
||||
@ -83,7 +74,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 4', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -96,16 +87,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet4, "https"),
|
||||
@ -113,7 +101,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 5', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
method: 'get',
|
||||
headers: {
|
||||
@ -122,16 +110,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet5, "https"),
|
||||
@ -139,7 +124,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 6', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -151,16 +136,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet6, "https"),
|
||||
@ -168,22 +150,19 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 7', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev',
|
||||
method: 'get'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet7, "https"),
|
||||
@ -191,7 +170,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 8', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -203,16 +182,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet8, "https"),
|
||||
@ -220,7 +196,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 9', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -230,16 +206,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet9, "https"),
|
||||
@ -247,7 +220,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 10', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
headers: {
|
||||
@ -256,16 +229,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -277,7 +247,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 11', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -290,16 +260,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet11, "https"),
|
||||
@ -307,22 +274,19 @@ axios(config)
|
||||
});
|
||||
|
||||
test('GET 12', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelGet12, "https"),
|
||||
@ -332,22 +296,19 @@ axios(config)
|
||||
|
||||
group('HEAD Request', () {
|
||||
test('HEAD 1', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev',
|
||||
method: 'head'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelHead1, "https"),
|
||||
@ -355,22 +316,19 @@ axios(config)
|
||||
});
|
||||
|
||||
test('HEAD 2', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'http://api.apidash.dev',
|
||||
method: 'head'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelHead2, "http"),
|
||||
@ -380,7 +338,7 @@ axios(config)
|
||||
|
||||
group('POST Request', () {
|
||||
test('POST 1', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
method: 'post',
|
||||
headers: {
|
||||
@ -390,16 +348,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelPost1, "https"),
|
||||
@ -407,7 +362,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('POST 2', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
method: 'post',
|
||||
headers: {
|
||||
@ -417,16 +372,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelPost2, "https"),
|
||||
@ -434,7 +386,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('POST 3', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
method: 'post',
|
||||
headers: {
|
||||
@ -445,26 +397,227 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 4', () {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"text": "API",
|
||||
"sep": "|",
|
||||
"times": "3"
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsAxios,
|
||||
requestModelPost4,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 5', () {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
"User-Agent": "Test Agent"
|
||||
},
|
||||
data: {
|
||||
"text": "API",
|
||||
"sep": "|",
|
||||
"times": "3"
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsAxios,
|
||||
requestModelPost5,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 6', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1997568083 for details regarding integration
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"token": "xyz",
|
||||
"imfile": fileInput1.files[0]
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsAxios,
|
||||
requestModelPost6,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 7', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1997568083 for details regarding integration
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"token": "xyz",
|
||||
"imfile": fileInput1.files[0]
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsAxios,
|
||||
requestModelPost7,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 8', () {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
method: 'post',
|
||||
params: {
|
||||
"size": "2",
|
||||
"len": "3"
|
||||
},
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"text": "API",
|
||||
"sep": "|",
|
||||
"times": "3"
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsAxios,
|
||||
requestModelPost8,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 9', () {
|
||||
const expectedCode =
|
||||
r"""// refer https://github.com/foss42/apidash/issues/293#issuecomment-1997568083 for details regarding integration
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
method: 'post',
|
||||
params: {
|
||||
"size": "2",
|
||||
"len": "3"
|
||||
},
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
"User-Agent": "Test Agent",
|
||||
"Keep-Alive": "true"
|
||||
},
|
||||
data: {
|
||||
"token": "xyz",
|
||||
"imfile": fileInput1.files[0]
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.jsAxios,
|
||||
requestModelPost9,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PUT Request', () {
|
||||
test('PUT 1', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'put',
|
||||
headers: {
|
||||
@ -474,16 +627,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelPut1, "https"),
|
||||
@ -493,7 +643,7 @@ axios(config)
|
||||
|
||||
group('PATCH Request', () {
|
||||
test('PATCH 1', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'patch',
|
||||
headers: {
|
||||
@ -503,16 +653,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(CodegenLanguage.jsAxios, requestModelPatch1, "https"),
|
||||
@ -522,22 +669,19 @@ axios(config)
|
||||
|
||||
group('DELETE Request', () {
|
||||
test('DELETE 1', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'delete'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -546,7 +690,7 @@ axios(config)
|
||||
});
|
||||
|
||||
test('DELETE 2', () {
|
||||
const expectedCode = r"""let config = {
|
||||
const expectedCode = r"""const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'delete',
|
||||
headers: {
|
||||
@ -556,16 +700,13 @@ axios(config)
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -517,6 +517,35 @@ fun main() {
|
||||
CodegenLanguage.kotlinOkHttp, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 4', () {
|
||||
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.MultipartBody
|
||||
|
||||
fun main() {
|
||||
val client = OkHttpClient()
|
||||
|
||||
val url = "https://api.apidash.dev/io/form"
|
||||
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("text","API")
|
||||
.addFormDataPart("sep","|")
|
||||
.addFormDataPart("times","3")
|
||||
.build()
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.build()
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
|
||||
println(response.code)
|
||||
println(response.body?.string())
|
||||
}
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.kotlinOkHttp, requestModelPost4, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 5', () {
|
||||
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||
@ -548,6 +577,141 @@ fun main() {
|
||||
CodegenLanguage.kotlinOkHttp, requestModelPost5, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 6', () {
|
||||
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
|
||||
fun main() {
|
||||
val client = OkHttpClient()
|
||||
|
||||
val url = "https://api.apidash.dev/io/img"
|
||||
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("token","xyz")
|
||||
|
||||
.addFormDataPart("imfile",File("Documents/up/1.png").name,File("Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
|
||||
.build()
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.build()
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
|
||||
println(response.code)
|
||||
println(response.body?.string())
|
||||
}
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.kotlinOkHttp, requestModelPost6, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 7', () {
|
||||
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
|
||||
fun main() {
|
||||
val client = OkHttpClient()
|
||||
|
||||
val url = "https://api.apidash.dev/io/img"
|
||||
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("token","xyz")
|
||||
|
||||
.addFormDataPart("imfile",File("Documents/up/1.png").name,File("Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
|
||||
.build()
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.build()
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
|
||||
println(response.code)
|
||||
println(response.body?.string())
|
||||
}
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.kotlinOkHttp, requestModelPost7, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 8', () {
|
||||
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.MultipartBody
|
||||
|
||||
fun main() {
|
||||
val client = OkHttpClient()
|
||||
|
||||
val url = "https://api.apidash.dev/io/form".toHttpUrl().newBuilder()
|
||||
.addQueryParameter("size", "2")
|
||||
.addQueryParameter("len", "3")
|
||||
.build()
|
||||
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("text","API")
|
||||
.addFormDataPart("sep","|")
|
||||
.addFormDataPart("times","3")
|
||||
.build()
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.build()
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
|
||||
println(response.code)
|
||||
println(response.body?.string())
|
||||
}
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.kotlinOkHttp, requestModelPost8, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 9', () {
|
||||
const expectedCode = r'''import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.MultipartBody
|
||||
import java.io.File
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
|
||||
fun main() {
|
||||
val client = OkHttpClient()
|
||||
|
||||
val url = "https://api.apidash.dev/io/img".toHttpUrl().newBuilder()
|
||||
.addQueryParameter("size", "2")
|
||||
.addQueryParameter("len", "3")
|
||||
.build()
|
||||
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("token","xyz")
|
||||
|
||||
.addFormDataPart("imfile",File("Documents/up/1.png").name,File("Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
|
||||
.build()
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.addHeader("User-Agent", "Test Agent")
|
||||
.addHeader("Keep-Alive", "true")
|
||||
.post(body)
|
||||
.build()
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
|
||||
println(response.code)
|
||||
println(response.body?.string())
|
||||
}
|
||||
''';
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.kotlinOkHttp, requestModelPost9, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PUT Request', () {
|
||||
|
@ -10,22 +10,19 @@ void main() {
|
||||
test('GET 1', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev',
|
||||
method: 'get'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -36,7 +33,7 @@ axios(config)
|
||||
test('GET 2', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/country/data',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -45,16 +42,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -65,7 +59,7 @@ axios(config)
|
||||
test('GET 3', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/country/data',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -74,16 +68,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -94,7 +85,7 @@ axios(config)
|
||||
test('GET 4', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -107,16 +98,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -127,7 +115,7 @@ axios(config)
|
||||
test('GET 5', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
method: 'get',
|
||||
headers: {
|
||||
@ -136,16 +124,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -156,7 +141,7 @@ axios(config)
|
||||
test('GET 6', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -168,16 +153,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -188,22 +170,19 @@ axios(config)
|
||||
test('GET 7', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev',
|
||||
method: 'get'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -214,7 +193,7 @@ axios(config)
|
||||
test('GET 8', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.github.com/repos/foss42/apidash',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -226,16 +205,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -246,7 +222,7 @@ axios(config)
|
||||
test('GET 9', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -256,16 +232,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -276,7 +249,7 @@ axios(config)
|
||||
test('GET 10', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
headers: {
|
||||
@ -285,16 +258,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -308,7 +278,7 @@ axios(config)
|
||||
test('GET 11', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get',
|
||||
params: {
|
||||
@ -321,16 +291,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -341,22 +308,19 @@ axios(config)
|
||||
test('GET 12', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/humanize/social',
|
||||
method: 'get'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -369,22 +333,19 @@ axios(config)
|
||||
test('HEAD 1', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev',
|
||||
method: 'head'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -395,22 +356,19 @@ axios(config)
|
||||
test('HEAD 2', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'http://api.apidash.dev',
|
||||
method: 'head'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -423,7 +381,7 @@ axios(config)
|
||||
test('POST 1', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
method: 'post',
|
||||
headers: {
|
||||
@ -433,16 +391,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -453,7 +408,7 @@ axios(config)
|
||||
test('POST 2', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
method: 'post',
|
||||
headers: {
|
||||
@ -463,16 +418,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -483,7 +435,7 @@ axios(config)
|
||||
test('POST 3', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/case/lower',
|
||||
method: 'post',
|
||||
headers: {
|
||||
@ -494,29 +446,236 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsAxios, requestModelPost3, "https"),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 4', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"text": "API",
|
||||
"sep": "|",
|
||||
"times": "3"
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsAxios,
|
||||
requestModelPost4,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
|
||||
test('POST 5', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
"User-Agent": "Test Agent"
|
||||
},
|
||||
data: {
|
||||
"text": "API",
|
||||
"sep": "|",
|
||||
"times": "3"
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsAxios,
|
||||
requestModelPost5,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 6', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
import fs from 'fs'
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"token": "xyz",
|
||||
"imfile": fs.createReadStream("/Documents/up/1.png")
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsAxios,
|
||||
requestModelPost6,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 7', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
import fs from 'fs'
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
method: 'post',
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"token": "xyz",
|
||||
"imfile": fs.createReadStream("/Documents/up/1.png")
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsAxios,
|
||||
requestModelPost7,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 8', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/form',
|
||||
method: 'post',
|
||||
params: {
|
||||
"size": "2",
|
||||
"len": "3"
|
||||
},
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data"
|
||||
},
|
||||
data: {
|
||||
"text": "API",
|
||||
"sep": "|",
|
||||
"times": "3"
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsAxios,
|
||||
requestModelPost8,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
test('POST 9', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
import fs from 'fs'
|
||||
|
||||
const config = {
|
||||
url: 'https://api.apidash.dev/io/img',
|
||||
method: 'post',
|
||||
params: {
|
||||
"size": "2",
|
||||
"len": "3"
|
||||
},
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
"User-Agent": "Test Agent",
|
||||
"Keep-Alive": "true"
|
||||
},
|
||||
data: {
|
||||
"token": "xyz",
|
||||
"imfile": fs.createReadStream("/Documents/up/1.png")
|
||||
}
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
CodegenLanguage.nodejsAxios,
|
||||
requestModelPost9,
|
||||
"https",
|
||||
),
|
||||
expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PUT Request', () {
|
||||
test('PUT 1', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'put',
|
||||
headers: {
|
||||
@ -526,16 +685,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -548,7 +704,7 @@ axios(config)
|
||||
test('PATCH 1', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'patch',
|
||||
headers: {
|
||||
@ -558,16 +714,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -580,22 +733,19 @@ axios(config)
|
||||
test('DELETE 1', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'delete'
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
@ -606,7 +756,7 @@ axios(config)
|
||||
test('DELETE 2', () {
|
||||
const expectedCode = r"""import axios from 'axios';
|
||||
|
||||
let config = {
|
||||
const config = {
|
||||
url: 'https://reqres.in/api/users/2',
|
||||
method: 'delete',
|
||||
headers: {
|
||||
@ -616,16 +766,13 @@ let config = {
|
||||
};
|
||||
|
||||
axios(config)
|
||||
.then(function (response) {
|
||||
// handle success
|
||||
console.log(response.status);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
console.log(error.response.status);
|
||||
console.log(error);
|
||||
});
|
||||
.then(res => {
|
||||
console.log(res.status);
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
""";
|
||||
expect(
|
||||
codeGen.getCode(
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user