Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / connection / RestClient.java
1 package com.moiseum.wolnelektury.connection;
2
3 import android.content.Context;
4
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;
14
15 import java.io.File;
16 import java.util.Random;
17 import java.util.concurrent.TimeUnit;
18
19 import okhttp3.Cache;
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;
26
27 /**
28  * @author golonkos.
29  */
30
31 public class RestClient {
32         public static final int PAGINATION_LIMIT = 30;
33
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 = "";
39
40         private static final String CACHE_DIR = "responses";
41         private static final int CACHE_SIZE_MB = 10;
42
43         private static final String CONSUMER_KEY = "";
44         private static final String CONSUMER_SECRET = "";
45
46         private final Retrofit retrofit;
47         private final OAuthSigningInterceptor oAuthInterceptor;
48
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());
54                 }
55                 UnauthorizedInterceptor unauthorizedInterceptor = new UnauthorizedInterceptor();
56                 NewApiInterceptor newApiInterceptor = new NewApiInterceptor();
57
58                 GsonBuilder gsonBuilder = new GsonBuilder();
59                 //gsonBuilder.registerTypeAdapter(Date.class, new RestClientDateSerializer());
60
61                 Gson gson = gsonBuilder.create();
62
63                 int cacheSize = CACHE_SIZE_MB * 1024 * 1024;
64                 File cacheDir = new File(context.getCacheDir(), CACHE_DIR);
65                 Cache cache = new Cache(cacheDir, cacheSize);
66
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);
75                 }
76                 builder.writeTimeout(60, TimeUnit.SECONDS);
77                 builder.readTimeout(60, TimeUnit.SECONDS);
78                 builder.connectTimeout(60, TimeUnit.SECONDS);
79
80                 OkHttpClient client = builder.cache(cache).build();
81                 retrofit = new Retrofit.Builder()
82                                 .baseUrl(BASE_URL)
83                                 .addConverterFactory(GsonConverterFactory.create(gson))
84                                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
85                                 .client(client)
86                                 .build();
87         }
88
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);
93                 return call;
94         }
95
96         public <S> S createService(Class<S> clazz) {
97                 return retrofit.create(clazz);
98         }
99
100         public void clearOAuthTokens() {
101                 oAuthInterceptor.setToken(null, null);
102         }
103
104         public BooksService obtainBookService() {
105                 return retrofit.create(BooksService.class);
106         }
107 }