Skip to content

Commit

Permalink
Merge pull request square#40 from square/jwilson/noguice
Browse files Browse the repository at this point in the history
Support JSR-330 instead of Guice.
  • Loading branch information
JakeWharton committed Jul 18, 2012
2 parents 14b98e5 + bc3bc51 commit 961e513
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 92 deletions.
5 changes: 2 additions & 3 deletions http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<classifier>no_aop</classifier>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
2 changes: 1 addition & 1 deletion http/src/main/java/retrofit/http/HttpRequestBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package retrofit.http;

import com.google.gson.Gson;
import com.google.inject.name.Named;
import javax.inject.Named;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicNameValuePair;
Expand Down
60 changes: 21 additions & 39 deletions http/src/main/java/retrofit/http/RestAdapter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package retrofit.http;

import com.google.gson.Gson;
import com.google.inject.Binder;
import com.google.inject.Module;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -37,27 +35,39 @@
@Singleton public class RestAdapter {
private static final Logger LOGGER = Logger.getLogger(RestAdapter.class.getName());

@Inject private Server server;
@Inject private Provider<HttpClient> httpClientProvider;
@Inject private Executor executor;
@Inject private MainThread mainThread;
@Inject private Headers headers;
@Inject private Gson gson;
@Inject private HttpProfiler profiler = HttpProfiler.NONE;
private final Server server;
private final Provider<HttpClient> httpClientProvider;
private final Executor executor;
private final MainThread mainThread;
private final Headers headers;
private final Gson gson;
private final HttpProfiler profiler;

private ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
private final ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
@Override protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("HH:mm:ss");
}
};

@Inject
RestAdapter(Server server, Provider<HttpClient> httpClientProvider, Executor executor,
MainThread mainThread, Headers headers, Gson gson, HttpProfiler profiler) {
this.server = server;
this.httpClientProvider = httpClientProvider;
this.executor = executor;
this.mainThread = mainThread;
this.headers = headers;
this.gson = gson;
this.profiler = profiler;
}

/**
* Adapts a Java interface to a REST API. HTTP requests happen in a background thread. Callbacks
* happen in the UI thread.
*
* <p>Gets the relative path for a given method from a {@link GET}, {@link POST}, {@link PUT}, or
* {@link DELETE} annotation on the method. Gets the names of URL parameters from {@link
* com.google.inject.name.Named} annotations on the method parameters.
* javax.inject.Named} annotations on the method parameters.
*
* <p>The last method parameter should be of type {@link Callback}. The JSON HTTP response will be
* converted to the callback's parameter type using GSON. If the callback parameter type uses a
Expand All @@ -74,40 +84,12 @@
*
* @param type to implement
*/
public static <T> Module service(final Class<T> type) {
return new Module() {
@Override public void configure(Binder binder) {
binder.bind(type).toProvider(createProvider(type));
}
};
}

/**
* Returns a new instance of {@code type} that uses this RestAdapter to
* convert Java method calls to Rest calls.
*/
@SuppressWarnings("unchecked")
public <T> T create(Class<T> type) {
return (T) Proxy.newProxyInstance(type.getClassLoader(),
new Class<?>[] {type}, new RestHandler());
}

/**
* Creates the {@link Provider} instances used by {@link #service(Class)}. Can be used by clients that
* want more control over the implementation of their service interfaces, e.g. to wrap them
* with caching logic.
* <p>
* Before use the provider must be injected via {@link com.google.inject.Injector#injectMembers}.
*/
public static <T> com.google.inject.Provider<T> createProvider(final Class<T> type) {
return new com.google.inject.Provider<T>() {
@Inject RestAdapter restAdapter;
@Override public T get() {
return restAdapter.create(type);
}
};
}

private class RestHandler implements InvocationHandler {

@Override public Object invoke(Object proxy, final Method method, final Object[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
package retrofit.http;

import com.google.gson.Gson;
import com.google.inject.name.Named;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.Set;
import java.util.UUID;
import javax.inject.Named;
import junit.framework.TestCase;
import org.apache.http.HttpMessage;
import org.apache.http.client.methods.HttpGet;
Expand Down
73 changes: 30 additions & 43 deletions http/src/test/java/retrofit/http/RestAdapterTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package retrofit.http;

import com.google.gson.Gson;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Named;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executor;
import javax.inject.Named;
import javax.inject.Provider;
import junit.framework.TestCase;
import org.apache.http.HttpMessage;
import org.apache.http.HttpResponse;
Expand All @@ -33,7 +31,6 @@
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static retrofit.http.RestAdapter.service;

public class RestAdapterTest extends TestCase {
private static final String ID = "123";
Expand All @@ -43,14 +40,13 @@ public class RestAdapterTest extends TestCase {
private static final String PATH_URL_PREFIX = BASE_URL + "/";
private static final String GET_DELETE_SIMPLE_URL = BASE_URL + "?";

private Injector injector;
private RestAdapter restAdapter;
private HttpClient mockHttpClient;
private Executor mockExecutor;
private MainThread mockMainThread;
private Headers mockHeaders;
@SuppressWarnings("rawtypes") private Callback mockCallback;
private HttpResponse mockResponse;
private HttpProfiler mockProfiler;
private Gson gson = new Gson();

@Override @Before public void setUp() throws Exception {
Expand All @@ -60,31 +56,22 @@ public class RestAdapterTest extends TestCase {
mockHeaders = createMock(Headers.class);
mockCallback = createMock(Callback.class);
mockResponse = createMock(HttpResponse.class);
mockProfiler = createMock(HttpProfiler.class);

injector = Guice.createInjector(
new AbstractModule() {
@Override protected void configure() {
bind(Server.class).toInstance(new Server("http://host/api/",
"http://host/web/", true));
bind(HttpClient.class).toInstance(mockHttpClient);
bind(Executor.class).toInstance(mockExecutor);
bind(MainThread.class).toInstance(mockMainThread);
bind(Headers.class).toInstance(mockHeaders);
bind(HttpProfiler.class).toInstance(HttpProfiler.NONE);
install(service(DeleteService.class));
install(service(GetService.class));
install(service(PostService.class));
install(service(PutService.class));
}
});

Server server = new Server("http://host/api/", "http://host/web/", true);
Provider<HttpClient> httpClientProvider = new Provider<HttpClient>() {
@Override public HttpClient get() {
return mockHttpClient;
}
};
restAdapter = new RestAdapter(server, httpClientProvider, mockExecutor, mockMainThread,
mockHeaders, gson, HttpProfiler.NONE);
}

@SuppressWarnings("unchecked") public void testServiceDeleteSimple() throws IOException {
expectLifecycle(HttpDelete.class, GET_DELETE_SIMPLE_URL);
replayAll();

DeleteService service = injector.getInstance(DeleteService.class);
DeleteService service = restAdapter.create(DeleteService.class);
service.delete(mockCallback);
verifyAll();
}
Expand All @@ -93,7 +80,7 @@ public class RestAdapterTest extends TestCase {
expectLifecycle(HttpDelete.class, GET_DELETE_SIMPLE_URL + "id=" + ID);
replayAll();

DeleteService service = injector.getInstance(DeleteService.class);
DeleteService service = restAdapter.create(DeleteService.class);
service.deleteWithParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -103,7 +90,7 @@ public class RestAdapterTest extends TestCase {
+ "id=" + ID);
replayAll();

DeleteService service = injector.getInstance(DeleteService.class);
DeleteService service = restAdapter.create(DeleteService.class);
service.deleteWithFixedParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -114,7 +101,7 @@ public void testServiceDeleteWithMultipleFixedParam() throws IOException {
+ "filter=merchant&name2=value2&"+ "id=" + ID);
replayAll();

DeleteService service = injector.getInstance(DeleteService.class);
DeleteService service = restAdapter.create(DeleteService.class);
service.deleteWithMultipleFixedParams(ID, mockCallback);
verifyAll();
}
Expand All @@ -123,7 +110,7 @@ public void testServiceDeleteWithMultipleFixedParam() throws IOException {
expectLifecycle(HttpDelete.class, PATH_URL_PREFIX + ID + "?");
replayAll();

DeleteService service = injector.getInstance(DeleteService.class);
DeleteService service = restAdapter.create(DeleteService.class);
service.deleteWithPathParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -132,7 +119,7 @@ public void testServiceDeleteWithMultipleFixedParam() throws IOException {
expectLifecycle(HttpGet.class, GET_DELETE_SIMPLE_URL);
replayAll();

GetService service = injector.getInstance(GetService.class);
GetService service = restAdapter.create(GetService.class);
service.get(mockCallback);
verifyAll();
}
Expand All @@ -141,7 +128,7 @@ public void testServiceDeleteWithMultipleFixedParam() throws IOException {
expectLifecycle(HttpGet.class, GET_DELETE_SIMPLE_URL + "id=" + ID);
replayAll();

GetService service = injector.getInstance(GetService.class);
GetService service = restAdapter.create(GetService.class);
service.getWithParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -151,7 +138,7 @@ public void testServiceDeleteWithMultipleFixedParam() throws IOException {
+ "id=" + ID);
replayAll();

GetService service = injector.getInstance(GetService.class);
GetService service = restAdapter.create(GetService.class);
service.getWithFixedParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -162,7 +149,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
+ "filter=merchant&name2=value2&"+ "id=" + ID);
replayAll();

GetService service = injector.getInstance(GetService.class);
GetService service = restAdapter.create(GetService.class);
service.getWithMultipleFixedParams(ID, mockCallback);
verifyAll();
}
Expand All @@ -171,7 +158,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpGet.class, PATH_URL_PREFIX + ID + "?");
replayAll();

GetService service = injector.getInstance(GetService.class);
GetService service = restAdapter.create(GetService.class);
service.getWithPathParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -180,7 +167,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpPost.class, BASE_URL);
replayAll();

PostService service = injector.getInstance(PostService.class);
PostService service = restAdapter.create(PostService.class);
service.post(mockCallback);
verifyAll();
}
Expand All @@ -189,7 +176,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycleClientError(HttpPost.class, BASE_URL);
replayAll();

PostService service = injector.getInstance(PostService.class);
PostService service = restAdapter.create(PostService.class);
service.post(mockCallback);
verifyAll();
}
Expand All @@ -198,7 +185,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycleServerError(HttpPost.class, BASE_URL);
replayAll();

PostService service = injector.getInstance(PostService.class);
PostService service = restAdapter.create(PostService.class);
service.post(mockCallback);
verifyAll();
}
Expand All @@ -207,7 +194,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpPost.class, BASE_URL);
replayAll();

PostService service = injector.getInstance(PostService.class);
PostService service = restAdapter.create(PostService.class);
service.postWithParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -216,7 +203,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpPost.class, PATH_URL_PREFIX + ID);
replayAll();

PostService service = injector.getInstance(PostService.class);
PostService service = restAdapter.create(PostService.class);
service.postWithPathParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -225,7 +212,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpPut.class, BASE_URL);
replayAll();

PutService service = injector.getInstance(PutService.class);
PutService service = restAdapter.create(PutService.class);
service.put(mockCallback);
verifyAll();
}
Expand All @@ -234,7 +221,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpPut.class, BASE_URL);
replayAll();

PutService service = injector.getInstance(PutService.class);
PutService service = restAdapter.create(PutService.class);
service.putWithParam(ID, mockCallback);
verifyAll();
}
Expand All @@ -243,7 +230,7 @@ public void testServiceGetWithMultipleFixedParams() throws IOException {
expectLifecycle(HttpPut.class, PATH_URL_PREFIX + ID);
replayAll();

PutService service = injector.getInstance(PutService.class);
PutService service = restAdapter.create(PutService.class);
service.putWithPathParam(ID, mockCallback);
verifyAll();
}
Expand Down
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
<!-- Dependencies -->
<android.version>2.2.1</android.version>
<gson.version>2.1</gson.version>
<guice.version>3.0</guice.version>
<httpcomponents.version>4.1.3</httpcomponents.version>
<javax.inject.version>1</javax.inject.version>

<!-- Test Dependencies -->
<junit.version>4.10</junit.version>
Expand Down Expand Up @@ -110,10 +110,9 @@
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
<classifier>no_aop</classifier>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>${javax.inject.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down

0 comments on commit 961e513

Please sign in to comment.