Skip to content

Instantly share code, notes, and snippets.

@falvojr
Created January 12, 2017 16:18
Show Gist options
  • Save falvojr/8ed285296a53052086d9a346018845ce to your computer and use it in GitHub Desktop.
Save falvojr/8ed285296a53052086d9a346018845ce to your computer and use it in GitHub Desktop.
@Module
public class NetworkModule {
static final String RETROFIT_GITHUB = "GitHub";
static final String RETROFIT_GITHUB_STATUS = "GitHubStatus";
static final String RETROFIT_GITHUB_OAUTH = "GitHubOAuth";
private static final int CACHE_SIZE_10_MB = 10 * 1024 * 1024;
@Provides
@Singleton
Cache providesOkHttpCache(Application application) {
int cacheSize = CACHE_SIZE_10_MB;
return new Cache(application.getCacheDir(), cacheSize);
}
@Provides
@Singleton
Gson providesGson() {
return new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
.create();
}
@Provides
@Singleton
HttpLoggingInterceptor providesHttpLoggingInterceptor() {
final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
@Provides
@Singleton
OkHttpClient providesOkHttpClient(Cache cache,
HttpLoggingInterceptor interceptor) {
return new OkHttpClient.Builder()
.addInterceptor(interceptor)
.cache(cache).build();
}
@Provides
@Singleton
GsonConverterFactory providesGsonConverterFactory(Gson gson) {
return GsonConverterFactory.create(gson);
}
@Provides
@Singleton
RxJavaCallAdapterFactory providesRxJavaCallAdapterFactory() {
return RxJavaCallAdapterFactory.create();
}
@Provides
@Singleton
@Named(RETROFIT_GITHUB)
Retrofit providesRetrofitGitHub(OkHttpClient okHttpClient,
GsonConverterFactory gsonFactory,
RxJavaCallAdapterFactory rxFactory) {
return buildRetrofit(okHttpClient,
gsonFactory,
rxFactory,
GitHubService.BASE_URL);
}
@Provides
@Singleton
@Named(RETROFIT_GITHUB_STATUS)
Retrofit providesRetrofitGitHubStatus(OkHttpClient okHttpClient,
GsonConverterFactory gsonFactory,
RxJavaCallAdapterFactory rxFactory) {
return buildRetrofit(okHttpClient,
gsonFactory,
rxFactory,
GitHubStatusService.BASE_URL);
}
@Provides
@Singleton
@Named(RETROFIT_GITHUB_OAUTH)
Retrofit providesRetrofitGitHubOAuth(OkHttpClient okHttpClient,
GsonConverterFactory gsonFactory,
RxJavaCallAdapterFactory rxFactory) {
return buildRetrofit(okHttpClient,
gsonFactory,
rxFactory,
GitHubOAuthService.BASE_URL);
}
private Retrofit buildRetrofit(OkHttpClient okHttpClient,
GsonConverterFactory converterFactory,
RxJavaCallAdapterFactory callAdapterFactory,
String baseUrl) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(converterFactory)
.addCallAdapterFactory(callAdapterFactory)
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment