Permalink
Browse files

Adding tracks operations and making client id available to api operat…

…ions classes as this is needed for tracks operations
  • Loading branch information...
michaellavelle committed Jan 7, 2012
1 parent bfa8c7a commit d784345646775ca82ed2eb5f7dbf4817413b7ea3
@@ -13,6 +13,6 @@
public MeOperations meOperations();
public UsersOperations usersOperations();
public ResolveOperations resolveOperations();
-
+ public TracksOperations tracksOperations();
}
@@ -4,6 +4,14 @@
private String permalinkUrl;
private String title;
+ private String id;
+
+ public String getId() {
+ return id;
+ }
+ public void setId(String id) {
+ this.id = id;
+ }
public String getPermalinkUrl() {
return permalinkUrl;
}
@@ -17,10 +25,11 @@ public void setTitle(String title) {
this.title = title;
}
- public Track(String permalinkUrl,String title)
+ public Track(String permalinkUrl,String title,String id)
{
this.title = title;
this.permalinkUrl = permalinkUrl;
+ this.id = id;
}
}
@@ -0,0 +1,9 @@
+package org.springframework.social.soundcloud.api;
+
+import java.util.List;
+
+public interface TracksOperations {
+
+ public List<Track> search(String query);
+
+}
@@ -6,10 +6,13 @@
protected final RestTemplate restTemplate;
protected final boolean isAuthorizedForUser;
+ protected final String clientId;
- public AbstractSoundCloudOperations(RestTemplate restTemplate, boolean isAuthorizedForUser) {
+
+ public AbstractSoundCloudOperations(String clientId,RestTemplate restTemplate, boolean isAuthorizedForUser) {
this.restTemplate = restTemplate;
this.isAuthorizedForUser = isAuthorizedForUser;
+ this.clientId = clientId;
}
protected String getApiBaseUrl()
@@ -21,3 +24,4 @@ protected String getApiBaseUrl()
}
+
@@ -4,15 +4,26 @@
public abstract class AbstractSoundCloudResourceOperations extends AbstractSoundCloudOperations{
- public AbstractSoundCloudResourceOperations(RestTemplate restTemplate, boolean isAuthorizedForUser) {
- super(restTemplate,isAuthorizedForUser);
+ private boolean appendClientIfIfNotAuthorized;
+
+ public AbstractSoundCloudResourceOperations(String clientId,RestTemplate restTemplate, boolean isAuthorizedForUser,boolean appendClientIfIfNotAuthorized) {
+ super(clientId,restTemplate,isAuthorizedForUser);
+ this.appendClientIfIfNotAuthorized = appendClientIfIfNotAuthorized;
}
+
protected abstract String getApiResourceBaseUrl();
+
protected String getApiResourceUrl(String resourcePath)
{
- return getApiResourceBaseUrl() + resourcePath;
+ String resourceUrl = getApiResourceBaseUrl() + resourcePath;
+ if (appendClientIfIfNotAuthorized && !isAuthorizedForUser && clientId != null)
+ {
+ String querySeparator = resourceUrl.indexOf("?") == -1 ? "?" : "&";
+ resourceUrl = resourceUrl + querySeparator + "client_id=" + clientId;
+ }
+ return resourceUrl;
}
@@ -10,9 +10,9 @@
public abstract class AbstractUserTemplate extends AbstractSoundCloudResourceOperations implements UserOperations {
- public AbstractUserTemplate(RestTemplate restTemplate,
- boolean isAuthorizedForUser) {
- super(restTemplate, isAuthorizedForUser);
+ public AbstractUserTemplate(String clientId,RestTemplate restTemplate,
+ boolean isAuthorizedForUser,boolean appendClientIdIfNotAuthorized) {
+ super(clientId,restTemplate, isAuthorizedForUser,appendClientIdIfNotAuthorized);
}
protected abstract String getUsersResourcePrefix();
@@ -6,7 +6,7 @@
public class MeTemplate extends AbstractUserTemplate implements MeOperations {
public MeTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
- super(restTemplate, isAuthorizedForUser);
+ super(null,restTemplate, isAuthorizedForUser,false);
}
@Override
@@ -12,9 +12,9 @@
- public ResolveTemplate(RestTemplate restTemplate,
+ public ResolveTemplate(String clientId,RestTemplate restTemplate,
boolean isAuthorizedForUser) {
- super(restTemplate, isAuthorizedForUser);
+ super(clientId,restTemplate, isAuthorizedForUser,true);
}
protected String getResolvableResourceBaseUrl()
@@ -12,6 +12,7 @@
import org.springframework.social.soundcloud.api.MeOperations;
import org.springframework.social.soundcloud.api.ResolveOperations;
import org.springframework.social.soundcloud.api.SoundCloud;
+import org.springframework.social.soundcloud.api.TracksOperations;
import org.springframework.social.soundcloud.api.UsersOperations;
import org.springframework.social.soundcloud.api.impl.json.SoundCloudModule;
import org.springframework.social.support.ClientHttpRequestFactorySelector;
@@ -23,7 +24,7 @@
private MeOperations meOperations;
private UsersOperations usersOperations;
private ResolveOperations resolveOperations;
-
+ private TracksOperations tracksOperations;
@@ -46,8 +47,8 @@ protected OAuth2Version getOAuth2Version() {
* constructor will support those operations. Those operations requiring
* authentication will throw {@link NotAuthorizedException}.
*/
- public SoundCloudTemplate() {
- initialize(null);
+ public SoundCloudTemplate(String clientId) {
+ initialize(clientId,null);
}
/**
@@ -58,9 +59,9 @@ public SoundCloudTemplate() {
* An access token given by SoundCloud after a successful OAuth 2
* authentication
*/
- public SoundCloudTemplate(String accessToken) {
+ public SoundCloudTemplate(String clientId,String accessToken) {
super(accessToken);
- initialize(accessToken);
+ initialize(clientId,accessToken);
}
@@ -76,25 +77,26 @@ public MeOperations meOperations() {
return meOperations;
}
- private void initSubApis(String accessToken) {
- usersOperations = new UsersTemplate(getRestTemplate(),isAuthorized());
+ private void initSubApis(String clientId,String accessToken) {
+ usersOperations = new UsersTemplate(clientId,getRestTemplate(),isAuthorized());
meOperations = new MeTemplate(getRestTemplate(),isAuthorized());
- resolveOperations = new ResolveTemplate(getRestTemplate(),isAuthorized());
+ resolveOperations = new ResolveTemplate(clientId,getRestTemplate(),isAuthorized());
+ tracksOperations = new TracksTemplate(clientId,getRestTemplate(),isAuthorized());
}
// private helpers
- private void initialize(String accessToken) {
+ private void initialize(String clientId,String accessToken) {
registerSoundCloudJsonModule(getRestTemplate());
getRestTemplate().setErrorHandler(new SoundCloudErrorHandler());
// Wrap the request factory with a BufferingClientHttpRequestFactory so
// that the error handler can do repeat reads on the response.getBody()
super.setRequestFactory(ClientHttpRequestFactorySelector
.bufferRequests(getRestTemplate().getRequestFactory()));
- initSubApis(accessToken);
+ initSubApis(clientId,accessToken);
@@ -127,5 +129,12 @@ public UsersOperations usersOperations() {
public ResolveOperations resolveOperations() {
return resolveOperations;
}
+
+
+ @Override
+ public TracksOperations tracksOperations() {
+ return tracksOperations;
+ }
+
}
@@ -0,0 +1,33 @@
+package org.springframework.social.soundcloud.api.impl;
+
+import java.net.URLEncoder;
+import java.util.List;
+
+import org.springframework.social.soundcloud.api.Track;
+import org.springframework.social.soundcloud.api.TracksOperations;
+import org.springframework.social.soundcloud.api.impl.json.TrackList;
+import org.springframework.web.client.RestTemplate;
+
+public class TracksTemplate extends AbstractSoundCloudResourceOperations implements TracksOperations {
+
+ public TracksTemplate(String clientId,RestTemplate restTemplate, boolean isAuthorizedForUser) {
+ super(clientId,restTemplate, isAuthorizedForUser,true);
+ }
+
+
+
+ @Override
+ public List<Track> search(String query) {
+ return restTemplate.getForObject(getApiResourceUrl("?q=" + URLEncoder.encode(query)), TrackList.class);
+ }
+
+
+
+ @Override
+ protected String getApiResourceBaseUrl() {
+ return getApiBaseUrl() + "/tracks";
+ }
+
+
+
+}
@@ -6,8 +6,8 @@
public class UserTemplate extends AbstractUserTemplate implements UserOperations {
- public UserTemplate(RestTemplate restTemplate, long userId, boolean isAuthorizedForUser) {
- super(restTemplate, isAuthorizedForUser);
+ public UserTemplate(String clientId,RestTemplate restTemplate, long userId, boolean isAuthorizedForUser) {
+ super(clientId,restTemplate, isAuthorizedForUser,true);
this.userId = userId;
}
@@ -8,20 +8,20 @@
public class UsersTemplate extends AbstractSoundCloudOperations implements UsersOperations {
- public UsersTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
- super(restTemplate, isAuthorizedForUser);
+ public UsersTemplate(String clientId,RestTemplate restTemplate, boolean isAuthorizedForUser) {
+ super(clientId,restTemplate, isAuthorizedForUser);
}
@Override
public UserOperations userOperations(long userId) {
- return new UserTemplate(restTemplate,userId,isAuthorizedForUser);
+ return new UserTemplate(clientId,restTemplate,userId,isAuthorizedForUser);
}
@Override
public UserOperations userOperations(String username) {
- ResolveOperations resolveOperations = new ResolveTemplate(restTemplate,isAuthorizedForUser);
+ ResolveOperations resolveOperations = new ResolveTemplate(clientId,restTemplate,isAuthorizedForUser);
SoundCloudProfile soundCloudProfile = resolveOperations.resolveUserProfile(username);
- return new UserTemplate(restTemplate,Long.parseLong(soundCloudProfile.getId()),isAuthorizedForUser);
+ return new UserTemplate(clientId,restTemplate,Long.parseLong(soundCloudProfile.getId()),isAuthorizedForUser);
}
@@ -17,7 +17,7 @@
@JsonCreator
TrackMixin(
@JsonProperty("permalink_url") String permalinkUrl,
- @JsonProperty("title") String title) {}
+ @JsonProperty("title") String title,@JsonProperty("id") String id) {}
}
@@ -10,15 +10,15 @@
*/
public class SoundCloudServiceProvider extends AbstractOAuth2ServiceProvider<SoundCloud>{
-
+ private String clientId;
public SoundCloudServiceProvider(String clientId, String clientSecret) {
super(new SoundCloudOAuth2Template(clientId, clientSecret));
}
@Override
public SoundCloud getApi(String accessToken) {
- return new SoundCloudTemplate(accessToken);
+ return new SoundCloudTemplate(clientId,accessToken);
}
}

0 comments on commit d784345

Please sign in to comment.