From cc9a78ba43de2f636db7f30f10f6c2142811d4d0 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sun, 18 Aug 2019 12:42:48 +0200 Subject: [PATCH] Set httpManager as a singleton to use only one instance of OkHttp, then fixing interceptors duplicates problem and improving global app speed --- .../app/repositories/FreshRSSRepository.java | 3 +- .../readropslibrary/services/API.java | 16 +++---- .../readropslibrary/utils/HttpManager.java | 43 ++++++++++++++----- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java index 88b34edc..e83b8c0e 100644 --- a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java @@ -17,7 +17,6 @@ import com.readrops.app.utils.Utils; import com.readrops.readropslibrary.services.SyncType; import com.readrops.readropslibrary.services.freshrss.FreshRSSAPI; import com.readrops.readropslibrary.services.freshrss.FreshRSSCredentials; -import com.readrops.readropslibrary.services.freshrss.FreshRSSService; import com.readrops.readropslibrary.services.freshrss.FreshRSSSyncData; import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFeed; import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFolder; @@ -45,7 +44,7 @@ public class FreshRSSRepository extends ARepository { return api.login(account.getLogin(), account.getPassword()) .flatMap(token -> { account.setToken(token); - api.buildAPI(new FreshRSSCredentials(token, account.getUrl()), FreshRSSService.class, FreshRSSService.END_POINT); + api.setCredentials(new FreshRSSCredentials(token, account.getUrl())); return api.getUserInfo(); }) diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/API.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/API.java index aadedb7b..89694816 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/services/API.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/services/API.java @@ -18,26 +18,26 @@ public abstract class API { protected T api; public API(Credentials credentials, @NonNull Class clazz, @NonNull String endPoint) { - buildAPI(credentials, clazz, endPoint); + api = createAPI(credentials, clazz, endPoint); } - protected Retrofit getConfiguredRetrofitInstance(@NonNull HttpManager httpManager, @NonNull String endPoint) { + protected Retrofit getConfiguredRetrofitInstance(@NonNull String endPoint) { return new Retrofit.Builder() - .baseUrl(httpManager.getCredentials().getUrl() + endPoint) + .baseUrl(HttpManager.getInstance().getCredentials().getUrl() + endPoint) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) - .client(httpManager.getOkHttpClient()) + .client(HttpManager.getInstance().getOkHttpClient()) .build(); } private T createAPI(@NonNull Credentials credentials, @NonNull Class clazz, @NonNull String endPoint) { - HttpManager httpManager = new HttpManager(credentials); - Retrofit retrofit = getConfiguredRetrofitInstance(httpManager, endPoint); + HttpManager.getInstance().setCredentials(credentials); + Retrofit retrofit = getConfiguredRetrofitInstance(endPoint); return retrofit.create(clazz); } - public void buildAPI(@NonNull Credentials credentials, @NonNull Class clazz, @NonNull String endPoint) { - api = createAPI(credentials, clazz, endPoint); + public void setCredentials(@NonNull Credentials credentials) { + HttpManager.getInstance().setCredentials(credentials); } } diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/HttpManager.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/HttpManager.java index 805f04f8..cb5baf63 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/HttpManager.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/HttpManager.java @@ -4,6 +4,7 @@ import com.readrops.readropslibrary.BuildConfig; import com.readrops.readropslibrary.services.Credentials; import java.io.IOException; +import java.util.concurrent.TimeUnit; import okhttp3.Interceptor; import okhttp3.OkHttpClient; @@ -16,13 +17,22 @@ public class HttpManager { private OkHttpClient okHttpClient; private Credentials credentials; + public HttpManager() { + buildOkHttp(); + } + public HttpManager(final Credentials credentials) { this.credentials = credentials; - OkHttpClient.Builder httpBuilder = HttpBuilder.getBuilder(); + buildOkHttp(); + } - if (credentials.getAuthorization() != null) - httpBuilder.addInterceptor(new AuthInterceptor()); + private void buildOkHttp() { + OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder() + .callTimeout(30, TimeUnit.SECONDS) + .readTimeout(1, TimeUnit.HOURS); + + httpBuilder.addInterceptor(new AuthInterceptor()); if (BuildConfig.DEBUG) { HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); @@ -38,10 +48,25 @@ public class HttpManager { return okHttpClient; } + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } + public Credentials getCredentials() { return credentials; } + + private static HttpManager instance; + + public static HttpManager getInstance() { + if (instance == null) { + instance = new HttpManager(); + } + + return instance; + } + public class AuthInterceptor implements Interceptor { public AuthInterceptor() { @@ -52,13 +77,11 @@ public class HttpManager { public Response intercept(Chain chain) throws IOException { Request request = chain.request(); - // AuthInterceptor is called twice when using Okhttp for the second time and I don't know why, - // which adds the authorization header twice and make the auth fail - // So preventively, I delete the first added header - // TODO : find why AuthInterceptor is called twice - request = request.newBuilder().removeHeader("Authorization") - .addHeader("Authorization", credentials.getAuthorization()) - .build(); + if (credentials.getAuthorization() != null) { + request = request.newBuilder() + .addHeader("Authorization", credentials.getAuthorization()) + .build(); + } return chain.proceed(request); }