Skip to content

Commit

Permalink
Remove OkHttpCall dependency on ServiceMethod
Browse files Browse the repository at this point in the history
Inject the necessary components for it to complete the HTTP request on its own.
  • Loading branch information
JakeWharton committed Jun 15, 2018
1 parent cd333f7 commit a3516fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
17 changes: 11 additions & 6 deletions retrofit/src/main/java/retrofit2/OkHttpCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import static retrofit2.Utils.throwIfFatal;

final class OkHttpCall<T> implements Call<T> {
private final ServiceMethod<T, ?> serviceMethod;
private final RequestFactory requestFactory;
private final @Nullable Object[] args;
private final okhttp3.Call.Factory callFactory;
private final Converter<ResponseBody, T> responseConverter;

private volatile boolean canceled;

Expand All @@ -42,14 +44,17 @@ final class OkHttpCall<T> implements Call<T> {
@GuardedBy("this")
private boolean executed;

OkHttpCall(ServiceMethod<T, ?> serviceMethod, @Nullable Object[] args) {
this.serviceMethod = serviceMethod;
OkHttpCall(RequestFactory requestFactory, @Nullable Object[] args,
okhttp3.Call.Factory callFactory, Converter<ResponseBody, T> responseConverter) {
this.requestFactory = requestFactory;
this.args = args;
this.callFactory = callFactory;
this.responseConverter = responseConverter;
}

@SuppressWarnings("CloneDoesntCallSuperClone") // We are a final type & this saves clearing state.
@Override public OkHttpCall<T> clone() {
return new OkHttpCall<>(serviceMethod, args);
return new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
}

@Override public synchronized Request request() {
Expand Down Expand Up @@ -182,7 +187,7 @@ private void callFailure(Throwable e) {
}

private okhttp3.Call createRawCall() throws IOException {
okhttp3.Call call = serviceMethod.toCall(args);
okhttp3.Call call = callFactory.newCall(requestFactory.create(args));
if (call == null) {
throw new NullPointerException("Call.Factory returned null.");
}
Expand Down Expand Up @@ -215,7 +220,7 @@ Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {

ExceptionCatchingRequestBody catchingBody = new ExceptionCatchingRequestBody(rawBody);
try {
T body = serviceMethod.toResponse(catchingBody);
T body = responseConverter.convert(catchingBody);
return Response.success(body, rawResponse);
} catch (RuntimeException e) {
// If the underlying source threw an exception, propagate that rather than indicating it was
Expand Down
14 changes: 2 additions & 12 deletions retrofit/src/main/java/retrofit2/ServiceMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package retrofit2;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -41,18 +40,9 @@ static <ResponseT, ReturnT> ServiceMethod<ResponseT, ReturnT> parseAnnotations(R
responseConverter = builder.responseConverter;
}

/** Builds an HTTP request from method arguments. */
okhttp3.Call toCall(@Nullable Object[] args) throws IOException {
return callFactory.newCall(requestFactory.create(args));
}

ReturnT invoke(@Nullable Object[] args) {
return callAdapter.adapt(new OkHttpCall<>(this, args));
}

/** Builds a method return value from an HTTP response body. */
ResponseT toResponse(ResponseBody body) throws IOException {
return responseConverter.convert(body);
return callAdapter.adapt(
new OkHttpCall<>(requestFactory, args, callFactory, responseConverter));
}

/**
Expand Down

0 comments on commit a3516fe

Please sign in to comment.