1111import com .franmontiel .persistentcookiejar .PersistentCookieJar ;
1212import com .franmontiel .persistentcookiejar .cache .SetCookieCache ;
1313import com .proxerme .library .BuildConfig ;
14- import com .proxerme .library .info .ProxerUrlHolder ;
1514import com .proxerme .library .util .SaveAllSharedPrefCookiePersistor ;
1615import com .squareup .moshi .JsonDataException ;
1716import com .squareup .moshi .Moshi ;
2221import okhttp3 .Call ;
2322import okhttp3 .Callback ;
2423import okhttp3 .CookieJar ;
25- import okhttp3 .Interceptor ;
2624import okhttp3 .OkHttpClient ;
25+ import okhttp3 .Request ;
2726import okhttp3 .Response ;
2827
2928/**
6766 */
6867public final class ProxerConnection {
6968
69+ private static final String API_KEY_HEADER = "proxer-api-key" ;
70+ private static final String USER_AGENT_HEADER = "User-Agent" ;
71+
7072 private String apiKey ;
73+ private String userAgent ;
7174 private Moshi moshi ;
7275 private OkHttpClient httpClient ;
7376
@@ -76,9 +79,10 @@ public final class ProxerConnection {
7679 private Handler handler = new Handler (Looper .getMainLooper ());
7780 private ConcurrentHashMap <Integer , ErrorListener > listenerMap ;
7881
79- private ProxerConnection (@ NonNull String apiKey , Moshi moshi , OkHttpClient httpClient ,
80- boolean deliverCancelledRequests ) {
82+ private ProxerConnection (@ NonNull String apiKey , @ NonNull String userAgent , Moshi moshi ,
83+ OkHttpClient httpClient , boolean deliverCancelledRequests ) {
8184 this .apiKey = apiKey ;
85+ this .userAgent = userAgent ;
8286 this .moshi = moshi ;
8387 this .httpClient = httpClient ;
8488 this .deliverCancelledRequests = deliverCancelledRequests ;
@@ -99,7 +103,7 @@ private ProxerConnection(@NonNull String apiKey, Moshi moshi, OkHttpClient httpC
99103 public <T > ProxerCall execute (@ NonNull final ProxerRequest <T > request ,
100104 @ Nullable final ProxerCallback <T > callback ,
101105 @ Nullable final ProxerErrorCallback errorCallback ) {
102- Call call = httpClient .newCall (request . build ( ));
106+ Call call = httpClient .newCall (buildRequest ( request ));
103107
104108 call .enqueue (new Callback () {
105109 @ Override
@@ -148,7 +152,7 @@ public void onFailure(Call call, IOException exception) {
148152 @ RequiresPermission (android .Manifest .permission .INTERNET )
149153 public <T > T executeSynchronized (@ NonNull final ProxerRequest <T > request )
150154 throws ProxerException {
151- final Call call = httpClient .newCall (request . build ( ));
155+ final Call call = httpClient .newCall (buildRequest ( request ));
152156 Response response = null ;
153157
154158 try {
@@ -297,6 +301,13 @@ private void notifyListener(@NonNull ProxerException exception) {
297301 }
298302 }
299303
304+ private <T > Request buildRequest (ProxerRequest <T > request ) {
305+ return request .build ().newBuilder ()
306+ .header (API_KEY_HEADER , apiKey )
307+ .header (USER_AGENT_HEADER , userAgent )
308+ .build ();
309+ }
310+
300311 public interface ErrorListener {
301312 void onError (@ NonNull ProxerException exception );
302313 }
@@ -306,9 +317,7 @@ public interface ErrorListener {
306317 */
307318 public static class Builder {
308319
309- private static final String API_KEY_HEADER = "proxer-api-key" ;
310320 private static final String DEFAULT_USER_AGENT = "ProxerLibAndroid" ;
311- private static final String USER_AGENT_HEADER = "User-Agent" ;
312321
313322 private String apiKey ;
314323 private Context context ;
@@ -354,7 +363,8 @@ public ProxerConnection build() {
354363 configureUserAgent ();
355364 configureOkHttp ();
356365
357- return new ProxerConnection (apiKey , moshi , httpClient , deliverCancelledRequests );
366+ return new ProxerConnection (apiKey , userAgent , moshi , httpClient ,
367+ deliverCancelledRequests );
358368 }
359369
360370 /**
@@ -435,48 +445,7 @@ private void configureOkHttp() {
435445 builder = httpClient .newBuilder ();
436446 }
437447
438- httpClient = builder .cookieJar (cookieJar )
439- .addInterceptor (new ApiKeyInterceptor (apiKey ))
440- .addInterceptor (new UserAgentInterceptor (userAgent ))
441- .build ();
442- }
443-
444- private class ApiKeyInterceptor implements Interceptor {
445-
446- private String apiKey ;
447-
448- ApiKeyInterceptor (String apiKey ) {
449- this .apiKey = apiKey ;
450- }
451-
452- @ Override
453- public Response intercept (Chain chain ) throws IOException {
454- if (chain .request ().url ().host ()
455- .equals (ProxerUrlHolder .getBaseApiHost ().host ())) {
456- return chain .proceed (chain .request ().newBuilder ()
457- .header (API_KEY_HEADER , apiKey )
458- .build ());
459- } else {
460- return chain .proceed (chain .request ());
461- }
462- }
463- }
464-
465- private class UserAgentInterceptor implements Interceptor {
466-
467- private String userAgent ;
468-
469- UserAgentInterceptor (@ NonNull String userAgent ) {
470- this .userAgent = userAgent ;
471- }
472-
473- @ Override
474- public Response intercept (Chain chain ) throws IOException {
475- return chain .proceed (chain .request ().newBuilder ()
476- .header (USER_AGENT_HEADER , userAgent )
477- .build ());
478- }
448+ httpClient = builder .cookieJar (cookieJar ).build ();
479449 }
480-
481450 }
482451}
0 commit comments