Browse files

Adding getSimilarTracks to TrackOperations, adding AbstractLastFmName…

…dNestedResponse and modifying LastFmTracksResponse
  • Loading branch information...
1 parent 2cb9a8b commit b6e222d7abb8c1b853e07f9a9f5c6a630bba4937 @michaellavelle committed Dec 4, 2011
View
3 src/main/java/org/springframework/social/lastfm/api/TrackOperations.java
@@ -26,5 +26,8 @@
public List<TrackSearchResult> searchByArtistAndTrackName(
String artistName, String trackName);
+
+ public List<Track> getSimilarTracks(TrackDescriptor trackDescriptor);
+
}
View
28 src/main/java/org/springframework/social/lastfm/api/impl/TrackTemplate.java
@@ -4,9 +4,16 @@
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.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.LastFmTrackSearchResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmTracksResponse;
import org.springframework.social.lastfm.auth.LastFmAccessGrant;
import org.springframework.web.client.RestTemplate;
@@ -43,4 +50,25 @@ public TrackTemplate(RestTemplate restTemplate,
.getNestedResponse().getTracksResponse().getTracks();
}
+ @Override
+ public List<Track> getSimilarTracks(TrackDescriptor trackDescriptor) {
+ Map<String, String> additionalParams = new HashMap<String, String>();
+ if (trackDescriptor.getName() != null)
+ {
+ additionalParams.put("track", trackDescriptor.getName());
+ }
+ if (trackDescriptor.getArtistName() != null)
+ {
+ additionalParams.put("artist", trackDescriptor.getArtistName());
+ }
+
+ LastFmApiMethodParameters methodParameters = new LastFmApiMethodParameters(
+ "track.similar", apiKey, secret, additionalParams);
+
+ return restTemplate
+ .getForObject(buildLastFmApiUrl(methodParameters),
+ LastFmSimilarTracksResponse.class)
+ .getNestedResponse().getNestedResponse().getTracks();
+ }
+
}
View
49 ...va/org/springframework/social/lastfm/api/impl/json/AbstractLastFmNamedNestedResponse.java
@@ -0,0 +1,49 @@
+/*
+ * 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 java.util.HashMap;
+
+/**
+ * @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;
+ }
+}
View
34 ...ain/java/org/springframework/social/lastfm/api/impl/json/LastFmSimilarTracksResponse.java
@@ -0,0 +1,34 @@
+/*
+ * 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.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+/**
+ * @author Michael Lavelle
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class LastFmSimilarTracksResponse extends AbstractLastFmNestedResponse<LastFmTracksResponse>{
+
+ public LastFmSimilarTracksResponse() {
+ super("similartracks");
+ }
+
+
+
+}
View
38 ...org/springframework/social/lastfm/api/impl/json/LastFmSimpleTrackDescriptorsResponse.java
@@ -0,0 +1,38 @@
+/*
+ * 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 java.util.List;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.springframework.social.lastfm.api.Track;
+import org.springframework.social.lastfm.api.impl.json.lists.SimpleTrackDescriptorListContainer;
+import org.springframework.social.lastfm.api.impl.json.lists.TrackListContainer;
+
+/**
+ * @author Michael Lavelle
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class LastFmSimpleTrackDescriptorsResponse extends AbstractLastFmNestedResponse<SimpleTrackDescriptorListContainer>{
+
+ @JsonCreator
+ public LastFmSimpleTrackDescriptorsResponse() {
+ super("track");
+ }
+
+}
View
15 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmTracksResponse.java
@@ -24,14 +24,25 @@
import org.springframework.social.lastfm.api.impl.json.lists.TrackListContainer;
/**
+ * Binds to the most common form of a LastFM json representation of a track. This class
+ * extends AbstractLastFmNamedNestedResponse rather than AbstractLastFmNestedResponse
+ * as we cannot use a Map to bind to a tracks response as the response contains different
+ * json representations for a track and we only wish to bind to one.
+ *
* @author Michael Lavelle
*/
@JsonIgnoreProperties(ignoreUnknown = true)
-public class LastFmTracksResponse extends AbstractLastFmNestedResponse<TrackListContainer>{
+public class LastFmTracksResponse extends AbstractLastFmNamedNestedResponse<TrackListContainer>{
@JsonCreator
public LastFmTracksResponse() {
- super("track");
+ super();
+ }
+
+ @Override
+ @JsonProperty("track")
+ public void setNamedNestedResponse(TrackListContainer nestedResponse) {
+ super.setNestedResponse(nestedResponse);
}
}
View
72 ...springframework/social/lastfm/api/impl/json/lists/SimpleTrackDescriptorListContainer.java
@@ -0,0 +1,72 @@
+/*
+ * 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.lists;
+
+import java.util.List;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.springframework.social.lastfm.api.ArtistDescriptor;
+import org.springframework.social.lastfm.api.Image;
+import org.springframework.social.lastfm.api.LastFmProfile;
+import org.springframework.social.lastfm.api.SimpleArtist;
+import org.springframework.social.lastfm.api.SimpleTrack;
+import org.springframework.social.lastfm.api.SimpleTrackDescriptor;
+
+/**
+ * @author Michael Lavelle
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+/**
+ * Container for a list of Last.fm json object - allows for automatic JSON binding from *either* a list of objects
+ * or a Map representation of a single object, as LastFm responds with different Json structures
+ * depending on whether a single object is returned where a list can be.
+ *
+ * @author Michael Lavelle
+ */
+public class SimpleTrackDescriptorListContainer extends AbstractLastFmListContainer<SimpleTrackDescriptor> {
+
+
+ @JsonCreator
+ public SimpleTrackDescriptorListContainer(List<SimpleTrackDescriptor> simpleTracks) {
+ super(simpleTracks);
+ }
+
+ public SimpleTrackDescriptorListContainer(String artistName, String name,
+ String musicBrainsId) {
+ super(new SimpleTrackDescriptor(artistName,name,musicBrainsId));
+ }
+
+ public SimpleTrackDescriptorListContainer(ArtistDescriptor artistDescriptor,
+ String name, String musicBrainsId) {
+ super(new SimpleTrackDescriptor(artistDescriptor,name,musicBrainsId));
+ }
+
+ public SimpleTrackDescriptorListContainer(String artistName, String name) {
+ super(new SimpleTrackDescriptor(artistName,name));
+ }
+
+ public SimpleTrackDescriptorListContainer(String musicBrainsId) {
+
+ super(new SimpleTrackDescriptor(musicBrainsId));
+ }
+
+ public List<SimpleTrackDescriptor> getTracks()
+ {
+ return elements;
+ }
+
+}
View
17 src/test/java/org/springframework/social/lastfm/api/UserTemplateTest.java
@@ -123,6 +123,23 @@ public void getLovedTracks() {
}
@Test
+ public void getSimilarTracks() {
+
+ mockServer
+ .expect(requestTo("http://ws.audioscrobbler.com/2.0/?format=json&api_key=someApiKey&method=track.similar&track=Music&artist=Madonna"))
+ .andExpect(method(GET))
+ .andExpect(header("User-Agent", "someUserAgent"))
+ .andRespond(
+ withResponse(jsonResource("testdata/similar-tracks"),
+ responseHeaders));
+
+ List<Track> tracks = lastFm.trackOperations().getSimilarTracks(new SimpleTrackDescriptor("Madonna","Music"));
+ assertEquals("Don't Tell Me",tracks.get(0).getName());
+ assertEquals("Madonna",tracks.get(0).getArtistName());
+
+ }
+
+ @Test
public void getShouts() {
mockServer
View
2 src/test/resources/org/springframework/social/lastfm/api/testdata/similar-tracks.json
2 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit b6e222d

Please sign in to comment.