<commit_before>/*
 * Copyright 2014-2016 Appvelopers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ca.appvelopers.mcgillmobile;

import android.content.Context;
import android.net.ConnectivityManager;

import com.squareup.moshi.Moshi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Named;
import javax.inject.Singleton;

import ca.appvelopers.mcgillmobile.model.retrofit.ConfigService;
import ca.appvelopers.mcgillmobile.model.retrofit.McGillService;
import ca.appvelopers.mcgillmobile.util.Passwords;
import dagger.Module;
import dagger.Provides;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.Credentials;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.moshi.MoshiConverterFactory;
import timber.log.Timber;

/**
 * Dagger module for all network injections
 * @author <NAME>
 * @since 2.0.4
 */
@Module(includes = AppModule.class)
public class NetworkModule {
    /**
     * Injection Names
     */
    public static final String CONFIG = "config";
    public static final String MCGILL = "mcgill";

    /**
     * @param context App context
     * @return The {@link ConnectivityManager} instance
     */
    @Provides
    protected ConnectivityManager provideConnectivityManager(Context context) {
        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    /* OKHTTP */

    /**
     * @return The {@link HttpLoggingInterceptor} instance for HTTP logging
     */
    @Provides
    @Singleton
    protected HttpLoggingInterceptor provideLoggingInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(
                new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Timber.tag("OkHttp").i(message);
                    }
                });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        return interceptor;
    }

    /**
     * @param interceptor The {@link HttpLoggingInterceptor} instance
     * @return The {@link OkHttpClient} instance
     */
    @Provides
    @Singleton
    @Named(MCGILL)
    public OkHttpClient provideMcGillOkHttpClient(HttpLoggingInterceptor interceptor) {
        return new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();

                    @Override
                    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                        //Save the cookies per URL host
                        cookieStore.put(url.host(), cookies);
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl url) {
                        //Use the cookies for the given URL host
                        List<Cookie> cookies = cookieStore.get(url.host());
                        return cookies == null ? new ArrayList<Cookie>() : cookies;
                    }
                })
                .addInterceptor(interceptor)
                .build();
    }

    /**
     * @param interceptor The {@link HttpLoggingInterceptor} instance
     * @return The {@link OkHttpClient} instance for the config server
     */
    @Provides
    @Singleton
    @Named(CONFIG)
    public OkHttpClient provideConfigOkHttpClient(HttpLoggingInterceptor interceptor) {
        return new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        //Add the authentication to the header
                        Request newRequest = chain.request()
                                .newBuilder()
                                .addHeader("Authorization", Credentials.basic(
                                        Passwords.CONFIG_USERNAME, Passwords.CONFIG_PASSWORD))
                                .build();

                        return chain.proceed(newRequest);
                    }
                })
                .build();
    }

    /* RETROFIT */

    /**
     * @param client {@link OkHttpClient} instance to use for the config server
     * @param moshi  {@link Moshi} instance for the converting
     * @return The {@link Retrofit} instance to use for the config server
     */
    @Provides
    @Singleton
    @Named(CONFIG)
    public Retrofit provideConfigRetrofit(@Named(CONFIG) OkHttpClient client, Moshi moshi) {
        return new Retrofit.Builder()
                .client(client)
                .baseUrl("http://mymartlet.herokuapp.com/api/v2/")
                .addConverterFactory(MoshiConverterFactory.create(moshi))<commit_msg>Removed basic auth from the config server.<commit_after>/*
 * Copyright 2014-2016 Appvelopers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ca.appvelopers.mcgillmobile;

import android.content.Context;
import android.net.ConnectivityManager;

import com.squareup.moshi.Moshi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Named;
import javax.inject.Singleton;

import ca.appvelopers.mcgillmobile.model.retrofit.ConfigService;
import ca.appvelopers.mcgillmobile.model.retrofit.McGillService;
import dagger.Module;
import dagger.Provides;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.moshi.MoshiConverterFactory;
import timber.log.Timber;

/**
 * Dagger module for all network injections
 * @author <NAME>
 * @since 2.0.4
 */
@Module(includes = AppModule.class)
public class NetworkModule {
    /**
     * Injection Names
     */
    public static final String CONFIG = "config";
    public static final String MCGILL = "mcgill";

    /**
     * @param context App context
     * @return The {@link ConnectivityManager} instance
     */
    @Provides
    protected ConnectivityManager provideConnectivityManager(Context context) {
        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    /* OKHTTP */

    /**
     * @return The {@link HttpLoggingInterceptor} instance for HTTP logging
     */
    @Provides
    @Singleton
    protected HttpLoggingInterceptor provideLoggingInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(
                new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Timber.tag("OkHttp").i(message);
                    }
                });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        return interceptor;
    }

    /**
     * @param interceptor The {@link HttpLoggingInterceptor} instance
     * @return The {@link OkHttpClient} instance
     */
    @Provides
    @Singleton
    @Named(MCGILL)
    public OkHttpClient provideMcGillOkHttpClient(HttpLoggingInterceptor interceptor) {
        return new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();

                    @Override
                    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                        //Save the cookies per URL host
                        cookieStore.put(url.host(), cookies);
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl url) {
                        //Use the cookies for the given URL host
                        List<Cookie> cookies = cookieStore.get(url.host());
                        return cookies == null ? new ArrayList<Cookie>() : cookies;
                    }
                })
                .addInterceptor(interceptor)
                .build();
    }

    /**
     * @param interceptor The {@link HttpLoggingInterceptor} instance
     * @return The {@link OkHttpClient} instance for the config server
     */
    @Provides
    @Singleton
    @Named(CONFIG)
    public OkHttpClient provideConfigOkHttpClient(HttpLoggingInterceptor interceptor) {
        return new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build();
    }

    /* RETROFIT */

    /**
     * @param client {@link OkHttpClient} instance to use for the config server
     * @param moshi  {@link Moshi} instance for the converting
     * @return The {@link Retrofit} instance to use for the config server
     */
    @Provides
    @Singleton
    @Named(CONFIG)
    public Retrofit provideConfigRetrofit(@Named(CONFIG) OkHttpClient client, Moshi moshi) {
        return new Retrofit.Builder()
                .client(client)
                .baseUrl("http://mymartlet.herokuapp.com/api/v2/")
                .addConverterFactory(MoshiConverterFactory.create(moshi))
{'fraction_non_alphanumeric': 0.08515177965279798, 'fraction_numerical': 0.004849190185239065, 'mean_word_length': 2.346965271015904, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2403659', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2630, 'n_words': 727}