Last active
September 3, 2015 16:10
-
-
Save jpotts18/c1daba47059e87f19c55 to your computer and use it in GitHub Desktop.
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
package com.verdad.core; | |
import android.content.Context; | |
import com.google.gson.FieldNamingPolicy; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import java.util.HashMap; | |
import java.util.Map; | |
import retrofit.RequestInterceptor; | |
import retrofit.RestAdapter; | |
import retrofit.converter.GsonConverter; | |
public class RestClient { | |
private final String TAG = this.getClass().getSimpleName(); | |
private static RestClient instance; | |
private RestAdapter mRestAdapter; | |
// creating a kind of cache for using different clients | |
private Map<String, Object> mClients = new HashMap<String, Object>(); | |
// singleton pattern | |
public static RestClient getInstance() { | |
if (null == instance) { | |
instance = new RestClient(); | |
} | |
return instance; | |
} | |
// used on Application.OnCreate() to initialize the service | |
public void configureRestAdapter(final Context context, String baseServerPath, RequestInterceptor interceptor ) { | |
Gson gson = new GsonBuilder() | |
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | |
.create(); | |
mRestAdapter = new RestAdapter.Builder() | |
.setEndpoint(baseServerPath) | |
.setConverter(new GsonConverter(gson)) | |
.setLogLevel(RestAdapter.LogLevel.FULL) | |
.setRequestInterceptor(interceptor) | |
.build(); | |
} | |
@SuppressWarnings("unchecked") | |
public <T> T getClient(Class<T> clazz) { | |
// guard for uninitialized mRestAdapter | |
if (mRestAdapter == null) { | |
return null; | |
} | |
// initializing generic client | |
T client = null; | |
// check service cache for client | |
if ((client = (T) mClients.get(clazz.getCanonicalName())) != null) { | |
return client; | |
} | |
// create a new client and save it in cache | |
client = mRestAdapter.create(clazz); | |
mClients.put(clazz.getCanonicalName(), client); | |
return client; | |
} | |
} |
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
package com.ringseven.verdad.domain; | |
import android.app.Application; | |
import android.content.SharedPreferences; | |
import com.ringseven.verdad.core.RestClient; | |
import com.ringseven.verdad.core.helpers.EncryptionHelper; | |
import com.ringseven.verdad.core.helpers.UserAgentHelper; | |
import com.ringseven.verdad.domain.Constants; | |
import retrofit.RequestInterceptor; | |
import se.simbio.encryption.Encryption; | |
import static com.ringseven.verdad.domain.Constants.Prefs.TOKEN; | |
import static com.ringseven.verdad.domain.Constants.SHARED_PREFS; | |
/** | |
* Created by samgubler on 4/30/15. | |
*/ | |
public class VerdadApplication extends Application { | |
private Session session; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
initializeRestClient(); | |
// Prime the encryption helper | |
EncryptionHelper.primeInstance(Constants.IV); | |
} | |
private void initializeRestClient() { | |
RestClient.getInstance().configureRestAdapter(this, Constants.MOCK_API_URL_STRING, new RequestInterceptor() { | |
@Override | |
public void intercept(RequestFacade request) { | |
request.addHeader("App-User-Agent", UserAgentHelper.agentString(getApplicationContext())); | |
request.addHeader(("Content-Type"), "application/json"); | |
} | |
}); | |
} | |
public Session getSession() { | |
if (session == null) { | |
session = new Session(); | |
} | |
return session; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment