Skip to content

Commit bbee76a

Browse files
committed
added CloudAPI#setDefaultContentType(String)
1 parent 456f9ad commit bbee76a

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Added Request#withFile(String, byte[]), Request#withFile(String, ByteBuffer)
66
* Added Request#withEntity(HttpEntity) and Request#withContent(String, String)
77
* Added PutResource example
8+
* Added setting to change the default content type for requests
9+
(CloudAPI#setDefaultContentType(String))
810

911
## 1.0.0 2011-05-19
1012

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
*/
7373
public class ApiWrapper implements CloudAPI, Serializable {
7474
private static final long serialVersionUID = 3662083416905771921L;
75+
public static final String DEFAULT_CONTENT_TYPE = "application/json";
7576

7677
/** The current environment */
7778
public final Env env;
@@ -85,6 +86,9 @@ public class ApiWrapper implements CloudAPI, Serializable {
8586
/** debug request details to stderr */
8687
public boolean debugRequests;
8788

89+
90+
private String mDefaultContentType;
91+
8892
/**
8993
* Constructs a new ApiWrapper instance.
9094
*
@@ -420,6 +424,15 @@ public void toFile(File f) throws IOException {
420424
oos.close();
421425
}
422426

427+
428+
public String getDefaultContentType() {
429+
return (mDefaultContentType == null) ? DEFAULT_CONTENT_TYPE : mDefaultContentType;
430+
}
431+
432+
public void setDefaultContentType(String contentType) {
433+
mDefaultContentType = contentType;
434+
}
435+
423436
/**
424437
* Read wrapper from a file
425438
* @param f the file
@@ -453,7 +466,7 @@ protected HttpRequest addAuthHeader(HttpRequest request) {
453466
/** Forces JSON */
454467
protected HttpRequest addAcceptHeader(HttpRequest request) {
455468
if (!request.containsHeader("Accept")) {
456-
request.addHeader("Accept", "application/json");
469+
request.addHeader("Accept", getDefaultContentType());
457470
}
458471
return request;
459472
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ public interface CloudAPI {
201201
*/
202202
URI authorizationCodeUrl(String... options);
203203

204+
/**
205+
* Changes the default content type sent in the "Accept" header.
206+
* If you don't set this it defaults to "application/json".
207+
*
208+
* @param contentType the request mime type.
209+
*/
210+
void setDefaultContentType(String contentType);
211+
204212
/**
205213
* Interested in changes to the current token.
206214
*/

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.soundcloud.api;
22

33
import static org.hamcrest.CoreMatchers.*;
4+
import static org.hamcrest.CoreMatchers.containsString;
45
import static org.junit.Assert.assertThat;
56

67
import org.apache.http.HttpResponse;
78
import org.json.JSONObject;
89
import org.junit.Before;
910
import org.junit.Test;
1011

12+
import javax.xml.ws.Endpoint;
1113
import java.io.File;
1214
import java.io.FileInputStream;
1315
import java.io.FileOutputStream;
@@ -125,6 +127,10 @@ public void readMyDetails() throws Exception {
125127
HttpResponse resp = api.get(Request.to(Endpoints.MY_DETAILS));
126128
assertThat(resp.getStatusLine().getStatusCode(), is(200));
127129

130+
assertThat(
131+
resp.getFirstHeader("Content-Type").getValue(),
132+
containsString("application/json"));
133+
128134
JSONObject me = Http.getJSON(resp);
129135

130136
assertThat(me.getString("username"), equalTo("api-testing"));
@@ -153,6 +159,18 @@ public void shouldNotRefreshWithNonExpiringScope() throws Exception {
153159
assertThat(resp.getStatusLine().getStatusCode(), is(401));
154160
}
155161

162+
@Test
163+
public void shouldChangeContentType() throws Exception {
164+
login();
165+
166+
api.setDefaultContentType("application/xml");
167+
HttpResponse resp = api.get(Request.to(Endpoints.MY_DETAILS));
168+
169+
assertThat(
170+
resp.getFirstHeader("Content-Type").getValue(),
171+
containsString("application/xml"));
172+
}
173+
156174
/*
157175
@Test
158176
public void updateMyDetails() throws Exception {

0 commit comments

Comments
 (0)