Advertisement
BlackZerg

Untitled

Oct 19th, 2021
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.89 KB | None | 0 0
  1. val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).apply {
  2.         tlsVersions(TlsVersion.TLS_1_2)
  3.         cipherSuites(
  4.             CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  5.             CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  6.             CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  7.         )
  8.     }.build()
  9.  
  10.     fun initKTorClient(
  11.         context: Context,
  12.         authCookieStorage: AuthCookieStorage,
  13.         connectivityInterceptor: ConnectivityInterceptor,
  14.         errorCodeInterceptor: ErrorCodeInterceptor
  15.     ) = HttpClient(OkHttp) {
  16.         val cacheSize = (10 * 1024 * 1024).toLong()
  17.         val httpCacheDirectory = File(context.cacheDir, "offlineCache")
  18.         val httpCache = Cache(httpCacheDirectory, cacheSize)
  19.  
  20.         install(JsonFeature) {
  21.             serializer = GsonSerializer {
  22.                 setPrettyPrinting()
  23.                 disableHtmlEscaping()
  24.             }
  25.             acceptContentTypes = acceptContentTypes + ContentType("application", "json")
  26.  
  27.             engine {
  28.                 config {
  29.                     followRedirects(true)
  30.  
  31.                     cache(httpCache)
  32.                     connectionSpecs(Collections.singletonList(connectionSpec))
  33.                 }
  34.                 addInterceptor(offlineCacheInterceptor(context))
  35.                 addInterceptor(connectivityInterceptor)
  36.                 addInterceptor(errorCodeInterceptor)
  37.                 addNetworkInterceptor(cacheInterceptor())
  38.  
  39.                 clientCacheSize = (10 * 1024 * 1024)
  40.             }
  41.         }
  42.  
  43.         install(HttpTimeout) {
  44.             requestTimeoutMillis = TIME_OUT
  45.         }
  46.  
  47.         install(Logging) {
  48.             logger = object : Logger {
  49.                 override fun log(message: String) {
  50.                     Log.v("KTor", "Logger Ktor => $message")
  51.                 }
  52.             }
  53.             level = LogLevel.ALL
  54.         }
  55.  
  56.         install(DefaultRequest) {
  57.             headers {
  58.                 append(HttpHeaders.Accept, "application/json")
  59.             }
  60.         }
  61.  
  62.         install(HttpCookies) {
  63.             storage = authCookieStorage
  64.         }
  65.  
  66.         HttpResponseValidator {
  67.             validateResponse { response ->
  68.                 Log.v("HTTPClient ->", "Response code => ${response.status.value}")
  69.                 Log.v("HTTPClient ->", "Response description => ${response.status.description}")
  70.                 val cookieAuth = response.headers["set-cookie"]
  71.                 if (cookieAuth == null) Log.v("HTTPClient ->", "cookieAuth ")
  72.                 if (BuildConfig.DEBUG)
  73.                     Log.v("HTTPClient ->", "auth cookie \'set-cookie\' => $cookieAuth}")
  74.             }
  75.  
  76.             handleResponseException { ex ->
  77.                 Log.v("HTTPClient ->", "Exception => ${ex.cause}")
  78.                 Log.v("HTTPClient ->", "Exception message => ${ex.message}")
  79.             }
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement