Skip to content

Instantly share code, notes, and snippets.

@iChintanSoni
Last active January 23, 2020 00:34
Show Gist options
  • Save iChintanSoni/e0b0aad5442fb4fd50b88b9701cbff43 to your computer and use it in GitHub Desktop.
Save iChintanSoni/e0b0aad5442fb4fd50b88b9701cbff43 to your computer and use it in GitHub Desktop.
Daggering kotlin-android: AppModule.kt
interface ApiService {
@GET("api")
fun getUsers(@Query("result") result: Int): Observable<RandomUserResponse>
}
@Module
class AppModule {
@Provides
@Singleton
fun file(application: Application): File {
val file = File(application.cacheDir, "OkHttpCache")
file.mkdirs()
return file
}
@Provides
@Singleton
fun cache(file: File): Cache {
return Cache(file, 10 * 1000 * 1000) // 10 mb cache file
}
@Singleton
@Provides
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
val logging = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message -> Timber.tag("OkHttp").d(message) })
logging.level = HttpLoggingInterceptor.Level.BODY
return logging
}
@Provides
@Singleton
fun okHttpClient(cache: Cache, httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
return OkHttpClient.Builder()
.cache(cache)
.addInterceptor(httpLoggingInterceptor)
.build()
}
@Singleton
@Provides
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
@Singleton
@Provides
fun provideApiService(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment