Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added "load all playlists" feature

  • Loading branch information...
commit d8a0d939fbf04314a5b4d5f1bb278ecfbae7ec15 1 parent a79cc1d
@xnickmx authored
View
2  google-play-client-tests/pom.xml
@@ -24,7 +24,7 @@
<groupId>com.faceture</groupId>
<artifactId>google-play-client-tests</artifactId>
- <version>1.0.2</version>
+ <version>1.0.3</version>
<dependencies>
<dependency>
View
14 google-play-client-tests/src/test/java/com/faceture/google/play/PlayClientIntegrationTest.java
@@ -18,6 +18,7 @@
package com.faceture.google.play;
+import com.faceture.google.play.domain.Playlist;
import com.faceture.google.play.domain.SearchResults;
import com.faceture.google.play.domain.Song;
import junit.framework.TestCase;
@@ -100,6 +101,19 @@ public void testEndToEnd() throws IOException, URISyntaxException {
Collection<Song> songs = playClient.loadAllTracks(playSession);
assertNotNull(songs);
assertTrue(songs.size() > 0);
+
+ // get all of the playlists
+ Collection<Playlist> playlists = playClient.loadAllPlaylists(playSession);
+
+ assertNotNull(playlists);
+ Playlist playlist = playlists.iterator().next();
+ Collection<Song> playlistSongs = playlist.getPlaylist();
+ assertNotNull(playlistSongs);
+ Song playlistSong = playlistSongs.iterator().next();
+ assertNotNull(playlistSong);
+ String title = playlistSong.getTitle();
+ assertNotNull(title);
+ assertFalse(title.isEmpty());
}
public void testLoginFailure() throws IOException, URISyntaxException {
View
2  google-play-client/pom.xml
@@ -24,7 +24,7 @@
<groupId>com.faceture</groupId>
<artifactId>google-play-client</artifactId>
- <version>1.0.2</version>
+ <version>1.0.3</version>
<dependencies>
<dependency>
View
1  google-play-client/src/main/java/com/faceture/google/play/Const.java
@@ -22,6 +22,7 @@
*/
public interface Const {
+ public static final String EMPTY_JSON = "{}";
public static final String GOOLE_LOGIN_AUTH = "Auth=";
public static final boolean USE_HTTPS = true;
View
1  google-play-client/src/main/java/com/faceture/google/play/Path.java
@@ -23,6 +23,7 @@
public interface Path {
public static final String GOOGLE_LOGIN = "/accounts/ClientLogin";
+ public static final String MUSIC_LOAD_PLAYLIST = "/music/services/loadplaylist";
public static final String MUSIC_LOAD_ALL_TRACKS = "/music/services/loadalltracks";
public static final String MUSIC_LOGIN = "/music/listen";
public static final String MUSIC_PLAY = "/music/play";
View
40 google-play-client/src/main/java/com/faceture/google/play/PlayClient.java
@@ -139,7 +139,7 @@ public LoginResponse login(String emailAddress, String password) throws IOExcept
// bad credentials
loginResponse = loginResponseFactory.create(LoginResult.BAD_CREDENTIALS, null);
- break;
+ break;
default:
// some other status code
@@ -298,4 +298,42 @@ public URI getPlayURI(String songId, PlaySession playSession) throws IOException
return songs;
}
+
+ public Collection<Playlist> loadAllPlaylists(PlaySession session) throws IOException, URISyntaxException {
+ if (null == session) {
+ throw new IllegalArgumentException("session is null");
+ }
+
+ // create the URL query params
+ Map<String, String> loadAllPlaylistsQueryParams = new HashMap<String, String>();
+ loadAllPlaylistsQueryParams.put(QueryParamConst.U_NAME, QueryParamConst.U_VALUE);
+ loadAllPlaylistsQueryParams.put(QueryParamConst.XT_NAME, session.getXtCookie());
+
+ // create the HTTP headers
+ Map<String, String> loadAllPlaylistsHeaders = new HashMap<String, String>();
+ String authHeader = googleUtil.createAuthHeaderValue(session.getAuthToken());
+ loadAllPlaylistsHeaders.put(HeaderName.AUTHORIZATION, authHeader);
+
+ // create the form
+ Map<String, String> form = new HashMap<String, String>();
+ form.put(FormFieldConst.JSON_NAME, Const.EMPTY_JSON);
+
+ // do the POST
+ RestResponse restResponse = restClient.doPost(Const.USE_HTTPS, HostName.PLAY, Path.MUSIC_LOAD_PLAYLIST,
+ loadAllPlaylistsQueryParams, loadAllPlaylistsHeaders, null, form);
+
+ if (restResponse.getStatusCode() != HttpStatus.SC_OK) {
+ throw new IllegalStateException("Bad status: " + restResponse.getStatusCode() + " response body: " +
+ restResponse.getBody());
+ }
+
+ // parse the response
+ String responseBody = restResponse.getBody();
+
+ LoadAllPlaylistsResponse loadAllPlaylistsResponse = gsonWrapper.fromJson(responseBody, LoadAllPlaylistsResponse.class);
+
+ return loadAllPlaylistsResponse.getPlaylists();
+ }
+
+
}
View
36 google-play-client/src/main/java/com/faceture/google/play/domain/LoadAllPlaylistsResponse.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2012. Faceture Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.faceture.google.play.domain;
+
+import java.util.Collection;
+
+/**
+ * Response to the LoadAllPlaylists call
+ */
+public class LoadAllPlaylistsResponse {
+
+ private Collection<Playlist> playlists;
+
+ public Collection<Playlist> getPlaylists() {
+ return playlists;
+ }
+
+ public void setPlaylists(Collection<Playlist> playlists) {
+ this.playlists = playlists;
+ }
+}
View
90 google-play-client/src/main/java/com/faceture/google/play/domain/Playlist.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2012. Faceture Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.faceture.google.play.domain;
+
+import java.util.Collection;
+
+/**
+ * A Google Play Playlist
+ */
+public class Playlist {
+
+ private String title;
+ private String playlistId;
+ private double requestTime;
+ private String continuationToken;
+ private boolean differentialUpdate;
+ private Collection<Song> playlist;
+ private boolean continuation;
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public String getPlaylistId() {
+ return playlistId;
+ }
+
+ public void setPlaylistId(String playlistId) {
+ this.playlistId = playlistId;
+ }
+
+ public double getRequestTime() {
+ return requestTime;
+ }
+
+ public void setRequestTime(double requestTime) {
+ this.requestTime = requestTime;
+ }
+
+ public String getContinuationToken() {
+ return continuationToken;
+ }
+
+ public void setContinuationToken(String continuationToken) {
+ this.continuationToken = continuationToken;
+ }
+
+ public boolean isDifferentialUpdate() {
+ return differentialUpdate;
+ }
+
+ public void setDifferentialUpdate(boolean differentialUpdate) {
+ this.differentialUpdate = differentialUpdate;
+ }
+
+ public Collection<Song> getPlaylist() {
+ return playlist;
+ }
+
+ public void setPlaylist(Collection<Song> playlist) {
+ this.playlist = playlist;
+ }
+
+ public boolean isContinuation() {
+ return continuation;
+ }
+
+ public void setContinuation(boolean continuation) {
+ this.continuation = continuation;
+ }
+}
View
40 google-play-client/src/test/java/com/faceture/google/play/PlayClientTest.java
@@ -28,6 +28,8 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -369,4 +371,42 @@ public void testLoadAllTracksFailsDueToNullPlaySession() throws IOException, URI
catch (IllegalArgumentException e) {}
}
+ public void testLoadAllPlaylistsFailsDueToNullSession() throws IOException, URISyntaxException {
+ try {
+ playClient.loadAllPlaylists(null);
+
+ fail("should have thrown exception");
+ }
+ catch (IllegalArgumentException e) {}
+ }
+
+ @SuppressWarnings("unchecked")
+ public void testLoadAllPlaylistsHappyPath() throws IOException, URISyntaxException {
+ // mock interactions
+ RestResponse restResponse = mock(RestResponse.class);
+ when(restClient.doPost(eq(Const.USE_HTTPS), eq(HostName.PLAY), eq(Path.MUSIC_LOAD_PLAYLIST),
+ isA((Map.class)), isA(Map.class), (Map<String, String>) isNull(), isA(Map.class))).thenReturn(restResponse);
+
+ when(restResponse.getStatusCode()).thenReturn(HttpStatus.SC_OK);
+
+ String responseBody = "responseBody";
+ when(restResponse.getBody()).thenReturn(responseBody);
+
+ LoadAllPlaylistsResponse loadAllPlaylistsResponse = mock(LoadAllPlaylistsResponse.class);
+ when(gsonWrapper.fromJson(responseBody, LoadAllPlaylistsResponse.class)).thenReturn(loadAllPlaylistsResponse);
+
+ Playlist playlist = mock(Playlist.class);
+ Collection<Playlist> playlists = new ArrayList<Playlist>();
+ playlists.add(playlist);
+
+ when(loadAllPlaylistsResponse.getPlaylists()).thenReturn(playlists);
+
+ // do the call
+ Collection<Playlist> resultPlaylists = playClient.loadAllPlaylists(playSession);
+
+ // verify results
+ assertNotNull(resultPlaylists);
+ assertFalse(resultPlaylists.isEmpty());
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.