Advertisement
BlackZerg

HttpModule

Sep 22nd, 2021
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.56 KB | None | 0 0
  1. @file:Suppress("EXPERIMENTAL_API_USAGE_FUTURE_ERROR")
  2.  
  3. package bz.lement.pro.di
  4.  
  5. import android.content.Context
  6. import android.util.Log
  7. import bz.lement.pro.data.http.*
  8. import io.ktor.client.*
  9. import io.ktor.client.engine.okhttp.*
  10. import io.ktor.client.features.*
  11. import io.ktor.client.features.cookies.*
  12. import io.ktor.client.features.json.*
  13. import io.ktor.client.features.logging.*
  14. import io.ktor.client.request.*
  15. import io.ktor.http.*
  16. import io.ktor.http.Cookie
  17. import okhttp3.*
  18. import org.koin.android.ext.koin.androidContext
  19. import org.koin.dsl.module
  20. import java.io.File
  21. import java.util.*
  22.  
  23. private const val TIME_OUT = 60_000
  24.  
  25. val networkModule = module {
  26.  
  27.     val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).apply {
  28.         tlsVersions(TlsVersion.TLS_1_2)
  29.         cipherSuites(
  30.             CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  31.             CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  32.             CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  33.         )
  34.     }.build()
  35.  
  36.     fun initKTorClient(
  37.         context: Context,
  38.         connectivityInterceptor: ConnectivityInterceptor,
  39.         errorCodeInterceptor: ErrorCodeInterceptor
  40.     ) = HttpClient(OkHttp) {
  41.         val cacheSize = (10 * 1024 * 1024).toLong()
  42.         val httpCacheDirectory = File(context.cacheDir, "offlineCache")
  43.         val httpCache = Cache(httpCacheDirectory, cacheSize)
  44.         var cookieAuth: String? = null
  45.  
  46.         install(JsonFeature) {
  47. //            serializer = KotlinxSerializer(
  48. //                kotlinx.serialization.json.Json {
  49. //                    prettyPrint = true
  50. //                    isLenient = true
  51. //                    ignoreUnknownKeys = true
  52. //                }
  53. //            )
  54.  
  55.             serializer = GsonSerializer() {
  56.                 setPrettyPrinting()
  57.                 disableHtmlEscaping()
  58.             }
  59.  
  60.             engine {
  61.                 config {
  62.                     followRedirects(true)
  63.  
  64.                     cache(httpCache)
  65.                     connectionSpecs(Collections.singletonList(connectionSpec))
  66.                 }
  67.                 addInterceptor(offlineCacheInterceptor(context))
  68.                 addInterceptor(connectivityInterceptor)
  69.                 addInterceptor(errorCodeInterceptor)
  70.                 addNetworkInterceptor(cacheInterceptor())
  71.  
  72.                 clientCacheSize = (10 * 1024 * 1024)
  73.             }
  74.         }
  75.  
  76.         install(Logging) {
  77.             logger = object : Logger {
  78.                 override fun log(message: String) {
  79.                     Log.v("KTor", "Logger Ktor => $message")
  80.                 }
  81.             }
  82.             level = LogLevel.ALL
  83.         }
  84.  
  85.         install(DefaultRequest) {
  86.             headers {
  87.                 append(HttpHeaders.Accept, "application/json")
  88.             }
  89.         }
  90.  
  91.         install(HttpCookies) {
  92.             storage = AcceptAllCookiesStorage()
  93.  
  94.             storage = ConstantCookiesStorage(
  95.                 Cookie("set-cookie", cookieAuth ?: "")
  96.             )
  97.         }
  98.  
  99.         HttpResponseValidator {
  100.             validateResponse { response ->
  101.                 cookieAuth = response.headers["set-cookie"]
  102.                 Log.v("HTTPClient ->", "set-cookie auth => $cookieAuth}")
  103.             }
  104.         }
  105.     }
  106.  
  107.     factory { ConnectivityInterceptor(context = androidContext()) }
  108.     factory { ErrorCodeInterceptor() }
  109.  
  110.     single {
  111.         initKTorClient(
  112.             context = androidContext(),
  113.             connectivityInterceptor = get(),
  114.             errorCodeInterceptor = get()
  115.         )
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement