@@ -7,7 +7,7 @@ import 'package:apidash/consts.dart';
7
7
8
8
class KotlinOkHttpCodeGen {
9
9
final String kTemplateStart = """import okhttp3.OkHttpClient
10
- import okhttp3.Request{{importForQuery}}{{importForBody}}{{importForFormData}}
10
+ import okhttp3.Request{{importForQuery}}{{importForBody}}{{importForFormData}}{{importForFile}}
11
11
12
12
fun main() {
13
13
val client = OkHttpClient()
@@ -27,6 +27,12 @@ import okhttp3.MediaType.Companion.toMediaType""";
27
27
28
28
import okhttp3.MultipartBody""" ;
29
29
30
+ final String kStringImportForFile = """
31
+
32
+ import java.io.File
33
+ import okhttp3.RequestBody.Companion.asRequestBody
34
+ import okhttp3.MediaType.Companion.toMediaType""" ;
35
+
30
36
final String kTemplateUrl = '''
31
37
32
38
val url = "{{url}}"
@@ -68,7 +74,7 @@ import okhttp3.MultipartBody""";
68
74
// Converting list of form data objects to kolin multi part data
69
75
String kFormDataBody = '''
70
76
val body = MultipartBody.Builder().setType(MultipartBody.FORM){% for item in formDataList %}{% if item.type == 'file' %}
71
- .addFormDataPart("{{item.name}}",null ,File("{{item.value}}").asRequestBody("application/octet-stream".toMediaType()))
77
+ .addFormDataPart("{{item.name}}",File("{{item.value}}").name ,File("{{item.value}}").asRequestBody("application/octet-stream".toMediaType()))
72
78
{% else %}.addFormDataPart("{{item.name}}","{{item.value}}")
73
79
{% endif %}{% endfor %}.build()
74
80
''' ;
@@ -81,6 +87,7 @@ import okhttp3.MultipartBody""";
81
87
bool hasQuery = false ;
82
88
bool hasBody = false ;
83
89
bool hasFormData = false ;
90
+ bool hasFile = false ;
84
91
85
92
var rec = getValidRequestUri (
86
93
requestModel.url,
@@ -111,8 +118,34 @@ import okhttp3.MultipartBody""";
111
118
hasFormData = true ;
112
119
var formDataTemplate = jj.Template (kFormDataBody);
113
120
121
+ List <Map <String ,String >> modifiedFormDataList = [];
122
+ for (var item in requestModel.formDataList) {
123
+ if (item.type == FormDataType .file ) {
124
+ if (item.value[0 ] == "/" ) {
125
+ modifiedFormDataList.add ({
126
+ "name" : item.name,
127
+ "value" : item.value.substring (1 ),
128
+ "type" : "file"
129
+ });
130
+ }else {
131
+ modifiedFormDataList.add ({
132
+ "name" : item.name,
133
+ "value" : item.value,
134
+ "type" : "file"
135
+ });
136
+ }
137
+ hasFile = true ;
138
+ }else {
139
+ modifiedFormDataList.add ({
140
+ "name" : item.name,
141
+ "value" : item.value,
142
+ "type" : "text"
143
+ });
144
+ }
145
+ }
146
+
114
147
result += formDataTemplate.render ({
115
- "formDataList" : requestModel.formDataMapList ,
148
+ "formDataList" : modifiedFormDataList ,
116
149
});
117
150
} else if (kMethodsWithBody.contains (method) && requestBody != null ) {
118
151
var contentLength = utf8.encode (requestBody).length;
@@ -129,7 +162,8 @@ import okhttp3.MultipartBody""";
129
162
var stringStart = templateStart.render ({
130
163
"importForQuery" : hasQuery ? kStringImportForQuery : "" ,
131
164
"importForBody" : hasBody ? kStringImportForBody : "" ,
132
- "importForFormData" : hasFormData ? kStringImportForFormData : ""
165
+ "importForFormData" : hasFormData ? kStringImportForFormData : "" ,
166
+ "importForFile" : hasFile ? kStringImportForFile : "" ,
133
167
});
134
168
135
169
result = stringStart + result;
0 commit comments