28 lines
712 B
Kotlin
28 lines
712 B
Kotlin
package io.xdrm.lebonprix.extensions
|
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
import okhttp3.Call
|
|
import okhttp3.Callback
|
|
import okhttp3.Response
|
|
import java.io.IOException
|
|
import kotlin.coroutines.resume
|
|
import kotlin.coroutines.resumeWithException
|
|
|
|
suspend fun Call.await() : Response{
|
|
return suspendCancellableCoroutine<Response> {
|
|
|
|
it.invokeOnCancellation {
|
|
this.cancel()
|
|
}
|
|
|
|
this.enqueue(object : Callback{
|
|
override fun onFailure(call: Call, e: IOException) {
|
|
it.cancel(e)
|
|
}
|
|
|
|
override fun onResponse(call: Call, response: Response) {
|
|
it.resume(response)
|
|
}
|
|
})
|
|
}
|
|
} |