/*******************************************************************************
 * Copyright (c) 2017 Intuit
 *
 * 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.intuit.oauth2.http;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.apache.http.NameValuePair;

import com.intuit.oauth2.exception.InvalidRequestException;

/**
 * Class to hold the request attributes for making the http call
 * 
 * @author dderose
 *
 */
public class Request {

	private final MethodType method;
    private final String url;
    private final List<NameValuePair> postParams;
    private final boolean requiresAuthentication;
    private final String authString;
    
    private Request(RequestBuilder builder) {
        this.method = builder.method;
        this.url = builder.url;
        this.requiresAuthentication = builder.requiresAuthentication;
        this.authString = builder.authString;
        this.postParams = builder.postParams;
	}


	public MethodType getMethod() {
		return method;
	}

	public String getUrl() {
		return url;
	}

	public boolean isRequiresAuthentication() {
		return requiresAuthentication;
	}


	public String getAuthString() {
		return authString;
	}
	
	public List<NameValuePair> getPostParams() {
		return postParams;
	}


	/**
	 * Prepares request URL
	 * 
	 * @return
	 * @throws InvalidRequestException
	 */
	public URL constructURL() throws InvalidRequestException {	
 
        String stringUri = url;

        try {
            URI uri = new URI(stringUri);
            return uri.toURL();
        } catch (final URISyntaxException e) {
            throw new InvalidRequestException("Bad URI: " + stringUri, e);
        } catch (final MalformedURLException e) {
            throw new InvalidRequestException("Bad URL: " + stringUri, e);
        }
    }
	
	public static class RequestBuilder {
		
		private final MethodType method;
	    private final String url;
	    private List<NameValuePair> postParams;
	    private boolean requiresAuthentication;
	    private String authString;

		public RequestBuilder(MethodType method, String url) {
			this.method = method;
			this.url = url;
		}
		
		public RequestBuilder postParams(List<NameValuePair> postParams) {
			this.postParams = postParams;
			return this;
		}
		
		public RequestBuilder requiresAuthentication(boolean requiresAuthentication) {
			this.requiresAuthentication = requiresAuthentication;
			return this;
		}
		
		public RequestBuilder authString(String authString) {
			this.authString = authString;
			return this;
		}

		public Request build() {
			return new Request(this);
		}
		
	}
    
}

@@ -1338,21 +1338,56 @@
 String;%0A
+    private final String postJson;%0A
     %0A
-
     priv
@@ -1652,24 +1652,66 @@
 postParams;%0A
+        this.postJson = builder.postJson;%0A
 %09%7D%0A%0A%0A%09public
@@ -2013,16 +2013,71 @@
 ams;%0A%09%7D%0A
+%09%0A%09public String getPostJson() %7B%0A%09%09return postJson;%0A%09%7D%0A
 %0A%0A%09/**%0A%09
@@ -2835,16 +2835,46 @@
 hString;
+%0A%09    private String postJson;
 %0A%0A%09%09publ
@@ -3313,24 +3313,24 @@
 thString) %7B%0A
-
 %09%09%09this.auth
@@ -3369,16 +3369,120 @@
 his;%0A%09%09%7D
+%0A%09%09%0A%09%09public RequestBuilder postJson(String postJson) %7B%0A%09%09%09this.postJson = postJson;%0A%09%09%09return this;%0A%09%09%7D
 %0A%0A%09%09publ

migrate API changes