package com.clickity;

import java.io.IOException;
import java.io.OutputStream;

import com.clickity.model.*;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;

public final class Clickity {
	
	 static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
	 static final JsonFactory JSON_FACTORY = new GsonFactory();
	 	 
	 static final HttpRequestFactory requestFactory =
		        HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
			          public void initialize(HttpRequest request) {
			        	request.addParser(new JsonHttpParser(JSON_FACTORY));
			          }
			        });

	final String BASE_URI = "https://api.clickity.io/v2";
	final String API_KEY;
	
	public Clickity(String apiKey) {
		API_KEY = apiKey;
	}
	
	private HttpRequest buildRequest(String method, String urlStr) throws IOException {
		return method == "POST" ?
				requestFactory.buildPostRequest(new GenericUrl(urlStr), null) :
				requestFactory.buildGetRequest(new GenericUrl(urlStr));
	}
	
	public Email[] GetEmails(String mailbox, EmailSearchCriteria criteria) throws IOException {
		HttpRequest request = buildRequest("GET", BASE_URI + "/emails?key=" + API_KEY + "&mailbox=" + mailbox);
		return request.execute().parseAs(Email[].class);
	}
	
	public Email GetEmail(String emailId) throws IOException {
		HttpRequest request = buildRequest("GET", BASE_URI + "/email/" + emailId + "?key=" + API_KEY);
		return request.execute().parseAs(Email.class);
	}
	
	public Boolean DeleteAllEmail(String mailbox) throws IOException {
		HttpRequest request = buildRequest("POST", BASE_URI + "/emails/deleteall?key=" + API_KEY + "&mailbox=" + mailbox);
		DeleteResult result = request.execute().parseAs(DeleteResult.class);
		return result.Ok;
	}
	
	public Boolean DeleteEmail(String emailId) throws IOException {
		HttpRequest request = buildRequest("POST", BASE_URI + "/email/" + emailId + "/delete?key=" + API_KEY);
		DeleteResult result = request.execute().parseAs(DeleteResult.class);
		return result.Ok;
	}
	
	public OutputStream GetAttachment(String attachmentId) throws IOException {
		HttpRequest request = buildRequest("GET", BASE_URI + "/attachment/" + attachmentId + "?key=" + API_KEY);
		OutputStream stream = null;
		request.execute().download(stream);
		return stream;
	}
	
	public String GetRawEmail(String rawId) throws IOException {
		HttpRequest request = buildRequest("GET", BASE_URI + "/raw/" + rawId + "?key=" + API_KEY);
		return request.execute().parseAsString();
	}
}

@@ -31,27 +31,37 @@
 java.io.
-IOException
+ByteArrayOutputStream
 ;%0Aimport
@@ -69,28 +69,27 @@
 java.io.
-OutputStream
+IOException
 ;%0A%0Aimpor
@@ -112,16 +112,17 @@
 odel.*;%0A
+%0A
 import c
@@ -2422,32 +2422,33 @@
  result.Ok;%0A%09%7D%0A%09
+%09
 %0A%09public OutputS
@@ -2440,28 +2440,22 @@
 %09public 
-OutputStream
+byte%5B%5D
  GetAtta
@@ -2612,16 +2612,25 @@
 KEY);%0A%09%09
+ByteArray
 OutputSt
@@ -2644,19 +2644,42 @@
 ream = n
-ull
+ew ByteArrayOutputStream()
 ;%0A%09%09requ
@@ -2725,16 +2725,30 @@
 n stream
+.toByteArray()
 ;%0A%09%7D%0A%09%0A%09
@@ -2754,22 +2754,22 @@
 %09public 
-String
+byte%5B%5D
  GetRawE
@@ -2907,46 +2907,133 @@
 ;%0A%09%09
-return request.execute().parseAsString
+ByteArrayOutputStream stream = new ByteArrayOutputStream();%0A%09%09request.execute().download(stream);%0A%09%09return stream.toByteArray
 ();%0A

Update download methods to return byte[]