From 67afcd7e8851091e188b3c0fbfe6732c58d79355 Mon Sep 17 00:00:00 2001 From: Angelo Suzuki <1063155+tinsukE@users.noreply.github.com> Date: Thu, 7 Sep 2023 12:39:19 +0200 Subject: [PATCH] Cleanup RedGIFs API usage - Add User-Agent and authentication transparently - Provide RedgifsAPI instance --- .../FetchGfycatOrRedgifsVideoLinks.java | 10 ++----- .../toldi/infinityforlemmy/NetworkModule.java | 11 +++++++- .../RedgifsAccessTokenAuthenticator.java | 27 ++++++++++++++++--- .../activities/ViewVideoActivity.java | 8 ++---- .../HistoryPostRecyclerViewAdapter.java | 8 +++--- .../PostDetailRecyclerViewAdapter.java | 2 +- .../adapters/PostRecyclerViewAdapter.java | 4 +-- .../infinityforlemmy/apis/RedgifsAPI.java | 4 +-- .../fragments/HistoryPostFragment.java | 5 +--- .../infinityforlemmy/utils/APIUtils.java | 6 ----- 10 files changed, 45 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/eu/toldi/infinityforlemmy/FetchGfycatOrRedgifsVideoLinks.java index 4ce90fa0..c641c7d4 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/FetchGfycatOrRedgifsVideoLinks.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/FetchGfycatOrRedgifsVideoLinks.java @@ -1,7 +1,5 @@ package eu.toldi.infinityforlemmy; -import android.content.Context; -import android.content.SharedPreferences; import android.os.Handler; import org.json.JSONException; @@ -12,9 +10,7 @@ import java.util.concurrent.Executor; import eu.toldi.infinityforlemmy.apis.GfycatAPI; import eu.toldi.infinityforlemmy.apis.RedgifsAPI; -import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.JSONUtils; -import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import retrofit2.Call; import retrofit2.Response; import retrofit2.Retrofit; @@ -44,14 +40,12 @@ public class FetchGfycatOrRedgifsVideoLinks { }); } - public static void fetchRedgifsVideoLinks(Context context, Executor executor, Handler handler, Retrofit redgifsRetrofit, - SharedPreferences currentAccountSharedPreferences, + public static void fetchRedgifsVideoLinks(Executor executor, Handler handler, Retrofit redgifsRetrofit, String gfycatId, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { executor.execute(() -> { try { - Response response = redgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")), - gfycatId, APIUtils.USER_AGENT).execute(); + Response response = redgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(gfycatId).execute(); if (response.isSuccessful()) { parseRedgifsVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); } else { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java b/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java index b4641ee8..7d8cefda 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/NetworkModule.java @@ -9,6 +9,7 @@ import javax.inject.Singleton; import dagger.Module; import dagger.Provides; +import eu.toldi.infinityforlemmy.apis.RedgifsAPI; import eu.toldi.infinityforlemmy.apis.StreamableAPI; import eu.toldi.infinityforlemmy.comment.LemmyCommentAPI; import eu.toldi.infinityforlemmy.post.LemmyPostAPI; @@ -37,7 +38,8 @@ abstract class NetworkModule { @Provides @Named("glide") @Singleton - static OkHttpClient provideGlideOkHttp(@Named("base") OkHttpClient baseOkHttp) { + static OkHttpClient provideGlideOkHttp(@Named("base") OkHttpClient baseOkHttp, + @Named("RedgifsAccessTokenAuthenticator") Interceptor redGifsAuthenticator) { return baseOkHttp.newBuilder() .addInterceptor(chain -> chain.proceed( chain.request() @@ -45,6 +47,7 @@ abstract class NetworkModule { .header("User-Agent", APIUtils.USER_AGENT) .build() )) + .addInterceptor(redGifsAuthenticator) .build(); } @@ -178,6 +181,12 @@ abstract class NetworkModule { .build(); } + @Provides + @Singleton + static RedgifsAPI provideRedgifsAPI(@Named("redgifs") Retrofit redgifsRetrofit) { + return redgifsRetrofit.create(RedgifsAPI.class); + } + @Provides @Named("imgur") @Singleton diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/RedgifsAccessTokenAuthenticator.java b/app/src/main/java/eu/toldi/infinityforlemmy/RedgifsAccessTokenAuthenticator.java index 02e4c8cf..afd688b8 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/RedgifsAccessTokenAuthenticator.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/RedgifsAccessTokenAuthenticator.java @@ -14,7 +14,6 @@ import java.util.Map; import eu.toldi.infinityforlemmy.apis.RedgifsAPI; import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; -import okhttp3.Headers; import okhttp3.Interceptor; import okhttp3.Response; import retrofit2.Call; @@ -22,6 +21,8 @@ import retrofit2.Retrofit; import retrofit2.converter.scalars.ScalarsConverterFactory; public class RedgifsAccessTokenAuthenticator implements Interceptor { + private static final String REDGIFS_HOST = "redgifs.com"; + private SharedPreferences mCurrentAccountSharedPreferences; public RedgifsAccessTokenAuthenticator(SharedPreferences currentAccountSharedPreferences) { @@ -60,7 +61,17 @@ public class RedgifsAccessTokenAuthenticator implements Interceptor { @NonNull @Override 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) { String accessTokenHeader = response.request().header(APIUtils.AUTHORIZATION_KEY); if (accessTokenHeader == null) { @@ -74,13 +85,21 @@ public class RedgifsAccessTokenAuthenticator implements Interceptor { String newAccessToken = refreshAccessToken(); if (!newAccessToken.equals("")) { 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 { return response; } } else { 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() + ); } } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewVideoActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewVideoActivity.java index 49c10931..c1662b8d 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewVideoActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewVideoActivity.java @@ -222,10 +222,6 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe @Named("default") SharedPreferences mSharedPreferences; - @Inject - @Named("current_account") - SharedPreferences mCurrentAccountSharedPreferences; - @Inject CustomThemeWrapper mCustomThemeWrapper; @@ -756,8 +752,8 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe } }); } else { - FetchGfycatOrRedgifsVideoLinks.fetchRedgifsVideoLinks(this, mExecutor, new Handler(), redgifsRetrofit, - mCurrentAccountSharedPreferences, gfycatId, new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { + FetchGfycatOrRedgifsVideoLinks.fetchRedgifsVideoLinks(mExecutor, new Handler(), redgifsRetrofit, + gfycatId, new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { @Override public void success(String webm, String mp4) { progressBar.setVisibility(View.GONE); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java index 268d397b..2b7e1abf 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/HistoryPostRecyclerViewAdapter.java @@ -139,7 +139,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter streambleApiProvider, CustomThemeWrapper customThemeWrapper, Locale locale, String accessToken, String accountName, int postType, int postLayout, boolean displaySubredditName, - SharedPreferences sharedPreferences, SharedPreferences currentAccountSharedPreferences, + SharedPreferences sharedPreferences, SharedPreferences nsfwAndSpoilerSharedPreferences, ExoCreator exoCreator, Callback callback) { super(DIFF_CALLBACK); @@ -235,7 +234,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter getRedgifsData(@HeaderMap Map headers, @Path("id") String id, @Query("user-agent") String userAgent); + Call getRedgifsData(@Path("id") String id); @FormUrlEncoded @POST("/v2/oauth/client") diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/HistoryPostFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/HistoryPostFragment.java index f1fd6ef8..b9f33e1e 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/HistoryPostFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/HistoryPostFragment.java @@ -178,9 +178,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato @Named("default") SharedPreferences mSharedPreferences; @Inject - @Named("current_account") - SharedPreferences mCurrentAccountSharedPreferences; - @Inject @Named("post_layout") SharedPreferences mPostLayoutSharedPreferences; @Inject @@ -383,7 +380,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato mAdapter = new HistoryPostRecyclerViewAdapter(activity, this, mExecutor, mRetrofit.getRetrofit(), mGfycatRetrofit, mRedgifsRetrofit, mStreamableApiProvider, mCustomThemeWrapper, locale, accessToken, accountName, postType, postLayout, true, - mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, + mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mExoCreator, new HistoryPostRecyclerViewAdapter.Callback() { @Override public void typeChipClicked(int filter) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java b/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java index 48bb5e5b..405d5474 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java @@ -142,12 +142,6 @@ public class APIUtils { return params; } - public static Map getRedgifsOAuthHeader(String redgifsAccessToken) { - Map params = new HashMap<>(); - params.put(APIUtils.AUTHORIZATION_KEY, APIUtils.AUTHORIZATION_BASE + redgifsAccessToken); - return params; - } - public static RequestBody getRequestBody(String s) { return RequestBody.create(s, MediaType.parse("text/plain")); }