jenkins-bot has submitted this change and it was merged.

Change subject: Correctly handle Site language when instantiating Retrofit 
clients.
......................................................................


Correctly handle Site language when instantiating Retrofit clients.

The switch to using a single instance of OkHttpClient left out a crucial
bit of information: the Site object contains a languageCode field that is
used to determine custom headers (for language variants) that are sent
with each request, whereas the new single-instance OkHttpClient was simply
using the subdomain name of the Url as the language code.

This restores the Site object as a parameter for making new Retrofit
instances, and preserves the Site for use by the Interceptor that adds
custom headers.

Change-Id: Ic40d795746d9722c490407813228273fb2c13ced
---
M app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java
M app/src/main/java/org/wikipedia/WikipediaApp.java
M app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java
M 
app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.java
M app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
5 files changed, 31 insertions(+), 23 deletions(-)

Approvals:
  Mholloway: Looks good to me, but someone else must approve
  jenkins-bot: Verified
  Niedzielski: Looks good to me, approved



diff --git a/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java 
b/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java
index 3742860..82a9026 100644
--- a/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java
+++ b/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java
@@ -13,12 +13,9 @@
 
 import okhttp3.Cache;
 import okhttp3.CookieJar;
-import okhttp3.Interceptor;
 import okhttp3.JavaNetCookieJar;
 import okhttp3.OkHttpClient;
 import okhttp3.OkUrlFactory;
-import okhttp3.Request;
-import okhttp3.Response;
 import okhttp3.logging.HttpLoggingInterceptor;
 
 public class OkHttpConnectionFactory implements HttpRequest.ConnectionFactory {
@@ -55,18 +52,6 @@
                 .cookieJar(cookieJar)
                 .cache(HTTP_CACHE)
                 .addInterceptor(loggingInterceptor)
-                .addInterceptor(new CustomHeaderInterceptor())
                 .build();
-    }
-
-    private static class CustomHeaderInterceptor implements Interceptor {
-        @Override
-        public Response intercept(Interceptor.Chain chain) throws IOException {
-            Request request = chain.request();
-            request = request.newBuilder()
-                    
.headers(WikipediaApp.getInstance().buildCustomHeaders(request))
-                    .build();
-            return chain.proceed(request);
-        }
     }
 }
diff --git a/app/src/main/java/org/wikipedia/WikipediaApp.java 
b/app/src/main/java/org/wikipedia/WikipediaApp.java
index 5ca6353..e456aee 100644
--- a/app/src/main/java/org/wikipedia/WikipediaApp.java
+++ b/app/src/main/java/org/wikipedia/WikipediaApp.java
@@ -528,9 +528,8 @@
     }
 
     /** For Retrofit requests. Keep in sync with #buildCustomHeadersMap */
-    public Headers buildCustomHeaders(Request request) {
-        Map<String, String> toSetHeaders
-                = buildCustomHeadersMap(getAcceptLanguage(new 
Site(request.url().host())));
+    public Headers buildCustomHeaders(Request request, Site site) {
+        Map<String, String> toSetHeaders = 
buildCustomHeadersMap(getAcceptLanguage(site));
 
         Headers.Builder moreHeaders = request.headers().newBuilder();
         for (String key : toSetHeaders.keySet()) {
diff --git 
a/app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java 
b/app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java
index deb98b6..b6d7887 100644
--- a/app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java
+++ b/app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java
@@ -4,23 +4,47 @@
 
 import org.wikipedia.OkHttpConnectionFactory;
 import org.wikipedia.Site;
+import org.wikipedia.WikipediaApp;
 import org.wikipedia.json.GsonUtil;
 
+import java.io.IOException;
+
+import okhttp3.Interceptor;
+import okhttp3.Request;
+import okhttp3.Response;
 import retrofit2.Retrofit;
 import retrofit2.converter.gson.GsonConverterFactory;
 
 public final class RetrofitFactory {
     public static Retrofit newInstance(@NonNull Site site) {
-        return newInstance(site.url() + "/");
+        return newInstance(site.url() + "/", site);
     }
 
-    public static Retrofit newInstance(@NonNull String endpoint) {
+    public static Retrofit newInstance(@NonNull String endpoint, @NonNull Site 
site) {
         return new Retrofit.Builder()
-                .client(OkHttpConnectionFactory.getClient())
+                .client(OkHttpConnectionFactory.getClient().newBuilder()
+                        .addInterceptor(new 
CustomHeaderInterceptor(site)).build())
                 .baseUrl(endpoint)
                 
.addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
                 .build();
     }
 
     private RetrofitFactory() { }
+
+    private static class CustomHeaderInterceptor implements Interceptor {
+        private final Site site;
+
+        CustomHeaderInterceptor(@NonNull Site site) {
+            this.site = site;
+        }
+
+        @Override
+        public Response intercept(Interceptor.Chain chain) throws IOException {
+            Request request = chain.request();
+            request = request.newBuilder()
+                    
.headers(WikipediaApp.getInstance().buildCustomHeaders(request, site))
+                    .build();
+            return chain.proceed(request);
+        }
+    }
 }
diff --git 
a/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.java
 
b/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.java
index a7d7043..93d0ff9 100644
--- 
a/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.java
+++ 
b/app/src/main/java/org/wikipedia/feed/aggregated/AggregatedFeedContentClient.java
@@ -39,7 +39,7 @@
         UtcDate date = DateUtil.getUtcRequestDateFor(age);
         String endpoint = String.format(Locale.ROOT, 
Prefs.getRestbaseUriFormat(), site.scheme(),
                 site.authority());
-        Retrofit retrofit = RetrofitFactory.newInstance(endpoint);
+        Retrofit retrofit = RetrofitFactory.newInstance(endpoint, site);
         AggregatedFeedContentClient.Service service = 
retrofit.create(Service.class);
         call = service.get(date.year(), date.month(), date.date());
         call.enqueue(new CallbackAdapter(cb, site, age));
diff --git 
a/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java 
b/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
index 37dc08c..5daf5f8 100644
--- a/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
+++ b/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
@@ -24,7 +24,7 @@
     public static Retrofit retrofit(@NonNull Site site) {
         String endpoint = String.format(Locale.ROOT, 
Prefs.getRestbaseUriFormat(), site.scheme(),
                 site.authority());
-        return RetrofitFactory.newInstance(endpoint);
+        return RetrofitFactory.newInstance(endpoint, site);
     }
 
     private RbPageEndpointsCache() {

-- 
To view, visit https://gerrit.wikimedia.org/r/315496
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic40d795746d9722c490407813228273fb2c13ced
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Dbrant <dbr...@wikimedia.org>
Gerrit-Reviewer: BearND <bsitzm...@wikimedia.org>
Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org>
Gerrit-Reviewer: Dbrant <dbr...@wikimedia.org>
Gerrit-Reviewer: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: Niedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to