Created
August 13, 2018 00:43
-
-
Save jeziellago/3a439932890d79df06448d84a26625ec to your computer and use it in GitHub Desktop.
This file contains 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 kotlinx.coroutines.experimental.CommonPool | |
import kotlinx.coroutines.experimental.android.UI | |
import kotlinx.coroutines.experimental.launch | |
import kotlinx.coroutines.experimental.withContext | |
import retrofit2.Call | |
import retrofit2.Retrofit | |
... | |
class Api { | |
... | |
fun <T> request(call: Call<T>, | |
onSuccess:(T?)-> Unit, | |
onFailure:(IOException)-> Unit) = launch(UI) { | |
try { | |
val result = withContext(CommonPool) { call.execute() } | |
onSuccess(result.body()) | |
} catch (exception: IOException) { | |
onFailure(exception) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment