I have a problem and I don't know how to resolve it with a better aproach. The problem is that I'm requesting to Spotify Web API and in some methods the artist image is returned and in others only basic artist info is obtained.
I have this two methods:
fun getAlbum(albumId: String): Single<SpotifyAlbumDTO>
fun getArtist(artistId: String): Single<SpotifyArtistDTO>
When I get an album, the artist info doesn't contains the artist image url. So, I need to call the getAlbum() method and use the result to obtain the artistId and then call to getArtist() method.
I have the following method to do all this stuff:
fun getAlbum(albumId: String): Single<Album>
On this method I need to call the previous two to return an Album object (my domain object). The only solution that worked for me it's the following:
fun getAlbum(albumId: String): Single<Album> {
return Single.create { emitter ->
_spotifyService.getAlbum(albumId).subscribe { spotifyAlbum ->
_spotifyService.getArtist(spotifyAlbum.artist.id).subscribe { spotifyArtist ->
val artistImage = spotifyArtist.imageUrl
spotifyAlbum.artist.image = artistImage
emitter.onNext(spotifyAlbum.toAlbum())
}
}
}
}
I think that must exist another better way to do this than concating subscribe calls in other subscribes and creating deeper calls. I also try the following:
_spotifyService.getAlbum(albumId).flatMap { spotifyAlbum ->
_spotifyService.getArtist(spotifyAlbum.artist.id)
}.flatMap { spotifyArtist ->
// Here I don't have the album and I can't to asign the image
}