-
-
Save AdamMc331/503b5c0686197f585047fba99360c1dd to your computer and use it in GitHub Desktop.
In this example I have a mapping method that takes in a Response<> from retrofit, and maps it to an `AlbumsView` state if successful, and otherwise throws an error.
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 mapFacebookAlbumsResponse(response: Response<FacebookAllAlbumsResponse>): SocialPhotoUploadState { | |
return if (response.isSuccessful) { | |
mapFacebookAlbumsSuccess(response) | |
} else { | |
mapFacebookAlbumsError(response) | |
} | |
} | |
private fun mapFacebookAlbumsError(response: Response<FacebookAllAlbumsResponse>): SocialPhotoUploadState { | |
if (response.code() == HttpURLConnection.HTTP_BAD_REQUEST) { | |
val error = SocialAuthenticationError("Need FB permission.") | |
throw error | |
} else { | |
throw Throwable("Error fetching albums") | |
} | |
} | |
private fun mapFacebookAlbumsSuccess(response: Response<FacebookAllAlbumsResponse>): SocialPhotoUploadState { | |
// We should only concern ourselves with albums that have photos. | |
val albums = response.body()?.data?.filter { it.count > 0 } ?: ArrayList() | |
return if (albums.isEmpty()) { | |
throw Throwable("Empty albums error.") | |
} else { | |
SocialPhotoUploadState.AlbumsView(albums, Constants.SOCIAL_PLATFORM_FACEBOOK, "Your FacebookAlbums") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example unit test: