use of com.spotify.apollo.example.data.Album in project apollo by spotify.
the class ArtistResource method parseTopTracks.
/**
* Parses an artist top tracks response from the
* <a href="https://developer.spotify.com/web-api/get-artists-top-tracks/">Spotify API</a>
*
* @param json The json response
* @return A list of top tracks
*/
private ArrayList<Track> parseTopTracks(String json) {
ArrayList<Track> tracks = new ArrayList<>();
try {
JsonNode jsonNode = this.objectMapper.readTree(json);
for (JsonNode trackNode : jsonNode.get("tracks")) {
JsonNode albumNode = trackNode.get("album");
String albumName = albumNode.get("name").asText();
String artistName = trackNode.get("artists").get(0).get("name").asText();
String trackName = trackNode.get("name").asText();
tracks.add(new Track(trackName, new Album(albumName, new Artist(artistName))));
}
} catch (IOException e) {
throw new RuntimeException("Failed to parse JSON", e);
}
return tracks;
}
use of com.spotify.apollo.example.data.Album in project apollo by spotify.
the class AlbumResource method getAlbums.
CompletionStage<Response<ArrayList<Album>>> getAlbums(RequestContext requestContext, String tag) {
// We need to first query the search API, parse the result, then query the album API.
Request searchRequest = Request.forUri(SEARCH_API + "?type=album&q=tag%3A" + tag);
Client client = requestContext.requestScopedClient();
return client.send(searchRequest).thenComposeAsync(response -> {
String ids = parseResponseAlbumIds(response.payload().get().utf8());
return client.send(Request.forUri(ALBUM_API + "?ids=" + ids));
}).thenApplyAsync(response -> Response.ok().withPayload(parseAlbumData(response.payload().get().utf8())));
}
use of com.spotify.apollo.example.data.Album in project apollo by spotify.
the class AlbumResource method parseAlbumData.
/**
* Parses an album response from a
* <a href="https://developer.spotify.com/web-api/album-endpoints/">Spotify API album query</a>.
*
* @param json The json response
* @return A list of albums with artist information
*/
private ArrayList<Album> parseAlbumData(String json) {
ArrayList<Album> albums = new ArrayList<>();
try {
JsonNode jsonNode = this.objectMapper.readTree(json);
for (JsonNode albumNode : jsonNode.get("albums")) {
JsonNode artistsNode = albumNode.get("artists");
// Exclude albums with 0 artists
if (artistsNode.size() >= 1) {
// Only keeping the first artist for simplicity
Artist artist = new Artist(artistsNode.get(0).get("name").asText());
Album album = new Album(albumNode.get("name").asText(), artist);
albums.add(album);
}
}
} catch (IOException e) {
throw new RuntimeException("Failed to parse JSON", e);
}
return albums;
}
Aggregations