Skip to content

Instantly share code, notes, and snippets.

@dustin-graham
Created March 25, 2014 22:09
Show Gist options
  • Save dustin-graham/9772518 to your computer and use it in GitHub Desktop.
Save dustin-graham/9772518 to your computer and use it in GitHub Desktop.
A simple class for caching RetroFit REST clients
public class ServiceClient {
public interface ServiceClientDelegate {
Client getClient();
}
private static ServiceClient instance;
private final int CONNECTION_TIMEOUT = 60000;
private RestAdapter mRestAdapter;
private Map<String, Object> mClients = new HashMap<String, Object>();
public static ServiceClient getInstance() {
if (null == instance) {
instance = new ServiceClient();
}
return instance;
}
public void configureRestAdapter(String baseServerPath, Client client) {
mRestAdapter = new RestAdapter.Builder().setServer(new Server(baseServerPath)).setClient(client).build();
}
@SuppressWarnings("unchecked")
public <T> T getClient(Class<T> clazz) {
if (mRestAdapter == null) {
return null;
}
T client = null;
if ((client = (T) mClients.get(clazz.getCanonicalName())) != null) {
return client;
}
client = mRestAdapter.create(clazz);
mClients.put(clazz.getCanonicalName(), client);
return client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment