/*
 * Copyright 2016 Jon Ander Peñalba
 *
 * 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.github.mobile.api;

import android.text.TextUtils;

import com.github.mobile.accounts.GitHubAccount;
import com.google.inject.Provider;

import org.eclipse.egit.github.core.util.EncodingUtils;

import java.io.IOException;

import okhttp3.Request;
import okhttp3.Response;

public class RequestConfiguration implements okhttp3.Interceptor {

    private static final String USER_AGENT = "ForkHub/1.2";

    private final Provider<GitHubAccount> accountProvider;

    public RequestConfiguration(final Provider<GitHubAccount> accountProvider) {
        this.accountProvider = accountProvider;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();

        String credentials;

        GitHubAccount account = accountProvider.get();
        String token = account.getAuthToken();
        if (!TextUtils.isEmpty(token)) {
            credentials = "token " + token;
        } else {
            credentials = "Basic " + EncodingUtils.toBase64(account.getUsername() + ':' + account.getPassword());
        }

        Request newRequest = originalRequest.newBuilder()
                .header("Authorization", credentials)
                .header("User-Agent", USER_AGENT)
                .build();

        return chain.proceed(newRequest);
    }
}

@@ -976,16 +976,23 @@
  String 
+HEADER_
 USER_AGE
@@ -1009,11 +1009,98 @@
 Hub/
-1.2
+2.0%22;%0A    private static final String HEADER_ACCEPT = %22application/vnd.github.v3.html+json
 %22;%0A%0A
@@ -1933,16 +1933,23 @@
 Agent%22, 
+HEADER_
 USER_AGE
@@ -1952,16 +1952,68 @@
 _AGENT)%0A
+                .addHeader(%22Accept%22, HEADER_ACCEPT)%0A
         

Update request headers