package com.andremion.heroes.api.auth;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class AuthenticatorInterceptor implements Interceptor {

    // TODO: Put your private key here
    private static final String PRIVATE_KEY = "0980acd08f14d1143ef60e0bd02a0cdf87ec45f2";
    // TODO: Put your public key here
    private static final String PUBLIC_KEY = "6862c876658e08ba941c8c6399933535";

    private static final String QUERY_NAME_TIMESTAMP = "ts";
    private static final String QUERY_NAME_APIKEY = "apikey";
    private static final String QUERY_NAME_HASH = "hash";

    @Override
    public Response intercept(Chain chain) throws IOException {

        String ts = String.valueOf(System.currentTimeMillis());
        String hash = HashHelper.generate(ts + PRIVATE_KEY + PUBLIC_KEY);

        Request currentRequest = chain.request();

        HttpUrl url = currentRequest.url().newBuilder()
                .addQueryParameter(QUERY_NAME_TIMESTAMP, ts)
                .addQueryParameter(QUERY_NAME_APIKEY, PUBLIC_KEY)
                .addQueryParameter(QUERY_NAME_HASH, hash).build();

        Request newRequest = currentRequest.newBuilder().url(url).build();

        return chain.proceed(newRequest);
    }
}

@@ -318,48 +318,11 @@
  = %22
-0980acd08f14d1143ef60e0bd02a0cdf87ec45f2
+###
 %22;%0A 
@@ -408,40 +408,11 @@
  = %22
-6862c876658e08ba941c8c6399933535
+###
 %22;%0A%0A

Remove API keys