diff --git a/app/src/main/java/com/mindorks/framework/mvp/data/network/ApiInterceptor.java b/app/src/main/java/com/mindorks/framework/mvp/data/network/ApiInterceptor.java index aac3086..96ac41c 100644 --- a/app/src/main/java/com/mindorks/framework/mvp/data/network/ApiInterceptor.java +++ b/app/src/main/java/com/mindorks/framework/mvp/data/network/ApiInterceptor.java @@ -16,14 +16,10 @@ package com.mindorks.framework.mvp.data.network; import java.io.IOException; -import java.util.concurrent.Callable; import javax.inject.Inject; import javax.inject.Singleton; -import io.reactivex.Observable; -import io.reactivex.ObservableSource; -import io.reactivex.functions.Consumer; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; @@ -37,44 +33,30 @@ public class ApiInterceptor implements Interceptor { private static final String TAG = "ApiInterceptor"; - private Observable mApiHeaderObservable; + private ApiHeader mApiHeader; @Inject public ApiInterceptor(final ApiHeader header) { - mApiHeaderObservable = - Observable.defer(new Callable>() { - @Override - public ObservableSource call() throws Exception { - return Observable.just(header); - } - }); + mApiHeader = header; } @Override public Response intercept(Chain chain) throws IOException { final Request request = chain.request(); final Request.Builder builder = request.newBuilder(); - mApiHeaderObservable.subscribe(new Consumer() { - @Override - public void accept(ApiHeader header) throws Exception { - - String apiAuthType = request.header(ApiHeader.API_AUTH_TYPE); - if (apiAuthType == null) { - apiAuthType = ApiHeader.PROTECTED_API; - } - - switch (apiAuthType) { - case ApiHeader.PROTECTED_API: - builder.addHeader(ApiHeader.HEADER_PARAM_API_KEY, header.getApiKey()); - builder.addHeader(ApiHeader.HEADER_PARAM_ACCESS_TOKEN, header.getAccessToken()); - builder.addHeader(ApiHeader.HEADER_PARAM_USER_ID, String.valueOf(header.getUserId())); - break; - case ApiHeader.PUBLIC_API: - default: - builder.addHeader(ApiHeader.HEADER_PARAM_API_KEY, header.getApiKey()); - } - } - }); + String apiAuthType = request.header(ApiHeader.API_AUTH_TYPE); + if (apiAuthType == null) { + apiAuthType = ApiHeader.PROTECTED_API; + } + + switch (apiAuthType) { + case ApiHeader.PROTECTED_API: + builder.addHeader(ApiHeader.HEADER_PARAM_ACCESS_TOKEN, mApiHeader.getAccessToken()); + builder.addHeader(ApiHeader.HEADER_PARAM_USER_ID, String.valueOf(mApiHeader.getUserId())); + case ApiHeader.PUBLIC_API: + default: + builder.addHeader(ApiHeader.HEADER_PARAM_API_KEY, mApiHeader.getApiKey()); + } return chain.proceed(builder.build()); } diff --git a/app/src/main/java/com/mindorks/framework/mvp/ui/base/BasePresenter.java b/app/src/main/java/com/mindorks/framework/mvp/ui/base/BasePresenter.java index 620caae..91f53aa 100644 --- a/app/src/main/java/com/mindorks/framework/mvp/ui/base/BasePresenter.java +++ b/app/src/main/java/com/mindorks/framework/mvp/ui/base/BasePresenter.java @@ -22,12 +22,16 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; +import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.mindorks.framework.mvp.R; import com.mindorks.framework.mvp.data.DataManager; import com.mindorks.framework.mvp.data.network.model.ApiError; -import com.mindorks.framework.mvp.utils.AppConstants; + +import java.io.IOException; +import java.net.HttpURLConnection; import javax.inject.Inject; +import javax.net.ssl.HttpsURLConnection; /** * Base class that implements the Presenter interface and provides a base implementation for @@ -72,27 +76,36 @@ public DataManager getDataManager() { } @Override - public void handleApiError(ApiError error) { - - final GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation(); - final Gson gson = builder.create(); + public void handleApiError(Throwable throwable) { - ApiError apiError; try { - if (error == null || error.getMessage() == null) { + + if (!(throwable instanceof HttpException)) { + getMvpView().onError(R.string.api_default_error); + return; + } + + HttpException httpException = (HttpException) throwable; + + final GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation(); + final Gson gson = builder.create(); + + ApiError apiError = gson.fromJson(httpException.response().errorBody().string(), ApiError.class); + + if (apiError == null || apiError.getMessage() == null) { getMvpView().onError(R.string.api_default_error); return; } - switch (error.getErrorCode()) { - case AppConstants.API_STATUS_CODE_BAD_REQUEST: - setUserAsLoggedOut(); + switch (httpException.code()) { + case HttpsURLConnection.HTTP_UNAUTHORIZED: + getDataManager().setUserAsLoggedOut(); getMvpView().openActivityOnTokenExpire(); - case AppConstants.API_STATUS_CODE_INTERNAL_SERVER_ERROR: - case AppConstants.API_STATUS_CODE_NOT_FOUND: + case HttpURLConnection.HTTP_INTERNAL_ERROR: + case HttpURLConnection.HTTP_NOT_FOUND: default: - getMvpView().onError(error.getMessage()); + getMvpView().onError(apiError.getMessage()); } - } catch (JsonSyntaxException | NullPointerException e) { + } catch (IOException | JsonSyntaxException | NullPointerException e) { e.printStackTrace(); getMvpView().onError(R.string.api_default_error); } diff --git a/app/src/main/java/com/mindorks/framework/mvp/ui/base/MvpPresenter.java b/app/src/main/java/com/mindorks/framework/mvp/ui/base/MvpPresenter.java index d340907..2e0fcd4 100644 --- a/app/src/main/java/com/mindorks/framework/mvp/ui/base/MvpPresenter.java +++ b/app/src/main/java/com/mindorks/framework/mvp/ui/base/MvpPresenter.java @@ -19,8 +19,6 @@ * Created by janisharali on 27/01/17. */ -import com.mindorks.framework.mvp.data.network.model.ApiError; - /** * Every presenter in the app must either implement this interface or extend BasePresenter * indicating the MvpView type that wants to be attached with. @@ -31,7 +29,7 @@ public interface MvpPresenter { void onDetach(); - void handleApiError(ApiError error); + void handleApiError(Throwable throwable); void setUserAsLoggedOut(); } diff --git a/app/src/main/java/com/mindorks/framework/mvp/ui/login/LoginPresenter.java b/app/src/main/java/com/mindorks/framework/mvp/ui/login/LoginPresenter.java index ac4f5b5..d02223f 100644 --- a/app/src/main/java/com/mindorks/framework/mvp/ui/login/LoginPresenter.java +++ b/app/src/main/java/com/mindorks/framework/mvp/ui/login/LoginPresenter.java @@ -79,7 +79,7 @@ public void accept(LoginResponse response) throws Exception { public void accept(Throwable throwable) throws Exception { getMvpView().hideLoading(); // handle the login error here - + // handleApiError(throwable); //for demo the next screen in made to open even in failure getMvpView().openMainActivity(); } @@ -113,7 +113,7 @@ public void accept(LoginResponse response) throws Exception { public void accept(Throwable throwable) throws Exception { getMvpView().hideLoading(); // handle the login error here - + // handleApiError(throwable); //for demo the next screen in made to open even in failure getMvpView().openMainActivity(); } @@ -147,7 +147,7 @@ public void accept(LoginResponse response) throws Exception { public void accept(Throwable throwable) throws Exception { getMvpView().hideLoading(); // handle the login error here - + // handleApiError(throwable); //for demo the next screen in made to open even in failure getMvpView().openMainActivity(); }