mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-02-10 00:50:49 +01:00
Merge pull request #1526 from johnjohndoe/retrofit2-call-awaitresponse
Use retrofit2.Call.awaitResponse extension provided by Retrofit 2.
This commit is contained in:
commit
3f44056243
@ -23,6 +23,7 @@ Build 🧱:
|
|||||||
- SDK is now API level 21 minimum, and so RiotX (#405)
|
- SDK is now API level 21 minimum, and so RiotX (#405)
|
||||||
|
|
||||||
Other changes:
|
Other changes:
|
||||||
|
- Use `retrofit2.Call.awaitResponse` extension provided by Retrofit 2. (#1526)
|
||||||
- Fix minor typo in contribution guide (#1512)
|
- Fix minor typo in contribution guide (#1512)
|
||||||
- Fix self-assignment of callback in `DefaultRoomPushRuleService#setRoomNotificationState` (#1520)
|
- Fix self-assignment of callback in `DefaultRoomPushRuleService#setRoomNotificationState` (#1520)
|
||||||
- Random housekeeping clean-ups indicated by Lint (#1520)
|
- Random housekeeping clean-ups indicated by Lint (#1520)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2019 New Vector Ltd
|
* Copyright 2020 New Vector Ltd
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -22,12 +22,13 @@ import kotlinx.coroutines.CancellationException
|
|||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.EventBus
|
||||||
import retrofit2.Call
|
import retrofit2.Call
|
||||||
|
import retrofit2.awaitResponse
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
internal suspend inline fun <DATA> executeRequest(eventBus: EventBus?,
|
internal suspend inline fun <DATA : Any> executeRequest(eventBus: EventBus?,
|
||||||
block: Request<DATA>.() -> Unit) = Request<DATA>(eventBus).apply(block).execute()
|
block: Request<DATA>.() -> Unit) = Request<DATA>(eventBus).apply(block).execute()
|
||||||
|
|
||||||
internal class Request<DATA>(private val eventBus: EventBus?) {
|
internal class Request<DATA : Any>(private val eventBus: EventBus?) {
|
||||||
|
|
||||||
var isRetryable = false
|
var isRetryable = false
|
||||||
var initialDelay: Long = 100L
|
var initialDelay: Long = 100L
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* * Copyright 2019 New Vector Ltd
|
* * Copyright 2020 New Vector Ltd
|
||||||
* *
|
* *
|
||||||
* * Licensed under the Apache License, Version 2.0 (the "License");
|
* * Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* * you may not use this file except in compliance with the License.
|
* * you may not use this file except in compliance with the License.
|
||||||
@ -26,8 +26,6 @@ import im.vector.matrix.android.internal.di.MoshiProvider
|
|||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
import okhttp3.ResponseBody
|
import okhttp3.ResponseBody
|
||||||
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.EventBus
|
||||||
import retrofit2.Call
|
|
||||||
import retrofit2.Callback
|
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@ -35,23 +33,6 @@ import java.net.HttpURLConnection
|
|||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
import kotlin.coroutines.resumeWithException
|
import kotlin.coroutines.resumeWithException
|
||||||
|
|
||||||
internal suspend fun <T> Call<T>.awaitResponse(): Response<T> {
|
|
||||||
return suspendCancellableCoroutine { continuation ->
|
|
||||||
continuation.invokeOnCancellation {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
enqueue(object : Callback<T> {
|
|
||||||
override fun onResponse(call: Call<T>, response: Response<T>) {
|
|
||||||
continuation.resume(response)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onFailure(call: Call<T>, t: Throwable) {
|
|
||||||
continuation.resumeWithException(t)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal suspend fun okhttp3.Call.awaitResponse(): okhttp3.Response {
|
internal suspend fun okhttp3.Call.awaitResponse(): okhttp3.Response {
|
||||||
return suspendCancellableCoroutine { continuation ->
|
return suspendCancellableCoroutine { continuation ->
|
||||||
continuation.invokeOnCancellation {
|
continuation.invokeOnCancellation {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user