Skip to content

Instantly share code, notes, and snippets.

View agustarc's full-sized avatar
🎯
Focusing

Leopold agustarc

🎯
Focusing
View GitHub Profile
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT, maxRetries = 5, interval = 2, unit = TimeUnit.SECONDS) { Single.just(Result.failure(it)) })
}
}
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT, NETWORK, SERVICE_UNAVAILABLE) { Single.just(Result.failure(it)) })
}
}
interface ImagesRepository {
fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>>
}
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT) { Single.just(Result.failure(it)) })
}
internal typealias RETRY_PREDICATE = (Throwable) -> Boolean
internal const val MAX_RETRIES = 3L
internal const val DEFAULT_INTERVAL = 1L
internal val TIMEOUT: RETRY_PREDICATE = { it is SocketTimeoutException }
internal val NETWORK: RETRY_PREDICATE = { it is IOException }
internal val SERVICE_UNAVAILABLE: RETRY_PREDICATE = { it is HttpException && it.code() == 503 }
internal fun <T> applyRetryPolicy(
internal typealias RETRY_PREDICATE = (Throwable) -> Boolean
internal const val MAX_RETRIES = 3L
internal const val DEFAULT_INTERVAL = 1L
internal val TIMEOUT: RETRY_PREDICATE = { it is SocketTimeoutException }
internal val NETWORK: RETRY_PREDICATE = { it is IOException }
internal val SERVICE_UNAVAILABLE: RETRY_PREDICATE = { it is HttpException && it.code() == 503 }
retryWhen { attempts ->
Flowables.zip(
attempts.map { error -> if (error is IOException) error else throw error },
Flowable.interval(1, TimeUnit.SECONDS)
).map { (error, retryCount) -> if (retryCount >= 3) throw error }
}
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<T> retryWhen(Function<? super Flowable<Throwable>, ? extends Publisher<?>> handler) {
return toSingle(toFlowable().retryWhen(handler));
}
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Koap.serializer = GsonSerializer(Gson())
Koap.bind(this, SettingPreference, AccountPreference)
}
}
Koap.serializer = GsonSerializer(Gson())
implementation "com.github.AgustaRC.koap:koap-gson-serializer:1.0.1"