Skip to content

Instantly share code, notes, and snippets.

@kilink
Created February 11, 2017 07:51
Show Gist options
  • Save kilink/3bb7d52050492831b618931fc9b786a8 to your computer and use it in GitHub Desktop.
Save kilink/3bb7d52050492831b618931fc9b786a8 to your computer and use it in GitHub Desktop.
import java.io.IOException
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import rx.Single
fun Call.toSingle(): Single<Response> =
Single.create { subscriber ->
enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
if (!subscriber.isUnsubscribed) {
subscriber.onSuccess(response)
}
}
override fun onFailure(call: Call, e: IOException) {
if (!subscriber.isUnsubscribed) {
subscriber.onError(e)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment