Skip to content

Instantly share code, notes, and snippets.

@skydoves
Created April 11, 2022 03:28
Show Gist options
  • Save skydoves/f7db8663112b88efb47c248f136928c1 to your computer and use it in GitHub Desktop.
Save skydoves/f7db8663112b88efb47c248f136928c1 to your computer and use it in GitHub Desktop.
network_result_call
class NetworkResultCall<T : Any>(
private val proxy: Call<T>
) : Call<NetworkResult<T>> {
override fun enqueue(callback: Callback<NetworkResult<T>>) {
proxy.enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
val networkResult = handleApi { response }
callback.onResponse(this@NetworkResultCall, Response.success(networkResult))
}
override fun onFailure(call: Call<T>, t: Throwable) {
val networkResult = ApiException<T>(t)
callback.onResponse(this@NetworkResultCall, Response.success(networkResult))
}
})
}
override fun execute(): Response<NetworkResult<T>> = throw NotImplementedError()
override fun clone(): Call<NetworkResult<T>> = NetworkResultCall(proxy.clone())
override fun request(): Request = proxy.request()
override fun timeout(): Timeout = proxy.timeout()
override fun isExecuted(): Boolean = proxy.isExecuted
override fun isCanceled(): Boolean = proxy.isCanceled
override fun cancel() { proxy.cancel() }
}
@HeQuanLi
Copy link

HeQuanLi commented Aug 2, 2022

The onResponse method gets an error because the handleApi method is suspend

@ashgautamDev
Copy link

How do we handle handleApi suspend method inside onResponse method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment