package com.commit451.gitlab.api;

import com.commit451.gitlab.model.Account;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import timber.log.Timber;

/**
 * Adds the private token to all requests
 * Created by Jawn on 9/15/2015.
 */
public class AuthenticationRequestInterceptor implements Interceptor {

    private static final String AUTHORIZATION_HEADER_FIELD = "Authorization";
    private static final String PRIVATE_TOKEN_HEADER_FIELD = "PRIVATE-TOKEN";
    private static final String PRIVATE_TOKEN_GET_PARAMETER = "private_token";

    private Account mAccount;

    public AuthenticationRequestInterceptor(Account account) {
        mAccount = account;
    }

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

        HttpUrl url = request.httpUrl();
        if (url.toString().startsWith(mAccount.getServerUrl().toString())) {
            String authorizationHeader = mAccount.getAuthorizationHeader();
            if (authorizationHeader != null) {
                request = request.newBuilder()
                        .header(AUTHORIZATION_HEADER_FIELD, authorizationHeader)
                        .build();
            }

            String privateToken = mAccount.getPrivateToken();
            if (privateToken == null) {
                Timber.e("The private token was null");
            } else {
                url = url.newBuilder()
                        .addQueryParameter(PRIVATE_TOKEN_GET_PARAMETER, privateToken)
                        .build();

                request = request.newBuilder()
                        .header(PRIVATE_TOKEN_HEADER_FIELD, privateToken)
                        .url(url)
                        .build();
            }
        }

        return chain.proceed(request);
    }
}

@@ -960,24 +960,25 @@
 pUrl();%0A
+%0A
         
 if (url.
@@ -973,72 +973,300 @@
     
-if (url.toString().startsWith(mAccount.getServerUrl().toString()
+String cleanUrl = url.toString();%0A        cleanUrl = cleanUrl.substring(cleanUrl.indexOf(':'));%0A%0A        String cleanServerUrl = mAccount.getServerUrl().toString();%0A        cleanServerUrl = cleanServerUrl.substring(cleanServerUrl.indexOf(':'));%0A%0A        if (cleanUrl.startsWith(cleanServerUrl
 )) %7B

Make the AuthenticationRequestInterceptor url check independent from the scheme (http/https). Should fix #86