Skip to content

Instantly share code, notes, and snippets.

@erikjhordan-rey
Last active November 23, 2017 17:25
Show Gist options
  • Save erikjhordan-rey/fabfc7e4227f836050223f541e76ccdb to your computer and use it in GitHub Desktop.
Save erikjhordan-rey/fabfc7e4227f836050223f541e76ccdb to your computer and use it in GitHub Desktop.
This class is an extension to facilitate the use of Retrofit callback responses .
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
fun <T> Call<T>.execute(success: (response: Response<T>) -> Unit,
failure: (t: Throwable) -> Unit) {
enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>?, response: Response<T>) = success(response)
override fun onFailure(call: Call<T>?, t: Throwable) = failure(t)
})
}
@erikjhordan-rey
Copy link
Author

Usage it!

        service.something().execute({ response ->
            when (response.isSuccessful) {
                true -> {
                    // SUCCESS RESPONSE 
                }
                false -> {
                     // SUCCESS RESPONSE WITH AN ERROR
                }
            }
        }, { throwable -> throwable.printStackTrace() })  

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