Niedzielski has uploaded a new change for review. https://gerrit.wikimedia.org/r/270244
Change subject: Hygiene: consolidate RestAdapter factory ...................................................................... Hygiene: consolidate RestAdapter factory No functional changes intended. Change-Id: I06fcca965a8aef09d7d7a646aeffb2cd44edf42e --- A app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java M app/src/main/java/org/wikipedia/server/mwapi/MwPageEndpointsCache.java M app/src/main/java/org/wikipedia/server/restbase/RbEndpointsCache.java 3 files changed, 57 insertions(+), 71 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/44/270244/1 diff --git a/app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java b/app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java new file mode 100644 index 0000000..a67ea55 --- /dev/null +++ b/app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java @@ -0,0 +1,47 @@ +package org.wikipedia.dataclient; + +import android.support.annotation.NonNull; + +import com.google.gson.GsonBuilder; + +import org.wikipedia.OkHttpConnectionFactory; +import org.wikipedia.Site; +import org.wikipedia.WikipediaApp; +import org.wikipedia.server.Protection; +import org.wikipedia.settings.Prefs; + +import retrofit.RequestInterceptor; +import retrofit.RestAdapter; +import retrofit.client.OkClient; +import retrofit.converter.GsonConverter; + +public final class RestAdapterFactory { + public static RestAdapter newInstance(@NonNull Site site) { + return newInstance(site, + WikipediaApp.getInstance().getNetworkProtocol() + "://" + site.getDomain()); + } + + public static RestAdapter newInstance(@NonNull final Site site, @NonNull String endpoint) { + final WikipediaApp app = WikipediaApp.getInstance(); + return new RestAdapter.Builder() + .setLogLevel(Prefs.getRetrofitLogLevel()) + .setClient(new OkClient(OkHttpConnectionFactory.createClient(app))) + .setRequestInterceptor(new RequestInterceptor() { + @Override + public void intercept(RequestFacade request) { + app.injectCustomHeaders(request, site); + } + }) + .setEndpoint(endpoint) + + // following is only needed for the hacky PageLead.Protection deserialization + // remove once https://phabricator.wikimedia.org/T69054 is resolved (see T111131) + .setConverter(new GsonConverter(new GsonBuilder() + .registerTypeAdapter(Protection.class, new Protection.Deserializer()) + .create())) + + .build(); + } + + private RestAdapterFactory() { } +} \ No newline at end of file diff --git a/app/src/main/java/org/wikipedia/server/mwapi/MwPageEndpointsCache.java b/app/src/main/java/org/wikipedia/server/mwapi/MwPageEndpointsCache.java index 59b0ef9..8d88e1c 100644 --- a/app/src/main/java/org/wikipedia/server/mwapi/MwPageEndpointsCache.java +++ b/app/src/main/java/org/wikipedia/server/mwapi/MwPageEndpointsCache.java @@ -1,17 +1,7 @@ package org.wikipedia.server.mwapi; -import org.wikipedia.OkHttpConnectionFactory; import org.wikipedia.Site; -import org.wikipedia.WikipediaApp; -import org.wikipedia.server.Protection; -import org.wikipedia.settings.Prefs; - -import com.google.gson.GsonBuilder; - -import retrofit.RequestInterceptor; -import retrofit.RestAdapter; -import retrofit.client.OkClient; -import retrofit.converter.GsonConverter; +import org.wikipedia.dataclient.RestAdapterFactory; /** * It's good to cache the Retrofit web service since it's a memory intensive object. @@ -34,32 +24,10 @@ return cachedWebService; } - private MwPageService.MwPageEndpoints createMwService(final Site site) { + private MwPageService.MwPageEndpoints createMwService(Site site) { MwPageService.MwPageEndpoints webService; - final String domain = site.getDomain(); - final WikipediaApp app = WikipediaApp.getInstance(); - RestAdapter restAdapter = new RestAdapter.Builder() - .setLogLevel(Prefs.getRetrofitLogLevel()) - - .setClient(new OkClient(OkHttpConnectionFactory.createClient(app))) - - .setRequestInterceptor(new RequestInterceptor() { - @Override - public void intercept(RequestFacade request) { - app.injectCustomHeaders(request, site); - } - }) - - .setEndpoint(WikipediaApp.getInstance().getNetworkProtocol() + "://" + domain) - - // following is only needed for the hacky PageLead.Protection deserialization - // remove once https://phabricator.wikimedia.org/T69054 is resolved - .setConverter(new GsonConverter(new GsonBuilder() - .registerTypeAdapter(Protection.class, new Protection.Deserializer()) - .create())) - - .build(); - webService = restAdapter.create(MwPageService.MwPageEndpoints.class); + webService = RestAdapterFactory.newInstance(site) + .create(MwPageService.MwPageEndpoints.class); return webService; } } diff --git a/app/src/main/java/org/wikipedia/server/restbase/RbEndpointsCache.java b/app/src/main/java/org/wikipedia/server/restbase/RbEndpointsCache.java index 460edbd..0075ad4 100644 --- a/app/src/main/java/org/wikipedia/server/restbase/RbEndpointsCache.java +++ b/app/src/main/java/org/wikipedia/server/restbase/RbEndpointsCache.java @@ -1,17 +1,9 @@ package org.wikipedia.server.restbase; -import org.wikipedia.OkHttpConnectionFactory; import org.wikipedia.Site; import org.wikipedia.WikipediaApp; -import org.wikipedia.server.Protection; +import org.wikipedia.dataclient.RestAdapterFactory; import org.wikipedia.settings.Prefs; - -import com.google.gson.GsonBuilder; - -import retrofit.RequestInterceptor; -import retrofit.RestAdapter; -import retrofit.client.OkClient; -import retrofit.converter.GsonConverter; import java.util.Locale; @@ -36,33 +28,12 @@ return cachedWebService; } - private RbContentService.RbEndpoints createRbService(final Site site) { + private RbContentService.RbEndpoints createRbService(Site site) { RbContentService.RbEndpoints webService; - final String domain = site.getDomain(); - final WikipediaApp app = WikipediaApp.getInstance(); - RestAdapter restAdapter = new RestAdapter.Builder() - .setLogLevel(Prefs.getRetrofitLogLevel()) - - .setClient(new OkClient(OkHttpConnectionFactory.createClient(app))) - - .setRequestInterceptor(new RequestInterceptor() { - @Override - public void intercept(RequestFacade request) { - app.injectCustomHeaders(request, site); - } - }) - - .setEndpoint(String.format(Locale.ROOT, Prefs.getRestbaseUriFormat(), - WikipediaApp.getInstance().getNetworkProtocol(), domain)) - - // following is only needed for the hacky PageLead.Protection deserialization - // remove once our service handles this better (see T111131) - .setConverter(new GsonConverter(new GsonBuilder() - .registerTypeAdapter(Protection.class, new Protection.Deserializer()) - .create())) - - .build(); - webService = restAdapter.create(RbContentService.RbEndpoints.class); + webService = RestAdapterFactory.newInstance(site, + String.format(Locale.ROOT, Prefs.getRestbaseUriFormat(), + WikipediaApp.getInstance().getNetworkProtocol(), site.getDomain())) + .create(RbContentService.RbEndpoints.class); return webService; } } -- To view, visit https://gerrit.wikimedia.org/r/270244 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I06fcca965a8aef09d7d7a646aeffb2cd44edf42e Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits