1 package com.moiseum.wolnelektury.connection;
3 import android.content.Context;
5 import com.google.gson.Gson;
6 import com.google.gson.GsonBuilder;
7 import com.moiseum.wolnelektury.BuildConfig;
8 import com.moiseum.wolnelektury.base.WLApplication;
9 import com.moiseum.wolnelektury.connection.interceptors.NewApiInterceptor;
10 import com.moiseum.wolnelektury.connection.interceptors.OAuthSigningInterceptor;
11 import com.moiseum.wolnelektury.connection.interceptors.UnauthorizedInterceptor;
12 import com.moiseum.wolnelektury.connection.models.OAuthTokenModel;
13 import com.moiseum.wolnelektury.connection.services.BooksService;
16 import java.util.Random;
17 import java.util.concurrent.TimeUnit;
20 import okhttp3.OkHttpClient;
21 import okhttp3.logging.HttpLoggingInterceptor;
22 import retrofit2.Call;
23 import retrofit2.Retrofit;
24 import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
25 import retrofit2.converter.gson.GsonConverterFactory;
31 public class RestClient {
32 public static final int PAGINATION_LIMIT = 30;
34 public static final String MEDIA_URL = "http://wolnelektury.pl/media/";
35 public static final String MEDIA_URL_HTTPS = "https://wolnelektury.pl/media/";
36 public static final String BASE_URL = "https://wolnelektury.pl/api/";
37 public static final String WEB_OAUTH_AUTHORIZATION_URL = "";
38 public static final String WEB_PAYPAL_FORM_URL = "";
40 private static final String CACHE_DIR = "responses";
41 private static final int CACHE_SIZE_MB = 10;
43 private static final String CONSUMER_KEY = "";
44 private static final String CONSUMER_SECRET = "";
46 private final Retrofit retrofit;
47 private final OAuthSigningInterceptor oAuthInterceptor;
49 public RestClient(Context context) {
50 OAuthTokenModel currentToken = WLApplication.getInstance().getPreferences().getAccessToken();
51 oAuthInterceptor = new OAuthSigningInterceptor(CONSUMER_KEY, CONSUMER_SECRET, new Random());
52 if (currentToken != null) {
53 oAuthInterceptor.setToken(currentToken.getToken(), currentToken.getTokenSecret());
55 UnauthorizedInterceptor unauthorizedInterceptor = new UnauthorizedInterceptor();
56 NewApiInterceptor newApiInterceptor = new NewApiInterceptor();
58 GsonBuilder gsonBuilder = new GsonBuilder();
59 //gsonBuilder.registerTypeAdapter(Date.class, new RestClientDateSerializer());
61 Gson gson = gsonBuilder.create();
63 int cacheSize = CACHE_SIZE_MB * 1024 * 1024;
64 File cacheDir = new File(context.getCacheDir(), CACHE_DIR);
65 Cache cache = new Cache(cacheDir, cacheSize);
67 OkHttpClient.Builder builder = new OkHttpClient.Builder();
68 builder.interceptors().add(newApiInterceptor);
69 builder.interceptors().add(oAuthInterceptor);
70 builder.interceptors().add(unauthorizedInterceptor);
71 if (BuildConfig.DEBUG) {
72 HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
73 loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
74 builder.interceptors().add(loggingInterceptor);
76 builder.writeTimeout(60, TimeUnit.SECONDS);
77 builder.readTimeout(60, TimeUnit.SECONDS);
78 builder.connectTimeout(60, TimeUnit.SECONDS);
80 OkHttpClient client = builder.cache(cache).build();
81 retrofit = new Retrofit.Builder()
83 .addConverterFactory(GsonConverterFactory.create(gson))
84 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
89 public <T, S> Call<T> call(RestClientCallback<T, S> restClientCallback, Class<S> clazz) {
90 S service = createService(clazz);
91 Call<T> call = restClientCallback.execute(service);
92 call.enqueue(restClientCallback);
96 public <S> S createService(Class<S> clazz) {
97 return retrofit.create(clazz);
100 public void clearOAuthTokens() {
101 oAuthInterceptor.setToken(null, null);
104 public BooksService obtainBookService() {
105 return retrofit.create(BooksService.class);