Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TokenInterceptor(
- private val repository: Repository
- ) : Interceptor {
- override fun intercept(chain: Interceptor.Chain): Response =
- chain.proceed(
- chain.request().newBuilder().apply {
- addHeader("Accept", "application/json")
- addHeader("Content-Type", "application/json")
- repository.token?.let {
- addHeader("Authorization", "Bearer $it")
- }
- }.build()
- )
- }
- // modules.kt
- val applicationModule = module {
- single { androidApplication() as App }
- }
- val networkModule = module {
- // single<ApiClient> {
- // ApiClientImpl(if (BuildConfig.DEBUG) DEV_API_URL else API_MAIN_URL, androidContext())
- // }
- val logger = run {
- val interceptor = HttpLoggingInterceptor()
- interceptor.apply {
- interceptor.level =
- if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
- }
- }
- // val logging = HttpLoggingInterceptor()
- // logging.level = (HttpLoggingInterceptor.Level.BODY)
- val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
- .tlsVersions(TlsVersion.TLS_1_2)
- .cipherSuites(
- CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
- CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
- CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
- )
- .build()
- fun httpClient(context: Context): OkHttpClient {
- val cacheSize = (10 * 1024 * 1024).toLong()
- val httpCacheDirectory = File(context.cacheDir, "offlineCache")
- val httpCache = Cache(httpCacheDirectory, cacheSize)
- return OkHttpClient.Builder()
- .cache(httpCache)
- .connectionSpecs(Collections.singletonList(connectionSpec))
- .addNetworkInterceptor(cacheInterceptor())
- .addInterceptor(offlineCacheInterceptor(context))
- .addInterceptor(tokenInterceptor()) // Вот здесь ошибка!!!!
- .addInterceptor(logger)
- .build()
- }
- fun provideRetrofit(httpClient: OkHttpClient, url: String): Retrofit =
- Retrofit.Builder()
- .baseUrl(url)
- .addConverterFactory(GsonConverterFactory.create())
- .client(httpClient)
- .build()
- single { TokenInterceptor(get()) }
- single { httpClient(androidContext()) }
- single { provideRetrofit(get(), if (BuildConfig.DEBUG) DEV_API_URL else API_MAIN_URL) }
- }
- val storageModule = module {
- factory<SharedPreferences> { androidContext().getSharedPreferences("local_storage", Context.MODE_PRIVATE) }
- }
- val dataBaseModule = module {
- fun provideDB(application: Application): RoomDB = RoomDB.getDataBase(application)
- fun provideTrainingsDao(db: RoomDB): TrainingsDao = db.trainingsDao()
- single { provideDB(androidApplication()) }
- single { provideTrainingsDao(get()) }
- }
- val repositoryModule = module {
- fun provideDataBaseRepository(trainingsDao: TrainingsDao): DataBase =
- DataBaseRepository(trainingsDao)
- single { provideDataBaseRepository(get()) }
- single<LocalStorage> { LocalStorageImpl(get()) }
- single<Repository> { RepositoryImpl(get(), get()) }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement