mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 06:37:30 +00:00
Merge pull request #840 from ammargitham/retrofit-intercept-errors
Global retrofit error interceptor
This commit is contained in:
commit
208e843778
@ -26,6 +26,9 @@ import static awais.instagrabber.utils.Utils.clipboardManager;
|
||||
import static awais.instagrabber.utils.Utils.datetimeParser;
|
||||
import static awais.instagrabber.utils.Utils.settingsHelper;
|
||||
|
||||
//import awaisomereport.LogCollector;
|
||||
//import static awais.instagrabber.utils.Utils.logCollector;
|
||||
|
||||
public final class InstaGrabberApplication extends Application {
|
||||
private static final String TAG = "InstaGrabberApplication";
|
||||
|
||||
|
@ -85,6 +85,7 @@ import awais.instagrabber.utils.TextUtils;
|
||||
import awais.instagrabber.utils.Utils;
|
||||
import awais.instagrabber.utils.emoji.EmojiParser;
|
||||
import awais.instagrabber.viewmodels.AppStateViewModel;
|
||||
import awais.instagrabber.webservices.RetrofitFactory;
|
||||
import awais.instagrabber.viewmodels.DirectInboxViewModel;
|
||||
import awais.instagrabber.webservices.SearchService;
|
||||
import retrofit2.Call;
|
||||
@ -133,6 +134,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable final Bundle savedInstanceState) {
|
||||
RetrofitFactory.setup(this);
|
||||
super.onCreate(savedInstanceState);
|
||||
binding = ActivityMainBinding.inflate(getLayoutInflater());
|
||||
final String cookie = settingsHelper.getString(Constants.COOKIE);
|
||||
@ -263,6 +265,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
|
||||
Log.e(TAG, "onDestroy: ", e);
|
||||
}
|
||||
unbindActivityCheckerService();
|
||||
RetrofitFactory.getInstance().destroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -905,6 +908,10 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
|
||||
return binding.toolbar;
|
||||
}
|
||||
|
||||
public View getRootView() {
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
public List<Tab> getCurrentTabs() {
|
||||
return currentTabs;
|
||||
}
|
||||
|
@ -114,6 +114,7 @@ public final class Constants {
|
||||
public static final int SHOW_ACTIVITY_REQUEST_CODE = 1738;
|
||||
public static final int SHOW_DM_THREAD = 2000;
|
||||
public static final int DM_SYNC_SERVICE_REQUEST_CODE = 3000;
|
||||
public static final int GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE = 7777;
|
||||
|
||||
public static final String ACTION_SHOW_ACTIVITY = "show_activity";
|
||||
public static final String ACTION_SHOW_DM_THREAD = "show_dm_thread";
|
||||
|
@ -17,7 +17,7 @@ import java.util.Map;
|
||||
|
||||
import awais.instagrabber.models.UploadPhotoOptions;
|
||||
import awais.instagrabber.models.UploadVideoOptions;
|
||||
import awais.instagrabber.webservices.AddCookiesInterceptor;
|
||||
import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.MediaType;
|
||||
|
@ -1,50 +1,8 @@
|
||||
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 {
|
||||
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) {
|
||||
// final long term = (random(2, 4) * 1000) + size + (random(15, 21) * 1000);
|
||||
// final float div = (float) size / random(2, 4);
|
||||
|
@ -1,5 +1,7 @@
|
||||
package awais.instagrabber.webservices;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -14,7 +16,6 @@ import awais.instagrabber.utils.Utils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class CollectionService extends BaseService {
|
||||
private static final String TAG = "ProfileService";
|
||||
@ -31,10 +32,9 @@ public class CollectionService extends BaseService {
|
||||
this.deviceUuid = deviceUuid;
|
||||
this.csrfToken = csrfToken;
|
||||
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() {
|
||||
@ -69,7 +69,7 @@ public class CollectionService extends BaseService {
|
||||
.map(Media::getPk)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
form.put("added_media_ids", "[" + String.join(",", ids) + "]");
|
||||
form.put("added_media_ids", "[" + TextUtils.join(",", ids) + "]");
|
||||
changeCollection(collectionId, "edit", form, callback);
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,6 @@ import awais.instagrabber.repositories.responses.giphy.GiphyGif;
|
||||
import awais.instagrabber.utils.TextUtils;
|
||||
import awais.instagrabber.utils.Utils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class DirectMessagesService extends BaseService {
|
||||
private static final String TAG = "DiscoverService";
|
||||
@ -59,10 +58,9 @@ public class DirectMessagesService extends BaseService {
|
||||
this.csrfToken = csrfToken;
|
||||
this.userId = userId;
|
||||
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() {
|
||||
|
@ -12,7 +12,6 @@ import awais.instagrabber.utils.TextUtils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class DiscoverService extends BaseService {
|
||||
|
||||
@ -23,10 +22,9 @@ public class DiscoverService extends BaseService {
|
||||
private static DiscoverService instance;
|
||||
|
||||
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() {
|
||||
|
@ -22,7 +22,6 @@ import awais.instagrabber.utils.TextUtils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class FeedService extends BaseService {
|
||||
private static final String TAG = "FeedService";
|
||||
@ -32,10 +31,9 @@ public class FeedService extends BaseService {
|
||||
private static FeedService instance;
|
||||
|
||||
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() {
|
||||
|
@ -25,7 +25,6 @@ import awais.instagrabber.utils.Utils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class FriendshipService extends BaseService {
|
||||
private static final String TAG = "FriendshipService";
|
||||
@ -42,10 +41,9 @@ public class FriendshipService extends BaseService {
|
||||
this.deviceUuid = deviceUuid;
|
||||
this.csrfToken = csrfToken;
|
||||
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() {
|
||||
|
@ -3,7 +3,6 @@ package awais.instagrabber.webservices;
|
||||
import awais.instagrabber.repositories.GifRepository;
|
||||
import awais.instagrabber.repositories.responses.giphy.GiphyGifResponse;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class GifService extends BaseService {
|
||||
|
||||
@ -12,10 +11,9 @@ public class GifService extends BaseService {
|
||||
private static GifService instance;
|
||||
|
||||
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() {
|
||||
|
@ -29,7 +29,6 @@ import awais.instagrabber.utils.TextUtils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class GraphQLService extends BaseService {
|
||||
private static final String TAG = "GraphQLService";
|
||||
@ -40,10 +39,9 @@ public class GraphQLService extends BaseService {
|
||||
private static GraphQLService instance;
|
||||
|
||||
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() {
|
||||
|
@ -13,7 +13,6 @@ import awais.instagrabber.utils.TextUtils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class LocationService extends BaseService {
|
||||
private static final String TAG = "LocationService";
|
||||
@ -23,10 +22,9 @@ public class LocationService extends BaseService {
|
||||
private static LocationService instance;
|
||||
|
||||
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() {
|
||||
|
@ -31,7 +31,6 @@ import awais.instagrabber.utils.Utils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class MediaService extends BaseService {
|
||||
private static final String TAG = "MediaService";
|
||||
@ -51,10 +50,9 @@ public class MediaService extends BaseService {
|
||||
this.deviceUuid = deviceUuid;
|
||||
this.csrfToken = csrfToken;
|
||||
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() {
|
||||
|
@ -22,7 +22,6 @@ import awais.instagrabber.utils.Constants;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class NewsService extends BaseService {
|
||||
private static final String TAG = "NewsService";
|
||||
@ -32,10 +31,9 @@ public class NewsService extends BaseService {
|
||||
private static NewsService instance;
|
||||
|
||||
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() {
|
||||
@ -122,7 +120,8 @@ public class NewsService extends BaseService {
|
||||
aymlUsers.addAll(oldSuggestions);
|
||||
}
|
||||
|
||||
final List<Notification> newsItems = aymlUsers.stream()
|
||||
final List<Notification> newsItems = aymlUsers
|
||||
.stream()
|
||||
.map(i -> {
|
||||
final User u = i.getUser();
|
||||
return new Notification(
|
||||
@ -164,7 +163,9 @@ public class NewsService extends BaseService {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<Notification> newsItems = body.getUsers().stream()
|
||||
final List<Notification> newsItems = body
|
||||
.getUsers()
|
||||
.stream()
|
||||
.map(u -> {
|
||||
return new Notification(
|
||||
new NotificationArgs(
|
||||
|
@ -23,7 +23,6 @@ import awais.instagrabber.utils.Utils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class ProfileService extends BaseService {
|
||||
private static final String TAG = "ProfileService";
|
||||
@ -33,10 +32,9 @@ public class ProfileService extends BaseService {
|
||||
private static ProfileService instance;
|
||||
|
||||
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() {
|
||||
|
@ -0,0 +1,112 @@
|
||||
package awais.instagrabber.webservices;
|
||||
|
||||
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.activities.MainActivity;
|
||||
import awais.instagrabber.repositories.responses.Caption;
|
||||
import awais.instagrabber.utils.Utils;
|
||||
import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor;
|
||||
import awais.instagrabber.webservices.interceptors.IgErrorsInterceptor;
|
||||
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 int cacheSize = 10 * 1024 * 1024; // 10 MB
|
||||
private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize);
|
||||
|
||||
private IgErrorsInterceptor igErrorsInterceptor;
|
||||
private MainActivity mainActivity;
|
||||
private Retrofit.Builder builder;
|
||||
private Retrofit retrofit;
|
||||
private Retrofit retrofitWeb;
|
||||
|
||||
public static void setup(@NonNull final MainActivity mainActivity) {
|
||||
if (instance == null) {
|
||||
synchronized (LOCK) {
|
||||
if (instance == null) {
|
||||
instance = new RetrofitFactory(mainActivity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static RetrofitFactory getInstance() {
|
||||
if (instance == null) {
|
||||
throw new RuntimeException("Setup not done!");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private RetrofitFactory(@NonNull final MainActivity mainActivity) {
|
||||
this.mainActivity = mainActivity;
|
||||
}
|
||||
|
||||
private Retrofit.Builder getRetrofitBuilder() {
|
||||
if (builder == null) {
|
||||
igErrorsInterceptor = new IgErrorsInterceptor(mainActivity);
|
||||
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
|
||||
.followRedirects(false)
|
||||
.followSslRedirects(false)
|
||||
.cache(cache);
|
||||
if (BuildConfig.DEBUG) {
|
||||
// clientBuilder.addInterceptor(new LoggingInterceptor());
|
||||
}
|
||||
clientBuilder.addInterceptor(new AddCookiesInterceptor())
|
||||
.addInterceptor(igErrorsInterceptor);
|
||||
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;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
if (igErrorsInterceptor != null) {
|
||||
igErrorsInterceptor.destroy();
|
||||
}
|
||||
igErrorsInterceptor = null;
|
||||
mainActivity = null;
|
||||
retrofit = null;
|
||||
retrofitWeb = null;
|
||||
builder = null;
|
||||
instance = null;
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@ import com.google.common.collect.ImmutableMap;
|
||||
import awais.instagrabber.repositories.SearchRepository;
|
||||
import awais.instagrabber.repositories.responses.search.SearchResponse;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class SearchService extends BaseService {
|
||||
private static final String TAG = "LocationService";
|
||||
@ -15,10 +14,9 @@ public class SearchService extends BaseService {
|
||||
private static SearchService instance;
|
||||
|
||||
private SearchService() {
|
||||
final Retrofit retrofit = getRetrofitBuilder()
|
||||
.baseUrl("https://www.instagram.com")
|
||||
.build();
|
||||
repository = retrofit.create(SearchRepository.class);
|
||||
repository = RetrofitFactory.getInstance()
|
||||
.getRetrofitWeb()
|
||||
.create(SearchRepository.class);
|
||||
}
|
||||
|
||||
public static SearchService getInstance() {
|
||||
|
@ -32,7 +32,6 @@ import awais.instagrabber.utils.Utils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class StoriesService extends BaseService {
|
||||
private static final String TAG = "StoriesService";
|
||||
@ -50,10 +49,9 @@ public class StoriesService extends BaseService {
|
||||
this.csrfToken = csrfToken;
|
||||
this.userId = userId;
|
||||
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() {
|
||||
|
@ -21,7 +21,6 @@ import awais.instagrabber.utils.Utils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class TagsService extends BaseService {
|
||||
|
||||
@ -32,10 +31,9 @@ public class TagsService extends BaseService {
|
||||
private final TagsRepository repository;
|
||||
|
||||
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() {
|
||||
|
@ -12,7 +12,6 @@ import awais.instagrabber.repositories.responses.WrappedUser;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class UserService extends BaseService {
|
||||
private static final String TAG = UserService.class.getSimpleName();
|
||||
@ -22,10 +21,9 @@ public class UserService extends BaseService {
|
||||
private static UserService instance;
|
||||
|
||||
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() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package awais.instagrabber.webservices;
|
||||
package awais.instagrabber.webservices.interceptors;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
@ -0,0 +1,136 @@
|
||||
package awais.instagrabber.webservices.interceptors;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.StringRes;
|
||||
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import awais.instagrabber.R;
|
||||
import awais.instagrabber.activities.MainActivity;
|
||||
import awais.instagrabber.dialogs.ConfirmDialogFragment;
|
||||
import awais.instagrabber.utils.Constants;
|
||||
import awais.instagrabber.utils.TextUtils;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
public class IgErrorsInterceptor implements Interceptor {
|
||||
private static final String TAG = IgErrorsInterceptor.class.getSimpleName();
|
||||
|
||||
private MainActivity mainActivity;
|
||||
|
||||
public IgErrorsInterceptor(@NonNull final MainActivity mainActivity) {
|
||||
this.mainActivity = mainActivity;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Response intercept(@NonNull final Chain chain) throws IOException {
|
||||
final Request request = chain.request();
|
||||
final Response response = chain.proceed(request);
|
||||
if (response.isSuccessful()) {
|
||||
return response;
|
||||
}
|
||||
checkError(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private void checkError(@NonNull final Response response) {
|
||||
final int errorCode = response.code();
|
||||
switch (errorCode) {
|
||||
case 429: // "429 Too Many Requests"
|
||||
// ('Throttled by Instagram because of too many API requests.');
|
||||
showErrorDialog(R.string.throttle_error);
|
||||
return;
|
||||
case 431: // "431 Request Header Fields Too Large"
|
||||
// show dialog?
|
||||
Log.e(TAG, "Network error: " + getMessage(errorCode, "The request start-line and/or headers are too large to process."));
|
||||
return;
|
||||
case 404:
|
||||
showErrorDialog(R.string.not_found);
|
||||
return;
|
||||
case 302: // redirect
|
||||
final String location = response.header("location");
|
||||
if (location.equals("https://www.instagram.com/accounts/login/")) {
|
||||
// rate limited
|
||||
showErrorDialog(R.string.rate_limit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
final ResponseBody body = response.body();
|
||||
if (body == null) return;
|
||||
try {
|
||||
final String bodyString = body.string();
|
||||
final JSONObject jsonObject = new JSONObject(bodyString);
|
||||
String message = jsonObject.optString("message", null);
|
||||
if (!TextUtils.isEmpty(message)) {
|
||||
message = message.toLowerCase();
|
||||
switch (message) {
|
||||
case "user_has_logged_out":
|
||||
showErrorDialog(R.string.account_logged_out);
|
||||
return;
|
||||
case "login_required":
|
||||
showErrorDialog(R.string.login_required);
|
||||
return;
|
||||
case "execution failure":
|
||||
showSnackbar(message);
|
||||
return;
|
||||
case "not authorized to view user": // Do we handle this in profile view fragment?
|
||||
case "challenge_required": // Since we make users login using browser, we should not be getting this error in api requests
|
||||
default:
|
||||
showSnackbar(message);
|
||||
Log.e(TAG, "checkError: " + bodyString);
|
||||
return;
|
||||
}
|
||||
}
|
||||
final String errorType = jsonObject.optString("error_type", null);
|
||||
if (TextUtils.isEmpty(errorType)) return;
|
||||
if (errorType.equals("sentry_block")) {
|
||||
showErrorDialog(R.string.sentry_block);
|
||||
return;
|
||||
}
|
||||
if (errorType.equals("inactive user")) {
|
||||
showErrorDialog(R.string.inactive_user);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "checkError: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void showSnackbar(final String message) {
|
||||
final View view = mainActivity.getRootView();
|
||||
if (view == null) return;
|
||||
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private String getMessage(final int errorCode, final String message) {
|
||||
return String.format("code: %s, internalMessage: %s", errorCode, message);
|
||||
}
|
||||
|
||||
private void showErrorDialog(@StringRes final int messageResId) {
|
||||
if (mainActivity == null) return;
|
||||
if (messageResId == 0) return;
|
||||
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
|
||||
Constants.GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE,
|
||||
R.string.error,
|
||||
messageResId,
|
||||
R.string.ok,
|
||||
0,
|
||||
0
|
||||
);
|
||||
dialogFragment.show(mainActivity.getSupportFragmentManager(), "network_error_dialog");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
mainActivity = null;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package awais.instagrabber.webservices;
|
||||
package awais.instagrabber.webservices.interceptors;
|
||||
|
||||
import android.util.Log;
|
||||
|
@ -475,8 +475,16 @@
|
||||
<string name="removed_keywords">Removed keyword: %s from filter list</string>
|
||||
<string name="marked_as_seen">Marked as seen</string>
|
||||
<string name="delete_unsuccessful">Delete unsuccessful</string>
|
||||
<string name="throttle_error">Throttled by Instagram because of too many API requests. Wait for some time before retrying.</string>
|
||||
<string name="error">Error</string>
|
||||
<string name="account_logged_out">This account has been logged out.</string>
|
||||
<string name="login_required">Login required!</string>
|
||||
<string name="sentry_block">Sentry block.</string>
|
||||
<string name="inactive_user">User is inactive!</string>
|
||||
<string name="crash_report_subject">Barinsta Crash Report</string>
|
||||
<string name="crash_report_title">Select an email app to send crash logs</string>
|
||||
<string name="not_found">Not found!</string>
|
||||
<string name="rate_limit">Your IP has been rate limited by Instagram. Wait for an hour and try again.</string>
|
||||
<string name="skip_update">Skip this update</string>
|
||||
<string name="on_latest_version">You\'re already on the latest version</string>
|
||||
<string name="tab_order">Screen order</string>
|
||||
|
Loading…
Reference in New Issue
Block a user