Permalink
Browse files

Simplifying AbstractLastFmNestedResponse so that it no longer require…

…s an attribute name passed to its constructor, removing attribute-name specific subclasses and replacing with more generic wrappers, and adding getTopArtists and getRecommendedArtists api methods and tests
  • Loading branch information...
1 parent cc11be8 commit b294b5c067fd7c1adfe82bc9421d56c69fa1fd29 @michaellavelle committed Mar 20, 2012
Showing with 249 additions and 124 deletions.
  1. +8 −0 src/main/java/org/springframework/social/lastfm/api/UserOperations.java
  2. +2 −2 src/main/java/org/springframework/social/lastfm/api/impl/TrackTemplate.java
  3. +78 −10 src/main/java/org/springframework/social/lastfm/api/impl/UserTemplate.java
  4. +0 −48 src/main/java/org/springframework/social/lastfm/api/impl/json/AbstractLastFmNamedNestedResponse.java
  5. +1 −8 src/main/java/org/springframework/social/lastfm/api/impl/json/AbstractLastFmNestedResponse.java
  6. +7 −5 ...ramework/social/lastfm/api/impl/json/{LastFmNeighboursResponse.java → LastFmArtistsResponse.java}
  7. +1 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmProfileResponse.java
  8. +2 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmShoutsResponse.java
  9. +0 −33 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmSimilarTracksResponse.java
  10. +1 −1 ...in/java/org/springframework/social/lastfm/api/impl/json/LastFmSimpleTrackDescriptorsResponse.java
  11. +3 −3 ...k/social/lastfm/api/impl/json/{LastFmRecentTracksResponse.java → LastFmSimpleTracksResponse.java}
  12. +1 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmTrackSearchResponse.java
  13. +3 −3 ...ramework/social/lastfm/api/impl/json/{LastFmLovedTracksResponse.java → LastFmTracksResponse.java}
  14. +3 −3 ...ringframework/social/lastfm/api/impl/json/{LastFmFriendsResponse.java → LastFmUsersResponse.java}
  15. +53 −0 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/ArtistListContainer.java
  16. +24 −5 .../social/lastfm/api/impl/json/{LastFmTopTracksResponse.java → lists/LastFmArtistListResponse.java}
  17. +59 −0 src/test/java/org/springframework/social/lastfm/api/UserTemplateTest.java
  18. +1 −0 src/test/resources/org/springframework/social/lastfm/api/testdata/recommended-artists.json
  19. +1 −0 src/test/resources/org/springframework/social/lastfm/api/testdata/top-artists-empty.json
  20. +1 −0 src/test/resources/org/springframework/social/lastfm/api/testdata/top-artists.json
