|
| 1 | +package se.michaelthelin.spotify.requests.data.playlists; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 4 | +import org.apache.hc.core5.http.ContentType; |
| 5 | +import org.apache.hc.core5.http.ParseException; |
| 6 | +import se.michaelthelin.spotify.exceptions.SpotifyWebApiException; |
| 7 | +import se.michaelthelin.spotify.model_objects.specification.Playlist; |
| 8 | +import se.michaelthelin.spotify.requests.data.AbstractDataRequest; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Create a playlist for the current Spotify user. (The playlist will be empty until you add tracks.) |
| 14 | + * Each user is generally limited to a maximum of 11000 playlists. |
| 15 | + */ |
| 16 | +@JsonDeserialize(builder = CreatePlaylistRequest.Builder.class) |
| 17 | +public class CreatePlaylistRequest extends AbstractDataRequest<Playlist> { |
| 18 | + |
| 19 | + /** |
| 20 | + * The private {@link CreatePlaylistRequest} constructor. |
| 21 | + * |
| 22 | + * @param builder A {@link CreatePlaylistRequest.Builder}. |
| 23 | + */ |
| 24 | + private CreatePlaylistRequest(final Builder builder) { |
| 25 | + super(builder); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Create a new playlist. |
| 30 | + * |
| 31 | + * @return The newly created {@link Playlist}. |
| 32 | + * @throws IOException In case of networking issues. |
| 33 | + * @throws SpotifyWebApiException The Web API returned an error further specified in this exception's root cause. |
| 34 | + */ |
| 35 | + public Playlist execute() throws |
| 36 | + IOException, |
| 37 | + SpotifyWebApiException, |
| 38 | + ParseException { |
| 39 | + return new Playlist.JsonUtil().createModelObject(postJson()); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Builder class for building a {@link CreatePlaylistRequest}. |
| 44 | + */ |
| 45 | + public static final class Builder extends AbstractDataRequest.Builder<Playlist, Builder> { |
| 46 | + |
| 47 | + /** |
| 48 | + * Create a new {@link CreatePlaylistRequest.Builder}. |
| 49 | + * <p> |
| 50 | + * Creating a public playlist for a user requires authorization of the {@code playlist-modify-public} |
| 51 | + * scope; creating a private playlist requires the {@code playlist-modify-private} scope. |
| 52 | + * |
| 53 | + * @param accessToken Required. A valid access token from the Spotify Accounts service. |
| 54 | + * @see <a href="https://developer.spotify.com/documentation/web-api/concepts/scopes">Spotify: Using Scopes</a> |
| 55 | + */ |
| 56 | + public Builder(final String accessToken) { |
| 57 | + super(accessToken); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * The playlist name setter. |
| 62 | + * |
| 63 | + * @param name Required. The name for the new playlist, for example {@code "Your Coolest Playlist"}. |
| 64 | + * This name does not need to be unique; a user may have several playlists with the same name. |
| 65 | + * @return A {@link CreatePlaylistRequest.Builder}. |
| 66 | + */ |
| 67 | + public Builder name(final String name) { |
| 68 | + assert (name != null); |
| 69 | + assert (!name.isEmpty()); |
| 70 | + return setBodyParameter("name", name); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * The public status setter. |
| 75 | + * |
| 76 | + * @param public_ Optional. Defaults to {@code true}. If {@code true} the playlist will be public, if |
| 77 | + * {@code false} it will be private. To be able to create private playlists, the user must |
| 78 | + * have granted the {@code playlist-modify-private} scope. |
| 79 | + * @return A {@link CreatePlaylistRequest.Builder}. |
| 80 | + */ |
| 81 | + public Builder public_(final Boolean public_) { |
| 82 | + return setBodyParameter("public", public_); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * The collaborative state setter. |
| 87 | + * |
| 88 | + * @param collaborative Optional. Defaults to {@code false}. If {@code true} the playlist will be collaborative. |
| 89 | + * <b>Note:</b> To create a collaborative playlist you must also set {@code public} to |
| 90 | + * {@code false}. To create collaborative playlists you must have granted |
| 91 | + * {@code playlist-modify-private} and {@code playlist-modify-public} scopes. |
| 92 | + * @return A {@link CreatePlaylistRequest.Builder}. |
| 93 | + */ |
| 94 | + public Builder collaborative(final Boolean collaborative) { |
| 95 | + return setBodyParameter("collaborative", collaborative); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * The playlist description setter. |
| 100 | + * |
| 101 | + * @param description Optional. Value for playlist description as displayed in Spotify Clients and in the Web API. |
| 102 | + * @return A {@link CreatePlaylistRequest.Builder}. |
| 103 | + */ |
| 104 | + public Builder description(final String description) { |
| 105 | + assert (description != null); |
| 106 | + assert (!description.isEmpty()); |
| 107 | + return setBodyParameter("description", description); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * The request build method. |
| 112 | + * |
| 113 | + * @return A custom {@link CreatePlaylistRequest}. |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public CreatePlaylistRequest build() { |
| 117 | + setContentType(ContentType.APPLICATION_JSON); |
| 118 | + setPath("/v1/me/playlists"); |
| 119 | + return new CreatePlaylistRequest(this); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected Builder self() { |
| 124 | + return this; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments