[kotlin][client] add support for okhttp requests with empty body (#10369)

* [kotlin][client] add support for okhttp requests with empty body

* [kotlin][client] add support for okhttp requests with empty body
This commit is contained in:
Bruno Coelho
2021-09-14 04:33:06 +01:00
committed by GitHub
parent 29f3869bda
commit 0211f7b82e
13 changed files with 178 additions and 53 deletions

View File

@@ -14,6 +14,7 @@ import okhttp3.MultipartBody
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import okhttp3.internal.EMPTY_REQUEST
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
@@ -103,9 +104,16 @@ open class ApiClient(val baseUrl: String) {
}
}.build()
}
mediaType == JsonMediaType -> Serializer.moshi.adapter(T::class.java).toJson(content).toRequestBody(
mediaType.toMediaTypeOrNull()
)
mediaType == JsonMediaType -> {
if (content == null) {
EMPTY_REQUEST
} else {
Serializer.moshi.adapter(T::class.java).toJson(content)
.toRequestBody(
mediaType.toMediaTypeOrNull()
)
}
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")