All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin-client.libraries.jvm-retrofit2.infrastructure.ResponseExt.kt.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
package {{packageName}}.infrastructure

{{#moshi}}
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Moshi
{{/moshi}}
{{#gson}}
import com.google.gson.GsonBuilder
import com.google.gson.JsonParseException
{{/gson}}
import retrofit2.Response

{{#moshi}}
@Throws(JsonDataException::class)
inline fun  Response<*>.getErrorResponse(serializerBuilder: Moshi.Builder = Serializer.moshiBuilder): T? {
    val serializer = serializerBuilder.build()
    val parser = serializer.adapter(T::class.java)
    val response = errorBody()?.string()
    if(response != null) {
        return parser.fromJson(response)
    }
    return null
}
{{/moshi}}
{{#gson}}
@Throws(JsonParseException::class)
inline fun  Response<*>.getErrorResponse(serializerBuilder: GsonBuilder = Serializer.gsonBuilder): T? {
    val serializer = serializerBuilder.create()
    val reader = errorBody()?.charStream()
    if(reader != null) {
        return serializer.fromJson(reader, T::class.java)
    }
    return null
}
{{/gson}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy