Skip to content

Commit

Permalink
Merge pull request square#1024 from square/jwilson_0822_contenttype
Browse files Browse the repository at this point in the history
Prefer the word contentType.
  • Loading branch information
JakeWharton committed Aug 22, 2015
2 parents 957e18a + 2d74a6a commit 38a7779
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion retrofit/src/main/java/retrofit/OkHttpCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class OkHttpCall<T> implements Call<T> {
return new OkHttpCall<>(client, requestFactory, responseConverter, args);
}

public void enqueue(final Callback<T> callback) {
@Override public void enqueue(final Callback<T> callback) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already executed");
executed = true;
Expand Down
33 changes: 15 additions & 18 deletions retrofit/src/main/java/retrofit/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,25 @@ final class RequestBuilder {
private HttpUrl.Builder urlBuilder;

private final Request.Builder requestBuilder;
private MediaType mediaType;
private MediaType contentType;

private final boolean hasBody;
private MultipartBuilder multipartBuilder;
private FormEncodingBuilder formEncodingBuilder;
private RequestBody body;

RequestBuilder(String method, HttpUrl baseUrl, String relativeUrl, Headers headers,
MediaType mediaType, boolean hasBody, boolean isFormEncoded, boolean isMultipart) {
MediaType contentType, boolean hasBody, boolean isFormEncoded, boolean isMultipart) {
this.method = method;

this.baseUrl = baseUrl;
this.relativeUrl = relativeUrl;
this.requestBuilder = new Request.Builder();
this.contentType = contentType;
this.hasBody = hasBody;

Request.Builder requestBuilder = new Request.Builder();
if (headers != null) {
requestBuilder.headers(headers);
}
this.requestBuilder = requestBuilder;
this.mediaType = mediaType;

this.hasBody = hasBody;

if (isFormEncoded) {
// Will be set to 'body' in 'build'.
Expand All @@ -73,7 +70,7 @@ void setRelativeUrl(String relativeUrl) {

void addHeader(String name, String value) {
if ("Content-Type".equalsIgnoreCase(name)) {
mediaType = MediaType.parse(value);
contentType = MediaType.parse(value);
} else {
requestBuilder.addHeader(name, value);
}
Expand Down Expand Up @@ -154,12 +151,12 @@ Request build() {
}
}

MediaType mediaType = this.mediaType;
if (mediaType != null) {
MediaType contentType = this.contentType;
if (contentType != null) {
if (body != null) {
body = new MediaTypeOverridingRequestBody(body, mediaType);
body = new ContentTypeOverridingRequestBody(body, contentType);
} else {
requestBuilder.addHeader("Content-Type", mediaType.toString());
requestBuilder.addHeader("Content-Type", contentType.toString());
}
}

Expand All @@ -169,17 +166,17 @@ Request build() {
.build();
}

private static class MediaTypeOverridingRequestBody extends RequestBody {
private static class ContentTypeOverridingRequestBody extends RequestBody {
private final RequestBody delegate;
private final MediaType mediaType;
private final MediaType contentType;

MediaTypeOverridingRequestBody(RequestBody delegate, MediaType mediaType) {
ContentTypeOverridingRequestBody(RequestBody delegate, MediaType contentType) {
this.delegate = delegate;
this.mediaType = mediaType;
this.contentType = contentType;
}

@Override public MediaType contentType() {
return mediaType;
return contentType;
}

@Override public long contentLength() throws IOException {
Expand Down
8 changes: 4 additions & 4 deletions retrofit/src/main/java/retrofit/RequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ final class RequestFactory {
private final BaseUrl baseUrl;
private final String relativeUrl;
private final Headers headers;
private final MediaType mediaType;
private final MediaType contentType;
private final boolean hasBody;
private final boolean isFormEncoded;
private final boolean isMultipart;
private final RequestBuilderAction[] requestBuilderActions;

RequestFactory(String method, BaseUrl baseUrl, String relativeUrl, Headers headers,
MediaType mediaType, boolean hasBody, boolean isFormEncoded, boolean isMultipart,
MediaType contentType, boolean hasBody, boolean isFormEncoded, boolean isMultipart,
RequestBuilderAction[] requestBuilderActions) {
this.method = method;
this.baseUrl = baseUrl;
this.relativeUrl = relativeUrl;
this.headers = headers;
this.mediaType = mediaType;
this.contentType = contentType;
this.hasBody = hasBody;
this.isFormEncoded = isFormEncoded;
this.isMultipart = isMultipart;
Expand All @@ -46,7 +46,7 @@ final class RequestFactory {

Request create(Object... args) {
RequestBuilder requestBuilder =
new RequestBuilder(method, baseUrl.url(), relativeUrl, headers, mediaType, hasBody,
new RequestBuilder(method, baseUrl.url(), relativeUrl, headers, contentType, hasBody,
isFormEncoded, isMultipart);

if (args != null) {
Expand Down
6 changes: 3 additions & 3 deletions retrofit/src/main/java/retrofit/RequestFactoryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static RequestFactory parse(Method method, BaseUrl baseUrl,
private boolean isMultipart;
private String relativeUrl;
private com.squareup.okhttp.Headers headers;
private MediaType mediaType;
private MediaType contentType;
private RequestBuilderAction[] requestBuilderActions;

private Set<String> relativeUrlParamNames;
Expand All @@ -80,7 +80,7 @@ private RequestFactoryParser(Method method) {
}

private RequestFactory toRequestFactory(BaseUrl baseUrl) {
return new RequestFactory(httpMethod, baseUrl, relativeUrl, headers, mediaType, hasBody,
return new RequestFactory(httpMethod, baseUrl, relativeUrl, headers, contentType, hasBody,
isFormEncoded, isMultipart, requestBuilderActions);
}

Expand Down Expand Up @@ -184,7 +184,7 @@ private com.squareup.okhttp.Headers parseHeaders(String[] headers) {
String headerName = header.substring(0, colon);
String headerValue = header.substring(colon + 1).trim();
if ("Content-Type".equalsIgnoreCase(headerName)) {
mediaType = MediaType.parse(headerValue);
contentType = MediaType.parse(headerValue);
} else {
builder.add(headerName, headerValue);
}
Expand Down
2 changes: 1 addition & 1 deletion retrofit/src/main/java/retrofit/Retrofit.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private Retrofit(OkHttpClient client, BaseUrl baseUrl, List<Converter.Factory> c
/** Create an implementation of the API defined by the {@code service} interface. */
@SuppressWarnings("unchecked") // Single-interface proxy creation guarded by parameter safety.
public <T> T create(Class<T> service) {
Utils.validateServiceClass(service);
Utils.validateServiceInterface(service);
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
handler);
}
Expand Down
8 changes: 4 additions & 4 deletions retrofit/src/main/java/retrofit/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@ static ResponseBody readBodyToBytesIfNecessary(final ResponseBody body) throws I
}

BufferedSource source = body.source();
final Buffer buffer = new Buffer();
Buffer buffer = new Buffer();
buffer.writeAll(source);
source.close();

return ResponseBody.create(body.contentType(), body.contentLength(), buffer);
}

static <T> void validateServiceClass(Class<T> service) {
static <T> void validateServiceInterface(Class<T> service) {
if (!service.isInterface()) {
throw new IllegalArgumentException("Only interface baseUrl definitions are supported.");
throw new IllegalArgumentException("API declarations must be interfaces.");
}
// Prevent API interfaces from extending other interfaces. This not only avoids a bug in
// Android (http://b.android.com/58753) but it forces composition of API declarations which is
// the recommended pattern.
if (service.getInterfaces().length > 0) {
throw new IllegalArgumentException("Interface definitions must not extend other interfaces.");
throw new IllegalArgumentException("API interfaces must not extend other interfaces.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion retrofit/src/test/java/retrofit/RetrofitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ interface VoidService {
retrofit.create(Extending.class);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Interface definitions must not extend other interfaces.");
assertThat(e).hasMessage("API interfaces must not extend other interfaces.");
}
}

Expand Down

0 comments on commit 38a7779

Please sign in to comment.