Browse Source

Create RetrofitFactory to reuse retrofit objects.

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
c57e305732
  1. 10
      app/src/main/java/awais/instagrabber/InstaGrabberApplication.java
  2. 2
      app/src/main/java/awais/instagrabber/utils/MediaUploader.java
  3. 42
      app/src/main/java/awais/instagrabber/webservices/BaseService.java
  4. 18
      app/src/main/java/awais/instagrabber/webservices/CollectionService.java
  5. 8
      app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java
  6. 8
      app/src/main/java/awais/instagrabber/webservices/DiscoverService.java
  7. 8
      app/src/main/java/awais/instagrabber/webservices/FeedService.java
  8. 16
      app/src/main/java/awais/instagrabber/webservices/FriendshipService.java
  9. 8
      app/src/main/java/awais/instagrabber/webservices/GifService.java
  10. 8
      app/src/main/java/awais/instagrabber/webservices/GraphQLService.java
  11. 8
      app/src/main/java/awais/instagrabber/webservices/LocationService.java
  12. 8
      app/src/main/java/awais/instagrabber/webservices/MediaService.java
  13. 30
      app/src/main/java/awais/instagrabber/webservices/NewsService.java
  14. 14
      app/src/main/java/awais/instagrabber/webservices/ProfileService.java
  15. 97
      app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java
  16. 11
      app/src/main/java/awais/instagrabber/webservices/StoriesService.java
  17. 8
      app/src/main/java/awais/instagrabber/webservices/TagsService.java
  18. 8
      app/src/main/java/awais/instagrabber/webservices/UserService.java
  19. 2
      app/src/main/java/awais/instagrabber/webservices/interceptors/AddCookiesInterceptor.java
  20. 2
      app/src/main/java/awais/instagrabber/webservices/interceptors/LoggingInterceptor.java

10
app/src/main/java/awais/instagrabber/InstaGrabberApplication.java

@ -17,17 +17,19 @@ import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.LocaleUtils; import awais.instagrabber.utils.LocaleUtils;
import awais.instagrabber.utils.SettingsHelper; import awais.instagrabber.utils.SettingsHelper;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.webservices.RetrofitFactory;
import awaisomereport.CrashReporter; import awaisomereport.CrashReporter;
//import awaisomereport.LogCollector;
import static awais.instagrabber.utils.CookieUtils.NET_COOKIE_MANAGER; import static awais.instagrabber.utils.CookieUtils.NET_COOKIE_MANAGER;
import static awais.instagrabber.utils.Utils.applicationHandler; import static awais.instagrabber.utils.Utils.applicationHandler;
import static awais.instagrabber.utils.Utils.cacheDir; import static awais.instagrabber.utils.Utils.cacheDir;
import static awais.instagrabber.utils.Utils.clipboardManager; import static awais.instagrabber.utils.Utils.clipboardManager;
import static awais.instagrabber.utils.Utils.datetimeParser; import static awais.instagrabber.utils.Utils.datetimeParser;
//import static awais.instagrabber.utils.Utils.logCollector;
import static awais.instagrabber.utils.Utils.settingsHelper; import static awais.instagrabber.utils.Utils.settingsHelper;
//import awaisomereport.LogCollector;
//import static awais.instagrabber.utils.Utils.logCollector;
public final class InstaGrabberApplication extends Application { public final class InstaGrabberApplication extends Application {
private static final String TAG = "InstaGrabberApplication"; private static final String TAG = "InstaGrabberApplication";
@ -56,7 +58,7 @@ public final class InstaGrabberApplication extends Application {
} }
if (!BuildConfig.DEBUG) CrashReporter.get(this).start(); if (!BuildConfig.DEBUG) CrashReporter.get(this).start();
// logCollector = new LogCollector(this);
// logCollector = new LogCollector(this);
CookieHandler.setDefault(NET_COOKIE_MANAGER); CookieHandler.setDefault(NET_COOKIE_MANAGER);
@ -85,5 +87,7 @@ public final class InstaGrabberApplication extends Application {
if (TextUtils.isEmpty(settingsHelper.getString(Constants.DEVICE_UUID))) { if (TextUtils.isEmpty(settingsHelper.getString(Constants.DEVICE_UUID))) {
settingsHelper.putString(Constants.DEVICE_UUID, UUID.randomUUID().toString()); settingsHelper.putString(Constants.DEVICE_UUID, UUID.randomUUID().toString());
} }
RetrofitFactory.setup(this);
} }
} }

2
app/src/main/java/awais/instagrabber/utils/MediaUploader.java

@ -17,7 +17,7 @@ import java.util.Map;
import awais.instagrabber.models.UploadPhotoOptions; import awais.instagrabber.models.UploadPhotoOptions;
import awais.instagrabber.models.UploadVideoOptions; import awais.instagrabber.models.UploadVideoOptions;
import awais.instagrabber.webservices.AddCookiesInterceptor;
import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor;
import okhttp3.Call; import okhttp3.Call;
import okhttp3.Headers; import okhttp3.Headers;
import okhttp3.MediaType; import okhttp3.MediaType;

42
app/src/main/java/awais/instagrabber/webservices/BaseService.java

@ -1,50 +1,8 @@
package awais.instagrabber.webservices; package awais.instagrabber.webservices;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import awais.instagrabber.BuildConfig;
import awais.instagrabber.repositories.responses.Caption;
import awais.instagrabber.utils.Utils;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public abstract class BaseService { public abstract class BaseService {
private static final String TAG = "BaseService"; private static final String TAG = "BaseService";
private Retrofit.Builder builder;
private final int cacheSize = 10 * 1024 * 1024; // 10 MB
private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize);
Retrofit.Builder getRetrofitBuilder() {
if (builder == null) {
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.addInterceptor(new AddCookiesInterceptor())
.followRedirects(false)
.followSslRedirects(false)
.cache(cache);
if (BuildConfig.DEBUG) {
// clientBuilder.addInterceptor(new LoggingInterceptor());
}
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Caption.class, new Caption.CaptionDeserializer())
.setLenient()
.create();
builder = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(clientBuilder.build());
}
return builder;
}
// protected String userBreadcrumb(final int size) { // protected String userBreadcrumb(final int size) {
// final long term = (random(2, 4) * 1000) + size + (random(15, 21) * 1000); // final long term = (random(2, 4) * 1000) + size + (random(15, 21) * 1000);
// final float div = (float) size / random(2, 4); // final float div = (float) size / random(2, 4);

18
app/src/main/java/awais/instagrabber/webservices/CollectionService.java

@ -1,5 +1,7 @@
package awais.instagrabber.webservices; package awais.instagrabber.webservices;
import android.text.TextUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.HashMap; import java.util.HashMap;
@ -14,7 +16,6 @@ import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class CollectionService extends BaseService { public class CollectionService extends BaseService {
private static final String TAG = "ProfileService"; private static final String TAG = "ProfileService";
@ -31,10 +32,9 @@ public class CollectionService extends BaseService {
this.deviceUuid = deviceUuid; this.deviceUuid = deviceUuid;
this.csrfToken = csrfToken; this.csrfToken = csrfToken;
this.userId = userId; this.userId = userId;
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(CollectionRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(CollectionRepository.class);
} }
public String getCsrfToken() { public String getCsrfToken() {
@ -66,10 +66,10 @@ public class CollectionService extends BaseService {
form.put("module_name", "feed_saved_add_to_collection"); form.put("module_name", "feed_saved_add_to_collection");
final List<String> ids; final List<String> ids;
ids = posts.stream() ids = posts.stream()
.map(Media::getPk)
.filter(Objects::nonNull)
.collect(Collectors.toList());
form.put("added_media_ids", "[" + String.join(",", ids) + "]");
.map(Media::getPk)
.filter(Objects::nonNull)
.collect(Collectors.toList());
form.put("added_media_ids", "[" + TextUtils.join(",", ids) + "]");
changeCollection(collectionId, "edit", form, callback); changeCollection(collectionId, "edit", form, callback);
} }

8
app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java

@ -41,7 +41,6 @@ import awais.instagrabber.repositories.responses.giphy.GiphyGif;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Retrofit;
public class DirectMessagesService extends BaseService { public class DirectMessagesService extends BaseService {
private static final String TAG = "DiscoverService"; private static final String TAG = "DiscoverService";
@ -59,10 +58,9 @@ public class DirectMessagesService extends BaseService {
this.csrfToken = csrfToken; this.csrfToken = csrfToken;
this.userId = userId; this.userId = userId;
this.deviceUuid = deviceUuid; this.deviceUuid = deviceUuid;
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(DirectMessagesRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(DirectMessagesRepository.class);
} }
public String getCsrfToken() { public String getCsrfToken() {

8
app/src/main/java/awais/instagrabber/webservices/DiscoverService.java

@ -12,7 +12,6 @@ import awais.instagrabber.utils.TextUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class DiscoverService extends BaseService { public class DiscoverService extends BaseService {
@ -23,10 +22,9 @@ public class DiscoverService extends BaseService {
private static DiscoverService instance; private static DiscoverService instance;
private DiscoverService() { private DiscoverService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(DiscoverRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(DiscoverRepository.class);
} }
public static DiscoverService getInstance() { public static DiscoverService getInstance() {

8
app/src/main/java/awais/instagrabber/webservices/FeedService.java

@ -22,7 +22,6 @@ import awais.instagrabber.utils.TextUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class FeedService extends BaseService { public class FeedService extends BaseService {
private static final String TAG = "FeedService"; private static final String TAG = "FeedService";
@ -32,10 +31,9 @@ public class FeedService extends BaseService {
private static FeedService instance; private static FeedService instance;
private FeedService() { private FeedService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(FeedRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(FeedRepository.class);
} }
public static FeedService getInstance() { public static FeedService getInstance() {

16
app/src/main/java/awais/instagrabber/webservices/FriendshipService.java

@ -25,7 +25,6 @@ import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class FriendshipService extends BaseService { public class FriendshipService extends BaseService {
private static final String TAG = "FriendshipService"; private static final String TAG = "FriendshipService";
@ -42,10 +41,9 @@ public class FriendshipService extends BaseService {
this.deviceUuid = deviceUuid; this.deviceUuid = deviceUuid;
this.csrfToken = csrfToken; this.csrfToken = csrfToken;
this.userId = userId; this.userId = userId;
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(FriendshipRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(FriendshipRepository.class);
} }
public String getCsrfToken() { public String getCsrfToken() {
@ -168,8 +166,8 @@ public class FriendshipService extends BaseService {
form.put("_uuid", deviceUuid); form.put("_uuid", deviceUuid);
form.put(story ? "target_reel_author_id" : "target_posts_author_id", String.valueOf(targetUserId)); form.put(story ? "target_reel_author_id" : "target_posts_author_id", String.valueOf(targetUserId));
final Call<FriendshipChangeResponse> request = repository.changeMute(unmute ? final Call<FriendshipChangeResponse> request = repository.changeMute(unmute ?
"unmute_posts_or_story_from_follow" :
"mute_posts_or_story_from_follow",
"unmute_posts_or_story_from_follow" :
"mute_posts_or_story_from_follow",
form); form);
request.enqueue(new Callback<FriendshipChangeResponse>() { request.enqueue(new Callback<FriendshipChangeResponse>() {
@Override @Override
@ -198,8 +196,8 @@ public class FriendshipService extends BaseService {
if (maxId != null) queryMap.put("max_id", maxId); if (maxId != null) queryMap.put("max_id", maxId);
final Call<String> request = repository.getList( final Call<String> request = repository.getList(
targetUserId, targetUserId,
follower ? "followers" : "following",
queryMap);
follower ? "followers" : "following",
queryMap);
request.enqueue(new Callback<String>() { request.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) { public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) {

8
app/src/main/java/awais/instagrabber/webservices/GifService.java

@ -3,7 +3,6 @@ package awais.instagrabber.webservices;
import awais.instagrabber.repositories.GifRepository; import awais.instagrabber.repositories.GifRepository;
import awais.instagrabber.repositories.responses.giphy.GiphyGifResponse; import awais.instagrabber.repositories.responses.giphy.GiphyGifResponse;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Retrofit;
public class GifService extends BaseService { public class GifService extends BaseService {
@ -12,10 +11,9 @@ public class GifService extends BaseService {
private static GifService instance; private static GifService instance;
private GifService() { private GifService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(GifRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(GifRepository.class);
} }
public static GifService getInstance() { public static GifService getInstance() {

8
app/src/main/java/awais/instagrabber/webservices/GraphQLService.java

@ -28,7 +28,6 @@ import awais.instagrabber.utils.TextUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class GraphQLService extends BaseService { public class GraphQLService extends BaseService {
private static final String TAG = "GraphQLService"; private static final String TAG = "GraphQLService";
@ -39,10 +38,9 @@ public class GraphQLService extends BaseService {
private static GraphQLService instance; private static GraphQLService instance;
private GraphQLService() { private GraphQLService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://www.instagram.com")
.build();
repository = retrofit.create(GraphQLRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofitWeb()
.create(GraphQLRepository.class);
} }
public static GraphQLService getInstance() { public static GraphQLService getInstance() {

8
app/src/main/java/awais/instagrabber/webservices/LocationService.java

@ -11,7 +11,6 @@ import awais.instagrabber.utils.TextUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class LocationService extends BaseService { public class LocationService extends BaseService {
private static final String TAG = "LocationService"; private static final String TAG = "LocationService";
@ -21,10 +20,9 @@ public class LocationService extends BaseService {
private static LocationService instance; private static LocationService instance;
private LocationService() { private LocationService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(LocationRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(LocationRepository.class);
} }
public static LocationService getInstance() { public static LocationService getInstance() {

8
app/src/main/java/awais/instagrabber/webservices/MediaService.java

@ -31,7 +31,6 @@ import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class MediaService extends BaseService { public class MediaService extends BaseService {
private static final String TAG = "MediaService"; private static final String TAG = "MediaService";
@ -51,10 +50,9 @@ public class MediaService extends BaseService {
this.deviceUuid = deviceUuid; this.deviceUuid = deviceUuid;
this.csrfToken = csrfToken; this.csrfToken = csrfToken;
this.userId = userId; this.userId = userId;
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(MediaRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(MediaRepository.class);
} }
public String getCsrfToken() { public String getCsrfToken() {

30
app/src/main/java/awais/instagrabber/webservices/NewsService.java

@ -1,39 +1,27 @@
package awais.instagrabber.webservices; package awais.instagrabber.webservices;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import awais.instagrabber.BuildConfig;
import awais.instagrabber.models.enums.NotificationType;
import awais.instagrabber.repositories.NewsRepository; import awais.instagrabber.repositories.NewsRepository;
import awais.instagrabber.repositories.responses.AymlResponse; import awais.instagrabber.repositories.responses.AymlResponse;
import awais.instagrabber.repositories.responses.AymlUser; import awais.instagrabber.repositories.responses.AymlUser;
import awais.instagrabber.repositories.responses.NotificationCounts;
import awais.instagrabber.repositories.responses.UserSearchResponse;
import awais.instagrabber.repositories.responses.NewsInboxResponse; import awais.instagrabber.repositories.responses.NewsInboxResponse;
import awais.instagrabber.repositories.responses.Notification; import awais.instagrabber.repositories.responses.Notification;
import awais.instagrabber.repositories.responses.NotificationArgs; import awais.instagrabber.repositories.responses.NotificationArgs;
import awais.instagrabber.repositories.responses.NotificationImage;
import awais.instagrabber.repositories.responses.NotificationCounts;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.UserSearchResponse;
import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class NewsService extends BaseService { public class NewsService extends BaseService {
private static final String TAG = "NewsService"; private static final String TAG = "NewsService";
@ -43,10 +31,9 @@ public class NewsService extends BaseService {
private static NewsService instance; private static NewsService instance;
private NewsService() { private NewsService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(NewsRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(NewsRepository.class);
} }
public static NewsService getInstance() { public static NewsService getInstance() {
@ -131,7 +118,8 @@ public class NewsService extends BaseService {
aymlUsers.addAll(oldSuggestions); aymlUsers.addAll(oldSuggestions);
} }
final List<Notification> newsItems = aymlUsers.stream()
final List<Notification> newsItems = aymlUsers
.stream()
.map(i -> { .map(i -> {
final User u = i.getUser(); final User u = i.getUser();
return new Notification( return new Notification(
@ -173,7 +161,9 @@ public class NewsService extends BaseService {
return; return;
} }
final List<Notification> newsItems = body.getUsers().stream()
final List<Notification> newsItems = body
.getUsers()
.stream()
.map(u -> { .map(u -> {
return new Notification( return new Notification(
new NotificationArgs( new NotificationArgs(

14
app/src/main/java/awais/instagrabber/webservices/ProfileService.java

@ -23,7 +23,6 @@ import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class ProfileService extends BaseService { public class ProfileService extends BaseService {
private static final String TAG = "ProfileService"; private static final String TAG = "ProfileService";
@ -33,10 +32,9 @@ public class ProfileService extends BaseService {
private static ProfileService instance; private static ProfileService instance;
private ProfileService() { private ProfileService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(ProfileRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(ProfileRepository.class);
} }
public static ProfileService getInstance() { public static ProfileService getInstance() {
@ -104,9 +102,9 @@ public class ProfileService extends BaseService {
posts = Collections.emptyList(); posts = Collections.emptyList();
} else { } else {
posts = items.stream() posts = items.stream()
.map(WrappedMedia::getMedia)
.filter(Objects::nonNull)
.collect(Collectors.toList());
.map(WrappedMedia::getMedia)
.filter(Objects::nonNull)
.collect(Collectors.toList());
} }
callback.onSuccess(new PostsFetchResponse( callback.onSuccess(new PostsFetchResponse(
posts, posts,

97
app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java

@ -0,0 +1,97 @@
package awais.instagrabber.webservices;
import android.app.Application;
import androidx.annotation.NonNull;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import awais.instagrabber.BuildConfig;
import awais.instagrabber.repositories.responses.Caption;
import awais.instagrabber.utils.Utils;
import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public final class RetrofitFactory {
private static final Object LOCK = new Object();
private static RetrofitFactory instance;
private final Application application;
private final int cacheSize = 10 * 1024 * 1024; // 10 MB
private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize);
private Retrofit.Builder builder;
private Retrofit retrofit;
private Retrofit retrofitWeb;
public static void setup(@NonNull final Application application) {
if (instance == null) {
synchronized (LOCK) {
if (instance == null) {
instance = new RetrofitFactory(application);
}
}
}
}
public static RetrofitFactory getInstance() {
if (instance == null) {
throw new RuntimeException("Setup not done!");
}
return instance;
}
private RetrofitFactory(@NonNull final Application application) {
this.application = application;
}
private Retrofit.Builder getRetrofitBuilder() {
if (builder == null) {
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.addInterceptor(new AddCookiesInterceptor())
.followRedirects(false)
.followSslRedirects(false)
.cache(cache);
if (BuildConfig.DEBUG) {
// clientBuilder.addInterceptor(new LoggingInterceptor());
}
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Caption.class, new Caption.CaptionDeserializer())
.setLenient()
.create();
builder = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(clientBuilder.build());
}
return builder;
}
public Retrofit getRetrofit() {
if (retrofit == null) {
retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
}
return retrofit;
}
public Retrofit getRetrofitWeb() {
if (retrofitWeb == null) {
retrofitWeb = getRetrofitBuilder()
.baseUrl("https://www.instagram.com")
.build();
}
return retrofitWeb;
}
}

11
app/src/main/java/awais/instagrabber/webservices/StoriesService.java

@ -32,7 +32,6 @@ import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class StoriesService extends BaseService { public class StoriesService extends BaseService {
private static final String TAG = "StoriesService"; private static final String TAG = "StoriesService";
@ -50,10 +49,9 @@ public class StoriesService extends BaseService {
this.csrfToken = csrfToken; this.csrfToken = csrfToken;
this.userId = userId; this.userId = userId;
this.deviceUuid = deviceUuid; this.deviceUuid = deviceUuid;
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(StoriesRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(StoriesRepository.class);
} }
public String getCsrfToken() { public String getCsrfToken() {
@ -190,8 +188,7 @@ public class StoriesService extends BaseService {
firstStoryModel = ResponseBodyUtils.parseStoryItem(itemJson, false, false, null); firstStoryModel = ResponseBodyUtils.parseStoryItem(itemJson, false, false, null);
} }
feedStoryModels.add(new FeedStoryModel(id, user, fullyRead, timestamp, firstStoryModel, mediaCount, false, isBestie)); feedStoryModels.add(new FeedStoryModel(id, user, fullyRead, timestamp, firstStoryModel, mediaCount, false, isBestie));
}
catch (Exception e) {} // to cover promotional reels with non-long user pk's
} catch (Exception e) {} // to cover promotional reels with non-long user pk's
} }
final JSONArray broadcasts = new JSONObject(body).getJSONArray("broadcasts"); final JSONArray broadcasts = new JSONObject(body).getJSONArray("broadcasts");
for (int i = 0; i < broadcasts.length(); ++i) { for (int i = 0; i < broadcasts.length(); ++i) {

8
app/src/main/java/awais/instagrabber/webservices/TagsService.java

@ -21,7 +21,6 @@ import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class TagsService extends BaseService { public class TagsService extends BaseService {
@ -32,10 +31,9 @@ public class TagsService extends BaseService {
private final TagsRepository repository; private final TagsRepository repository;
private TagsService() { private TagsService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com/")
.build();
repository = retrofit.create(TagsRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(TagsRepository.class);
} }
public static TagsService getInstance() { public static TagsService getInstance() {

8
app/src/main/java/awais/instagrabber/webservices/UserService.java

@ -12,7 +12,6 @@ import awais.instagrabber.repositories.responses.WrappedUser;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class UserService extends BaseService { public class UserService extends BaseService {
private static final String TAG = UserService.class.getSimpleName(); private static final String TAG = UserService.class.getSimpleName();
@ -22,10 +21,9 @@ public class UserService extends BaseService {
private static UserService instance; private static UserService instance;
private UserService() { private UserService() {
final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com")
.build();
repository = retrofit.create(UserRepository.class);
repository = RetrofitFactory.getInstance()
.getRetrofit()
.create(UserRepository.class);
} }
public static UserService getInstance() { public static UserService getInstance() {

2
app/src/main/java/awais/instagrabber/webservices/AddCookiesInterceptor.java → app/src/main/java/awais/instagrabber/webservices/interceptors/AddCookiesInterceptor.java

@ -1,4 +1,4 @@
package awais.instagrabber.webservices;
package awais.instagrabber.webservices.interceptors;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;

2
app/src/main/java/awais/instagrabber/webservices/LoggingInterceptor.java → app/src/main/java/awais/instagrabber/webservices/interceptors/LoggingInterceptor.java

@ -1,4 +1,4 @@
package awais.instagrabber.webservices;
package awais.instagrabber.webservices.interceptors;
import android.util.Log; import android.util.Log;
Loading…
Cancel
Save