Clean up NetworkModule (#1276)

Rename prefixes from `provides` to `provide`

Add Named annotations to all OkHttp and Retrofit providers

Remove RPAN OkHttp client
This commit is contained in:
Sergei Kozelko 2022-12-15 20:23:41 +08:00 committed by GitHub
parent 99f10bddbc
commit 20c729244e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,8 +22,9 @@ import retrofit2.converter.scalars.ScalarsConverterFactory;
abstract class NetworkModule { abstract class NetworkModule {
@Provides @Provides
@Named("base")
@Singleton @Singleton
static OkHttpClient providesBaseOkhttp() { static OkHttpClient provideBaseOkhttp() {
return new OkHttpClient.Builder() return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS)
@ -32,8 +33,9 @@ abstract class NetworkModule {
} }
@Provides @Provides
@Named("base")
@Singleton @Singleton
static Retrofit providesBaseRetrofit(OkHttpClient okHttpClient) { static Retrofit provideBaseRetrofit(@Named("base") OkHttpClient okHttpClient) {
return new Retrofit.Builder() return new Retrofit.Builder()
.baseUrl(APIUtils.API_BASE_URI) .baseUrl(APIUtils.API_BASE_URI)
.client(okHttpClient) .client(okHttpClient)
@ -44,19 +46,20 @@ abstract class NetworkModule {
} }
@Provides @Provides
static ConnectionPool providesConnectionPool() { static ConnectionPool provideConnectionPool() {
return new ConnectionPool(0, 1, TimeUnit.NANOSECONDS); return new ConnectionPool(0, 1, TimeUnit.NANOSECONDS);
} }
@Provides @Provides
@Named("no_oauth") @Named("no_oauth")
static Retrofit provideRetrofit(Retrofit retrofit) { static Retrofit provideRetrofit(@Named("base") Retrofit retrofit) {
return retrofit; return retrofit;
} }
@Provides @Provides
@Named("oauth") @Named("oauth")
static Retrofit providesOAuthRetrofit(Retrofit retrofit, @Named("default") OkHttpClient okHttpClient) { static Retrofit provideOAuthRetrofit(@Named("base") Retrofit retrofit,
@Named("default") OkHttpClient okHttpClient) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.OAUTH_API_BASE_URI) .baseUrl(APIUtils.OAUTH_API_BASE_URI)
.client(okHttpClient) .client(okHttpClient)
@ -66,8 +69,8 @@ abstract class NetworkModule {
@Provides @Provides
@Named("default") @Named("default")
@Singleton @Singleton
static OkHttpClient provideOkHttpClient(OkHttpClient httpClient, static OkHttpClient provideOkHttpClient(@Named("base") OkHttpClient httpClient,
Retrofit retrofit, @Named("base") Retrofit retrofit,
RedditDataRoomDatabase accountRoomDatabase, RedditDataRoomDatabase accountRoomDatabase,
@Named("current_account") SharedPreferences currentAccountSharedPreferences, @Named("current_account") SharedPreferences currentAccountSharedPreferences,
ConnectionPool connectionPool) { ConnectionPool connectionPool) {
@ -80,7 +83,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("oauth_without_authenticator") @Named("oauth_without_authenticator")
@Singleton @Singleton
static Retrofit provideOauthWithoutAuthenticatorRetrofit(Retrofit retrofit) { static Retrofit provideOauthWithoutAuthenticatorRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.OAUTH_API_BASE_URI) .baseUrl(APIUtils.OAUTH_API_BASE_URI)
.build(); .build();
@ -89,7 +92,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("upload_media") @Named("upload_media")
@Singleton @Singleton
static Retrofit provideUploadMediaRetrofit(Retrofit retrofit) { static Retrofit provideUploadMediaRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.API_UPLOAD_MEDIA_URI) .baseUrl(APIUtils.API_UPLOAD_MEDIA_URI)
.build(); .build();
@ -98,7 +101,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("upload_video") @Named("upload_video")
@Singleton @Singleton
static Retrofit provideUploadVideoRetrofit(Retrofit retrofit) { static Retrofit provideUploadVideoRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.API_UPLOAD_VIDEO_URI) .baseUrl(APIUtils.API_UPLOAD_VIDEO_URI)
.build(); .build();
@ -107,7 +110,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("download_media") @Named("download_media")
@Singleton @Singleton
static Retrofit provideDownloadRedditVideoRetrofit(Retrofit retrofit) { static Retrofit provideDownloadRedditVideoRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl("http://localhost/") .baseUrl("http://localhost/")
.build(); .build();
@ -116,7 +119,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("gfycat") @Named("gfycat")
@Singleton @Singleton
static Retrofit provideGfycatRetrofit(Retrofit retrofit) { static Retrofit provideGfycatRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.GFYCAT_API_BASE_URI) .baseUrl(APIUtils.GFYCAT_API_BASE_URI)
.build(); .build();
@ -132,8 +135,8 @@ abstract class NetworkModule {
@Named("redgifs") @Named("redgifs")
@Singleton @Singleton
static Retrofit provideRedgifsRetrofit(@Named("RedgifsAccessTokenAuthenticator") Interceptor accessTokenAuthenticator, static Retrofit provideRedgifsRetrofit(@Named("RedgifsAccessTokenAuthenticator") Interceptor accessTokenAuthenticator,
OkHttpClient httpClient, @Named("base") OkHttpClient httpClient,
Retrofit retrofit, @Named("base") Retrofit retrofit,
ConnectionPool connectionPool) { ConnectionPool connectionPool) {
OkHttpClient.Builder okHttpClientBuilder = httpClient.newBuilder() OkHttpClient.Builder okHttpClientBuilder = httpClient.newBuilder()
.addInterceptor(chain -> chain.proceed( .addInterceptor(chain -> chain.proceed(
@ -154,7 +157,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("imgur") @Named("imgur")
@Singleton @Singleton
static Retrofit provideImgurRetrofit(Retrofit retrofit) { static Retrofit provideImgurRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.IMGUR_API_BASE_URI) .baseUrl(APIUtils.IMGUR_API_BASE_URI)
.build(); .build();
@ -163,7 +166,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("pushshift") @Named("pushshift")
@Singleton @Singleton
static Retrofit providePushshiftRetrofit(Retrofit retrofit) { static Retrofit providePushshiftRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.PUSHSHIFT_API_BASE_URI) .baseUrl(APIUtils.PUSHSHIFT_API_BASE_URI)
.build(); .build();
@ -172,7 +175,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("reveddit") @Named("reveddit")
@Singleton @Singleton
static Retrofit provideRevedditRetrofit(Retrofit retrofit) { static Retrofit provideRevedditRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.REVEDDIT_API_BASE_URI) .baseUrl(APIUtils.REVEDDIT_API_BASE_URI)
.build(); .build();
@ -181,7 +184,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("vReddIt") @Named("vReddIt")
@Singleton @Singleton
static Retrofit provideVReddItRetrofit(Retrofit retrofit) { static Retrofit provideVReddItRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl("http://localhost/") .baseUrl("http://localhost/")
.build(); .build();
@ -190,7 +193,7 @@ abstract class NetworkModule {
@Provides @Provides
@Named("streamable") @Named("streamable")
@Singleton @Singleton
static Retrofit provideStreamableRetrofit(Retrofit retrofit) { static Retrofit provideStreamableRetrofit(@Named("base") Retrofit retrofit) {
return retrofit.newBuilder() return retrofit.newBuilder()
.baseUrl(APIUtils.STREAMABLE_API_BASE_URI) .baseUrl(APIUtils.STREAMABLE_API_BASE_URI)
.build(); .build();