Permalink
Browse files

#16 Modifying api methods which return lists to return pages instead

  • Loading branch information...
1 parent 8f795b1 commit cc11be8868ebaa71d96523f1f13230dbfb31f0f2 @michaellavelle committed Mar 19, 2012
Showing with 605 additions and 155 deletions.
  1. +5 −0 .settings/org.eclipse.jdt.core.prefs
  2. +5 −0 pom.xml
  3. +13 −5 src/main/java/org/springframework/social/lastfm/api/TrackOperations.java
  4. +21 −9 src/main/java/org/springframework/social/lastfm/api/UserOperations.java
  5. +12 −0 src/main/java/org/springframework/social/lastfm/api/impl/AbstractLastFmOperations.java
  6. +9 −0 src/main/java/org/springframework/social/lastfm/api/impl/LastFmErrorHandler.java
  7. +70 −18 src/main/java/org/springframework/social/lastfm/api/impl/TrackTemplate.java
  8. +148 −21 src/main/java/org/springframework/social/lastfm/api/impl/UserTemplate.java
  9. +0 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/AbstractLastFmNamedNestedResponse.java
  10. +0 −2 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmLovedTracksResponse.java
  11. +0 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmProfileResponse.java
  12. +2 −2 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmRecentTracksResponse.java
  13. +0 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmShoutsResponse.java
  14. +2 −3 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmSimilarTracksResponse.java
  15. +0 −5 ...in/java/org/springframework/social/lastfm/api/impl/json/LastFmSimpleTrackDescriptorsResponse.java
  16. +2 −2 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmTopTracksResponse.java
  17. +40 −0 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmTrackMatchesResponse.java
  18. +0 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmTrackSearchResponse.java
  19. +0 −48 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmTracksResponse.java
  20. +1 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/LastFmShoutListResponse.java
  21. +16 −6 ...lastfm/api/impl/json/{LastFmSimpleTracksResponse.java → lists/LastFmSimpleTrackListResponse.java}
  22. +2 −3 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/LastFmTrackListResponse.java
  23. +6 −2 ...va/org/springframework/social/lastfm/api/impl/json/lists/LastFmTrackSearchResultListResponse.java
  24. +1 −1 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/LastFmUserListResponse.java
  25. +62 −0 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/PageInfo.java
  26. +42 −0 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/PageInfoContainer.java
  27. +0 −4 ...ava/org/springframework/social/lastfm/api/impl/json/lists/SimpleTrackDescriptorListContainer.java
  28. +0 −2 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/SimpleTrackListContainer.java
  29. +1 −2 ...in/java/org/springframework/social/lastfm/api/impl/json/lists/TrackSearchResultListContainer.java
  30. +93 −13 src/test/java/org/springframework/social/lastfm/api/UserTemplateTest.java
  31. +2 −0 src/test/resources/org/springframework/social/lastfm/api/testdata/loved-tracks.json
  32. +1 −0 src/test/resources/org/springframework/social/lastfm/api/testdata/recent-tracks-empty.json
  33. +19 −0 ...resources/org/springframework/social/lastfm/api/testdata/recent-tracks-single-track-response.json
  34. +3 −1 src/test/resources/org/springframework/social/lastfm/api/testdata/recent-tracks.json
  35. +1 −0 src/test/resources/org/springframework/social/lastfm/api/testdata/top-tracks-empty.json
  36. +23 −0 ...st/resources/org/springframework/social/lastfm/api/testdata/top-tracks-single-track-response.json
  37. +3 −1 src/test/resources/org/springframework/social/lastfm/api/testdata/top-tracks.json
@@ -0,0 +1,5 @@
+#Mon Mar 19 08:33:31 GMT 2012
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.source=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
View
@@ -27,6 +27,11 @@
<version>${org.springframework.social.version}</version>
</dependency>
<dependency>
+ <groupId>org.springframework.data</groupId>
+ <artifactId>spring-data-commons-core</artifactId>
+ <version>1.1.0.RELEASE</version>
+</dependency>
+<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
@@ -15,19 +15,27 @@
*/
package org.springframework.social.lastfm.api;
-import java.util.List;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
/**
* @author Michael Lavelle
*/
public interface TrackOperations {
- public List<TrackSearchResult> searchByTrackName(String trackName);
-
- public List<TrackSearchResult> searchByArtistAndTrackName(
+ public Page<TrackSearchResult> searchByTrackName(String trackName);
+
+ public Page<TrackSearchResult> searchByTrackName(String trackName,Pageable pageable);
+
+ public Page<TrackSearchResult> searchByArtistAndTrackName(
String artistName, String trackName);
- public List<Track> getSimilarTracks(TrackDescriptor trackDescriptor);
+ public Page<TrackSearchResult> searchByArtistAndTrackName(
+ String artistName, String trackName,Pageable pageable);
+
+ public Page<Track> getSimilarTracks(TrackDescriptor trackDescriptor);
+
+ public Page<Track> getSimilarTracks(TrackDescriptor trackDescriptor,int limit);
}
@@ -18,6 +18,9 @@
import java.util.Date;
import java.util.List;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
/**
* @author Michael Lavelle
*/
@@ -29,27 +32,36 @@
public void shout(String userName,String message);
- public List<Track> getLovedTracks(String userName);
+ public Page<Track> getLovedTracks(String userName,Pageable pageable);
+
+ public Page<Track> getLovedTracks(String userName);
+
+ public Page<Shout> getShouts(String userName);
- public List<Shout> getShouts(String userName);
+ public Page<Shout> getShouts(String userName,Pageable pageable);
- public List<LastFmProfile> getFriends(String userName);
+ public Page<LastFmProfile> getFriends(String userName);
+
+ public Page<LastFmProfile> getFriends(String userName,Pageable pageable);
public List<LastFmProfile> getNeighbours(String userName);
+
+ public List<LastFmProfile> getNeighbours(String userName,int limit);
+ public Page<Track> getTopTracks(String userName);
+
+ public Page<Track> getTopTracks(String userName,Pageable pageable);
-
- public List<Track> getTopTracks(String userName);
-
- public List<SimpleTrack> getRecentTracks(String userName);
+ public Page<SimpleTrack> getRecentTracks(String userName);
+
+ public Page<SimpleTrack> getRecentTracks(String userName,Pageable pageable);
public void scrobble(TrackDescriptor trackDescriptor, Date timestamp);
public void love(String artistName,String trackName);
+
public void unlove(String artistName,String trackName);
-
-
public void updateNowPlaying(TrackDescriptor trackDescriptor);
}
@@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map;
+import org.springframework.data.domain.Pageable;
import org.springframework.social.MissingAuthorizationException;
import org.springframework.social.lastfm.auth.LastFmAccessGrant;
import org.springframework.web.client.RestTemplate;
@@ -51,6 +52,17 @@ protected void requireAuthorization() {
}
}
+ protected void setPageableParamsIfSpecified(Map<String,String> params,Pageable pageable)
+ {
+ if (pageable != null)
+ {
+ params.put("page",new Integer(pageable.getPageNumber()).toString());
+ params.put("limit", new Integer(pageable.getPageSize()).toString());
+ }
+ }
+
+
+
protected String buildLastFmApiUrl(
LastFmApiMethodParameters methodParameters) {
String delim = "?";
@@ -73,6 +73,15 @@ void handleLastFmError(HttpStatus statusCode,
if (errorDetails.containsKey(6)) {
throw new ResourceNotFoundException(message);
}
+ if (errorDetails.containsKey(10)) {
+ throw new NotAuthorizedException(message);
+ }
+ if (errorDetails.containsKey(8)) {
+ throw new ResourceNotFoundException(message);
+ }
+ if (errorDetails.containsKey(13)) {
+ throw new NotAuthorizedException(message);
+ }
} else if (statusCode == HttpStatus.BAD_REQUEST) {
throw new ResourceNotFoundException(message);
@@ -1,19 +1,23 @@
package org.springframework.social.lastfm.api.impl;
+import java.util.ArrayList;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
-import org.springframework.social.lastfm.api.SimpleTrack;
-import org.springframework.social.lastfm.api.SimpleTrackDescriptor;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
import org.springframework.social.lastfm.api.Track;
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.LastFmSimpleTrackDescriptorsResponse;
+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;
import org.springframework.social.lastfm.auth.LastFmAccessGrant;
import org.springframework.web.client.RestTemplate;
@@ -27,31 +31,70 @@ public TrackTemplate(RestTemplate restTemplate,
isAuthorizedForUser);
}
+
@Override
- public List<TrackSearchResult> searchByTrackName(String trackName) {
- return searchByArtistAndTrackName(null, trackName);
+ public Page<TrackSearchResult> searchByTrackName(String trackName) {
+ return searchByArtistAndTrackName(null, trackName,null);
}
-
+
+ @Override
+ public Page<TrackSearchResult> searchByTrackName(String trackName,Pageable pageable) {
+ return searchByArtistAndTrackName(null, trackName,pageable);
+ }
+
@Override
- public List<TrackSearchResult> searchByArtistAndTrackName(
+ public Page<TrackSearchResult> searchByArtistAndTrackName(
String artistName, String trackName) {
+ return searchByArtistAndTrackName(artistName,trackName,null);
+ }
+
+ @Override
+ public Page<TrackSearchResult> searchByArtistAndTrackName(
+ String artistName, String trackName,Pageable pageable) {
Map<String, String> additionalParams = new HashMap<String, String>();
additionalParams.put("track", trackName);
if (artistName != null) {
additionalParams.put("artist", artistName);
}
+
+ setPageableParamsIfSpecified(additionalParams,pageable);
LastFmApiMethodParameters methodParameters = new LastFmApiMethodParameters(
"track.search", apiKey, secret, additionalParams);
- return restTemplate
- .getForObject(buildLastFmApiUrl(methodParameters),
- LastFmTrackSearchResponse.class)
- .getNestedResponse().getTracksResponse().getTracks();
+ LastFmTrackMatchesResponse trackMatchesResponse = restTemplate
+ .getForObject(buildLastFmApiUrl(methodParameters),
+ LastFmTrackSearchResponse.class)
+ .getNestedResponse();
+
+ LastFmTrackSearchResultListResponse trackSearchListResponse = trackMatchesResponse.getTracksResponse();
+
+ PageInfo pageInfo = trackMatchesResponse.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<TrackSearchResult>(new ArrayList<TrackSearchResult>(),pageable,pageInfo.getTotal());
+ }
+
+ return new PageImpl<TrackSearchResult>(trackSearchListResponse.getTracks(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+
+
+
}
@Override
- public List<Track> getSimilarTracks(TrackDescriptor trackDescriptor) {
+ public Page<Track> getSimilarTracks(TrackDescriptor trackDescriptor) {
+ return getSimilarTracksWithLimit(trackDescriptor,null);
+ }
+
+ @Override
+ public Page<Track> getSimilarTracks(TrackDescriptor trackDescriptor,int limit) {
+ return getSimilarTracksWithLimit(trackDescriptor,limit);
+ }
+
+ private Page<Track> getSimilarTracksWithLimit(TrackDescriptor trackDescriptor,Integer limit) {
Map<String, String> additionalParams = new HashMap<String, String>();
if (trackDescriptor.getName() != null)
{
@@ -61,14 +104,23 @@ public TrackTemplate(RestTemplate restTemplate,
{
additionalParams.put("artist", trackDescriptor.getArtistName());
}
+ if (limit != null)
+ {
+ additionalParams.put("limit", limit.toString());
+ }
LastFmApiMethodParameters methodParameters = new LastFmApiMethodParameters(
"track.similar", apiKey, secret, additionalParams);
- return restTemplate
- .getForObject(buildLastFmApiUrl(methodParameters),
- LastFmSimilarTracksResponse.class)
- .getNestedResponse().getNestedResponse().getTracks();
+ LastFmTrackListResponse trackListResponse = restTemplate
+ .getForObject(buildLastFmApiUrl(methodParameters),
+ LastFmSimilarTracksResponse.class)
+ .getNestedResponse();
+
+ PageInfo pageInfo = trackListResponse.getPageInfo();
+
+
+ return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(1,limit == null ? trackListResponse.getTracks().size() : limit),trackListResponse.getTracks().size());
}
}
Oops, something went wrong.

0 comments on commit cc11be8

Please sign in to comment.