daily pastebin goal
87%
SHARE
TWEET

RetrofitClient

a guest Jan 23rd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package id.co.vascomm.smart.support.api
  2.  
  3. import android.util.Log
  4. import id.co.vascomm.smart.BuildConfig
  5. import okhttp3.Interceptor
  6. import okhttp3.OkHttpClient
  7. import okhttp3.logging.HttpLoggingInterceptor
  8. import retrofit2.Retrofit
  9. import retrofit2.converter.scalars.ScalarsConverterFactory
  10. import java.util.concurrent.TimeUnit
  11.  
  12. class MainClient {
  13.     companion object {
  14.         //const val BASE_URL_AP = "http://192.168.4.1:80"
  15.         const val BASE_URL_AP = "http://169.254.14.254:9998"
  16.         //const val BASE_URL_END = "http://11.0.0.54:5000"
  17. //        const val BASE_URL_END = "http://192.168.172.19:5000"
  18.         const val BASE_URL_END = "https://api.vasdev.co.id:8066/sitamoto/"
  19.     }
  20.  
  21.     private val log by lazy { HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY) }
  22.  
  23.     fun clientMain(url: String, sessionId: String? = null): MainApi {
  24.         Log.d("Client", "$url $sessionId")
  25.         val clientMain = OkHttpClient.Builder().apply {
  26.             if (BuildConfig.DEBUG) {
  27.                 addInterceptor(log)
  28.             }
  29.             if (sessionId != null) addInterceptor {
  30.                 val original = it.request()
  31.                 val request = original.newBuilder()
  32.                         .addHeader("Session-Id", "$sessionId")
  33.                         .method(original.method(), original.body())
  34.                         .build()
  35.  
  36.                 return@addInterceptor it.proceed(request)
  37.             }
  38.             addInterceptor {
  39.                 val original = it.request()
  40.                 val request = original.newBuilder()
  41.                         .addHeader("Session-Key", "apps")
  42.                         .method(original.method(), original.body())
  43.                         .build()
  44.  
  45.                 return@addInterceptor it.proceed(request)
  46.             }
  47.         }
  48.                 .connectTimeout(60, TimeUnit.SECONDS)
  49.                 .writeTimeout(60, TimeUnit.SECONDS)
  50.                 .readTimeout(60, TimeUnit.SECONDS)
  51.  
  52.         val retrofit by lazy {
  53.             Retrofit.Builder().baseUrl(url)
  54.                     .addConverterFactory(ScalarsConverterFactory.create())
  55.                     .client(clientMain.build())
  56.                     .build()
  57.         }
  58.         return retrofit.create(MainApi::class.java)
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top