io.github.eendroroy.sdk.bkash.converter.ResponseConverter.kt Maven / Gradle / Ivy
The newest version!
package io.github.eendroroy.sdk.bkash.converter
import io.github.eendroroy.sdk.bkash.response.BaseResponse
import retrofit2.Response
import retrofit2.Retrofit
/**
* @author indrajit
*/
class ResponseConverter(var retrofit: Retrofit, var apiResponse: Response) {
@Throws(Exception::class)
inline fun convert(): T? {
val response: T? = when {
apiResponse.isSuccessful -> {
apiResponse.body() as T?
}
null == apiResponse.errorBody() -> {
throw Exception(apiResponse.code().toString() + " : " + apiResponse.message())
}
0L == apiResponse.errorBody()!!.contentLength() -> {
throw Exception(apiResponse.code().toString() + " : " + apiResponse.message())
}
else -> {
val errorConverter = retrofit.responseBodyConverter(T::class.java, arrayOfNulls(0))
errorConverter.convert(apiResponse.errorBody()!!)
}
}
if (response is BaseResponse) {
(response as BaseResponse)
.httpResponseCode(apiResponse.code())
.httpResponseMessage(apiResponse.message())
}
return response
}
}