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

se.michaelthelin.spotify.requests.data.search.SearchItemRequest Maven / Gradle / Ivy

There is a newer version: 9.0.0-RC1
Show newest version
package se.michaelthelin.spotify.requests.data.search;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.neovisionaries.i18n.CountryCode;
import org.apache.hc.core5.http.ParseException;
import se.michaelthelin.spotify.exceptions.SpotifyWebApiException;
import se.michaelthelin.spotify.model_objects.special.SearchResult;
import se.michaelthelin.spotify.requests.data.AbstractDataRequest;

import java.io.IOException;

/**
 * Get Spotify catalog information about artists, albums, episodes, playlists, shows or tracks that match a keyword string.
 */
@JsonDeserialize(builder = SearchItemRequest.Builder.class)
public class SearchItemRequest extends AbstractDataRequest {

  /**
   * The private {@link SearchItemRequest} constructor.
   *
   * @param builder A {@link SearchItemRequest.Builder}.
   */
  private SearchItemRequest(final Builder builder) {
    super(builder);
  }

  /**
   * Search for an item.
   *
   * @return A {@link SearchResult}.
   * @throws IOException            In case of networking issues.
   * @throws SpotifyWebApiException The Web API returned an error further specified in this exception's root cause.
   */
  public SearchResult execute() throws
    IOException,
    SpotifyWebApiException,
    ParseException {
    return new SearchResult.JsonUtil().createModelObject(getJson());
  }

  /**
   * Builder class for building a {@link SearchItemRequest}.
   */
  public static final class Builder extends AbstractDataRequest.Builder {

    /**
     * Create a new {@link SearchItemRequest.Builder}.
     *
     * @param accessToken Required. A valid access token from the Spotify Accounts service.
     */
    public Builder(final String accessToken) {
      super(accessToken);
    }

    /**
     * The search query setter.
     *
     * @param q Required. The search query's keywords (and optional field filters and operators).
     * @return A {@link SearchItemRequest.Builder}.
     * @see Spotify: Search Query Options
     */
    public Builder q(final String q) {
      assert (q != null);
      assert (!q.equals(""));
      return setQueryParameter("q", q);
    }

    /**
     * The type setter.
     *
     * @param type Required. A comma-separated list of item types to search across. Valid types are: {@code album},
     *             {@code artist}, {@code episode}, {@code playlist}, {@code show} and {@code track}.
     * @return A {@link SearchItemRequest.Builder}.
     */
    public Builder type(final String type) {
      assert (type != null);
      assert (type.matches("((^|,)(album|artist|episode|playlist|show|track))+$"));
      return setQueryParameter("type", type);
    }

    /**
     * The market country code setter.
     *
     * @param market Optional. An ISO 3166-1 alpha-2 country code. If a country code is given, only artists,
     *               albums, and tracks with content playable in that market will be returned. (Playlist
     *               results are not affected by the market parameter.)
     * @return A {@link SearchItemRequest.Builder}.
     * @see Wikipedia: ISO 3166-1 alpha-2 country codes
     */
    public Builder market(final CountryCode market) {
      assert (market != null);
      return setQueryParameter("market", market);
    }

    /**
     * The limit setter.
     *
     * @param limit Optional. The maximum number of results to return. Default: 20. Minimum: 1. Maximum: 50.
     * @return A {@link SearchItemRequest.Builder}.
     */
    public Builder limit(final Integer limit) {
      assert (limit != null);
      assert (1 <= limit && limit <= 50);
      return setQueryParameter("limit", limit);
    }

    /**
     * The offset setter.
     *
     * @param offset Optional. The index of the first result to return. Default: 0 (i.e., the first result). Maximum
     *               offset: 100.000. Use with {@link #limit(Integer)} to get the next page of search results.
     * @return A {@link SearchItemRequest.Builder}.
     */
    public Builder offset(final Integer offset) {
      assert (offset != null);
      assert (0 <= offset && offset <= 100000);
      return setQueryParameter("offset", offset);
    }

    /**
     * The include external setter.
     *
     * @param includeExternal Optional. Possible values: {@code audio}. If {@code audio} is set
     *                        the response will include any relevant audio content that is hosted externally.
     *                        By default external content is filtered out from responses.
     * @return A {@link SearchItemRequest.Builder}.
     */
    public Builder includeExternal(String includeExternal) {
      assert (includeExternal != null);
      assert (includeExternal.matches("audio"));
      return setQueryParameter("include_external", includeExternal);
    }

    /**
     * The request build method.
     *
     * @return A {@link SearchItemRequest.Builder}.
     */
    @Override
    public SearchItemRequest build() {
      setPath("/v1/search");
      return new SearchItemRequest(this);
    }

    @Override
    protected Builder self() {
      return this;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy