From cd16fa94e428391963c7d7e2592b9423c3437d8c Mon Sep 17 00:00:00 2001 From: Wolftein Date: Mon, 5 Jan 2015 16:33:26 -0300 Subject: [PATCH] Added Snapshot class. Added missing request (RemoveTracksFromPlaylist request is still missing). --- .../spotify/webapi/android/ClientRestAPI.java | 32 +++++++++++ .../webapi/android/model/Snapshot.java | 33 +++++++++++ .../android/request/AbstractRequest.java | 15 ++++- .../request/PlaylistAddTracksRequest.java | 51 +++++++++++++++++ .../request/PlaylistChangeDetailsRequest.java | 54 ++++++++++++++++++ .../request/PlaylistCreateRequest.java | 56 +++++++++++++++++++ .../request/PlaylistReplaceTracksRequest.java | 50 +++++++++++++++++ 7 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 library/src/main/java/me/siegenthaler/spotify/webapi/android/model/Snapshot.java create mode 100644 library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistAddTracksRequest.java create mode 100644 library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistChangeDetailsRequest.java create mode 100644 library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistCreateRequest.java create mode 100644 library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistReplaceTracksRequest.java diff --git a/library/src/main/java/me/siegenthaler/spotify/webapi/android/ClientRestAPI.java b/library/src/main/java/me/siegenthaler/spotify/webapi/android/ClientRestAPI.java index ac8c6e0..788099a 100644 --- a/library/src/main/java/me/siegenthaler/spotify/webapi/android/ClientRestAPI.java +++ b/library/src/main/java/me/siegenthaler/spotify/webapi/android/ClientRestAPI.java @@ -29,7 +29,11 @@ import me.siegenthaler.spotify.webapi.android.request.ArtistTopTrackRequest; import me.siegenthaler.spotify.webapi.android.request.AuthoriseClientFlowRequest; import me.siegenthaler.spotify.webapi.android.request.AuthoriseRefreshRequest; +import me.siegenthaler.spotify.webapi.android.request.PlaylistAddTracksRequest; +import me.siegenthaler.spotify.webapi.android.request.PlaylistChangeDetailsRequest; +import me.siegenthaler.spotify.webapi.android.request.PlaylistCreateRequest; import me.siegenthaler.spotify.webapi.android.request.PlaylistListRequest; +import me.siegenthaler.spotify.webapi.android.request.PlaylistReplaceTracksRequest; import me.siegenthaler.spotify.webapi.android.request.PlaylistRequest; import me.siegenthaler.spotify.webapi.android.request.PlaylistTrackRequest; import me.siegenthaler.spotify.webapi.android.request.SearchAlbumRequest; @@ -237,6 +241,34 @@ public FollowCheckRequest isFollowing(String type, String... ids) { return addDefaultHeader(new FollowCheckRequest().setType(type).setIds(ids)); } + /** + * (non-doc) + */ + public PlaylistAddTracksRequest addTrackToPlaylist(String user, String playlist) { + return addDefaultHeader(new PlaylistAddTracksRequest().setPlaylist(user, playlist)); + } + + /** + * (non-doc) + */ + public PlaylistChangeDetailsRequest changePlaylistDetail(String user, String playlist) { + return addDefaultHeader(new PlaylistChangeDetailsRequest().setPlaylist(user, playlist)); + } + + /** + * (non-doc) + */ + public PlaylistCreateRequest createPlaylist(String user, String playlist) { + return addDefaultHeader(new PlaylistCreateRequest().setUser(user).serName(playlist)); + } + + /** + * (non-doc) + */ + public PlaylistReplaceTracksRequest replacePlaylistTracks(String user, String playlist) { + return addDefaultHeader(new PlaylistReplaceTracksRequest().setPlaylist(user, playlist)); + } + /** * (non-doc) */ diff --git a/library/src/main/java/me/siegenthaler/spotify/webapi/android/model/Snapshot.java b/library/src/main/java/me/siegenthaler/spotify/webapi/android/model/Snapshot.java new file mode 100644 index 0000000..a00f9ba --- /dev/null +++ b/library/src/main/java/me/siegenthaler/spotify/webapi/android/model/Snapshot.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2014 Siegenthaler Solutions. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.siegenthaler.spotify.webapi.android.model; + +import com.google.gson.annotations.SerializedName; + +/** + * (non-doc) + */ +public class Snapshot { + @SerializedName("snapshot_id") + private String mId; + + /** + * (non-doc) + */ + final public String getId() { + return mId; + } +} diff --git a/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/AbstractRequest.java b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/AbstractRequest.java index d867172..2ce2494 100644 --- a/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/AbstractRequest.java +++ b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/AbstractRequest.java @@ -46,6 +46,7 @@ public final class AbstractRequest extends Request { private final Type mType; private final String mParent; private final Response.Listener mListener; + private final boolean mJsonBody; /** * (non-doc) @@ -57,6 +58,7 @@ public AbstractRequest(Builder builder) { this.mHeaders = builder.mHeaders; this.mParent = builder.mParent; this.mListener = builder.mListener; + this.mJsonBody = builder.mJsonBody; setTag(builder.mIdentifier); } @@ -65,7 +67,9 @@ public AbstractRequest(Builder builder) { */ @Override final public String getBodyContentType() { - return "application/x-www-form-urlencoded; charset=UTF-8"; + return mJsonBody + ? "application/json; charset=UTF-8" + : "application/x-www-form-urlencoded; charset=UTF-8"; } /** @@ -130,6 +134,7 @@ public static class Builder { protected Response.Listener mListener; protected String mIdentifier; protected RequestQueue mClient; + protected boolean mJsonBody; /** * (non-doc) @@ -154,6 +159,14 @@ public Builder(Type type, int method, String parent) { this.mParent = parent; } + /** + * (non-doc) + */ + final protected J setJsonBody(boolean isJsonBody) { + mJsonBody = isJsonBody; + return (J) this; + } + /** * (non-doc) */ diff --git a/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistAddTracksRequest.java b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistAddTracksRequest.java new file mode 100644 index 0000000..2415cdd --- /dev/null +++ b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistAddTracksRequest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2014 Siegenthaler Solutions. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.siegenthaler.spotify.webapi.android.request; + +import android.text.TextUtils; + +import com.android.volley.Request; + +import java.util.Locale; + +import me.siegenthaler.spotify.webapi.android.model.Snapshot; + +/** + * (non-doc) + */ +public class PlaylistAddTracksRequest extends AbstractRequest.Builder { + /** + * (non-doc) + */ + public PlaylistAddTracksRequest() { + super(Snapshot.class, Request.Method.POST); + } + + /** + * (non-doc) + */ + final public PlaylistAddTracksRequest setPlaylist(String user, String playlist) { + return setPath(String.format(Locale.ENGLISH, "/v1/users/%s/playlists/%s/tracks", user, playlist)); + } + + /** + * (non-doc) + */ + final public PlaylistAddTracksRequest setTracks(int position, String... ids) { + addParameter("position", String.valueOf(position)); + return addParameter("uris", TextUtils.join(",", ids)); + } +} diff --git a/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistChangeDetailsRequest.java b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistChangeDetailsRequest.java new file mode 100644 index 0000000..38b26d0 --- /dev/null +++ b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistChangeDetailsRequest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2014 Siegenthaler Solutions. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.siegenthaler.spotify.webapi.android.request; + +import com.android.volley.Request; + +import java.util.Locale; + +/** + * (non-doc) + */ +public class PlaylistChangeDetailsRequest extends AbstractRequest.Builder { + /** + * (non-doc) + */ + public PlaylistChangeDetailsRequest() { + super(Void.class, Request.Method.PUT); + setJsonBody(true); + } + + /** + * (non-doc) + */ + final public PlaylistChangeDetailsRequest setPlaylist(String user, String playlist) { + return setPath(String.format(Locale.ENGLISH, "/v1/users/%s/playlists/%s", user, playlist)); + } + + /** + * (non-doc) + */ + final public PlaylistChangeDetailsRequest serName(String name) { + return addParameter("name", name); + } + + /** + * (non-doc) + */ + final public PlaylistChangeDetailsRequest setPublic(boolean isPublic) { + return addParameter("public", isPublic ? "true" : "false"); + } +} diff --git a/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistCreateRequest.java b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistCreateRequest.java new file mode 100644 index 0000000..ec4dee4 --- /dev/null +++ b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistCreateRequest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2014 Siegenthaler Solutions. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.siegenthaler.spotify.webapi.android.request; + +import com.android.volley.Request; + +import java.util.Locale; + +import me.siegenthaler.spotify.webapi.android.model.Playlist; + +/** + * (non-doc) + */ +public class PlaylistCreateRequest extends AbstractRequest.Builder { + /** + * (non-doc) + */ + public PlaylistCreateRequest() { + super(Playlist.class, Request.Method.POST); + setJsonBody(true); + } + + /** + * (non-doc) + */ + final public PlaylistCreateRequest setUser(String user) { + return setPath(String.format(Locale.ENGLISH, "/v1/users/%s/playlists", user)); + } + + /** + * (non-doc) + */ + final public PlaylistCreateRequest serName(String name) { + return addParameter("name", name); + } + + /** + * (non-doc) + */ + final public PlaylistCreateRequest setPublic(boolean isPublic) { + return addParameter("public", isPublic ? "true" : "false"); + } +} diff --git a/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistReplaceTracksRequest.java b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistReplaceTracksRequest.java new file mode 100644 index 0000000..50f9364 --- /dev/null +++ b/library/src/main/java/me/siegenthaler/spotify/webapi/android/request/PlaylistReplaceTracksRequest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2014 Siegenthaler Solutions. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.siegenthaler.spotify.webapi.android.request; + +import android.text.TextUtils; + +import com.android.volley.Request; + +import java.util.Locale; + +import me.siegenthaler.spotify.webapi.android.model.Snapshot; + +/** + * (non-doc) + */ +public class PlaylistReplaceTracksRequest extends AbstractRequest.Builder { + /** + * (non-doc) + */ + public PlaylistReplaceTracksRequest() { + super(Snapshot.class, Request.Method.PUT); + } + + /** + * (non-doc) + */ + final public PlaylistReplaceTracksRequest setPlaylist(String user, String playlist) { + return setPath(String.format(Locale.ENGLISH, "/v1/users/%s/playlists/%s/tracks", user, playlist)); + } + + /** + * (non-doc) + */ + final public PlaylistReplaceTracksRequest setTracks(String... ids) { + return addParameter("uris", TextUtils.join(",", ids)); + } +}