Skip to content

Commit e29d35c

Browse files
author
Jan Berkel
committed
Always add client id to request
Closes #23
1 parent ff587d7 commit e29d35c

File tree

3 files changed

+9
-39
lines changed

3 files changed

+9
-39
lines changed

src/main/java/com/soundcloud/api/ApiWrapper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,16 +469,17 @@ public long resolve(String url) throws IOException {
469469
HttpResponse resp = get(Request.to(Endpoints.RESOLVE).with("url", url));
470470
if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
471471
Header location = resp.getFirstHeader("Location");
472-
if (location != null) {
473-
String s = location.getValue();
474-
if (s.contains("/")) {
472+
if (location != null && location.getValue() != null) {
473+
final String path = URI.create(location.getValue()).getPath();
474+
if (path != null && path.contains("/")) {
475475
try {
476-
return Integer.parseInt(s.substring(s.lastIndexOf("/") + 1, s.length()));
476+
final String id = path.substring(path.lastIndexOf("/") + 1);
477+
return Integer.parseInt(id);
477478
} catch (NumberFormatException e) {
478479
throw new ResolverException(e, resp);
479480
}
480481
} else {
481-
throw new ResolverException("Invalid string:"+s, resp);
482+
throw new ResolverException("Invalid string:"+path, resp);
482483
}
483484
} else {
484485
throw new ResolverException("No location header", resp);
@@ -613,8 +614,7 @@ protected HttpResponse execute(Request req, Class<? extends HttpRequestBase> req
613614
}
614615

615616
protected Request addClientIdIfNecessary(Request req) {
616-
return (mToken != EMPTY_TOKEN || req.getParams().containsKey(CLIENT_ID)) ?
617-
req : new Request(req).add(CLIENT_ID, mClientId);
617+
return req.getParams().containsKey(CLIENT_ID) ? req : new Request(req).add(CLIENT_ID, mClientId);
618618
}
619619

620620
protected void logRequest( Class<? extends HttpRequestBase> reqType, Request request) {

src/test/java/com/soundcloud/api/ApiWrapperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,9 @@ public void testAddClientIdWithoutToken() throws Exception {
531531
}
532532

533533
@Test
534-
public void testDontAddClientIdWithToken() throws Exception {
534+
public void testShouldAlwaysAddClientIdEvenWhenAuthenticated() throws Exception {
535535
api.setToken(new Token("access", "refresh"));
536-
assertThat(api.addClientIdIfNecessary(Request.to("/foo")).toUrl(), equalTo("/foo"));
536+
assertThat(api.addClientIdIfNecessary(Request.to("/foo")).toUrl(), equalTo("/foo?client_id=" + TEST_CLIENT_ID));
537537
}
538538

539539
@Test

src/test/java/com/soundcloud/api/CloudAPIIntegrationTest.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,6 @@ private void testResolveNonApiStreamUrls() throws IOException {
273273
assertThat(resolved.eTag, equalTo("\"5eeb63b73f99ff2de44a60441d421d2a\""));
274274
}
275275

276-
@Test @Ignore /* playcounts not deployed on sandbox */
277-
public void shouldResolveStreamUrlAndSkipPlaycountLogging() throws Exception {
278-
// need the playcount scope for this to work
279-
assertTrue(login(Token.SCOPE_PLAYCOUNT).scoped(Token.SCOPE_PLAYCOUNT));
280-
281-
long trackId = api.resolve(TRACK_PERMALINK);
282-
283-
int count = Http.getJSON(api.get(Request.to("/tracks/"+trackId))).getInt("playback_count");
284-
api.resolveStreamUrl("https://api.soundcloud.com/tracks/"+trackId+"/stream", false);
285-
int count2 = Http.getJSON(api.get(Request.to("/tracks/"+trackId))).getInt("playback_count");
286-
287-
assertTrue(String.format("%d !> %d", count2, count), count2 > count);
288-
289-
// resolve again, this time skipping count
290-
api.resolveStreamUrl("https://api.soundcloud.com/tracks/"+trackId+"/stream", true);
291-
292-
int count3 = Http.getJSON(api.get(Request.to("/tracks/"+trackId))).getInt("playback_count");
293-
assertTrue(String.format("%d != %d", count3, count2), count3 == count2);
294-
}
295-
296276
@Test
297277
public void shouldThrowResolverExceptionWhenStreamCannotBeResolved() throws Exception {
298278
login();
@@ -463,16 +443,6 @@ public void run() {
463443
System.err.println("all threads finished");
464444
}
465445

466-
@Test @Ignore
467-
public void updateMyDetails() throws Exception {
468-
Request updateMe = Request.to(MY_DETAILS).with(
469-
Params.User.WEBSITE, "http://mywebsite.com")
470-
.withFile(Params.User.AVATAR, new File(getClass().getResource("cat.jpg").getFile()));
471-
472-
HttpResponse resp = api.put(updateMe);
473-
assertThat(resp.getStatusLine().getStatusCode(), is(200));
474-
}
475-
476446
@SuppressWarnings({"UnusedDeclaration"})
477447
private void writeResponse(HttpResponse resp, String file) throws IOException {
478448
FileOutputStream fos = new FileOutputStream(file);

0 commit comments

Comments
 (0)