Created
February 11, 2017 07:26
-
-
Save kilink/18333e6b262189df30b9d37713f61e31 to your computer and use it in GitHub Desktop.
OkHttp Java 8 CompletableFuture extension method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import okhttp3.Call | |
import okhttp3.Callback | |
import okhttp3.Response | |
import java.io.IOException | |
import java.util.concurrent.CompletableFuture | |
fun Call.executeAsync(): CompletableFuture<Response> { | |
val future = CompletableFuture<Response>() | |
enqueue(object : Callback { | |
override fun onResponse(call: Call, response: Response) { | |
future.complete(response) | |
} | |
override fun onFailure(call: Call, e: IOException) { | |
future.completeExceptionally(e) | |
} | |
}) | |
return future | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment