Skip to content

Instantly share code, notes, and snippets.

@Danil0v3s
Created December 28, 2017 23:47
Show Gist options
  • Save Danil0v3s/16b076db9614232adb7d42adc971166f to your computer and use it in GitHub Desktop.
Save Danil0v3s/16b076db9614232adb7d42adc971166f to your computer and use it in GitHub Desktop.
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