package org.azavea.otm.rest;

import android.util.Log;

import com.loopj.android.http.*;

// This class is designed to take care of the base-url
// and otm api-key for REST requests
public class RestClient {
	private String baseUrl;
	
	private String apiKey;
	
	private AsyncHttpClient client;
	
	
	public RestClient() {
		baseUrl = getBaseUrl();
		apiKey = getApiKey();
		client = new AsyncHttpClient();
	}

	// Dependency injection to support mocking
	// in unit-tests
	public void setAsyncClient(AsyncHttpClient client) {
		this.client = client;
	}
	
	public void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		RequestParams reqParams = prepareParams(params);
		client.get(getAbsoluteUrl(url), reqParams, responseHandler);
	}
	
	public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		RequestParams reqParams = prepareParams(params);
		client.post(getAbsoluteUrl(url), reqParams, responseHandler);
	}
	
	public void delete(String url, AsyncHttpResponseHandler responseHandler) {
		client.delete(getAbsoluteUrl(url), responseHandler);
	}
	
	private RequestParams prepareParams(RequestParams params) {
		// We'll always need a RequestParams object since we'll always
		// be sending an apikey
		RequestParams reqParams;
		if (params == null) {
			reqParams = new RequestParams();
		} else {
			reqParams = params;
		}
		
		reqParams.put("apikey", apiKey);
		
		return reqParams;
	}
	
	private String getBaseUrl() {
		// TODO: Expand once configuration management has been implemented
		//return "http://localhost:8000/api/v0.1";
		return "http://httpbin.org";
	}
	
	private String getApiKey() {
		// TODO: Expand once authentication management has been implemented
		return "123456";
	}
	
	private String getAbsoluteUrl(String relativeUrl) {
		return baseUrl + relativeUrl;
	}
}

@@ -23,16 +23,152 @@
 .rest;%0A%0A
+import org.apache.http.Header;%0Aimport org.apache.http.message.BasicHeader;%0A%0Aimport android.content.Context;%0Aimport android.util.Base64;%0A
 import a
@@ -902,334 +902,1718 @@
 %0A%09%0A%09
-public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) %7B%0A%09%09RequestParams reqParams = prepareParams(params);%0A%09%09client.post(getAbsoluteUrl(url), reqParams, responseHandler);%0A%09%7D%0A%09%0A%09public void delete(String url, AsyncHttpResponseHandler responseHandler) %7B%0A%09%09client.delete(getAbsoluteUrl(url)
+/**%0A%09 * Executes a get request and adds basic authentication headers to the request.%0A%09 */%0A%09public void getWithAuthentication(Context context, String url, String username, %0A%09%09%09String password, RequestParams params, AsyncHttpResponseHandler responseHandler) %7B%0A%09%09RequestParams reqParams = prepareParams(params);%0A%09%09Header%5B%5D headers = %7BcreateBasicAuthenticationHeader(username, password)%7D;%0A%09%09client.get(context, getAbsoluteUrl(url), headers, reqParams, responseHandler);%0A%09%7D%0A%09%0A%09public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) %7B%0A%09%09RequestParams reqParams = prepareParams(params);%0A%09%09client.post(getAbsoluteUrl(url), reqParams, responseHandler);%0A%09%7D%0A%09%0A%09/**%0A%09 * Executes a post request and adds basic authentication headers to the request.%0A%09 */%0A%09public void postWithAuthentication(Context context, String url, String username, %0A%09%09%09String password, RequestParams params, String contentType, %0A%09%09%09AsyncHttpResponseHandler responseHandler) %7B%0A%09%09RequestParams reqParams = prepareParams(params);%0A%09%09Header%5B%5D headers = %7BcreateBasicAuthenticationHeader(username, password)%7D;%0A%09%09client.post(context, getAbsoluteUrl(url), headers, reqParams, contentType, %0A%09%09%09%09responseHandler);%0A%09%7D%0A%0A%09public void delete(String url, AsyncHttpResponseHandler responseHandler) %7B%0A%09%09client.delete(getAbsoluteUrl(url), responseHandler);%0A%09%7D%0A%0A%09/**%0A%09 * Executes a delete request and adds basic authentication headers to the request.%0A%09 */%0A%09public void deleteWithAuthentication(Context context, String url, String username, %0A%09%09%09String password, AsyncHttpResponseHandler responseHandler) %7B%0A%09%09Header%5B%5D headers = %7BcreateBasicAuthenticationHeader(username, password)%7D;%0A%09%09client.delete(context, getAbsoluteUrl(url), headers
 , re
@@ -3369,10 +3369,333 @@
 Url;%0A%09%7D%0A
+%09%0A%09private Header createBasicAuthenticationHeader(String username, String password) %7B%0A%09%09String credentials = String.format(%22%25s:%25s%22, username, password);%0A%09%09String encoded = Base64.encodeToString(credentials.getBytes(), Base64.DEFAULT);%0A%09%09return new BasicHeader(%22Authorization%22, String.format(%22%25s %25s%22, %22Basic%22, encoded));%0A%09%7D%0A
 %7D%0A

Add Authenticated version of GET, POST, DELETE