Last active
January 23, 2019 22:35
-
-
Save naturalwarren/9535273faa8a629015083db09da54b78 to your computer and use it in GitHub Desktop.
Making a Retrofit API call with RxJava
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
authApi.getTokens() | |
.subscribe({ response: Response<AccessToken> -> | |
when { | |
response.isSuccessful && response.body() != null -> { | |
// Case 1: Success. We got a response with a body. | |
} | |
response.errorBody() != null -> { | |
// Case 2: Failure. We got an error from the backend, deserialize it. | |
val error = moshi.adapter(Error::class.java).fromJson(errorBody.source()) | |
} | |
else -> { | |
// Case 3: Failure. Response didn't have a body. Show a vanilla error. | |
} | |
} | |
}, { t -> | |
// Case 4: Failure. Network error occurred, notify the user of connection troubles. | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment