Last active
February 9, 2021 12:07
-
-
Save prokash-sarkar/a6d950ab5fee725ee0aac9c961b1e1cb to your computer and use it in GitHub Desktop.
This Gist is a part of the Article: https://prokash-sarkar.medium.com/unit-testing-with-dagger-2-brewing-a-potion-with-fakes-mocks-and-custom-rules-in-android-7f0ab7b22cb
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
@Module | |
class NetworkModule { | |
@Singleton | |
@Provides | |
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor { | |
return HttpLoggingInterceptor() | |
.setLevel(HttpLoggingInterceptor.Level.BODY) | |
} | |
@Singleton | |
@Provides | |
fun provideOkHttpClient( | |
loggingInterceptor: HttpLoggingInterceptor | |
): OkHttpClient { | |
return OkHttpClient.Builder() | |
.connectTimeout(30, TimeUnit.SECONDS) | |
.writeTimeout(40, TimeUnit.SECONDS) | |
.readTimeout(60, TimeUnit.SECONDS) | |
.addInterceptor(loggingInterceptor) | |
.build() | |
} | |
@Singleton | |
@Provides | |
fun provideGson(): Gson { | |
val gson = GsonBuilder() | |
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | |
.create() | |
return gson | |
} | |
@Singleton | |
@Provides | |
fun provideMoshi(): Moshi { | |
val moshi = Moshi.Builder() | |
.add(KotlinJsonAdapterFactory()) | |
.build() | |
return moshi | |
} | |
@Singleton | |
@Provides | |
fun provideRetrofit( | |
gson: Gson, | |
moshi: Moshi, | |
okHttpClient: OkHttpClient, | |
): Retrofit { | |
return Retrofit.Builder() | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.addConverterFactory(MoshiConverterFactory.create(moshi)) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.baseUrl("https://api.github.com/") | |
.client(okHttpClient) | |
.build() | |
} | |
@Singleton | |
@Provides | |
fun provideApiClient(retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment