mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-11-04 00:33:55 +01:00 
			
		
		
		
	Convert the NetworkingModule to kotlin
This commit is contained in:
		
							parent
							
								
									1f545c4755
								
							
						
					
					
						commit
						f5388c4acc
					
				
					 5 changed files with 330 additions and 371 deletions
				
			
		| 
						 | 
					@ -3,7 +3,7 @@ package fr.free.nrw.commons.actions
 | 
				
			||||||
import fr.free.nrw.commons.CommonsApplication
 | 
					import fr.free.nrw.commons.CommonsApplication
 | 
				
			||||||
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient
 | 
					import fr.free.nrw.commons.auth.csrf.CsrfTokenClient
 | 
				
			||||||
import fr.free.nrw.commons.auth.csrf.InvalidLoginTokenException
 | 
					import fr.free.nrw.commons.auth.csrf.InvalidLoginTokenException
 | 
				
			||||||
import fr.free.nrw.commons.di.NetworkingModule.NAMED_COMMONS_CSRF
 | 
					import fr.free.nrw.commons.di.NetworkingModule.Companion.NAMED_COMMONS_CSRF
 | 
				
			||||||
import io.reactivex.Observable
 | 
					import io.reactivex.Observable
 | 
				
			||||||
import javax.inject.Inject
 | 
					import javax.inject.Inject
 | 
				
			||||||
import javax.inject.Named
 | 
					import javax.inject.Named
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,350 +0,0 @@
 | 
				
			||||||
package fr.free.nrw.commons.di;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import android.content.Context;
 | 
					 | 
				
			||||||
import androidx.annotation.NonNull;
 | 
					 | 
				
			||||||
import com.google.gson.Gson;
 | 
					 | 
				
			||||||
import dagger.Module;
 | 
					 | 
				
			||||||
import dagger.Provides;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.BetaConstants;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.BuildConfig;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.OkHttpConnectionFactory;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.actions.PageEditClient;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.actions.PageEditInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.actions.ThanksInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.auth.SessionManager;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.auth.csrf.CsrfTokenInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.auth.csrf.LogoutClient;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.auth.login.LoginClient;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.auth.login.LoginInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.category.CategoryInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.explore.depictions.DepictsClient;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.media.MediaDetailInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.media.MediaInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.media.PageMediaInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.media.WikidataMediaInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.mwapi.UserInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.notification.NotificationInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.review.ReviewInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.upload.UploadInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.upload.WikiBaseInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.upload.depicts.DepictsInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.wikidata.CommonsServiceFactory;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.wikidata.WikidataInterface;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.wikidata.cookies.CommonsCookieJar;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.wikidata.cookies.CommonsCookieStorage;
 | 
					 | 
				
			||||||
import java.io.File;
 | 
					 | 
				
			||||||
import java.util.Locale;
 | 
					 | 
				
			||||||
import java.util.concurrent.TimeUnit;
 | 
					 | 
				
			||||||
import javax.inject.Named;
 | 
					 | 
				
			||||||
import javax.inject.Singleton;
 | 
					 | 
				
			||||||
import okhttp3.Cache;
 | 
					 | 
				
			||||||
import okhttp3.HttpUrl;
 | 
					 | 
				
			||||||
import okhttp3.OkHttpClient;
 | 
					 | 
				
			||||||
import okhttp3.logging.HttpLoggingInterceptor;
 | 
					 | 
				
			||||||
import okhttp3.logging.HttpLoggingInterceptor.Level;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.wikidata.model.WikiSite;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.wikidata.GsonUtil;
 | 
					 | 
				
			||||||
import timber.log.Timber;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@Module
 | 
					 | 
				
			||||||
@SuppressWarnings({"WeakerAccess", "unused"})
 | 
					 | 
				
			||||||
public class NetworkingModule {
 | 
					 | 
				
			||||||
    private static final String WIKIDATA_SPARQL_QUERY_URL = "https://query.wikidata.org/sparql";
 | 
					 | 
				
			||||||
    private static final String TOOLS_FORGE_URL = "https://tools.wmflabs.org/commons-android-app/tool-commons-android-app";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static final long OK_HTTP_CACHE_SIZE = 10 * 1024 * 1024;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private static final String NAMED_WIKI_DATA_WIKI_SITE = "wikidata-wikisite";
 | 
					 | 
				
			||||||
    private static final String NAMED_WIKI_PEDIA_WIKI_SITE = "wikipedia-wikisite";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static final String NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE = "language-wikipedia-wikisite";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static final String NAMED_COMMONS_CSRF = "commons-csrf";
 | 
					 | 
				
			||||||
    public static final String NAMED_WIKI_CSRF = "wiki-csrf";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public OkHttpClient provideOkHttpClient(Context context,
 | 
					 | 
				
			||||||
                                            HttpLoggingInterceptor httpLoggingInterceptor) {
 | 
					 | 
				
			||||||
        File dir = new File(context.getCacheDir(), "okHttpCache");
 | 
					 | 
				
			||||||
        return new OkHttpClient.Builder()
 | 
					 | 
				
			||||||
            .connectTimeout(120, TimeUnit.SECONDS)
 | 
					 | 
				
			||||||
            .writeTimeout(120, TimeUnit.SECONDS)
 | 
					 | 
				
			||||||
                .addInterceptor(httpLoggingInterceptor)
 | 
					 | 
				
			||||||
            .readTimeout(120, TimeUnit.SECONDS)
 | 
					 | 
				
			||||||
            .cache(new Cache(dir, OK_HTTP_CACHE_SIZE))
 | 
					 | 
				
			||||||
            .build();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CommonsServiceFactory serviceFactory(CommonsCookieJar cookieJar) {
 | 
					 | 
				
			||||||
        return new CommonsServiceFactory(OkHttpConnectionFactory.getClient(cookieJar));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public HttpLoggingInterceptor provideHttpLoggingInterceptor() {
 | 
					 | 
				
			||||||
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(message -> {
 | 
					 | 
				
			||||||
            Timber.tag("OkHttp").v(message);
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
        httpLoggingInterceptor.setLevel(BuildConfig.DEBUG ? Level.BODY: Level.BASIC);
 | 
					 | 
				
			||||||
        return httpLoggingInterceptor;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public OkHttpJsonApiClient provideOkHttpJsonApiClient(OkHttpClient okHttpClient,
 | 
					 | 
				
			||||||
                                                          DepictsClient depictsClient,
 | 
					 | 
				
			||||||
                                                          @Named("tools_forge") HttpUrl toolsForgeUrl,
 | 
					 | 
				
			||||||
                                                          @Named("default_preferences") JsonKvStore defaultKvStore,
 | 
					 | 
				
			||||||
                                                          Gson gson) {
 | 
					 | 
				
			||||||
        return new OkHttpJsonApiClient(okHttpClient,
 | 
					 | 
				
			||||||
                depictsClient,
 | 
					 | 
				
			||||||
                toolsForgeUrl,
 | 
					 | 
				
			||||||
                WIKIDATA_SPARQL_QUERY_URL,
 | 
					 | 
				
			||||||
                BuildConfig.WIKIMEDIA_CAMPAIGNS_URL,
 | 
					 | 
				
			||||||
            gson);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CommonsCookieStorage provideCookieStorage(
 | 
					 | 
				
			||||||
        @Named("default_preferences") JsonKvStore preferences) {
 | 
					 | 
				
			||||||
        CommonsCookieStorage cookieStorage = new CommonsCookieStorage(preferences);
 | 
					 | 
				
			||||||
        cookieStorage.load();
 | 
					 | 
				
			||||||
        return cookieStorage;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CommonsCookieJar provideCookieJar(CommonsCookieStorage storage) {
 | 
					 | 
				
			||||||
        return new CommonsCookieJar(storage);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Named(NAMED_COMMONS_CSRF)
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CsrfTokenClient provideCommonsCsrfTokenClient(SessionManager sessionManager,
 | 
					 | 
				
			||||||
        @Named("commons-csrf-interface") CsrfTokenInterface tokenInterface, LoginClient loginClient, LogoutClient logoutClient) {
 | 
					 | 
				
			||||||
        return new CsrfTokenClient(sessionManager, tokenInterface, loginClient, logoutClient);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Provides a singleton instance of CsrfTokenClient for Wikidata.
 | 
					 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param sessionManager The session manager to manage user sessions.
 | 
					 | 
				
			||||||
     * @param tokenInterface The interface for obtaining CSRF tokens.
 | 
					 | 
				
			||||||
     * @param loginClient    The client for handling login operations.
 | 
					 | 
				
			||||||
     * @param logoutClient   The client for handling logout operations.
 | 
					 | 
				
			||||||
     * @return A singleton instance of CsrfTokenClient.
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @Named(NAMED_WIKI_CSRF)
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CsrfTokenClient provideWikiCsrfTokenClient(SessionManager sessionManager,
 | 
					 | 
				
			||||||
        @Named("wikidata-csrf-interface") CsrfTokenInterface tokenInterface, LoginClient loginClient, LogoutClient logoutClient) {
 | 
					 | 
				
			||||||
        return new CsrfTokenClient(sessionManager, tokenInterface, loginClient, logoutClient);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Provides a singleton instance of CsrfTokenInterface for Wikidata.
 | 
					 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param serviceFactory The factory used to create service interfaces.
 | 
					 | 
				
			||||||
     * @return A singleton instance of CsrfTokenInterface for Wikidata.
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @Named("wikidata-csrf-interface")
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CsrfTokenInterface provideWikidataCsrfTokenInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.WIKIDATA_URL, CsrfTokenInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Named("commons-csrf-interface")
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CsrfTokenInterface provideCsrfTokenInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, CsrfTokenInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public LoginInterface provideLoginInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, LoginInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public LoginClient provideLoginClient(LoginInterface loginInterface) {
 | 
					 | 
				
			||||||
        return new LoginClient(loginInterface);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Named("wikimedia_api_host")
 | 
					 | 
				
			||||||
    @NonNull
 | 
					 | 
				
			||||||
    @SuppressWarnings("ConstantConditions")
 | 
					 | 
				
			||||||
    public String provideMwApiUrl() {
 | 
					 | 
				
			||||||
        return BuildConfig.WIKIMEDIA_API_HOST;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Named("tools_forge")
 | 
					 | 
				
			||||||
    @NonNull
 | 
					 | 
				
			||||||
    @SuppressWarnings("ConstantConditions")
 | 
					 | 
				
			||||||
    public HttpUrl provideToolsForgeUrl() {
 | 
					 | 
				
			||||||
        return HttpUrl.parse(TOOLS_FORGE_URL);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    @Named(NAMED_WIKI_DATA_WIKI_SITE)
 | 
					 | 
				
			||||||
    public WikiSite provideWikidataWikiSite() {
 | 
					 | 
				
			||||||
        return new WikiSite(BuildConfig.WIKIDATA_URL);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Gson objects are very heavy. The app should ideally be using just one instance of it instead of creating new instances everywhere.
 | 
					 | 
				
			||||||
     * @return returns a singleton Gson instance
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public Gson provideGson() {
 | 
					 | 
				
			||||||
        return GsonUtil.getDefaultGson();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public ReviewInterface provideReviewInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, ReviewInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public DepictsInterface provideDepictsInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.WIKIDATA_URL, DepictsInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public WikiBaseInterface provideWikiBaseInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, WikiBaseInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public UploadInterface provideUploadInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, UploadInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Named("commons-page-edit-service")
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public PageEditInterface providePageEditService(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, PageEditInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Named("wikidata-page-edit-service")
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public PageEditInterface provideWikiDataPageEditService(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.WIKIDATA_URL, PageEditInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Named("commons-page-edit")
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public PageEditClient provideCommonsPageEditClient(@Named(NAMED_COMMONS_CSRF) CsrfTokenClient csrfTokenClient,
 | 
					 | 
				
			||||||
                                                       @Named("commons-page-edit-service") PageEditInterface pageEditInterface) {
 | 
					 | 
				
			||||||
        return new PageEditClient(csrfTokenClient, pageEditInterface);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Provides a singleton instance of PageEditClient for Wikidata.
 | 
					 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param csrfTokenClient    The client used to manage CSRF tokens.
 | 
					 | 
				
			||||||
     * @param pageEditInterface  The interface for page edit operations.
 | 
					 | 
				
			||||||
     * @return A singleton instance of PageEditClient for Wikidata.
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @Named("wikidata-page-edit")
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public PageEditClient provideWikidataPageEditClient(@Named(NAMED_WIKI_CSRF) CsrfTokenClient csrfTokenClient,
 | 
					 | 
				
			||||||
        @Named("wikidata-page-edit-service") PageEditInterface pageEditInterface) {
 | 
					 | 
				
			||||||
        return new PageEditClient(csrfTokenClient, pageEditInterface);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public MediaInterface provideMediaInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, MediaInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Add provider for WikidataMediaInterface
 | 
					 | 
				
			||||||
     * It creates a retrofit service for the commons wiki site
 | 
					 | 
				
			||||||
     * @param commonsWikiSite commonsWikiSite
 | 
					 | 
				
			||||||
     * @return WikidataMediaInterface
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public WikidataMediaInterface provideWikidataMediaInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BetaConstants.COMMONS_URL, WikidataMediaInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public MediaDetailInterface providesMediaDetailInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, MediaDetailInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public CategoryInterface provideCategoryInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, CategoryInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public ThanksInterface provideThanksInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, ThanksInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public NotificationInterface provideNotificationInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, NotificationInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public UserInterface provideUserInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.COMMONS_URL, UserInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public WikidataInterface provideWikidataInterface(CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(BuildConfig.WIKIDATA_URL, WikidataInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Add provider for PageMediaInterface
 | 
					 | 
				
			||||||
     * It creates a retrofit service for the wiki site using device's current language
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    public PageMediaInterface providePageMediaInterface(@Named(NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE) WikiSite wikiSite, CommonsServiceFactory serviceFactory) {
 | 
					 | 
				
			||||||
        return serviceFactory.create(wikiSite.url(), PageMediaInterface.class);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Provides
 | 
					 | 
				
			||||||
    @Singleton
 | 
					 | 
				
			||||||
    @Named(NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE)
 | 
					 | 
				
			||||||
    public WikiSite provideLanguageWikipediaSite() {
 | 
					 | 
				
			||||||
        return WikiSite.forLanguageCode(Locale.getDefault().getLanguage());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										316
									
								
								app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.kt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										316
									
								
								app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.kt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,316 @@
 | 
				
			||||||
 | 
					package fr.free.nrw.commons.di
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import android.content.Context
 | 
				
			||||||
 | 
					import com.google.gson.Gson
 | 
				
			||||||
 | 
					import dagger.Module
 | 
				
			||||||
 | 
					import dagger.Provides
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.BetaConstants
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.BuildConfig
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.OkHttpConnectionFactory
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.actions.PageEditClient
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.actions.PageEditInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.actions.ThanksInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.auth.SessionManager
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.auth.csrf.CsrfTokenClient
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.auth.csrf.CsrfTokenInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.auth.csrf.LogoutClient
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.auth.login.LoginClient
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.auth.login.LoginInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.category.CategoryInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.explore.depictions.DepictsClient
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.kvstore.JsonKvStore
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.media.MediaDetailInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.media.MediaInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.media.PageMediaInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.media.WikidataMediaInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.mwapi.UserInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.notification.NotificationInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.review.ReviewInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.upload.UploadInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.upload.WikiBaseInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.upload.depicts.DepictsInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.CommonsServiceFactory
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.GsonUtil
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.WikidataInterface
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.cookies.CommonsCookieJar
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.cookies.CommonsCookieStorage
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.model.WikiSite
 | 
				
			||||||
 | 
					import okhttp3.Cache
 | 
				
			||||||
 | 
					import okhttp3.HttpUrl
 | 
				
			||||||
 | 
					import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
 | 
				
			||||||
 | 
					import okhttp3.OkHttpClient
 | 
				
			||||||
 | 
					import okhttp3.logging.HttpLoggingInterceptor
 | 
				
			||||||
 | 
					import okhttp3.logging.HttpLoggingInterceptor.Level
 | 
				
			||||||
 | 
					import timber.log.Timber
 | 
				
			||||||
 | 
					import java.io.File
 | 
				
			||||||
 | 
					import java.util.Locale
 | 
				
			||||||
 | 
					import java.util.concurrent.TimeUnit
 | 
				
			||||||
 | 
					import javax.inject.Named
 | 
				
			||||||
 | 
					import javax.inject.Singleton
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Module
 | 
				
			||||||
 | 
					@Suppress("unused")
 | 
				
			||||||
 | 
					class NetworkingModule {
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideOkHttpClient(
 | 
				
			||||||
 | 
					        context: Context,
 | 
				
			||||||
 | 
					        httpLoggingInterceptor: HttpLoggingInterceptor
 | 
				
			||||||
 | 
					    ): OkHttpClient = OkHttpClient.Builder()
 | 
				
			||||||
 | 
					        .connectTimeout(120, TimeUnit.SECONDS)
 | 
				
			||||||
 | 
					        .writeTimeout(120, TimeUnit.SECONDS)
 | 
				
			||||||
 | 
					        .addInterceptor(httpLoggingInterceptor)
 | 
				
			||||||
 | 
					        .readTimeout(120, TimeUnit.SECONDS)
 | 
				
			||||||
 | 
					        .cache(Cache(File(context.cacheDir, "okHttpCache"), OK_HTTP_CACHE_SIZE))
 | 
				
			||||||
 | 
					        .build()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun serviceFactory(cookieJar: CommonsCookieJar): CommonsServiceFactory =
 | 
				
			||||||
 | 
					        CommonsServiceFactory(OkHttpConnectionFactory.getClient(cookieJar))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor =
 | 
				
			||||||
 | 
					        HttpLoggingInterceptor { message: String? ->
 | 
				
			||||||
 | 
					            Timber.tag("OkHttp").v(message)
 | 
				
			||||||
 | 
					        }.apply {
 | 
				
			||||||
 | 
					            level = if (BuildConfig.DEBUG) Level.BODY else Level.BASIC
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideOkHttpJsonApiClient(
 | 
				
			||||||
 | 
					        okHttpClient: OkHttpClient,
 | 
				
			||||||
 | 
					        depictsClient: DepictsClient,
 | 
				
			||||||
 | 
					        @Named("tools_forge") toolsForgeUrl: HttpUrl,
 | 
				
			||||||
 | 
					        gson: Gson
 | 
				
			||||||
 | 
					    ): OkHttpJsonApiClient = OkHttpJsonApiClient(
 | 
				
			||||||
 | 
					        okHttpClient, depictsClient, toolsForgeUrl, WIKIDATA_SPARQL_QUERY_URL,
 | 
				
			||||||
 | 
					        BuildConfig.WIKIMEDIA_CAMPAIGNS_URL, gson
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideCookieStorage(
 | 
				
			||||||
 | 
					        @Named("default_preferences") preferences: JsonKvStore
 | 
				
			||||||
 | 
					    ): CommonsCookieStorage = CommonsCookieStorage(preferences).also {
 | 
				
			||||||
 | 
					        it.load()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideCookieJar(storage: CommonsCookieStorage): CommonsCookieJar =
 | 
				
			||||||
 | 
					        CommonsCookieJar(storage)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Named(NAMED_COMMONS_CSRF)
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideCommonsCsrfTokenClient(
 | 
				
			||||||
 | 
					        sessionManager: SessionManager,
 | 
				
			||||||
 | 
					        @Named("commons-csrf-interface") tokenInterface: CsrfTokenInterface,
 | 
				
			||||||
 | 
					        loginClient: LoginClient,
 | 
				
			||||||
 | 
					        logoutClient: LogoutClient
 | 
				
			||||||
 | 
					    ): CsrfTokenClient = CsrfTokenClient(sessionManager, tokenInterface, loginClient, logoutClient)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Provides a singleton instance of CsrfTokenClient for Wikidata.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param sessionManager The session manager to manage user sessions.
 | 
				
			||||||
 | 
					     * @param tokenInterface The interface for obtaining CSRF tokens.
 | 
				
			||||||
 | 
					     * @param loginClient    The client for handling login operations.
 | 
				
			||||||
 | 
					     * @param logoutClient   The client for handling logout operations.
 | 
				
			||||||
 | 
					     * @return A singleton instance of CsrfTokenClient.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Named(NAMED_WIKI_CSRF)
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideWikiCsrfTokenClient(
 | 
				
			||||||
 | 
					        sessionManager: SessionManager,
 | 
				
			||||||
 | 
					        @Named("wikidata-csrf-interface") tokenInterface: CsrfTokenInterface,
 | 
				
			||||||
 | 
					        loginClient: LoginClient,
 | 
				
			||||||
 | 
					        logoutClient: LogoutClient
 | 
				
			||||||
 | 
					    ): CsrfTokenClient = CsrfTokenClient(sessionManager, tokenInterface, loginClient, logoutClient)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Provides a singleton instance of CsrfTokenInterface for Wikidata.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param factory The factory used to create service interfaces.
 | 
				
			||||||
 | 
					     * @return A singleton instance of CsrfTokenInterface for Wikidata.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Named("wikidata-csrf-interface")
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideWikidataCsrfTokenInterface(factory: CommonsServiceFactory): CsrfTokenInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.WIKIDATA_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Named("commons-csrf-interface")
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideCsrfTokenInterface(factory: CommonsServiceFactory): CsrfTokenInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideLoginInterface(factory: CommonsServiceFactory): LoginInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideLoginClient(loginInterface: LoginInterface): LoginClient =
 | 
				
			||||||
 | 
					        LoginClient(loginInterface)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Named("tools_forge")
 | 
				
			||||||
 | 
					    fun provideToolsForgeUrl(): HttpUrl = TOOLS_FORGE_URL.toHttpUrlOrNull()!!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    @Named(NAMED_WIKI_DATA_WIKI_SITE)
 | 
				
			||||||
 | 
					    fun provideWikidataWikiSite(): WikiSite = WikiSite(BuildConfig.WIKIDATA_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Gson objects are very heavy. The app should ideally be using just one instance of it instead of creating new instances everywhere.
 | 
				
			||||||
 | 
					     * @return returns a singleton Gson instance
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideGson(): Gson = GsonUtil.getDefaultGson()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideReviewInterface(factory: CommonsServiceFactory): ReviewInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideDepictsInterface(factory: CommonsServiceFactory): DepictsInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.WIKIDATA_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideWikiBaseInterface(factory: CommonsServiceFactory): WikiBaseInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideUploadInterface(factory: CommonsServiceFactory): UploadInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Named("commons-page-edit-service")
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun providePageEditService(factory: CommonsServiceFactory): PageEditInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Named("wikidata-page-edit-service")
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideWikiDataPageEditService(factory: CommonsServiceFactory): PageEditInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.WIKIDATA_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Named("commons-page-edit")
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideCommonsPageEditClient(
 | 
				
			||||||
 | 
					        @Named(NAMED_COMMONS_CSRF) csrfTokenClient: CsrfTokenClient,
 | 
				
			||||||
 | 
					        @Named("commons-page-edit-service") pageEditInterface: PageEditInterface
 | 
				
			||||||
 | 
					    ): PageEditClient = PageEditClient(csrfTokenClient, pageEditInterface)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Provides a singleton instance of PageEditClient for Wikidata.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param csrfTokenClient    The client used to manage CSRF tokens.
 | 
				
			||||||
 | 
					     * @param pageEditInterface  The interface for page edit operations.
 | 
				
			||||||
 | 
					     * @return A singleton instance of PageEditClient for Wikidata.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Named("wikidata-page-edit")
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideWikidataPageEditClient(
 | 
				
			||||||
 | 
					        @Named(NAMED_WIKI_CSRF) csrfTokenClient: CsrfTokenClient,
 | 
				
			||||||
 | 
					        @Named("wikidata-page-edit-service") pageEditInterface: PageEditInterface
 | 
				
			||||||
 | 
					    ): PageEditClient = PageEditClient(csrfTokenClient, pageEditInterface)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideMediaInterface(factory: CommonsServiceFactory): MediaInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Add provider for WikidataMediaInterface
 | 
				
			||||||
 | 
					     * It creates a retrofit service for the commons wiki site
 | 
				
			||||||
 | 
					     * @param commonsWikiSite commonsWikiSite
 | 
				
			||||||
 | 
					     * @return WikidataMediaInterface
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideWikidataMediaInterface(factory: CommonsServiceFactory): WikidataMediaInterface =
 | 
				
			||||||
 | 
					        factory.create(BetaConstants.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun providesMediaDetailInterface(factory: CommonsServiceFactory): MediaDetailInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideCategoryInterface(factory: CommonsServiceFactory): CategoryInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideThanksInterface(factory: CommonsServiceFactory): ThanksInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideNotificationInterface(factory: CommonsServiceFactory): NotificationInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideUserInterface(factory: CommonsServiceFactory): UserInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.COMMONS_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun provideWikidataInterface(factory: CommonsServiceFactory): WikidataInterface =
 | 
				
			||||||
 | 
					        factory.create(BuildConfig.WIKIDATA_URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Add provider for PageMediaInterface
 | 
				
			||||||
 | 
					     * It creates a retrofit service for the wiki site using device's current language
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    fun providePageMediaInterface(
 | 
				
			||||||
 | 
					        @Named(NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE) wikiSite: WikiSite,
 | 
				
			||||||
 | 
					        factory: CommonsServiceFactory
 | 
				
			||||||
 | 
					    ): PageMediaInterface = factory.create(wikiSite.url())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
 | 
					    @Named(NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE)
 | 
				
			||||||
 | 
					    fun provideLanguageWikipediaSite(): WikiSite {
 | 
				
			||||||
 | 
					        return WikiSite.forLanguageCode(Locale.getDefault().language)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    companion object {
 | 
				
			||||||
 | 
					        private const val WIKIDATA_SPARQL_QUERY_URL = "https://query.wikidata.org/sparql"
 | 
				
			||||||
 | 
					        private const val TOOLS_FORGE_URL =
 | 
				
			||||||
 | 
					            "https://tools.wmflabs.org/commons-android-app/tool-commons-android-app"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const val OK_HTTP_CACHE_SIZE: Long = (10 * 1024 * 1024).toLong()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private const val NAMED_WIKI_DATA_WIKI_SITE = "wikidata-wikisite"
 | 
				
			||||||
 | 
					        private const val NAMED_WIKI_PEDIA_WIKI_SITE = "wikipedia-wikisite"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const val NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE: String = "language-wikipedia-wikisite"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const val NAMED_COMMONS_CSRF: String = "commons-csrf"
 | 
				
			||||||
 | 
					        const val NAMED_WIKI_CSRF: String = "wiki-csrf"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -3,7 +3,10 @@ package fr.free.nrw.commons.mwapi;
 | 
				
			||||||
import static fr.free.nrw.commons.category.CategoryClientKt.CATEGORY_PREFIX;
 | 
					import static fr.free.nrw.commons.category.CategoryClientKt.CATEGORY_PREFIX;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.google.gson.Gson;
 | 
					import com.google.gson.Gson;
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.BuildConfig;
 | 
				
			||||||
import fr.free.nrw.commons.category.CategoryItem;
 | 
					import fr.free.nrw.commons.category.CategoryItem;
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.mwapi.MwQueryPage;
 | 
				
			||||||
 | 
					import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse;
 | 
				
			||||||
import io.reactivex.Single;
 | 
					import io.reactivex.Single;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.Collections;
 | 
					import java.util.Collections;
 | 
				
			||||||
| 
						 | 
					@ -11,14 +14,11 @@ import java.util.LinkedHashSet;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Set;
 | 
					import java.util.Set;
 | 
				
			||||||
import javax.inject.Inject;
 | 
					import javax.inject.Inject;
 | 
				
			||||||
import javax.inject.Named;
 | 
					 | 
				
			||||||
import okhttp3.HttpUrl;
 | 
					import okhttp3.HttpUrl;
 | 
				
			||||||
import okhttp3.OkHttpClient;
 | 
					import okhttp3.OkHttpClient;
 | 
				
			||||||
import okhttp3.Request;
 | 
					import okhttp3.Request;
 | 
				
			||||||
import okhttp3.Response;
 | 
					import okhttp3.Response;
 | 
				
			||||||
import okhttp3.ResponseBody;
 | 
					import okhttp3.ResponseBody;
 | 
				
			||||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryPage;
 | 
					 | 
				
			||||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse;
 | 
					 | 
				
			||||||
import timber.log.Timber;
 | 
					import timber.log.Timber;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					@ -30,14 +30,11 @@ import timber.log.Timber;
 | 
				
			||||||
public class CategoryApi {
 | 
					public class CategoryApi {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final OkHttpClient okHttpClient;
 | 
					    private final OkHttpClient okHttpClient;
 | 
				
			||||||
    private final String commonsBaseUrl;
 | 
					 | 
				
			||||||
    private final Gson gson;
 | 
					    private final Gson gson;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Inject
 | 
					    @Inject
 | 
				
			||||||
    public CategoryApi(OkHttpClient okHttpClient, Gson gson,
 | 
					    public CategoryApi(final OkHttpClient okHttpClient, final Gson gson) {
 | 
				
			||||||
                       @Named("wikimedia_api_host") String commonsBaseUrl) {
 | 
					 | 
				
			||||||
        this.okHttpClient = okHttpClient;
 | 
					        this.okHttpClient = okHttpClient;
 | 
				
			||||||
        this.commonsBaseUrl = commonsBaseUrl;
 | 
					 | 
				
			||||||
        this.gson = gson;
 | 
					        this.gson = gson;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -75,9 +72,9 @@ public class CategoryApi {
 | 
				
			||||||
     * @param coords Coordinates to build query with
 | 
					     * @param coords Coordinates to build query with
 | 
				
			||||||
     * @return URL for API query
 | 
					     * @return URL for API query
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private HttpUrl buildUrl(String coords) {
 | 
					    private HttpUrl buildUrl(final String coords) {
 | 
				
			||||||
        return HttpUrl
 | 
					        return HttpUrl
 | 
				
			||||||
                .parse(commonsBaseUrl)
 | 
					                .parse(BuildConfig.WIKIMEDIA_API_HOST)
 | 
				
			||||||
                .newBuilder()
 | 
					                .newBuilder()
 | 
				
			||||||
                .addQueryParameter("action", "query")
 | 
					                .addQueryParameter("action", "query")
 | 
				
			||||||
                .addQueryParameter("prop", "categories|coordinates|pageprops")
 | 
					                .addQueryParameter("prop", "categories|coordinates|pageprops")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,7 +8,7 @@ import retrofit2.converter.gson.GsonConverterFactory
 | 
				
			||||||
class CommonsServiceFactory(
 | 
					class CommonsServiceFactory(
 | 
				
			||||||
    private val okHttpClient: OkHttpClient,
 | 
					    private val okHttpClient: OkHttpClient,
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
    private val builder: Retrofit.Builder by lazy {
 | 
					    val builder: Retrofit.Builder by lazy {
 | 
				
			||||||
        // All instances of retrofit share this configuration, but create it lazily
 | 
					        // All instances of retrofit share this configuration, but create it lazily
 | 
				
			||||||
        Retrofit
 | 
					        Retrofit
 | 
				
			||||||
            .Builder()
 | 
					            .Builder()
 | 
				
			||||||
| 
						 | 
					@ -17,15 +17,11 @@ class CommonsServiceFactory(
 | 
				
			||||||
            .addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
 | 
					            .addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private val retrofitCache: MutableMap<String, Retrofit> = mutableMapOf()
 | 
					    val retrofitCache: MutableMap<String, Retrofit> = mutableMapOf()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun <T : Any> create(
 | 
					    inline fun <reified T: Any> create(baseUrl: String): T =
 | 
				
			||||||
        baseUrl: String,
 | 
					        retrofitCache.getOrPut(baseUrl) {
 | 
				
			||||||
        service: Class<T>,
 | 
					 | 
				
			||||||
    ): T =
 | 
					 | 
				
			||||||
        retrofitCache
 | 
					 | 
				
			||||||
            .getOrPut(baseUrl) {
 | 
					 | 
				
			||||||
            // Cache instances of retrofit based on API backend
 | 
					            // Cache instances of retrofit based on API backend
 | 
				
			||||||
            builder.baseUrl(baseUrl).build()
 | 
					            builder.baseUrl(baseUrl).build()
 | 
				
			||||||
            }.create(service)
 | 
					        }.create(T::class.java)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue