Advertisement
BlackZerg

Untitled

Mar 16th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.28 KB | None | 0 0
  1. class TokenInterceptor(
  2.     private val repository: Repository
  3. ) : Interceptor {
  4.     override fun intercept(chain: Interceptor.Chain): Response =
  5.         chain.proceed(
  6.             chain.request().newBuilder().apply {
  7.                 addHeader("Accept", "application/json")
  8.                 addHeader("Content-Type", "application/json")
  9.                 repository.token?.let {
  10.                     addHeader("Authorization", "Bearer $it")
  11.                 }
  12.             }.build()
  13.         )
  14. }
  15.  
  16. // modules.kt
  17. val applicationModule = module {
  18.     single { androidApplication() as App }
  19. }
  20.  
  21. val networkModule = module {
  22. //    single<ApiClient> {
  23. //        ApiClientImpl(if (BuildConfig.DEBUG) DEV_API_URL else API_MAIN_URL, androidContext())
  24. //    }
  25.  
  26.     val logger = run {
  27.         val interceptor = HttpLoggingInterceptor()
  28.         interceptor.apply {
  29.             interceptor.level =
  30.                 if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
  31.         }
  32.     }
  33.  
  34. //    val logging = HttpLoggingInterceptor()
  35. //    logging.level = (HttpLoggingInterceptor.Level.BODY)
  36.  
  37.     val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
  38.         .tlsVersions(TlsVersion.TLS_1_2)
  39.         .cipherSuites(
  40.             CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  41.             CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  42.             CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  43.         )
  44.         .build()
  45.  
  46.     fun httpClient(context: Context): OkHttpClient {
  47.         val cacheSize = (10 * 1024 * 1024).toLong()
  48.         val httpCacheDirectory = File(context.cacheDir, "offlineCache")
  49.         val httpCache = Cache(httpCacheDirectory, cacheSize)
  50.  
  51.         return OkHttpClient.Builder()
  52.             .cache(httpCache)
  53.             .connectionSpecs(Collections.singletonList(connectionSpec))
  54.             .addNetworkInterceptor(cacheInterceptor())
  55.             .addInterceptor(offlineCacheInterceptor(context))
  56.             .addInterceptor(tokenInterceptor()) // Вот здесь ошибка!!!!
  57.             .addInterceptor(logger)
  58.             .build()
  59.     }
  60.  
  61.     fun provideRetrofit(httpClient: OkHttpClient, url: String): Retrofit =
  62.         Retrofit.Builder()
  63.             .baseUrl(url)
  64.             .addConverterFactory(GsonConverterFactory.create())
  65.             .client(httpClient)
  66.             .build()
  67.  
  68.     single { TokenInterceptor(get()) }
  69.     single { httpClient(androidContext()) }
  70.     single { provideRetrofit(get(), if (BuildConfig.DEBUG) DEV_API_URL else API_MAIN_URL) }
  71. }
  72.  
  73. val storageModule = module {
  74.     factory<SharedPreferences> { androidContext().getSharedPreferences("local_storage", Context.MODE_PRIVATE) }
  75. }
  76.  
  77. val dataBaseModule = module {
  78.     fun provideDB(application: Application): RoomDB = RoomDB.getDataBase(application)
  79.     fun provideTrainingsDao(db: RoomDB): TrainingsDao = db.trainingsDao()
  80.  
  81.     single { provideDB(androidApplication()) }
  82.     single { provideTrainingsDao(get()) }
  83. }
  84.  
  85. val repositoryModule = module {
  86.     fun provideDataBaseRepository(trainingsDao: TrainingsDao): DataBase =
  87.         DataBaseRepository(trainingsDao)
  88.  
  89.     single { provideDataBaseRepository(get()) }
  90.     single<LocalStorage> { LocalStorageImpl(get()) }
  91.     single<Repository> { RepositoryImpl(get(), get()) }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement