Set httpManager as a singleton to use only one instance of OkHttp, then fixing interceptors duplicates problem and improving global app speed

This commit is contained in:
Shinokuni 2019-08-18 12:42:48 +02:00
parent f462933c8d
commit cc9a78ba43
3 changed files with 42 additions and 20 deletions

View File

@ -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();
})

View File

@ -18,26 +18,26 @@ public abstract class API<T> {
protected T api;
public API(Credentials credentials, @NonNull Class<T> 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<T> 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<T> clazz, @NonNull String endPoint) {
api = createAPI(credentials, clazz, endPoint);
public void setCredentials(@NonNull Credentials credentials) {
HttpManager.getInstance().setCredentials(credentials);
}
}

View File

@ -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);
}