mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-27 10:04:45 +01:00
Cleanup RedGIFs API usage
- Add User-Agent and authentication transparently - Provide RedgifsAPI instance
This commit is contained in:
parent
9406f29562
commit
67afcd7e88
@ -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<String> response = redgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(APIUtils.getRedgifsOAuthHeader(currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "")),
|
||||
gfycatId, APIUtils.USER_AGENT).execute();
|
||||
Response<String> response = redgifsRetrofit.create(RedgifsAPI.class).getRedgifsData(gfycatId).execute();
|
||||
if (response.isSuccessful()) {
|
||||
parseRedgifsVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
|
||||
} else {
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -139,7 +139,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
|
||||
private BaseActivity mActivity;
|
||||
private HistoryPostFragment mFragment;
|
||||
private SharedPreferences mSharedPreferences;
|
||||
private SharedPreferences mCurrentAccountSharedPreferences;
|
||||
private Executor mExecutor;
|
||||
private Retrofit retrofit;
|
||||
private Retrofit mGfycatRetrofit;
|
||||
@ -227,7 +226,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
|
||||
Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, Provider<StreamableAPI> 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<Post, Recy
|
||||
mActivity = activity;
|
||||
mFragment = fragment;
|
||||
mSharedPreferences = sharedPreferences;
|
||||
mCurrentAccountSharedPreferences = currentAccountSharedPreferences;
|
||||
mExecutor = executor;
|
||||
retrofit = oauthRetrofit;
|
||||
mGfycatRetrofit = gfycatRetrofit;
|
||||
@ -698,7 +696,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
|
||||
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
|
||||
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
|
||||
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(),
|
||||
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
|
||||
post.isGfycat(), mAutomaticallyTryRedgifs,
|
||||
@ -875,7 +873,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
|
||||
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
|
||||
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
|
||||
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(),
|
||||
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
|
||||
post.isGfycat(), mAutomaticallyTryRedgifs,
|
||||
|
@ -698,7 +698,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
|
||||
if (mPost.isGfycat() || mPost.isRedgifs() && !mPost.isLoadGfycatOrStreamableVideoSuccess()) {
|
||||
((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
|
||||
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(),
|
||||
((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
|
||||
mPost.isGfycat(), mAutomaticallyTryRedgifs,
|
||||
|
@ -787,7 +787,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
|
||||
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
|
||||
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
|
||||
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(),
|
||||
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
|
||||
post.isGfycat(), mAutomaticallyTryRedgifs,
|
||||
@ -979,7 +979,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
|
||||
if ((post.isGfycat() || post.isRedgifs()) && !post.isLoadGfycatOrStreamableVideoSuccess()) {
|
||||
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall =
|
||||
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(),
|
||||
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrStreamableVideoCall,
|
||||
post.isGfycat(), mAutomaticallyTryRedgifs,
|
||||
|
@ -6,14 +6,12 @@ import retrofit2.Call;
|
||||
import retrofit2.http.FieldMap;
|
||||
import retrofit2.http.FormUrlEncoded;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.HeaderMap;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.Path;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
public interface RedgifsAPI {
|
||||
@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
|
||||
@POST("/v2/oauth/client")
|
||||
|
@ -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) {
|
||||
|
@ -142,12 +142,6 @@ public class APIUtils {
|
||||
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) {
|
||||
return RequestBody.create(s, MediaType.parse("text/plain"));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user