All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lajospolya.spotifyapiwrapper.request.GetRecommendations Maven / Gradle / Ivy

Go to download

This project wraps the Spotify public API in order to allow users to intuitively use it

There is a newer version: 3.0.RELEASE
Show newest version
package com.lajospolya.spotifyapiwrapper.request;

import com.lajospolya.spotifyapiwrapper.enumeration.TuneableTrackAttributeFactory;
import com.lajospolya.spotifyapiwrapper.response.Recommendation;

import java.net.http.HttpRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lajos Polya
 *
 * Represents the endpoint at GET https://api.spotify.com/v1/recommendations as descrbibed at
 * https://developer.spotify.com/documentation/web-api/reference-beta/
 */
public class GetRecommendations extends AbstractSpotifyRequest
{
    private static final String REQUEST_URI_STRING = SPOTIFY_V1_API_URI +  "recommendations";

    private GetRecommendations(HttpRequest.Builder requestBuilder)
    {
        super(requestBuilder);
    }

    public static class Builder extends AbstractBuilder
    {
        private List seed_artists;
        private List seed_tracks;
        private List seed_genres;

        private Integer limit;
        private String market;

        private Map tuneableAttributes = new HashMap<>();


        public Builder(List seed_artists, List seed_tracks, List seed_genres) throws IllegalArgumentException
        {
            spotifyRequestParamValidationService.validateParametersNotNull(seed_artists, seed_tracks, seed_genres);
            this.seed_artists = seed_artists;
            this.seed_tracks = seed_tracks;
            this.seed_genres = seed_genres;
        }

        @Override
        public GetRecommendations build()
        {
            SpotifyRequestBuilder spotifyRequestBuilder = new SpotifyRequestBuilder(REQUEST_URI_STRING);
            spotifyRequestBuilder.queryParam(SEED_ARTISTS_QUERY_PARAM, seed_artists);
            spotifyRequestBuilder.queryParam(SEED_TRACKS_QUERY_PARAM, seed_tracks);
            spotifyRequestBuilder.queryParam(SEED_GENRES_QUERY_PARAM, seed_genres);
            addOptionalQueryParams(spotifyRequestBuilder);

            return new GetRecommendations(spotifyRequestBuilder.createGetRequests());
        }

        private void addOptionalQueryParams(SpotifyRequestBuilder requestUriBuilder)
        {
            if(market != null)
            {
                requestUriBuilder.queryParam(MARKET_QUERY_PARAM, market);
            }
            if(limit != null)
            {
                requestUriBuilder.queryParam(LIMIT_QUERY_PARAM, limit);
            }

            requestUriBuilder.queryParam(tuneableAttributes);
        }

        public Builder limit(Integer limit)
        {
            spotifyRequestParamValidationService.validateLimit100(limit);
            this.limit = limit;
            return this;
        }

        public Builder market(String market)
        {
            this.market = market;
            return this;
        }

        public  Builder min(TuneableTrackAttributeFactory.AbstractTuneableTrackAttribute tuneableAttribute, T value)
        {
            tuneableAttribute.validate(value);
            tuneableAttributes.put(MIN_PARAM_PREFIX + tuneableAttribute.name(), value);
            return this;
        }

        public  Builder max(TuneableTrackAttributeFactory.AbstractTuneableTrackAttribute tuneableAttribute, T value)
        {
            tuneableAttribute.validate(value);
            tuneableAttributes.put(MAX_PARAM_PREFIX + tuneableAttribute.name(), value);
            return this;
        }

        public  Builder target(TuneableTrackAttributeFactory.AbstractTuneableTrackAttribute tuneableAttribute, T value)
        {
            tuneableAttribute.validate(value);
            tuneableAttributes.put(TARGET_PARAM_PREFIX + tuneableAttribute.name(), value);
            return this;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy