/*
 *   Copyright 2020 Vonage
 *
 *   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.vonage.client.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.vonage.client.VonageUnexpectedException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class TokenAuthMethod extends AbstractAuthMethod {
    private final int SORT_KEY = 30;

    private String apiKey;
    private String apiSecret;

    public TokenAuthMethod(String apiKey, String apiSecret) {
        this.apiKey = apiKey;
        this.apiSecret = apiSecret;
    }

    @Override
    public RequestBuilder apply(RequestBuilder request) {
        return request.addParameter("api_key", this.apiKey).addParameter("api_secret", this.apiSecret);
    }

    @Override
    public RequestBuilder applyAsBasicAuth(RequestBuilder request) {
        String headerValue = Base64.encodeBase64String((this.apiKey + ":" + this.apiSecret).getBytes());
        Header authHeader = new BasicHeader("Authorization", "Basic " + headerValue);
        return request.addHeader(authHeader);
    }

    @Override
    public RequestBuilder applyAsJsonProperties(RequestBuilder request) {
        HttpEntity entity = request.getEntity();
        try {
            ObjectNode json = (ObjectNode) new ObjectMapper().readTree(EntityUtils.toString(entity));
            json.put("api_key", this.apiKey);
            json.put("api_secret", this.apiSecret);

            return request.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
        } catch (IOException e) {
            throw new VonageUnexpectedException("Failed to attach api key and secret to json.", e);
        }
    }

    @Override
    public int getSortKey() {
        return SORT_KEY;
    }
}

@@ -1569,29 +1569,24 @@
 (%22api_key%22, 
-this.
 apiKey).addP
@@ -1600,37 +1600,32 @@
 r(%22api_secret%22, 
-this.
 apiSecret);%0A    
@@ -1766,21 +1766,16 @@
 String((
-this.
 apiKey +
@@ -1781,21 +1781,16 @@
 + %22:%22 + 
-this.
 apiSecre
@@ -2229,21 +2229,16 @@
 i_key%22, 
-this.
 apiKey);
@@ -2273,21 +2273,16 @@
 ecret%22, 
-this.
 apiSecre

Remove unnecessary references to `this`