Created
March 17, 2017 08:06
-
-
Save deniszink/c1d7b6c8e1a2a842e9e12ad6ef30ae02 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.android.handsfree.data; | |
import com.android.handsfree.BuildConfig; | |
/** | |
* Created by denis on 4/11/16. | |
*/ | |
public interface RestAPI { | |
String API_ENDPOINT = BuildConfig.BASE_URL; | |
} |
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.android.handsfree.data; | |
/** | |
* @author Denis_Zinkovskiy. | |
*/ | |
public interface RestClient { | |
<T> T create(Class<T> clazz); | |
} |
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.android.handsfree.data; | |
import com.android.handsfree.BuildConfig; | |
import com.android.handsfree.presentation.DefaultSubsriber; | |
import com.android.handsfree.data.error_handling.RxErrorHandlingCallAdapterFactory; | |
import com.android.handsfree.data.models.Token; | |
import com.android.handsfree.domain.interactors.GetTokenUseCase; | |
import java.io.IOException; | |
import javax.inject.Inject; | |
import javax.inject.Singleton; | |
import okhttp3.Interceptor; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
@Singleton | |
public final class RestClientRetrofit implements RestClient { | |
private Retrofit retrofit; | |
private GetTokenUseCase tokenUseCase; | |
@Inject | |
public RestClientRetrofit(GetTokenUseCase tokenUseCase) { | |
this.tokenUseCase = tokenUseCase; | |
init(); | |
} | |
private void init() { | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); | |
if (BuildConfig.LOG_ENABLED) | |
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
else | |
interceptor.setLevel(HttpLoggingInterceptor.Level.NONE); | |
OkHttpClient httpClient = new OkHttpClient.Builder() | |
.addInterceptor(interceptor) | |
.addInterceptor(new TokenInterceptor(tokenUseCase)) | |
.build(); | |
this.retrofit = new Retrofit.Builder() | |
.client(httpClient) | |
.baseUrl(RestAPI.API_ENDPOINT) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create()) | |
.build(); | |
} | |
@Override | |
public <T> T create(final Class<T> clazz) { | |
return retrofit.create(clazz); | |
} | |
private static class TokenInterceptor implements Interceptor { | |
private GetTokenUseCase tokenUseCase; | |
private String token; | |
TokenInterceptor(GetTokenUseCase tokenUseCase) { | |
this.tokenUseCase = tokenUseCase; | |
} | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
initToken(); | |
Request request = chain.request(); | |
request = request.newBuilder() | |
.addHeader("Authorization","Bearer "+token) | |
.addHeader("Content-Type","application/json") | |
.build(); | |
return chain.proceed(request); | |
} | |
private void initToken(){ | |
tokenUseCase.executeSync(new DefaultSubsriber<Token>(){ | |
@Override | |
public void onNext(Token token1) { | |
token = token1.getToken(); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment