Permalink
Browse files

Simplify callback system to generalize usage.

  • Loading branch information...
JakeWharton committed Dec 19, 2012
1 parent f87f9b3 commit 7008c62cfa18ee47997324a8d94e97a656431d38
View
@@ -52,7 +52,7 @@
<property name="max" value="100"/>
</module>
<module name="MethodLength"/>
- <module name="ParameterNumber"/>
+ <!--module name="ParameterNumber"/-->
<!-- Checks for whitespace -->
@@ -1,4 +1,4 @@
-// Copyright 2010 Square, Inc.
+// Copyright 2012 Square, Inc.
package retrofit.http;
/**
@@ -10,65 +10,12 @@
*/
public interface Callback<T> {
- /**
- * Handles a response.
- *
- * @param t response
- */
- void call(T t);
-
- /**
- * The session expired or the account has been disabled. Prompt the user to log in again.
- *
- * @param error message to show user, or null if no message was returned
- */
- void sessionExpired(ServerError error);
-
- /** Couldn't reach the server. Check network settings and try again. */
- void networkError();
-
- /**
- * The server returned a client error. In most cases, this is a programming error, but it can also
- * signify a user input error.
- *
- * @param statusCode the HTTP response code, typically 4XX
- */
- void clientError(T response, int statusCode);
-
- /**
- * We reached the server, but it encountered an error (5xx) or its response was unparseable.
- * Please try again later.
- *
- * @param error message to show user, or null if no message was returned
- * @param statusCode the HTTP response code
- */
- void serverError(ServerError error, int statusCode);
+ /** Successful HTTP response. */
+ void success(T t);
/**
- * An unexpected error occurred. Called if the framework throws an unexpected exception or if the
- * server returns a 400 (Bad Request) error. In either case, the client software likely contains a
- * bug; otherwise, the error would have been caught sooner. The user should try updating their
- * client.
+ * Unsuccessful HTTP response due to network failure, non-2XX status code, or unexpected
+ * exception.
*/
- void unexpectedError(Throwable t);
-
- /** JSON object for parsing server error responses. */
- static final class ServerError {
- public final String message;
-
- public ServerError(String message) {
- this.message = message;
- }
-
- @Override public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- ServerError that = (ServerError) o;
- return message == null ? that.message == null : message.equals(that.message);
- }
-
- @Override public int hashCode() {
- return message != null ? message.hashCode() : 0;
- }
- }
+ void failure(RetrofitError error);
}
@@ -1,13 +1,6 @@
// Copyright 2012 Square, Inc.
package retrofit.http;
-import retrofit.http.Callback.ServerError;
-import retrofit.http.RestException.ClientHttpException;
-import retrofit.http.RestException.NetworkException;
-import retrofit.http.RestException.ServerHttpException;
-import retrofit.http.RestException.UnauthorizedHttpException;
-import retrofit.http.RestException.UnexpectedException;
-
import java.util.concurrent.Executor;
/**
@@ -31,43 +24,13 @@
final Object response = obtainResponse();
callbackExecutor.execute(new Runnable() {
@Override public void run() {
- callback.call((T) response);
- }
- });
- } catch (final ClientHttpException ce) {
- callbackExecutor.execute(new Runnable() {
- @Override public void run() {
- callback.clientError((T) ce.getResponse(), ce.getStatus());
- }
- });
- } catch (final ServerHttpException se) {
- callbackExecutor.execute(new Runnable() {
- @Override public void run() {
- callback.serverError((ServerError) se.getResponse(), se.getStatus());
- }
- });
- } catch (final UnauthorizedHttpException ue) {
- callbackExecutor.execute(new Runnable() {
- @Override public void run() {
- callback.sessionExpired((ServerError) ue.getResponse());
- }
- });
- } catch (final NetworkException ne) {
- callbackExecutor.execute(new Runnable() {
- @Override public void run() {
- callback.networkError();
- }
- });
- } catch (final UnexpectedException ue) {
- callbackExecutor.execute(new Runnable() {
- @Override public void run() {
- callback.unexpectedError(ue.getCause());
+ callback.success((T) response);
}
});
- } catch (final Throwable t) {
+ } catch (final RetrofitError e) {
callbackExecutor.execute(new Runnable() {
@Override public void run() {
- callback.unexpectedError(t);
+ callback.failure(e);
}
});
}
@@ -0,0 +1,43 @@
+// Copyright 2012 Square, Inc.
+package retrofit.http;
+
+/** Represents an HTTP header name/value pair. */
+public final class Header {
+ private final String name;
+ private final String value;
+
+ Header(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ @Override public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Header header = (Header) o;
+
+ if (name != null ? !name.equals(header.name) : header.name != null) return false;
+ if (value != null ? !value.equals(header.value) : header.value != null) return false;
+
+ return true;
+ }
+
+ @Override public int hashCode() {
+ int result = name != null ? name.hashCode() : 0;
+ result = 31 * result + (value != null ? value.hashCode() : 0);
+ return result;
+ }
+
+ @Override public String toString() {
+ return (name != null ? name : "") + ": " + (value != null ? value : "");
+ }
+}
@@ -1,3 +1,4 @@
+// Copyright 2012 Square, Inc.
package retrofit.http;
import java.io.IOException;
@@ -15,25 +16,14 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Provider;
-import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;
-import retrofit.http.Callback.ServerError;
import retrofit.http.HttpProfiler.RequestInformation;
-import retrofit.http.RestException.ClientHttpException;
-import retrofit.http.RestException.HttpException;
-import retrofit.http.RestException.NetworkException;
-import retrofit.http.RestException.ServerHttpException;
-import retrofit.http.RestException.UnauthorizedHttpException;
-import retrofit.http.RestException.UnexpectedException;
-
-import static java.util.logging.Level.WARNING;
-import static org.apache.http.HttpStatus.SC_UNAUTHORIZED;
/**
* Converts Java method calls to Rest calls.
@@ -50,17 +40,18 @@
private final Provider<HttpClient> httpClientProvider;
private final Executor httpExecutor;
private final Executor callbackExecutor;
- private final Headers headers;
+ private final Headers requestHeaders;
private final Converter converter;
private final HttpProfiler profiler;
private RestAdapter(Server server, Provider<HttpClient> httpClientProvider, Executor httpExecutor,
- Executor callbackExecutor, Headers headers, Converter converter, HttpProfiler profiler) {
+ Executor callbackExecutor, Headers requestHeaders, Converter converter,
+ HttpProfiler profiler) {
this.server = server;
this.httpClientProvider = httpClientProvider;
this.httpExecutor = httpExecutor;
this.callbackExecutor = callbackExecutor;
- this.headers = headers;
+ this.requestHeaders = requestHeaders;
this.converter = converter;
this.profiler = profiler;
}
@@ -79,7 +70,7 @@ private RestAdapter(Server server, Provider<HttpClient> httpClientProvider, Exec
* response will be converted to the callback's parameter type using the specified
* {@link Converter}. If the callback parameter type uses a wildcard, the lower bound will be used
* as the conversion type.</li>
- * <li>On the current thread returning the response or throwing a {@link RestException}. The HTTP
+ * <li>On the current thread returning the response or throwing a {@link RetrofitError}. The HTTP
* response will be converted to the method's return type using the specified
* {@link Converter}.</li>
* </ul>
@@ -126,11 +117,7 @@ public Object invoke(Object proxy, final Method method, final Object[] args) {
* Execute an HTTP request.
*
* @return HTTP response object of specified {@code type}.
- * @throws ClientHttpException If HTTP 4XX error occurred.
- * @throws UnauthorizedHttpException If HTTP 401 error occurred.
- * @throws ServerHttpException If HTTP 5XX error occurred.
- * @throws NetworkException If the {@code request} URL was unreachable.
- * @throws UnexpectedException If an exception was thrown while processing the request.
+ * @throws RetrofitError Thrown if any error occurs during the HTTP request.
*/
private Object invokeRequest(Method method, Object[] args, boolean isSynchronousInvocation) {
long start = System.nanoTime();
@@ -141,7 +128,7 @@ private Object invokeRequest(Method method, Object[] args, boolean isSynchronous
.setMethod(method, isSynchronousInvocation)
.setArgs(args)
.setApiUrl(url)
- .setHeaders(headers)
+ .setHeaders(requestHeaders)
.build();
url = request.getURI().toString();
@@ -182,43 +169,30 @@ private Object invokeRequest(Method method, Object[] args, boolean isSynchronous
logResponseBody(url, body, statusCode, elapsedTime);
}
- try {
- if (statusCode >= 200 && statusCode < 300) { // 2XX == successful request
- return converter.to(body, type);
- } else if (statusCode == SC_UNAUTHORIZED) { // 401 == unauthorized user
- ServerError serverError = (ServerError) converter.to(body, ServerError.class);
- throw new UnauthorizedHttpException(url, statusLine.getReasonPhrase(), serverError);
- } else if (statusCode >= 500) { // 5XX == server error
- ServerError serverError = (ServerError) converter.to(body, ServerError.class);
- throw new ServerHttpException(url, statusCode, statusLine.getReasonPhrase(),
- serverError);
- } else { // 4XX == client error
- Object clientError = converter.to(body, type);
- throw new ClientHttpException(url, statusCode, statusLine.getReasonPhrase(),
- clientError);
+ org.apache.http.Header[] realHeaders = response.getAllHeaders();
+ Header[] headers = null;
+ if (realHeaders != null) {
+ headers = new Header[realHeaders.length];
+ for (int i = 0; i < realHeaders.length; i++) {
+ org.apache.http.Header realHeader = realHeaders[i];
+ headers[i] = new Header(realHeader.getName(), realHeader.getValue());
}
- } catch (ConversionException e) {
- LOGGER.log(WARNING, e.getMessage() + " from " + url, e);
- throw new ServerHttpException(url, statusCode, statusLine.getReasonPhrase(), e);
}
- } catch (HttpException e) {
- if (LOGGER.isLoggable(Level.FINE)) {
- LOGGER.fine("Sever returned "
- + e.getStatus()
- + ", "
- + e.getMessage()
- + ". Body: "
- + e.getResponse()
- + ". Url: "
- + e.getUrl());
+
+ if (statusCode >= 200 && statusCode < 300) { // 2XX == successful request
+ try {
+ return converter.to(body, type);
+ } catch (ConversionException e) {
+ throw RetrofitError.conversionError(url, converter, statusCode, headers, body, type, e);
+ }
}
- throw e; // Allow any rest-related exceptions to pass through.
+ throw RetrofitError.httpError(url, converter, statusCode, headers, body, type);
+ } catch (RetrofitError e) {
+ throw e; // Pass through our own errors.
} catch (IOException e) {
- LOGGER.log(WARNING, e.getMessage() + " from " + url, e);
- throw new NetworkException(url, e);
+ throw RetrofitError.networkError(url, e);
} catch (Throwable t) {
- LOGGER.log(WARNING, t.getMessage() + " from " + url, t);
- throw new UnexpectedException(url, t);
+ throw RetrofitError.unexpectedError(url, t);
}
}
}
@@ -251,7 +225,7 @@ private static void logResponseBody(String url, byte[] body, int statusCode, lon
HttpEntity entity = entityReq.getEntity();
contentLength = entity.getContentLength();
- Header entityContentType = entity.getContentType();
+ org.apache.http.Header entityContentType = entity.getContentType();
contentType = entityContentType != null ? entityContentType.getValue() : null;
}
Oops, something went wrong.

0 comments on commit 7008c62

Please sign in to comment.