package com.simplenote.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class APIBase {
	
	public static class Response {
		public int statusCode;
		public String resp;
	}
	
	/**
	 * Sends an HTTP POST request
	 * 
	 * @param url
	 * @param postData
	 * @return Response object containing status code and response body
	 */
	public static Response HTTPPost( String url, String postData ) {
		Response response = new Response();
		try {
			// Setup connection
			HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
			conn.setRequestMethod( "POST" );
			conn.setDoOutput( true );
			conn.connect();
			
			// Send POST data to the server
			OutputStreamWriter out = new OutputStreamWriter( conn.getOutputStream() );
			out.write( postData );
			out.flush();
			
			// Get the response from the server
			BufferedReader in = new BufferedReader (new InputStreamReader( conn.getInputStream() ));
			StringBuilder sb = new StringBuilder();
			
			for ( String line = in.readLine(); line != null; line = in.readLine() ) {
				sb.append(line + '\n');
			}
			
			// Store response information in Response object
			try {
				response.statusCode = conn.getResponseCode();
			} catch (IOException e) {
				response.statusCode = 401;
			}
			response.resp = sb.toString();
			
			// Clean up
			conn.disconnect();
			conn = null;
			out  = null;
			in   = null;
			sb   = null;
			
		} catch (IOException e) {
			response.statusCode = 401;
		}
		return response;
	}
	
	/**
	 * Sends an HTTP GET request
	 * 
	 * @param url
	 * @return Response object containing status  code and response body
	 */
	public static Response HTTPGet( String url ) {
		Response response = new Response();
		try {
			// Setup connection
			HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
			conn.setRequestMethod("GET");
			conn.connect();

			// Get response from the server
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuilder sb = new StringBuilder();

			for (String line = in.readLine(); line != null; line = in.readLine()) {
				sb.append(line + '\n');
			}

			// Store response in a Response object
			try {
				response.statusCode = conn.getResponseCode();
			} catch (IOException e) {
				response.statusCode = 401;
			}
			response.resp = sb.toString(); // Remove extraneous CR/LF

			// Clean up
			conn.disconnect();
			conn = null;
			in = null;
			sb = null;
		} catch (IOException e) {
			response.statusCode = 401;
		}
		return response;
	}
	
	public static String encode(String str) {
		return encode( str, false, true ); // Don't Base64 encode by default
	}
	
	public static String encode(String str, Boolean base64Encode) {
		return encode( str, false, true ); // Don't Base64 encode by default
	}
	
	public static String encode(String str, Boolean base64Encode, boolean urlEncode) {
		if (urlEncode) {
			try {
				return base64Encode ? Base64.encodeBytes( URLEncoder.encode( str, "UTF-8" ).getBytes() ) : URLEncoder.encode( str, "UTF-8" );
			} catch (UnsupportedEncodingException e) {
				return base64Encode ? Base64.encodeBytes( URLEncoder.encode( str ).getBytes() ) : URLEncoder.encode( str );
			}
		} else {
			return base64Encode ? Base64.encodeBytes(str.getBytes()) : str;
		}
	}

}

@@ -1086,17 +1086,16 @@
 edReader
- 
 (new Inp
@@ -1109,17 +1109,16 @@
 mReader(
- 
 conn.get
@@ -1130,18 +1130,22 @@
 Stream()
- 
 )
+,8192
 );%0A%09%09%09St
@@ -2207,16 +2207,21 @@
 tream())
+,8192
 );%0A%09%09%09St
@@ -3172,33 +3172,32 @@
 e64.encodeBytes(
- 
 URLEncoder.encod
@@ -3190,33 +3190,32 @@
 LEncoder.encode(
- 
 str, %22UTF-8%22 ).g
@@ -3210,17 +3210,16 @@
  %22UTF-8%22
- 
 ).getByt
@@ -3214,33 +3214,32 @@
 F-8%22).getBytes()
- 
 ) : URLEncoder.e
@@ -3240,25 +3240,24 @@
 oder.encode(
- 
 str, %22UTF-8%22
@@ -3256,17 +3256,16 @@
  %22UTF-8%22
- 
 );%0A%09%09%09%7D 
@@ -3350,17 +3350,16 @@
 deBytes(
- 
 URLEncod
@@ -3368,21 +3368,19 @@
 .encode(
- 
 str
- 
 ).getByt
@@ -3383,17 +3383,16 @@
 tBytes()
- 
 ) : URLE
@@ -3409,13 +3409,11 @@
 ode(
- 
 str
- 
 );%0A%09

Add size to BufferedReader instantiation