Created
December 28, 2017 23:47
-
-
Save Danil0v3s/16b076db9614232adb7d42adc971166f 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
fun <T> callback(success: (Response<T>) -> Unit, failure: (t: Throwable) -> Unit): Callback<T>? { | |
return object : Callback<T> { | |
override fun onResponse(call: Call<T>, response: retrofit2.Response<T>) = success(response) | |
override fun onFailure(call: Call<T>, t: Throwable) = failure(t) | |
} | |
} | |
fun parseResponse(listener: (AppResponse?, Throwable?) -> Unit): Callback<AppResponse>? { | |
return callback( | |
{ response -> if (response.body() != null) listener(response.body(), null) else listener(null, Throwable(response.errorBody().toString())) }, | |
{ throwable -> listener(null, throwable) } | |
) | |
} | |
fun saveCheckIn(checkIn: CheckIn, listener: (AppResponse?, Throwable?) -> Unit) = checkInService.saveCheckIn(checkIn).enqueue(parseResponse(listener)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment