package org.octoprint.api;

import org.json.simple.JsonObject;

/**
 * An object to encapsulate the http request 
 * 
 * @author rweber
 *  
 */
public class OctoPrintHttpRequest {
	private String m_url = null;
	private String m_type = null;
	private JsonObject m_params = null;
	
	public OctoPrintHttpRequest(String url) {
		m_url = "/api/" + url;
		m_type = "GET";
		m_params = new JsonObject();
	}
	
	protected String getURL(){
		return m_url;
	}
	
	protected String getType(){
		return m_type;
	}
	
	protected String getParams(){
		return m_params.toJson();
	}
	
	public void setType(String type){
		m_type = type;
	}
	
	public void addParam(String name, Object value){
		m_params.put(name,value);
	}
	
	public void setPayload(JsonObject json){
		m_params = json;
	}
	
	public boolean hasParams(){
		return !m_params.isEmpty();
	}
}

@@ -21,16 +21,141 @@
 t.api;%0A%0A
+import java.io.OutputStream;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0A%0A
 import o
@@ -953,10 +953,768 @@
 y();%0A%09%7D%0A
+%09%0A%09public HttpURLConnection createConnection(URL url, String key) %7B%0A%09%09HttpURLConnection connection = null;%0A%0A%09%09try%7B%0A%0A%09%09%09URL apiUrl = new URL(m_url + this.getURL());%0A%09%09%09%0A%09%09%09//create the connection%0A%09%09%09connection = (HttpURLConnection)apiUrl.openConnection();%0A%0A%09%09%09//set default connection parameters%0A%09%09%09connection.setRequestProperty(%22X-Api-Key%22, key);%0A%09%09%09connection.setRequestProperty(%22Content-Type%22,%22application/json%22);%0A%09%09%09connection.setRequestMethod(this.getType());%0A%09%09%09connection.setDoInput(true);%0A%09%09%09connection.setDoOutput(true);%0A%0A%09%09%09if(this.hasParams())%0A%09%09%09%7B%0A%09%09%09%09OutputStream os = connection.getOutputStream();%0A%09%09%09%09os.write(this.getParams().getBytes());%0A%09%09%09%09os.flush();%0A%09%09%09%7D%0A%0A%09%09%7D%0A%09%09catch(Exception e)%0A%09%09%7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%0A%09%09return connection;%0A%09%7D%0A
 %7D%0A

move connection code to httprequest class and allow URLs to be set directly in the instance object