Browse Source
Create RetrofitFactory to reuse retrofit objects.
renovate/org.robolectric-robolectric-4.x
Create RetrofitFactory to reuse retrofit objects.
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
20 changed files with 170 additions and 146 deletions
-
10app/src/main/java/awais/instagrabber/InstaGrabberApplication.java
-
2app/src/main/java/awais/instagrabber/utils/MediaUploader.java
-
42app/src/main/java/awais/instagrabber/webservices/BaseService.java
-
18app/src/main/java/awais/instagrabber/webservices/CollectionService.java
-
8app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java
-
8app/src/main/java/awais/instagrabber/webservices/DiscoverService.java
-
8app/src/main/java/awais/instagrabber/webservices/FeedService.java
-
16app/src/main/java/awais/instagrabber/webservices/FriendshipService.java
-
8app/src/main/java/awais/instagrabber/webservices/GifService.java
-
8app/src/main/java/awais/instagrabber/webservices/GraphQLService.java
-
8app/src/main/java/awais/instagrabber/webservices/LocationService.java
-
8app/src/main/java/awais/instagrabber/webservices/MediaService.java
-
30app/src/main/java/awais/instagrabber/webservices/NewsService.java
-
14app/src/main/java/awais/instagrabber/webservices/ProfileService.java
-
97app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java
-
11app/src/main/java/awais/instagrabber/webservices/StoriesService.java
-
8app/src/main/java/awais/instagrabber/webservices/TagsService.java
-
8app/src/main/java/awais/instagrabber/webservices/UserService.java
-
2app/src/main/java/awais/instagrabber/webservices/interceptors/AddCookiesInterceptor.java
-
2app/src/main/java/awais/instagrabber/webservices/interceptors/LoggingInterceptor.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; |
||||
|
} |
||||
|
} |
@ -1,4 +1,4 @@ |
|||||
package awais.instagrabber.webservices; |
|
||||
|
package awais.instagrabber.webservices.interceptors; |
||||
|
|
||||
import androidx.annotation.NonNull; |
import androidx.annotation.NonNull; |
||||
|
|
@ -1,4 +1,4 @@ |
|||||
package awais.instagrabber.webservices; |
|
||||
|
package awais.instagrabber.webservices.interceptors; |
||||
|
|
||||
import android.util.Log; |
import android.util.Log; |
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue