Search in sources :

Example 1 with Album

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;
}
Also used : Artist(com.spotify.apollo.example.data.Artist) ArrayList(java.util.ArrayList) Album(com.spotify.apollo.example.data.Album) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteString(okio.ByteString) IOException(java.io.IOException) Track(com.spotify.apollo.example.data.Track)

Example 2 with Album

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())));
}
Also used : Response(com.spotify.apollo.Response) AsyncHandler(com.spotify.apollo.route.AsyncHandler) Request(com.spotify.apollo.Request) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) RequestContext(com.spotify.apollo.RequestContext) Album(com.spotify.apollo.example.data.Album) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonSerializerMiddlewares(com.spotify.apollo.route.JsonSerializerMiddlewares) IOException(java.io.IOException) Route(com.spotify.apollo.route.Route) ArrayList(java.util.ArrayList) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) StringJoiner(java.util.StringJoiner) ByteString(okio.ByteString) Artist(com.spotify.apollo.example.data.Artist) JsonNode(com.fasterxml.jackson.databind.JsonNode) Client(com.spotify.apollo.Client) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Client(com.spotify.apollo.Client)

Example 3 with Album

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;
}
Also used : Artist(com.spotify.apollo.example.data.Artist) ArrayList(java.util.ArrayList) Album(com.spotify.apollo.example.data.Album) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Album (com.spotify.apollo.example.data.Album)3 Artist (com.spotify.apollo.example.data.Artist)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ByteString (okio.ByteString)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 Client (com.spotify.apollo.Client)1 Request (com.spotify.apollo.Request)1 RequestContext (com.spotify.apollo.RequestContext)1 Response (com.spotify.apollo.Response)1 Track (com.spotify.apollo.example.data.Track)1 AsyncHandler (com.spotify.apollo.route.AsyncHandler)1 JsonSerializerMiddlewares (com.spotify.apollo.route.JsonSerializerMiddlewares)1 Route (com.spotify.apollo.route.Route)1 StringJoiner (java.util.StringJoiner)1 CompletionStage (java.util.concurrent.CompletionStage)1 Stream (java.util.stream.Stream)1