Skip to content

Commit

Permalink
fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PCoder23 committed Mar 15, 2024
1 parent 9e007fa commit 8b3a356
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
42 changes: 38 additions & 4 deletions lib/codegen/kotlin/okhttp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}}"
Expand Down Expand Up @@ -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()
''';
Expand All @@ -81,6 +87,7 @@ import okhttp3.MultipartBody""";
bool hasQuery = false;
bool hasBody = false;
bool hasFormData = false;
bool hasFile = false;

var rec = getValidRequestUri(
requestModel.url,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions test/codegen/kotlin_okhttp_codegen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,17 @@ fun main() {
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",null,File("/Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
.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)
Expand All @@ -610,14 +613,17 @@ fun main() {
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",null,File("/Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
.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)
Expand Down Expand Up @@ -673,6 +679,9 @@ fun main() {
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()
Expand All @@ -683,7 +692,7 @@ fun main() {
.build()
val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("token","xyz")
.addFormDataPart("imfile",null,File("/Documents/up/1.png").asRequestBody("application/octet-stream".toMediaType()))
.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)
Expand Down

0 comments on commit 8b3a356

Please sign in to comment.