/*******************************************************************************
 * Copyright (c) 2017 Rizky Kharisma (@ngengs)
 *
 *
 * 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 com.ngengs.android.app.filkomnewsreader.network;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ngengs.android.app.filkomnewsreader.BuildConfig;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import timber.log.Timber;

@SuppressWarnings("WeakerAccess")
public class Connection {
    public static final String BASE_URL = "http://filkom-news-reader.ngengs.com/api/";

    private static final long CACHE_SIZE = 10 * 1024 * 1024;    // 10 MB
    private static final int CONNECT_TIMEOUT = 15;
    private static final int WRITE_TIMEOUT = 60;
    private static final int TIMEOUT = 60;

    /**
     * Build okHttp client.
     *
     * @param cacheDirectory
     *         Cache directory location
     *
     * @return okHttp client
     */
    @NonNull
    public static OkHttpClient provideOkHttp(File cacheDirectory) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message -> {
            if (BuildConfig.DEBUG) {
                Timber.tag("OkHttp").d(message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        Cache cache = new Cache(cacheDirectory, CACHE_SIZE);

        return new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .build();
    }


    /**
     * Build retrofit client.
     *
     * @param okHttpClient
     *         okHttp configured client.
     *
     * @return retrofit client
     */
    @NonNull
    public static Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy HH:mm:ss").create();
        return new Retrofit.Builder()
                .baseUrl(Connection.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
                .client(okHttpClient)
                .build();
    }


    /**
     * Build FilkomService client.
     *
     * @param cacheDirectory
     *         The application cache location
     *
     * @return Filkom Service client from retrofit.
     */
    public static FilkomService build(File cacheDirectory) {
        return provideRetrofit(provideOkHttp(cacheDirectory)).create(FilkomService.class);
    }

}

@@ -1426,16 +1426,17 @@
  = %22http
+s
 ://filko

Change url to ssl