package com.namelessmc.NamelessAPI;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class NamelessAPI {

	/**
	 * Checks if a web API connection can be established
	 * @return An exception if the connection was unsuccessful, null if the connection was successful.
	 */
	public static NamelessException checkWebAPIConnection(URL url) {
		try {
			boolean https = url.toString().startsWith("https");
			
			URL apiConnection = new URL(url + "/checkConnection");
			
			URLConnection connection;
			
			if (https) {
				HttpsURLConnection httpsConnection = (HttpsURLConnection) apiConnection.openConnection();
				httpsConnection.setRequestMethod("POST");
				connection = httpsConnection;
			} else {
				HttpURLConnection httpConnection = (HttpURLConnection) apiConnection.openConnection();
				httpConnection.setRequestMethod("POST");
				connection = httpConnection;
			}
			
			connection.setRequestProperty("Content-Length", Integer.toString(0));
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			connection.setDoOutput(true);
			connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");


			// Initialize output stream
			DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
			// Write request
			outputStream.writeBytes("");

			// Initialize input stream
			InputStream inputStream = connection.getInputStream();

			// Handle response
			BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			StringBuilder responseBuilder = new StringBuilder();

			String responseString;
			while ((responseString = streamReader.readLine()) != null)
				responseBuilder.append(responseString);

			JsonObject response = new JsonObject();
			JsonParser parser = new JsonParser();

			response = parser.parse(responseBuilder.toString()).getAsJsonObject();
			
			outputStream.flush();
			outputStream.close();
			inputStream.close();

			if (https) {
				((HttpsURLConnection) connection).disconnect();
			} else {
				((HttpURLConnection) connection).disconnect();
			}
			
			if (response.has("success")
					|| response.get("message").getAsString().equalsIgnoreCase("Invalid API method")) {
				return null;
			} else if (response.has("error")) {
				return new NamelessException(response.get("message").getAsString());
			} else {
				return new NamelessException();
			}
		} catch (Exception e) {
			return new NamelessException(e);
		}
	}
	
	protected static String urlEncodeString(String string) {
		try {
			return URLEncoder.encode(string, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

}
@@ -49,322 +49,149 @@
 .io.
-BufferedReader;%0Aimport java.io.DataOutputStream;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL;%0Aimport java.net.URLConnection;%0Aimport java.net.URLEncoder;%0A%0Aimport javax.net.ssl.HttpsURLConnection
+UnsupportedEncodingException;%0Aimport java.net.URL;%0Aimport java.net.URLEncoder;%0A%0Aimport com.namelessmc.NamelessAPI.utils.NamelessRequestUtil
 ;%0A
-%0A
 impo
@@ -201,65 +201,64 @@
 com.
-google.gson.JsonObject;%0Aimport com.google.gson.JsonParser
+namelessmc.NamelessAPI.utils.NamelessRequestUtil.Request
 ;%0A%0Ap
@@ -463,24 +463,16 @@
  static 
-Nameless
 Exceptio
@@ -504,24 +504,214 @@
 RL url) %7B%0A%09%09
+Request request = NamelessRequestUtil.sendPostRequest(url, %22checkConnection%22, null);%0A%09%09if (request.hasSucceeded()) %7B%0A%09%09%09return null;%0A%09%09%7D else %7B%0A%09%09%09return request.getException();%0A%09%09%7D%0A%09%09%0A%09%09/* 
 try %7B%0A%09%09%09boo
@@ -2845,24 +2845,26 @@
 tion(e);%0A%09%09%7D
+*/
 %0A%09%7D%0A%09%0A%09prote

Use request util for checking connection