mirror of
https://github.com/foss42/apidash.git
synced 2025-07-15 00:52:22 +08:00
fix: fixed okhttp3 code gen
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:apidash/consts.dart';
|
import 'package:apidash/consts.dart';
|
||||||
|
import 'package:apidash/utils/convert_utils.dart';
|
||||||
|
import 'package:apidash/utils/extensions/request_model_extension.dart';
|
||||||
import 'package:jinja/jinja.dart' as jj;
|
import 'package:jinja/jinja.dart' as jj;
|
||||||
import 'package:apidash/utils/utils.dart'
|
import 'package:apidash/utils/utils.dart'
|
||||||
show getValidRequestUri, stripUriParams;
|
show getValidRequestUri, stripUriParams;
|
||||||
@ -61,6 +63,13 @@ import okhttp3.MediaType.Companion.toMediaType""";
|
|||||||
}
|
}
|
||||||
|
|
||||||
""";
|
""";
|
||||||
|
// 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()))
|
||||||
|
{% else %}.addFormDataPart("{{item.name}}","{{item.value}}")
|
||||||
|
{% endif %}{% endfor %}.build()
|
||||||
|
''';
|
||||||
|
|
||||||
String? getCode(
|
String? getCode(
|
||||||
RequestModel requestModel,
|
RequestModel requestModel,
|
||||||
@ -68,7 +77,6 @@ import okhttp3.MediaType.Companion.toMediaType""";
|
|||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
String result = "";
|
String result = "";
|
||||||
bool hasHeaders = false;
|
|
||||||
bool hasQuery = false;
|
bool hasQuery = false;
|
||||||
bool hasBody = false;
|
bool hasBody = false;
|
||||||
|
|
||||||
@ -99,7 +107,13 @@ import okhttp3.MediaType.Companion.toMediaType""";
|
|||||||
|
|
||||||
var method = requestModel.method;
|
var method = requestModel.method;
|
||||||
var requestBody = requestModel.requestBody;
|
var requestBody = requestModel.requestBody;
|
||||||
if (kMethodsWithBody.contains(method) && requestBody != null) {
|
if (requestModel.isFormDataRequest) {
|
||||||
|
var formDataTemplate = jj.Template(kFormDataBody);
|
||||||
|
|
||||||
|
result += formDataTemplate.render({
|
||||||
|
"formDataList": rowsToFormDataMap(requestModel.formDataList),
|
||||||
|
});
|
||||||
|
} else if (kMethodsWithBody.contains(method) && requestBody != null) {
|
||||||
var contentLength = utf8.encode(requestBody).length;
|
var contentLength = utf8.encode(requestBody).length;
|
||||||
if (contentLength > 0) {
|
if (contentLength > 0) {
|
||||||
hasBody = true;
|
hasBody = true;
|
||||||
@ -124,7 +138,6 @@ import okhttp3.MediaType.Companion.toMediaType""";
|
|||||||
if (headersList != null) {
|
if (headersList != null) {
|
||||||
var headers = requestModel.headersMap;
|
var headers = requestModel.headersMap;
|
||||||
if (headers.isNotEmpty) {
|
if (headers.isNotEmpty) {
|
||||||
hasHeaders = true;
|
|
||||||
result += getHeaders(headers);
|
result += getHeaders(headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,7 +145,7 @@ import okhttp3.MediaType.Companion.toMediaType""";
|
|||||||
var templateRequestEnd = jj.Template(kTemplateRequestEnd);
|
var templateRequestEnd = jj.Template(kTemplateRequestEnd);
|
||||||
result += templateRequestEnd.render({
|
result += templateRequestEnd.render({
|
||||||
"method": method.name.toLowerCase(),
|
"method": method.name.toLowerCase(),
|
||||||
"hasBody": hasBody ? "body" : "",
|
"hasBody": (hasBody || requestModel.isFormDataRequest) ? "body" : "",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
Reference in New Issue
Block a user