Created
October 31, 2018 09:14
-
-
Save namphho/393fa41de89efa1e9369acc4511e7460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.horray.data.network | |
import okhttp3.CacheControl | |
import okhttp3.Interceptor | |
import okhttp3.OkHttpClient | |
import okhttp3.Response | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Retrofit | |
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory | |
import retrofit2.converter.gson.GsonConverterFactory | |
import rx.schedulers.Schedulers | |
import java.io.IOException | |
import java.util.concurrent.TimeUnit | |
import javax.inject.Inject | |
/** | |
* Created by nampham on 10/31/18. | |
*/ | |
class PumService @Inject constructor() { | |
private var mRetrofit: Retrofit? = null | |
var mAccessToken = "" | |
var mTokenKey = "" | |
init { | |
val client = OkHttpClient.Builder() | |
.addInterceptor(RequestApiInterceptor()) | |
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) | |
.readTimeout(PumConfigurations.TIMEOUT.toLong(), TimeUnit.SECONDS) | |
.connectTimeout(PumConfigurations.TIMEOUT.toLong(), TimeUnit.SECONDS) | |
.build() | |
//run asynchronous | |
val rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()) | |
mRetrofit = Retrofit.Builder() | |
.baseUrl(RxApi.API_ENDPOINT) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.client(client) | |
.addCallAdapterFactory(rxAdapter) | |
.build() | |
} | |
fun setTokenKey(tokenKey: String) { | |
mTokenKey = tokenKey | |
val client = OkHttpClient.Builder() | |
.addInterceptor(RequestApiInterceptorWithHeader(tokenKey)) | |
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) | |
.build() | |
//run asynchronous | |
val rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()) | |
mRetrofit = Retrofit.Builder() | |
.baseUrl(RxApi.API_ENDPOINT) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.client(client) | |
.addCallAdapterFactory(rxAdapter) | |
.build() | |
} | |
private class RequestApiInterceptor : Interceptor { | |
@Throws(IOException::class) | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val request = chain.request() | |
//handle request on url | |
val originalHttpUrl = request.url() | |
val url = originalHttpUrl.newBuilder() | |
.build() | |
// Request customization: add request headers | |
val requestBuilder = request.newBuilder() | |
.url(url) | |
.cacheControl(CacheControl.FORCE_NETWORK) | |
val r = requestBuilder.build() | |
return chain.proceed(r) | |
} | |
} | |
private class RequestApiInterceptorWithHeader(token: String) : Interceptor { | |
private var token = "" | |
init { | |
this.token = token | |
} | |
@Throws(IOException::class) | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val request = chain.request() | |
// Request customization: add request headers | |
val requestBuilder = request.newBuilder() | |
.header("Authorization", String.format("BEAR %s", token)) | |
.cacheControl(CacheControl.FORCE_NETWORK) | |
val r = requestBuilder.build() | |
return chain.proceed(r) | |
} | |
} | |
fun getAnonApi(): RxApi.Anon { | |
return mRetrofit!!.create(RxApi.Anon::class.java) | |
} | |
fun getAuthApi(): RxApi.Auth { | |
return mRetrofit!!.create(RxApi.Auth::class.java) | |
} | |
fun getRetrofit(): Retrofit? { | |
return mRetrofit | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment