Cleanup RedGIFs API usage

- Add User-Agent and authentication transparently
- Provide RedgifsAPI instance
This commit is contained in:
Angelo Suzuki 2023-09-07 12:39:19 +02:00
parent 9406f29562
commit 67afcd7e88
10 changed files with 45 additions and 40 deletions

View File

@ -1,7 +1,5 @@
package eu.toldi.infinityforlemmy; package eu.toldi.infinityforlemmy;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import org.json.JSONException; import org.json.JSONException;
@ -12,9 +10,7 @@ import java.util.concurrent.Executor;
import eu.toldi.infinityforlemmy.apis.GfycatAPI; import eu.toldi.infinityforlemmy.apis.GfycatAPI;
import eu.toldi.infinityforlemmy.apis.RedgifsAPI; import eu.toldi.infinityforlemmy.apis.RedgifsAPI;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import eu.toldi.infinityforlemmy.utils.JSONUtils; import eu.toldi.infinityforlemmy.utils.JSONUtils;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -44,14 +40,12 @@ public class FetchGfycatOrRedgifsVideoLinks {
}); });
} }
public static void fetchRedgifsVideoLinks(Context context, Executor executor, Handler handler, Retrofit redgifsRetrofit, public static void fetchRedgifsVideoLinks(Executor executor, Handler handler, Retrofit redgifsRetrofit,
SharedPreferences currentAccountSharedPreferences,
String gfycatId, String gfycatId,
FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
executor.execute(() -> { executor.execute(() -> {
try { try {
Response<String> response = redgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")), Response<String> response = redgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(gfycatId).execute();
gfycatId, APIUtils.USER_AGENT).execute();
if (response.isSuccessful()) { if (response.isSuccessful()) {
parseRedgifsVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); parseRedgifsVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
} else { } else {

View File

@ -9,6 +9,7 @@ import javax.inject.Singleton;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import eu.toldi.infinityforlemmy.apis.RedgifsAPI;
import eu.toldi.infinityforlemmy.apis.StreamableAPI; import eu.toldi.infinityforlemmy.apis.StreamableAPI;
import eu.toldi.infinityforlemmy.comment.LemmyCommentAPI; import eu.toldi.infinityforlemmy.comment.LemmyCommentAPI;
import eu.toldi.infinityforlemmy.post.LemmyPostAPI; import eu.toldi.infinityforlemmy.post.LemmyPostAPI;
@ -37,7 +38,8 @@ abstract class NetworkModule {
@Provides @Provides
@Named("glide") @Named("glide")
@Singleton @Singleton
static OkHttpClient provideGlideOkHttp(@Named("base") OkHttpClient baseOkHttp) { static OkHttpClient provideGlideOkHttp(@Named("base") OkHttpClient baseOkHttp,
@Named("RedgifsAccessTokenAuthenticator") Interceptor redGifsAuthenticator) {
return baseOkHttp.newBuilder() return baseOkHttp.newBuilder()
.addInterceptor(chain -> chain.proceed( .addInterceptor(chain -> chain.proceed(
chain.request() chain.request()
@ -45,6 +47,7 @@ abstract class NetworkModule {
.header("User-Agent", APIUtils.USER_AGENT) .header("User-Agent", APIUtils.USER_AGENT)
.build() .build()
)) ))
.addInterceptor(redGifsAuthenticator)
.build(); .build();
} }
@ -178,6 +181,12 @@ abstract class NetworkModule {
.build(); .build();
} }
@Provides
@Singleton
static RedgifsAPI provideRedgifsAPI(@Named("redgifs") Retrofit redgifsRetrofit) {
return redgifsRetrofit.create(RedgifsAPI.class);
}
@Provides @Provides
@Named("imgur") @Named("imgur")
@Singleton @Singleton

View File

@ -14,7 +14,6 @@ import java.util.Map;
import eu.toldi.infinityforlemmy.apis.RedgifsAPI; import eu.toldi.infinityforlemmy.apis.RedgifsAPI;
import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.APIUtils;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import okhttp3.Headers;
import okhttp3.Interceptor; import okhttp3.Interceptor;
import okhttp3.Response; import okhttp3.Response;
import retrofit2.Call; import retrofit2.Call;
@ -22,6 +21,8 @@ import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory;
public class RedgifsAccessTokenAuthenticator implements Interceptor { public class RedgifsAccessTokenAuthenticator implements Interceptor {
private static final String REDGIFS_HOST = "redgifs.com";
private SharedPreferences mCurrentAccountSharedPreferences; private SharedPreferences mCurrentAccountSharedPreferences;
public RedgifsAccessTokenAuthenticator(SharedPreferences currentAccountSharedPreferences) { public RedgifsAccessTokenAuthenticator(SharedPreferences currentAccountSharedPreferences) {
@ -60,7 +61,17 @@ public class RedgifsAccessTokenAuthenticator implements Interceptor {
@NonNull @NonNull
@Override @Override
public Response intercept(@NonNull Chain chain) throws IOException { public Response intercept(@NonNull Chain chain) throws IOException {
Response response = chain.proceed(chain.request()); if (!chain.request().url().host().endsWith(REDGIFS_HOST)) {
return chain.proceed(chain.request());
}
String currentAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "");
Response response = chain.proceed(
chain.request().newBuilder()
.addHeader(APIUtils.AUTHORIZATION_KEY, APIUtils.AUTHORIZATION_BASE + currentAccessToken)
.build()
);
if (response.code() == 401 || response.code() == 400) { if (response.code() == 401 || response.code() == 400) {
String accessTokenHeader = response.request().header(APIUtils.AUTHORIZATION_KEY); String accessTokenHeader = response.request().header(APIUtils.AUTHORIZATION_KEY);
if (accessTokenHeader == null) { if (accessTokenHeader == null) {
@ -74,13 +85,21 @@ public class RedgifsAccessTokenAuthenticator implements Interceptor {
String newAccessToken = refreshAccessToken(); String newAccessToken = refreshAccessToken();
if (!newAccessToken.equals("")) { if (!newAccessToken.equals("")) {
response.close(); response.close();
return chain.proceed(response.request().newBuilder().headers(Headers.of(APIUtils.getRedgifsOAuthHeader(newAccessToken))).build()); return chain.proceed(
chain.request().newBuilder()
.addHeader(APIUtils.AUTHORIZATION_KEY, APIUtils.AUTHORIZATION_BASE + newAccessToken)
.build()
);
} else { } else {
return response; return response;
} }
} else { } else {
response.close(); response.close();
return chain.proceed(response.request().newBuilder().headers(Headers.of(APIUtils.getRedgifsOAuthHeader(accessTokenFromSharedPreferences))).build()); return chain.proceed(
chain.request().newBuilder()
.addHeader(APIUtils.AUTHORIZATION_KEY, APIUtils.AUTHORIZATION_BASE + accessTokenFromSharedPreferences)
.build()
);
} }
} }
} }

View File

@ -222,10 +222,6 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe
@Named("default") @Named("default")
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
@ -756,8 +752,8 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe
} }
}); });
} else { } else {
FetchGfycatOrRedgifsVideoLinks.fetchRedgifsVideoLinks(this, mExecutor, new Handler(), redgifsRetrofit, FetchGfycatOrRedgifsVideoLinks.fetchRedgifsVideoLinks(mExecutor, new Handler(), redgifsRetrofit,
mCurrentAccountSharedPreferences, gfycatId, new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { gfycatId, new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() {
@Override @Override
public void success(String webm, String mp4) { public void success(String webm, String mp4) {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);

View File

@ -139,7 +139,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
private BaseActivity mActivity; private BaseActivity mActivity;
private HistoryPostFragment mFragment; private HistoryPostFragment mFragment;
private SharedPreferences mSharedPreferences; private SharedPreferences mSharedPreferences;
private SharedPreferences mCurrentAccountSharedPreferences;
private Executor mExecutor; private Executor mExecutor;
private Retrofit retrofit; private Retrofit retrofit;
private Retrofit mGfycatRetrofit; private Retrofit mGfycatRetrofit;
@ -227,7 +226,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, Provider<StreamableAPI> streambleApiProvider, Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, Provider<StreamableAPI> streambleApiProvider,
CustomThemeWrapper customThemeWrapper, Locale locale, CustomThemeWrapper customThemeWrapper, Locale locale,
String accessToken, String accountName, int postType, int postLayout, boolean displaySubredditName, String accessToken, String accountName, int postType, int postLayout, boolean displaySubredditName,
SharedPreferences sharedPreferences, SharedPreferences currentAccountSharedPreferences, SharedPreferences sharedPreferences,
SharedPreferences nsfwAndSpoilerSharedPreferences, SharedPreferences nsfwAndSpoilerSharedPreferences,
ExoCreator exoCreator, Callback callback) { ExoCreator exoCreator, Callback callback) {
super(DIFF_CALLBACK); super(DIFF_CALLBACK);
@ -235,7 +234,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
mActivity = activity; mActivity = activity;
mFragment = fragment; mFragment = fragment;
mSharedPreferences = sharedPreferences; mSharedPreferences = sharedPreferences;
mCurrentAccountSharedPreferences = currentAccountSharedPreferences;
mExecutor = executor; mExecutor = executor;
retrofit = oauthRetrofit; retrofit = oauthRetrofit;
mGfycatRetrofit = gfycatRetrofit; mGfycatRetrofit = gfycatRetrofit;
@ -698,7 +696,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) { if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall = ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) : post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) :
mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")), post.getGfycatId(), APIUtils.USER_AGENT); mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(post.getGfycatId());
FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(), FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall, ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
post.isGfycat(), mAutomaticallyTryRedgifs, post.isGfycat(), mAutomaticallyTryRedgifs,
@ -875,7 +873,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) { if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall = ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) : post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) :
mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")), post.getGfycatId(), APIUtils.USER_AGENT); mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(post.getGfycatId());
FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(), FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall, ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
post.isGfycat(), mAutomaticallyTryRedgifs, post.isGfycat(), mAutomaticallyTryRedgifs,

View File

@ -698,7 +698,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
if (mPost.isGfycat() || mPost.isRedgifs() && !mPost.isLoadGfycatOrStreamableVideoSuccess()) { if (mPost.isGfycat() || mPost.isRedgifs() && !mPost.isLoadGfycatOrStreamableVideoSuccess()) {
((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall = ((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
mPost.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(mPost.getGfycatId()) : mPost.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(mPost.getGfycatId()) :
mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")), mPost.getGfycatId(), APIUtils.USER_AGENT); mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(mPost.getGfycatId());
FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(), FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall, ((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
mPost.isGfycat(), mAutomaticallyTryRedgifs, mPost.isGfycat(), mAutomaticallyTryRedgifs,

View File

@ -787,7 +787,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) { if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall = ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) : post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) :
mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")), post.getGfycatId(), APIUtils.USER_AGENT); mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(post.getGfycatId());
FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(), FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall, ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
post.isGfycat(), mAutomaticallyTryRedgifs, post.isGfycat(), mAutomaticallyTryRedgifs,
@ -979,7 +979,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) { if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall = ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) : post.isGfycat() ? mGfycatRetrofit.create(GfycatAPI.class).getGfycatData(post.getGfycatId()) :
mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")), post.getGfycatId(), APIUtils.USER_AGENT); mRedgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(post.getGfycatId());
FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(), FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall, ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
post.isGfycat(), mAutomaticallyTryRedgifs, post.isGfycat(), mAutomaticallyTryRedgifs,

View File

@ -6,14 +6,12 @@ import retrofit2.Call;
import retrofit2.http.FieldMap; import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded; import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.POST; import retrofit2.http.POST;
import retrofit2.http.Path; import retrofit2.http.Path;
import retrofit2.http.Query;
public interface RedgifsAPI { public interface RedgifsAPI {
@GET("/v2/gifs/{id}") @GET("/v2/gifs/{id}")
Call<String> getRedgifsData(@HeaderMap Map<String, String> headers, @Path("id") String id, @Query("user-agent") String userAgent); Call<String> getRedgifsData(@Path("id") String id);
@FormUrlEncoded @FormUrlEncoded
@POST("/v2/oauth/client") @POST("/v2/oauth/client")

View File

@ -178,9 +178,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
@Named("default") @Named("default")
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
@Inject @Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
@Named("post_layout") @Named("post_layout")
SharedPreferences mPostLayoutSharedPreferences; SharedPreferences mPostLayoutSharedPreferences;
@Inject @Inject
@ -383,7 +380,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
mAdapter = new HistoryPostRecyclerViewAdapter(activity, this, mExecutor, mRetrofit.getRetrofit(), mGfycatRetrofit, mAdapter = new HistoryPostRecyclerViewAdapter(activity, this, mExecutor, mRetrofit.getRetrofit(), mGfycatRetrofit,
mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale,
accessToken, accountName, postType, postLayout, true, accessToken, accountName, postType, postLayout, true,
mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mSharedPreferences, mNsfwAndSpoilerSharedPreferences,
mExoCreator, new HistoryPostRecyclerViewAdapter.Callback() { mExoCreator, new HistoryPostRecyclerViewAdapter.Callback() {
@Override @Override
public void typeChipClicked(int filter) { public void typeChipClicked(int filter) {

View File

@ -142,12 +142,6 @@ public class APIUtils {
return params; return params;
} }
public static Map<String, String> getRedgifsOAuthHeader(String redgifsAccessToken) {
Map<String, String> params = new HashMap<>();
params.put(APIUtils.AUTHORIZATION_KEY, APIUtils.AUTHORIZATION_BASE + redgifsAccessToken);
return params;
}
public static RequestBody getRequestBody(String s) { public static RequestBody getRequestBody(String s) {
return RequestBody.create(s, MediaType.parse("text/plain")); return RequestBody.create(s, MediaType.parse("text/plain"));
} }