Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).apply {
- 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 initKTorClient(
- context: Context,
- authCookieStorage: AuthCookieStorage,
- connectivityInterceptor: ConnectivityInterceptor,
- errorCodeInterceptor: ErrorCodeInterceptor
- ) = HttpClient(OkHttp) {
- val cacheSize = (10 * 1024 * 1024).toLong()
- val httpCacheDirectory = File(context.cacheDir, "offlineCache")
- val httpCache = Cache(httpCacheDirectory, cacheSize)
- install(JsonFeature) {
- serializer = GsonSerializer {
- setPrettyPrinting()
- disableHtmlEscaping()
- }
- acceptContentTypes = acceptContentTypes + ContentType("application", "json")
- engine {
- config {
- followRedirects(true)
- cache(httpCache)
- connectionSpecs(Collections.singletonList(connectionSpec))
- }
- addInterceptor(offlineCacheInterceptor(context))
- addInterceptor(connectivityInterceptor)
- addInterceptor(errorCodeInterceptor)
- addNetworkInterceptor(cacheInterceptor())
- clientCacheSize = (10 * 1024 * 1024)
- }
- }
- install(HttpTimeout) {
- requestTimeoutMillis = TIME_OUT
- }
- install(Logging) {
- logger = object : Logger {
- override fun log(message: String) {
- Log.v("KTor", "Logger Ktor => $message")
- }
- }
- level = LogLevel.ALL
- }
- install(DefaultRequest) {
- headers {
- append(HttpHeaders.Accept, "application/json")
- }
- }
- install(HttpCookies) {
- storage = authCookieStorage
- }
- HttpResponseValidator {
- validateResponse { response ->
- Log.v("HTTPClient ->", "Response code => ${response.status.value}")
- Log.v("HTTPClient ->", "Response description => ${response.status.description}")
- val cookieAuth = response.headers["set-cookie"]
- if (cookieAuth == null) Log.v("HTTPClient ->", "cookieAuth ")
- if (BuildConfig.DEBUG)
- Log.v("HTTPClient ->", "auth cookie \'set-cookie\' => $cookieAuth}")
- }
- handleResponseException { ex ->
- Log.v("HTTPClient ->", "Exception => ${ex.cause}")
- Log.v("HTTPClient ->", "Exception message => ${ex.message}")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement