lebonprix.apk/java/io/xdrm/lebonprix/extensions/Call.kt

28 lines
712 B
Kotlin
Raw Permalink Normal View History

2018-12-15 11:17:28 +00:00
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)
}
})
}
}