package fi.jyu.data.twitter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import fi.jyu.util.CommHelpers;

public class Client {

	private Config config;
	
	public Client(Config config) {
		this.config = config;
	}
	
	public void search(String keyword, int count) throws IOException {
		HttpsURLConnection connection = null;
		
		String request = "https://api.twitter.com/1.1/search/tweets.json?q=" + URLEncoder.encode(keyword, "UTF-8") + "&count=" + count;
		
		try {
			URL url = new URL(request); 
			connection = (HttpsURLConnection) url.openConnection();           
			connection.setDoOutput(true);
			connection.setDoInput(true); 
			connection.setRequestMethod("GET"); 
			connection.setRequestProperty("Host", "api.twitter.com");
			connection.setRequestProperty("User-Agent", "Data Challenge");
			connection.setRequestProperty("Authorization", "Bearer " + config.getBearerToken());
			connection.setUseCaches(false);
				
			File f = new File("twitter_data.json");
			if (f.exists()) f.delete();
			
			FileWriter file = new FileWriter("twitter_data.json");
			file.write(CommHelpers.readResponse(connection));
			file.close();
		}
		catch (MalformedURLException e) {
			throw new IOException("Invalid endpoint URL specified.", e);
		}
		finally {
			if (connection != null) {
				connection.disconnect();
			}
		}		
	}
	
	public void test() throws IOException {
		HttpsURLConnection connection = null;
					
		try {
			URL url = new URL("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jotudin&count=2"); 
			connection = (HttpsURLConnection) url.openConnection();           
			connection.setDoOutput(true);
			connection.setDoInput(true); 
			connection.setRequestMethod("GET"); 
			connection.setRequestProperty("Host", "api.twitter.com");
			connection.setRequestProperty("User-Agent", "Your Program Name");
			connection.setRequestProperty("Authorization", "Bearer " + config.getBearerToken());
			connection.setUseCaches(false);
				
				
			// Parse the JSON response into a JSON mapped object to fetch fields from.
			JSONArray obj = (JSONArray)JSONValue.parse(CommHelpers.readResponse(connection));
				
			if (obj != null) {
				String tweet = ((JSONObject)obj.get(0)).get("text").toString();
				
				System.out.println("Last thing Mikko Lampi said in Twitter was...");
				System.out.println((tweet != null) ? tweet : "");
			}
		}
		catch (MalformedURLException e) {
			throw new IOException("Invalid endpoint URL specified.", e);
		}
		finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
	}
 
}
@@ -2235,91 +2235,8 @@
 %09%09%09%0A
-%09%09%09%09%0A%09%09%09// Parse the JSON response into a JSON mapped object to fetch fields from.%0A
 %09%09%09J
@@ -2316,21 +2316,16 @@
 tion));%0A
-%09%09%09%09%0A
 %09%09%09if (o
@@ -2736,8 +2736,9 @@
 %7D%0A%09%7D%0A %0A%7D
+%0A

Update Client.java