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

se.michaelthelin.spotify.requests.data.playlists.CreatePlaylistRequest Maven / Gradle / Ivy

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

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import se.michaelthelin.spotify.exceptions.SpotifyWebApiException;
import se.michaelthelin.spotify.model_objects.specification.Playlist;
import se.michaelthelin.spotify.requests.data.AbstractDataRequest;

import java.io.IOException;

/**
 * Create a playlist for a Spotify user. (The playlist will be empty until you add tracks
 * with an {@link AddItemsToPlaylistRequest}.)
 */
@JsonDeserialize(builder = CreatePlaylistRequest.Builder.class)
public class CreatePlaylistRequest extends AbstractDataRequest {

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

  /**
   * Create a new playlist.
   *
   * @return The newly created {@link Playlist}.
   * @throws IOException            In case of networking issues.
   * @throws SpotifyWebApiException The Web API returned an error further specified in this exception's root cause.
   */
  public Playlist execute() throws
    IOException,
    SpotifyWebApiException,
    ParseException {
    return new Playlist.JsonUtil().createModelObject(postJson());
  }

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

    /**
     * Create a new {@link CreatePlaylistRequest.Builder}.
     * 

* Creating a public playlists requires authorization of the {@code playlist-modify-public} * scope; Creating a private playlist requires the {@code playlist-modify-private} scope. * * @param accessToken Required. A valid access token from the Spotify Accounts service. * @see Spotify: Using Scopes */ public Builder(final String accessToken) { super(accessToken); } /** * The user ID setter. * * @param user_id The user's Spotify user ID. * @return A {@link CreatePlaylistRequest.Builder}. * @see Spotify: URIs & IDs */ public Builder user_id(final String user_id) { assert (user_id != null); assert (!user_id.equals("")); return setPathParameter("user_id", user_id); } /** * The playlist name setter. * * @param name Optional. The name for the playlist. This name does not need * to be unique; a user may have several playlists with the same name. * @return A {@link CreatePlaylistRequest.Builder}. */ public Builder name(final String name) { assert (name != null); assert (!name.equals("")); return setBodyParameter("name", name); } /** * The public status setter. *

* Note: To be able to create private playlists, the user must have * granted the {@code playlist-modify-private} scope. * * @param public_ Optional. If {@code true} the playlist will be public, if {@code false} it will be private. * @return A {@link CreatePlaylistRequest.Builder}. */ public Builder public_(final Boolean public_) { return setBodyParameter("public", public_); } /** * The collaborative state setter. * * @param collaborative Optional, default {@code false}. If {@code true} the playlist will be collaborative. * Note: To create a collaborative playlist you must also set {@link #public_(Boolean)} * to {@code false}. To create collaborative playlists you must have granted * {@code playlist-modify-private} and {@code playlist-modify-public} scopes. * @return A {@link CreatePlaylistRequest.Builder}. */ public Builder collaborative(final Boolean collaborative) { return setBodyParameter("collaborative", collaborative); } /** * The playlist description setter. * * @param description Optional, value for playlist description as displayed in Spotify Clients and in the Web API. * @return A {@link CreatePlaylistRequest.Builder}. */ public Builder description(final String description) { assert (description != null); assert (!description.equals("")); return setBodyParameter("description", description); } /** * the request build method. * * @return A custom {@link CreatePlaylistRequest}. */ @Override public CreatePlaylistRequest build() { setContentType(ContentType.APPLICATION_JSON); setPath("/v1/users/{user_id}/playlists"); return new CreatePlaylistRequest(this); } @Override protected Builder self() { return this; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy