Created
March 25, 2014 22:09
-
-
Save dustin-graham/9772518 to your computer and use it in GitHub Desktop.
A simple class for caching RetroFit REST clients
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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