Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import okhttp3.MultipartBody
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
{{#jvm-okhttp3}}
import okhttp3.internal.Util.EMPTY_REQUEST
{{/jvm-okhttp3}}
{{#jvm-okhttp4}}
import okhttp3.internal.EMPTY_REQUEST
{{/jvm-okhttp4}}
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
Expand Down Expand Up @@ -161,25 +167,49 @@ import com.squareup.moshi.adapter
}.build()
}
{{#jvm-okhttp3}}
mediaType == JsonMediaType -> RequestBody.create(
{{#moshi}}
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
{{/moshi}}
{{#gson}}
MediaType.parse(mediaType), Serializer.gson.toJson(content, T::class.java)
{{/gson}}
{{#jackson}}
MediaType.parse(mediaType), Serializer.jacksonObjectMapper.writeValueAsString(content)
{{/jackson}}
{{#kotlinx_serialization}}
MediaType.parse(mediaType), Serializer.jvmJson.encodeToString(content)
{{/kotlinx_serialization}}
)
mediaType == JsonMediaType -> {
if (content == null) {
EMPTY_REQUEST
} else {
RequestBody.create(
{{#moshi}}
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
{{/moshi}}
{{#gson}}
MediaType.parse(mediaType), Serializer.gson.toJson(content, T::class.java)
{{/gson}}
{{#jackson}}
MediaType.parse(mediaType), Serializer.jacksonObjectMapper.writeValueAsString(content)
{{/jackson}}
{{#kotlinx_serialization}}
MediaType.parse(mediaType), Serializer.jvmJson.encodeToString(content)
{{/kotlinx_serialization}}
)
}
}
{{/jvm-okhttp3}}
{{#jvm-okhttp4}}
mediaType == JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).toJson(content){{/moshi}}{{#gson}}Serializer.gson.toJson(content, T::class.java){{/gson}}{{#jackson}}Serializer.jacksonObjectMapper.writeValueAsString(content){{/jackson}}{{#kotlinx_serialization}}Serializer.jvmJson.encodeToString(content){{/kotlinx_serialization}}.toRequestBody(
mediaType.toMediaTypeOrNull()
)
mediaType == JsonMediaType -> {
if (content == null) {
EMPTY_REQUEST
} else {
{{#moshi}}
Serializer.moshi.adapter(T::class.java).toJson(content)
{{/moshi}}
{{#gson}}
Serializer.gson.toJson(content, T::class.java)
{{/gson}}
{{#jackson}}
Serializer.jacksonObjectMapper.writeValueAsString(content)
{{/jackson}}
{{#kotlinx_serialization}}
Serializer.jvmJson.encodeToString(content)
{{/kotlinx_serialization}}
.toRequestBody(
mediaType.toMediaTypeOrNull()
)
}
}
{{/jvm-okhttp4}}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
// TODO: this should be extended with other serializers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,9 +104,16 @@ open class ApiClient(val baseUrl: String) {
}
}.build()
}
mediaType == JsonMediaType -> Serializer.gson.toJson(content, T::class.java).toRequestBody(
mediaType.toMediaTypeOrNull()
)
mediaType == JsonMediaType -> {
if (content == null) {
EMPTY_REQUEST
} else {
Serializer.gson.toJson(content, T::class.java)
.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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,9 +104,16 @@ open class ApiClient(val baseUrl: String) {
}
}.build()
}
mediaType == JsonMediaType -> Serializer.jacksonObjectMapper.writeValueAsString(content).toRequestBody(
mediaType.toMediaTypeOrNull()
)
mediaType == JsonMediaType -> {
if (content == null) {
EMPTY_REQUEST
} else {
Serializer.jacksonObjectMapper.writeValueAsString(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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
Expand Down Expand Up @@ -104,9 +105,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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,9 +107,16 @@ open class ApiClient(val baseUrl: String) {
}
}.build()
}
mediaType == JsonMediaType -> Serializer.gson.toJson(content, T::class.java).toRequestBody(
mediaType.toMediaTypeOrNull()
)
mediaType == JsonMediaType -> {
if (content == null) {
EMPTY_REQUEST
} else {
Serializer.gson.toJson(content, T::class.java)
.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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,9 +104,16 @@ internal 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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import okhttp3.MultipartBody
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import okhttp3.internal.Util.EMPTY_REQUEST
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
Expand Down Expand Up @@ -101,9 +102,15 @@ open class ApiClient(val baseUrl: String) {
}
}.build()
}
mediaType == JsonMediaType -> RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
mediaType == JsonMediaType -> {
if (content == null) {
EMPTY_REQUEST
} else {
RequestBody.create(
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
)
}
}
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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
Loading