Dbrant has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/315498

Change subject: Hygiene: refactor insertion of common headers in Retrofit 
requests.
......................................................................

Hygiene: refactor insertion of common headers in Retrofit requests.

Change-Id: Id6b13d9bc1483506346e69170a396fc9fa4f3f43
---
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/analytics/EventLoggingEvent.java
M app/src/main/java/org/wikipedia/analytics/Funnel.java
M app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java
M app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java
6 files changed, 26 insertions(+), 27 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/98/315498/1

diff --git a/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java 
b/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java
index 82a9026..4727d84 100644
--- a/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java
+++ b/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java
@@ -13,9 +13,12 @@
 
 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 {
@@ -52,6 +55,21 @@
                 .cookieJar(cookieJar)
                 .cache(HTTP_CACHE)
                 .addInterceptor(loggingInterceptor)
+                .addInterceptor(new CommonHeaderInterceptor())
                 .build();
     }
+
+    private static class CommonHeaderInterceptor implements Interceptor {
+        @Override
+        public Response intercept(Interceptor.Chain chain) throws IOException {
+            WikipediaApp app = WikipediaApp.getInstance();
+            Request request = chain.request();
+            request = request.newBuilder()
+                    .header("User-Agent", app.getUserAgent())
+                    .header(app.isEventLoggingEnabled() ? "X-WMF-UUID" : "DNT",
+                            app.isEventLoggingEnabled() ? 
app.getAppInstallID() : "1")
+                    .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 e456aee..210aead 100644
--- a/app/src/main/java/org/wikipedia/WikipediaApp.java
+++ b/app/src/main/java/org/wikipedia/WikipediaApp.java
@@ -65,9 +65,6 @@
 import java.util.Random;
 import java.util.UUID;
 
-import okhttp3.Headers;
-import okhttp3.Request;
-
 import static org.wikipedia.util.DimenUtil.getFontSizeFromSp;
 import static org.wikipedia.util.ReleaseUtil.getChannel;
 import static org.wikipedia.util.StringUtil.emptyIfNull;
@@ -527,18 +524,8 @@
         return PrefsOnboardingStateMachine.getInstance();
     }
 
-    /** For Retrofit requests. Keep in sync with #buildCustomHeadersMap */
-    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()) {
-            moreHeaders.set(key, toSetHeaders.get(key));
-        }
-        return moreHeaders.build();
-    }
-
     /** For java-mwapi API requests. */
+    @Deprecated
     private Map<String, String> buildCustomHeadersMap(String acceptLanguage) {
         Map<String, String> headers = new HashMap<>();
         headers.put("User-Agent", getUserAgent());
diff --git a/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java 
b/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java
index a956ad7..fdadd1b 100644
--- a/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java
+++ b/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java
@@ -25,7 +25,6 @@
             ? EVENTLOG_URL_DEV : EVENTLOG_URL_PROD;
 
     private final JSONObject data;
-    private final String userAgent;
 
     /**
      * Create an EventLoggingEvent that logs to a given revision of a given 
schema with
@@ -34,11 +33,10 @@
      * @param schema Schema name (as specified on meta.wikimedia.org)
      * @param revID Revision of the schema to log to
      * @param wiki DBName (enwiki, dewiki, etc) of the wiki in which we are 
operating
-     * @param userAgent User-Agent string to use for this request
      * @param eventData Data for the actual event payload. Considered to be
      *
      */
-    public EventLoggingEvent(String schema, int revID, String wiki, String 
userAgent, JSONObject eventData) {
+    public EventLoggingEvent(String schema, int revID, String wiki, JSONObject 
eventData) {
         data = new JSONObject();
         try {
             data.put("schema", schema);
@@ -48,7 +46,6 @@
         } catch (JSONException e) {
             throw new RuntimeException(e);
         }
-        this.userAgent = userAgent;
     }
 
     /**
@@ -72,7 +69,7 @@
             String dataURL = Uri.parse(EVENTLOG_URL)
                     .buildUpon().query(data.toString())
                     .build().toString();
-            Request request = new 
Request.Builder().url(dataURL).header("User-Agent", userAgent).build();
+            Request request = new Request.Builder().url(dataURL).build();
             Response response = 
OkHttpConnectionFactory.getClient().newCall(request).execute();
             try {
                 return response.code();
diff --git a/app/src/main/java/org/wikipedia/analytics/Funnel.java 
b/app/src/main/java/org/wikipedia/analytics/Funnel.java
index f8dd5de..00a6114 100644
--- a/app/src/main/java/org/wikipedia/analytics/Funnel.java
+++ b/app/src/main/java/org/wikipedia/analytics/Funnel.java
@@ -143,7 +143,6 @@
                         schemaName,
                         revision,
                         getDBNameForSite(site == null ? getApp().getSite() : 
site),
-                        app.getUserAgent(),
                         preprocessData(eventData)
                 ).log();
             }
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 b6d7887..5a5d98c 100644
--- a/app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java
+++ b/app/src/main/java/org/wikipedia/dataclient/retrofit/RetrofitFactory.java
@@ -23,7 +23,7 @@
     public static Retrofit newInstance(@NonNull String endpoint, @NonNull Site 
site) {
         return new Retrofit.Builder()
                 .client(OkHttpConnectionFactory.getClient().newBuilder()
-                        .addInterceptor(new 
CustomHeaderInterceptor(site)).build())
+                        .addInterceptor(new 
LanguageVariantHeaderInterceptor(site)).build())
                 .baseUrl(endpoint)
                 
.addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
                 .build();
@@ -31,10 +31,10 @@
 
     private RetrofitFactory() { }
 
-    private static class CustomHeaderInterceptor implements Interceptor {
+    private static class LanguageVariantHeaderInterceptor implements 
Interceptor {
         private final Site site;
 
-        CustomHeaderInterceptor(@NonNull Site site) {
+        LanguageVariantHeaderInterceptor(@NonNull Site site) {
             this.site = site;
         }
 
@@ -42,7 +42,7 @@
         public Response intercept(Interceptor.Chain chain) throws IOException {
             Request request = chain.request();
             request = request.newBuilder()
-                    
.headers(WikipediaApp.getInstance().buildCustomHeaders(request, site))
+                    .header("Accept-Language", 
WikipediaApp.getInstance().getAcceptLanguage(site))
                     .build();
             return chain.proceed(request);
         }
diff --git 
a/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java 
b/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java
index d44d8e6..f320ae5 100644
--- a/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java
+++ b/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java
@@ -151,9 +151,7 @@
             return true;
         }
 
-        Request request = new Request.Builder().url(url)
-                                               .header("User-Agent", 
WikipediaApp.getInstance().getUserAgent())
-                                               .build();
+        Request request = new Request.Builder().url(url).build();
         Response response = 
OkHttpConnectionFactory.getClient().newCall(request).execute();
 
         try {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id6b13d9bc1483506346e69170a396fc9fa4f3f43
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Dbrant <dbr...@wikimedia.org>

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

Reply via email to