package com.constantcontact.util;

import com.constantcontact.util.http.HttpProcessor;
import com.constantcontact.util.http.MultipartBody;
import com.constantcontact.util.http.constants.ProcessorBase.ContentType;
import com.constantcontact.util.http.constants.ProcessorBase.HttpMethod;

/**
 * Implementation of REST client in Constant Contact.
 * 
 * @author ConstantContact
 * 
 */
public class RestClient implements IRestClient {
	/**
	 * Make HTTP GET request.
	 * 
	 * @param url Request URL.
	 * @param accessToken Constant Contact OAuth2 access token.
	 * @return The response body, http info, and error (if one exists).
	 */
	public RawApiResponse get(String url, String accessToken) {
		return makeHttpRequest(url, HttpMethod.GET, accessToken, null);
	}

	/**
	 * Make an HTTP POST request.
	 * 
	 * @param url Request URL.
	 * @param accessToken Constant Contact OAuth2 access token.
	 * @param data Data to send with request.
	 * @return The response body, http info, and error (if one exists).
	 */
	public RawApiResponse post(String url, String accessToken, String data) {
		return makeHttpRequest(url, HttpMethod.POST, accessToken, data);
	}
	
	/**
     * Make an HTTP POST request with a Content-Type of multipart/form-data.
     * 
     * @param url Request URL.
     * @param accessToken Constant Contact OAuth2 access token.
     * @param data Data to send with request.
     * @return The response body, http info, and error (if one exists).
     */
    public RawApiResponse postMultipart(String url, String accessToken, MultipartBody data) {
        return makeMultipartRequest(url, accessToken, data);
    }

	/**
	 * Make an HTTP PUT request.
	 * 
	 * @param url Request URL.
	 * @param accessToken Constant Contact OAuth2 access token.
	 * @param data Data to send with request.
	 * @return The response body, http info, and error (if one exists).
	 */
	public RawApiResponse put(String url, String accessToken, String data) {
		return makeHttpRequest(url, HttpMethod.PUT, accessToken, data);
	}

	/**
	 * Make an HTTP DELETE request.
	 * 
	 * @param url Request URL.
	 * @param accessToken Constant Contact OAuth2 access token
	 * @return The response body, http info, and error (if one exists).
	 */
	public RawApiResponse delete(String url, String accessToken) {
		return makeHttpRequest(url, HttpMethod.DELETE, accessToken, null);
	}

    /**
     * Make an HTTP PATCH request.
     *
     * @param url Request URL.
     * @param accessToken Constant Contact OAuth2 access token.
     * @param data Data to send with request.
     * @return The response body, http info, and error (if one exists).
     */
    public RawApiResponse patch(String url, String accessToken, String data) {
        return makeHttpRequest(url, HttpMethod.PATCH, accessToken, data);
    }

	protected RawApiResponse makeHttpRequest(String urlParam, HttpMethod method, String accessToken, String data) {
		return new HttpProcessor().makeHttpRequest(urlParam, method, ContentType.JSON, accessToken, data);
	}
	
	protected RawApiResponse makeMultipartRequest(String urlParam, String accessToken, MultipartBody data) {
	    
	    byte[] bodyBytes = data.getBytes();
	    
	    return new HttpProcessor().makeHttpRequest(urlParam, HttpMethod.POST, ContentType.FORM_DATA, accessToken, bodyBytes);
    }
	
	/**
	 * Default constructor.
	 */
	public RestClient() {
		super();
	}
}

@@ -426,16 +426,330 @@
 lient %7B%0A
+%0A%09private HttpProcessor httpProcessor;%0A%0A%09/**%0A%09 * @return the httpProcessor%0A%09 */%0A%09public HttpProcessor getHttpProcessor() %7B%0A%09%09return httpProcessor;%0A%09%7D%0A%0A%09/**%0A%09 * @param httpProcessor the httpProcessor to set%0A%09 */%0A%09public void setHttpProcessor(HttpProcessor httpProcessor) %7B%0A%09%09this.httpProcessor = httpProcessor;%0A%09%7D%0A%0A
 %09/**%0A%09 *
@@ -3219,28 +3219,32 @@
  %7B%0A%09%09return 
-new 
+this.get
 HttpProcesso
@@ -3499,12 +3499,16 @@
 urn 
-new 
+this.get
 Http
@@ -3687,13 +3687,59 @@
 uper();%0A
+%09%09this.setHttpProcessor(new HttpProcessor());%0A
 %09%7D%0A%7D%0A

Change the usage of HttpProcessor in Java and design it in .NET so that it is a property of RestClient, with a getter and setter, instead of being instantiated on every request.