@@ -51,6 +51,10 @@
public Page<Track> getTopTracks(String userName);
public Page<Track> getTopTracks(String userName,Pageable pageable);
+
+ public Page<Artist> getTopArtists(String userName);
+
+ public Page<Artist> getTopArtists(String userName,Pageable pageable);
public Page<SimpleTrack> getRecentTracks(String userName);
@@ -64,4 +68,8 @@
public void updateNowPlaying(TrackDescriptor trackDescriptor);
+ public Page<Artist> getRecommendedArtists();
+ public Page<Artist> getRecommendedArtists(Pageable pageable);
+
+
}
@@ -12,9 +12,9 @@
import org.springframework.social.lastfm.api.TrackDescriptor;
import org.springframework.social.lastfm.api.TrackOperations;
import org.springframework.social.lastfm.api.TrackSearchResult;
-import org.springframework.social.lastfm.api.impl.json.LastFmSimilarTracksResponse;
import org.springframework.social.lastfm.api.impl.json.LastFmTrackMatchesResponse;
import org.springframework.social.lastfm.api.impl.json.LastFmTrackSearchResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmTracksResponse;
import org.springframework.social.lastfm.api.impl.json.lists.LastFmTrackListResponse;
import org.springframework.social.lastfm.api.impl.json.lists.LastFmTrackSearchResultListResponse;
import org.springframework.social.lastfm.api.impl.json.lists.PageInfo;
@@ -114,7 +114,7 @@ public TrackTemplate(RestTemplate restTemplate,
LastFmTrackListResponse trackListResponse = restTemplate
.getForObject(buildLastFmApiUrl(methodParameters),
- LastFmSimilarTracksResponse.class)
+ LastFmTracksResponse.class)
.getNestedResponse();
PageInfo pageInfo = trackListResponse.getPageInfo();
@@ -25,19 +25,20 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
+import org.springframework.social.lastfm.api.Artist;
import org.springframework.social.lastfm.api.LastFmProfile;
import org.springframework.social.lastfm.api.Shout;
import org.springframework.social.lastfm.api.SimpleTrack;
import org.springframework.social.lastfm.api.Track;
import org.springframework.social.lastfm.api.TrackDescriptor;
import org.springframework.social.lastfm.api.UserOperations;
-import org.springframework.social.lastfm.api.impl.json.LastFmFriendsResponse;
-import org.springframework.social.lastfm.api.impl.json.LastFmLovedTracksResponse;
-import org.springframework.social.lastfm.api.impl.json.LastFmNeighboursResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmArtistsResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmUsersResponse;
import org.springframework.social.lastfm.api.impl.json.LastFmProfileResponse;
-import org.springframework.social.lastfm.api.impl.json.LastFmRecentTracksResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmSimpleTracksResponse;
import org.springframework.social.lastfm.api.impl.json.LastFmShoutsResponse;
-import org.springframework.social.lastfm.api.impl.json.LastFmTopTracksResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmTracksResponse;
+import org.springframework.social.lastfm.api.impl.json.lists.LastFmArtistListResponse;
import org.springframework.social.lastfm.api.impl.json.lists.LastFmShoutListResponse;
import org.springframework.social.lastfm.api.impl.json.lists.LastFmSimpleTrackListResponse;
import org.springframework.social.lastfm.api.impl.json.lists.LastFmTrackListResponse;
@@ -104,7 +105,7 @@ public LastFmProfile getUserProfile(String userName) {
LastFmSimpleTrackListResponse simpleTrackListResponse = restTemplate
.getForObject(buildLastFmApiUrl(methodParameters),
- LastFmRecentTracksResponse.class).getNestedResponse();
+ LastFmSimpleTracksResponse.class).getNestedResponse();
PageInfo pageInfo = simpleTrackListResponse.getPageInfo();
@@ -140,7 +141,7 @@ public LastFmProfile getUserProfile(String userName) {
LastFmTrackListResponse trackListResponse = restTemplate
.getForObject(buildLastFmApiUrl(methodParameters),
- LastFmLovedTracksResponse.class).getNestedResponse();
+ LastFmTracksResponse.class).getNestedResponse();
PageInfo pageInfo = trackListResponse.getPageInfo();
@@ -173,7 +174,7 @@ public LastFmProfile getUserProfile(String userName) {
LastFmTrackListResponse trackListResponse = restTemplate
.getForObject(buildLastFmApiUrl(methodParameters),
- LastFmTopTracksResponse.class).getNestedResponse();
+ LastFmTracksResponse.class).getNestedResponse();
PageInfo pageInfo = trackListResponse.getPageInfo();
@@ -189,6 +190,41 @@ public LastFmProfile getUserProfile(String userName) {
return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),trackListResponse.getPageInfo().getTotal());
}
+
+ @Override
+ public Page<Artist> getTopArtists(String userName) {
+ return getTopArtists(userName,null);
+ }
+
+ @Override
+ public Page<Artist> getTopArtists(String userName,Pageable pageable) {
+
+ Map<String, String> additionalParams = new HashMap<String, String>();
+ additionalParams.put("user", userName);
+ setPageableParamsIfSpecified(additionalParams,pageable);
+
+
+ LastFmApiMethodParameters methodParameters = new LastFmApiMethodParameters(
+ "user.gettopartists", apiKey, null, null, additionalParams);
+
+ LastFmArtistListResponse artistListResponse = restTemplate
+ .getForObject(buildLastFmApiUrl(methodParameters),
+ LastFmArtistsResponse.class).getNestedResponse();
+
+ PageInfo pageInfo = artistListResponse.getPageInfo();
+
+
+
+ // Last.Fm will return the last page available if a page number is requested greater than the total pages
+ // Ensure that we override this behaviour and return an empty page for this case
+ if (pageable != null && pageable.getPageNumber() > pageInfo.getTotalPages())
+ {
+ return new PageImpl<Artist>(new ArrayList<Artist>(),pageable,pageInfo.getTotal());
+ }
+
+ return new PageImpl<Artist>(artistListResponse.getArtists(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+
+ }
@Override
public void scrobble(TrackDescriptor trackDescriptor, Date timestamp) {
@@ -275,6 +311,38 @@ public void shout(String userName, String message) {
return new PageImpl<Shout>(shoutListResponse.getShouts(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
}
+ @Override
+ public Page<Artist> getRecommendedArtists() {
+ return getRecommendedArtists(null);
+ }
+
+ @Override
+ public Page<Artist> getRecommendedArtists(Pageable pageable) {
+ Map<String, String> additionalParams = new HashMap<String, String>();
+ setPageableParamsIfSpecified(additionalParams,pageable);
+ LastFmApiMethodParameters methodParameters = new LastFmApiMethodParameters(
+ "user.getrecommendedartists", apiKey, lastFmAccessGrant.getToken(), secret, lastFmAccessGrant.getSessionKey(),additionalParams);
+
+
+ LastFmArtistListResponse artistListResponse = restTemplate
+ .getForObject(buildLastFmApiUrl(methodParameters),
+ LastFmArtistsResponse.class).getNestedResponse();
+
+
+ PageInfo pageInfo = artistListResponse.getPageInfo();
+
+
+
+ // Last.Fm will return the last page available if a page number is requested greater than the total pages
+ // Ensure that we override this behaviour and return an empty page for this case
+ if (pageable != null && pageable.getPageNumber() > pageInfo.getTotalPages())
+ {
+ return new PageImpl<Artist>(new ArrayList<Artist>(),pageable,pageInfo.getTotal());
+ }
+
+ return new PageImpl<Artist>(artistListResponse.getArtists(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+ }
+
@Override
public Page<LastFmProfile> getFriends(String userName) {
@@ -294,7 +362,7 @@ public void shout(String userName, String message) {
LastFmUserListResponse userListResponse = restTemplate
.getForObject(buildLastFmApiUrl(methodParameters),
- LastFmFriendsResponse.class).getNestedResponse();
+ LastFmUsersResponse.class).getNestedResponse();
PageInfo pageInfo = userListResponse.getPageInfo();
@@ -336,7 +404,7 @@ public void shout(String userName, String message) {
return restTemplate
.getForObject(buildLastFmApiUrl(methodParameters),
- LastFmNeighboursResponse.class).getNestedResponse().getUsers();
+ LastFmUsersResponse.class).getNestedResponse().getUsers();
}
@Override
@@ -1,48 +0,0 @@
-/*
- * Copyright 2011 the original author or authors.
- *
- * 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 org.springframework.social.lastfm.api.impl.json;
-
-
-/**
- * @author Michael Lavelle
- */
-public abstract class AbstractLastFmNamedNestedResponse<T> {
-
-
- private T nestedResponse;
-
- protected void setNestedResponse(T nestedResponse)
- {
- this.nestedResponse = nestedResponse;
- }
-
- public abstract void setNamedNestedResponse(T nestedResponse);
-
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
-
- public AbstractLastFmNamedNestedResponse()
- {
- }
-
- public T getNestedResponse() {
- return nestedResponse;
- }
-}
@@ -26,15 +26,8 @@
*
*/
private static final long serialVersionUID = 1L;
-
- private final String responseKey;
-
- public AbstractLastFmNestedResponse(final String responseKey)
- {
- this.responseKey = responseKey;
- }
public T getNestedResponse() {
- return get(responseKey);
+ return this.values().iterator().next();
}
}
@@ -17,18 +17,20 @@
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
-import org.springframework.social.lastfm.api.impl.json.lists.LastFmUserListResponse;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.springframework.social.lastfm.api.impl.json.lists.LastFmArtistListResponse;
+import org.springframework.social.lastfm.api.impl.json.lists.LastFmShoutListResponse;
/**
* @author Michael Lavelle
*/
@JsonIgnoreProperties(ignoreUnknown = true)
-public class LastFmNeighboursResponse extends AbstractLastFmNestedResponse<LastFmUserListResponse>{
+public class LastFmArtistsResponse extends AbstractLastFmNestedResponse<LastFmArtistListResponse> {
+
@JsonCreator
- public LastFmNeighboursResponse() {
- super("neighbours");
+ public LastFmArtistsResponse() {
+ super();
}
-
}
@@ -28,7 +28,7 @@
@JsonCreator
public LastFmProfileResponse() {
- super("user");
+ super();
}
@@ -17,6 +17,7 @@
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
import org.springframework.social.lastfm.api.impl.json.lists.LastFmShoutListResponse;
/**
@@ -28,7 +29,7 @@
@JsonCreator
public LastFmShoutsResponse() {
- super("shouts");
+ super();
}
}
@@ -1,33 +0,0 @@
-/*
- * Copyright 2011 the original author or authors.
- *
- * 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 org.springframework.social.lastfm.api.impl.json;
-
-import org.codehaus.jackson.annotate.JsonIgnoreProperties;
-import org.springframework.social.lastfm.api.impl.json.lists.LastFmTrackListResponse;
-
-/**
- * @author Michael Lavelle
- */
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class LastFmSimilarTracksResponse extends AbstractLastFmNestedResponse<LastFmTrackListResponse>{
-
- public LastFmSimilarTracksResponse() {
- super("similartracks");
- }
-
-
-
-}
@@ -27,7 +27,7 @@
@JsonCreator
public LastFmSimpleTrackDescriptorsResponse() {
- super("track");
+ super();
}
}
@@ -23,12 +23,12 @@
* @author Michael Lavelle
*/
@JsonIgnoreProperties(ignoreUnknown = true)
-public class LastFmRecentTracksResponse extends AbstractLastFmNestedResponse<LastFmSimpleTrackListResponse> {
+public class LastFmSimpleTracksResponse extends AbstractLastFmNestedResponse<LastFmSimpleTrackListResponse> {
@JsonCreator
- public LastFmRecentTracksResponse()
+ public LastFmSimpleTracksResponse()
{
- super("recenttracks");
+ super();
}
}
@@ -27,7 +27,7 @@
@JsonCreator
public LastFmTrackSearchResponse()
{
- super("results");
+ super();
}
}
@@ -22,10 +22,10 @@
* @author Michael Lavelle
*/
@JsonIgnoreProperties(ignoreUnknown = true)
-public class LastFmLovedTracksResponse extends AbstractLastFmNestedResponse<LastFmTrackListResponse>{
+public class LastFmTracksResponse extends AbstractLastFmNestedResponse<LastFmTrackListResponse>{
- public LastFmLovedTracksResponse() {
- super("lovedtracks");
+ public LastFmTracksResponse() {
+ super();
}
Oops, something went wrong.

0 comments on commit b294b5c

Please sign in to comment.