Search in sources :

Example 1 with Track

use of com.spotify.apollo.example.data.Track 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 Track

use of com.spotify.apollo.example.data.Track in project apollo by spotify.

the class ArtistResource method getArtistTopTracks.

CompletionStage<Response<ArrayList<Track>>> getArtistTopTracks(RequestContext requestContext) {
    // Validate request
    Optional<String> query = requestContext.request().parameter("q");
    if (!query.isPresent()) {
        return CompletableFuture.completedFuture(Response.forStatus(Status.BAD_REQUEST.withReasonPhrase("No search query")));
    }
    String country = requestContext.pathArgs().get("country");
    // We need to first query the search API, parse the result, then query top-tracks.
    Request searchRequest = Request.forUri(SEARCH_API + "?type=artist&q=" + query.get());
    Client client = requestContext.requestScopedClient();
    return client.send(searchRequest).thenComposeAsync(response -> {
        String topArtistId = parseFirstArtistId(response.payload().get().utf8());
        Request topTracksRequest = Request.forUri(String.format("%s/%s/top-tracks?country=%s", ARTIST_API, topArtistId, country));
        return client.send(topTracksRequest);
    }).thenApplyAsync(response -> Response.ok().withPayload(parseTopTracks(response.payload().get().utf8()))).exceptionally(throwable -> Response.forStatus(Status.INTERNAL_SERVER_ERROR.withReasonPhrase("Something failed")));
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) Route(com.spotify.apollo.route.Route) ArrayList(java.util.ArrayList) Track(com.spotify.apollo.example.data.Track) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) ByteString(okio.ByteString) Artist(com.spotify.apollo.example.data.Artist) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) Client(com.spotify.apollo.Client) Status(com.spotify.apollo.Status) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Client(com.spotify.apollo.Client)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Album (com.spotify.apollo.example.data.Album)2 Artist (com.spotify.apollo.example.data.Artist)2 Track (com.spotify.apollo.example.data.Track)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 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 Status (com.spotify.apollo.Status)1 AsyncHandler (com.spotify.apollo.route.AsyncHandler)1 JsonSerializerMiddlewares (com.spotify.apollo.route.JsonSerializerMiddlewares)1 Route (com.spotify.apollo.route.Route)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1