
com.nhaarman.httpmonads.RxSingleHttpTryCallAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of httpmonads-retrofit-rx Show documentation
Show all versions of httpmonads-retrofit-rx Show documentation
HttpMonads introduces monad types for working with Http requests.
The newest version!
package com.nhaarman.httpmonads
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import com.nhaarman.httpmonads.HttpError.NetworkError
import io.reactivex.Scheduler
import io.reactivex.Single
import retrofit2.Call
import retrofit2.CallAdapter
import retrofit2.Retrofit
import java.io.IOException
import java.lang.reflect.Type
internal class RxSingleHttpTryCallAdapter(
private val responseType: Type,
private val rxDelegate: CallAdapter>
) : CallAdapter> {
override fun adapt(call: Call): Single<*> {
return rxDelegate.adapt(call)
.map { HttpTry.success(it) }
.onErrorResumeNext { t ->
when (t) {
is HttpException -> Single.just(HttpTry.failure(t.response().toHttpError()))
is IOException -> Single.just(HttpTry.failure(NetworkError(t)))
else -> Single.error(t)
}
}
}
override fun responseType(): Type {
return responseType
}
companion object {
fun create(
returnType: Type,
responseType: Type,
annotations: Array,
retrofit: Retrofit,
scheduler: Scheduler
): RxSingleHttpTryCallAdapter {
val rxDelegate = rxDelegate(scheduler, returnType, annotations, retrofit)
return RxSingleHttpTryCallAdapter(
responseType,
rxDelegate
)
}
@Suppress("UNCHECKED_CAST")
private fun rxDelegate(scheduler: Scheduler, returnType: Type, annotations: Array, retrofit: Retrofit): CallAdapter> {
return RxJava2CallAdapterFactory.createWithScheduler(scheduler).get(returnType, annotations, retrofit) as CallAdapter>
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy