Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge cff4030 into 1a7bc38
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Aug 8, 2017
2 parents 1a7bc38 + cff4030 commit b3d1e94
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subreddit.android.appstore.backend.reddit.wiki.WikiRepositoryModule;
import subreddit.android.appstore.backend.scrapers.MediaScraper;
import subreddit.android.appstore.backend.scrapers.ScraperModule;
import subreddit.android.appstore.backend.HttpModule;
import subreddit.android.appstore.util.dagger.ApplicationScope;


Expand All @@ -17,7 +18,8 @@
AndroidModule.class,
WikiRepositoryModule.class,
ScraperModule.class,
GithubRepositoryModule.class
GithubRepositoryModule.class,
HttpModule.class
})
public interface AppComponent {
SharedPreferences providePreferences();
Expand All @@ -26,5 +28,5 @@ public interface AppComponent {

MediaScraper mediaScraper();

GithubRepository selfUpdater();
GithubRepository githubRepository();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package subreddit.android.appstore.backend;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import subreddit.android.appstore.BuildConfig;
import subreddit.android.appstore.util.dagger.ApplicationScope;

@Module
public class HttpModule {

@Provides
@ApplicationScope
Gson provideGson() {
return new GsonBuilder().create();
}

@Provides
@ApplicationScope
OkHttpClient provideOkHttpClient(UserAgentInterceptor userAgent) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
}
builder.addInterceptor(userAgent);
return builder.build();
}

@Provides
@ApplicationScope
Retrofit provideRetrofit(OkHttpClient client) {
return new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("https://api.github.com/")
.build();
}


}
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
package subreddit.android.appstore.backend.github;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import subreddit.android.appstore.BuildConfig;
import subreddit.android.appstore.backend.UserAgentInterceptor;

public class LiveGithubRepository implements GithubRepository {
private static final String BASEURL = "https://api.github.com/";
private final GithubApi githubApi;
private Observable<Release> latestReleaseCache;
private Observable<List<Contributor>> latestContributorsCache;

public LiveGithubRepository(UserAgentInterceptor userAgent) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
}
builder.addInterceptor(userAgent);
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(BASEURL)
.build();
public LiveGithubRepository(Retrofit retrofit) {
githubApi = retrofit.create(GithubApi.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,16 @@
import android.util.Base64;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.Header;
import retrofit2.http.POST;
import subreddit.android.appstore.BuildConfig;
import subreddit.android.appstore.backend.DeviceIdentifier;
import subreddit.android.appstore.backend.UserAgentInterceptor;
import timber.log.Timber;

public class TokenRepository {
Expand All @@ -41,31 +31,19 @@ public class TokenRepository {
private final Gson gson;
private final SharedPreferences preferences;

public TokenRepository(Context context, DeviceIdentifier deviceIdentifier, UserAgentInterceptor userAgent) {
public TokenRepository(Context context,
DeviceIdentifier deviceIdentifier,
Retrofit retrofit,
Gson gson) {
this.context = context;
this.deviceIdentifier = deviceIdentifier;
this.gson = gson;

OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
}
builder.addInterceptor(userAgent);
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(BASEURL)
.build();
tokenApi = retrofit.create(TokenApi.class);

String credentials = CLIENT_ID + ":";
encodedCredentials = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

gson = new GsonBuilder().create();

preferences = PreferenceManager.getDefaultSharedPreferences(context);
}

Expand Down Expand Up @@ -105,8 +83,9 @@ void storeToken(@NonNull Token token) {
}

interface TokenApi {
// BASEURL overides retrofit.baseUrl()
@FormUrlEncoded
@POST("api/v1/access_token")
@POST(BASEURL + "api/v1/access_token")
Observable<Token> getUserlessAuthToken(
@Header("Authorization") String authentication,
@Field("device_id") String deviceId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package subreddit.android.appstore.backend.reddit.wiki;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.ArrayList;
import java.util.Collection;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.ReplaySubject;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Query;
import subreddit.android.appstore.BuildConfig;
import subreddit.android.appstore.backend.UserAgentInterceptor;
import subreddit.android.appstore.backend.data.AppInfo;
import subreddit.android.appstore.backend.data.AppTags;
import subreddit.android.appstore.backend.reddit.TokenRepository;
Expand All @@ -26,9 +19,8 @@


public class LiveWikiRepository implements WikiRepository {
static final String BASEURL = " https://oauth.reddit.com/";
static final String BASEURL = "https://oauth.reddit.com/";
static final int NUMOFREVISIONS = 6;
final OkHttpClient client = new OkHttpClient();
final WikiDiskCache wikiDiskCache;
final TokenRepository tokenRepository;
final WikiApi wikiApi;
Expand All @@ -38,26 +30,12 @@ public class LiveWikiRepository implements WikiRepository {

public LiveWikiRepository(TokenRepository tokenRepository,
WikiDiskCache wikiDiskCache,
UserAgentInterceptor userAgent,
BodyParser bodyParser) {
BodyParser bodyParser,
Retrofit retrofit) {
this.tokenRepository = tokenRepository;
this.wikiDiskCache = wikiDiskCache;
this.bodyParser = bodyParser;

OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
}
builder.addInterceptor(userAgent);
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(BASEURL)
.build();
wikiApi = retrofit.create(WikiApi.class);
}

Expand Down Expand Up @@ -135,21 +113,22 @@ private String saveAuthString(String authString) {
}

interface WikiApi {
@GET("r/Android/wiki/page")
// BASEURL overides retrofit.baseUrl()
@GET(BASEURL + "r/Android/wiki/page")
Observable<WikiPageResponse> getWikiPage(@Header("Authorization") String authentication,
@Query("page") String page);

@GET("r/Android/wiki/revisions/page&limit")
@GET(BASEURL + "r/Android/wiki/revisions/page&limit")
Observable<WikiRevisionsResponse> getWikiRevisions(@Header("Authorization") String authentication,
@Query("page") String page,
@Query("limit") String lim);

@GET("r/Android/wiki/page&v")
@GET(BASEURL + "r/Android/wiki/page&v")
Observable<WikiPageResponse> getWikiRevision(@Header("Authorization") String authentication,
@Query("page") String page,
@Query("v") String id);

@GET("r/Android/wiki/page&v&v2")
@GET(BASEURL + "r/Android/wiki/page&v&v2")
Observable<WikiPageResponse> getWikiRevisionDiff(@Header("Authorization") String authentication,
@Query("page") String page,
@Query("v") String id1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.support.v4.util.LruCache;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import okhttp3.OkHttpClient;
import subreddit.android.appstore.backend.UnsupportedScrapeTargetException;
import subreddit.android.appstore.backend.data.AppInfo;
import subreddit.android.appstore.backend.data.Download;
Expand All @@ -16,10 +16,11 @@
public class LiveMediaScraper implements MediaScraper {
final LruCache<AppInfo, Observable<ScrapeResult>> scrapeCache = new LruCache<>(1);
final ScrapeDiskCache scrapeDiskCache;
final GPlayScraper gPlayScraper = new GPlayScraper();
final GPlayScraper gPlayScraper;

public LiveMediaScraper(ScrapeDiskCache scrapeDiskCache) {
public LiveMediaScraper(ScrapeDiskCache scrapeDiskCache, OkHttpClient client) {
this.scrapeDiskCache = scrapeDiskCache;
this.gPlayScraper = new GPlayScraper(client);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Function;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import subreddit.android.appstore.backend.DeadLinkException;
import subreddit.android.appstore.backend.data.AppInfo;
import subreddit.android.appstore.backend.data.Download;
Expand All @@ -23,7 +18,12 @@
import timber.log.Timber;

public class GPlayScraper implements MediaScraper {
final OkHttpClient client = new OkHttpClient();
// TODO: okhttp
final OkHttpClient client;

public GPlayScraper(OkHttpClient client) {
this.client = client;
}

@NonNull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

githubRepository = AppStoreApp.Injector.INSTANCE.getAppComponent().selfUpdater();
githubRepository = AppStoreApp.Injector.INSTANCE.getAppComponent().githubRepository();

setSupportActionBar(mToolbar);
mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_48px);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import subreddit.android.appstore.backend.scrapers.ImgSize;
import subreddit.android.appstore.backend.scrapers.MediaScraper;
import subreddit.android.appstore.backend.scrapers.ScrapeResult;


public class IconRequestModelLoader implements ModelLoader<IconRequest, InputStream> {
Expand Down Expand Up @@ -116,6 +114,7 @@ public void loadData(Priority priority, final DataCallback<? super InputStream>
if (iconUrl == null) {
throw new IllegalArgumentException("Icon url was null for" + iconRequest.getAppInfo());
}
//TODO: okhttp
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(iconUrl).build();
return client.newCall(request).execute().body().byteStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import dagger.Module;
import dagger.Provides;
import subreddit.android.appstore.backend.UserAgentInterceptor;
import retrofit2.Retrofit;
import subreddit.android.appstore.backend.HttpModule;
import subreddit.android.appstore.util.dagger.ApplicationScope;


@Module
@Module(includes = HttpModule.class)
public class GithubRepositoryModule {

@Provides
@ApplicationScope
public GithubRepository provideGithubRepository(UserAgentInterceptor userAgentInterceptor) {
return new LiveGithubRepository(userAgentInterceptor);
public GithubRepository provideGithubRepository(Retrofit retrofit) {
return new LiveGithubRepository(retrofit);
}
}
Loading

0 comments on commit b3d1e94

Please sign in to comment.