package com.mogo.cloud.network

import com.mogo.cloud.network.NetConstants.Companion.CONNECT_TIMEOUT
import com.mogo.cloud.network.NetConstants.Companion.READ_TIMEOUT
import com.mogo.cloud.network.NetConstants.Companion.WRITE_TIMEOUT
import com.mogo.cloud.network.SSLSocketFactoryUtils.createSSLSocketFactory
import com.mogo.cloud.network.SSLSocketFactoryUtils.createTrustAllManager
import com.mogo.cloud.network.cronet.CronetInterceptor
import com.mogo.cloud.network.interceptor.HttpHeaderInterceptor
import com.mogo.cloud.network.interceptor.HttpLoggingInterceptor
import com.mogo.cloud.network.interceptor.HttpPassportInterceptor
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit


/**
 *
 * @Author:         wujifei
 * @CreateDate:     1/20/21 12:16 AM
 * @Description:    获取OkHttpClient
 */
class OkHttpFactory private constructor() {
    companion object {

        val okHttpClient: OkHttpClient by lazy {
            OkHttpClient.Builder()
                    .addInterceptor(HttpHeaderInterceptor())
                    .addInterceptor(HttpPassportInterceptor())
                    .addInterceptor(CronetInterceptor())
                    .addInterceptor(HttpLoggingInterceptor())
                    .sslSocketFactory(createSSLSocketFactory(), createTrustAllManager())
                    .hostnameVerifier(SSLSocketFactoryUtils.TrustAllHostnameVerifier())
                    .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                    .readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                    .eventListenerFactory(WeakNetworkEventFactory.FACTORY)
                    .build()
        }


    }
}