Created
October 17, 2015 16:49
-
-
Save patrickhammond/0df8a8dc3c7fa9d6ef71 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
import android.app.Application; | |
import com.fasterxml.jackson.databind.DeserializationFeature; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.squareup.okhttp.OkHttpClient; | |
import javax.inject.Singleton; | |
import dagger.Module; | |
import dagger.Provides; | |
import retrofit.Endpoint; | |
import retrofit.Endpoints; | |
import retrofit.RestAdapter; | |
import retrofit.client.Client; | |
import retrofit.converter.Converter; | |
import retrofit.converter.JacksonConverter; | |
@Module | |
public class ServiceModule { | |
private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB | |
@Singleton | |
@Provides | |
OkHttpClient provideOkHttpClient(Application app) { | |
OkHttpClient client = new OkHttpClient(); | |
// Install an HTTP cache in the application cache directory. | |
File cacheDir = new File(app.getCacheDir(), "http"); | |
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); | |
client.setCache(cache); | |
return client; | |
} | |
@Singleton | |
@Provides | |
Client provideClient(OkHttpClient client) { | |
return new OkClient(client); | |
} | |
@Singleton | |
@Provides | |
Endpoint provideEndpoint() { | |
return Endpoints.newFixedEndpoint("http://www.client.com"); | |
} | |
@Singleton | |
@Provides | |
ObjectMapper provideObjectMapper() { | |
ObjectMapper objectMapper = new ObjectMapper(); | |
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
return objectMapper; | |
} | |
@Singleton | |
@Provides | |
Converter provideConverter(ObjectMapper objectMapper) { | |
return new JacksonConverter(objectMapper); | |
} | |
@Singleton | |
@Provides | |
RestAdapter provideRestAdapter(Client client, Endpoint endpoint, Converter converter) { | |
return new RestAdapter.Builder() | |
.setClient(client) | |
.setConverter(converter) | |
.setEndpoint(endpoint) | |
.build(); | |
} | |
@Singleton | |
@Provides | |
ClientApiService provideApiService(RestAdapter restAdapter) { | |
return restAdapter.create(ClientApiService.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment