package com.namelessmc.NamelessAPI;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;

import com.namelessmc.NamelessAPI.utils.NamelessRequestUtil;
import com.namelessmc.NamelessAPI.utils.NamelessRequestUtil.Request;

public final class NamelessAPI {

	private 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 Exception checkWebAPIConnection(URL url) {
		Request request = NamelessRequestUtil.sendPostRequest(url, "checkConnection", null);
		if (request.hasSucceeded()) {
			return null;
		} else {
			return request.getException();
		}
		
		/* 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);
		}*/
	}
	
	static String urlEncodeString(String string) {
		try {
			return URLEncoder.encode(string, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

}
@@ -728,2170 +728,8 @@
 %09%09%7D%0A
-%09%09%0A%09%09/* try %7B%0A%09%09%09boolean https = url.toString().startsWith(%22https%22);%0A%09%09%09%0A%09%09%09URL apiConnection = new URL(url + %22/checkConnection%22);%0A%09%09%09%0A%09%09%09URLConnection connection;%0A%09%09%09%0A%09%09%09if (https) %7B%0A%09%09%09%09HttpsURLConnection httpsConnection = (HttpsURLConnection) apiConnection.openConnection();%0A%09%09%09%09httpsConnection.setRequestMethod(%22POST%22);%0A%09%09%09%09connection = httpsConnection;%0A%09%09%09%7D else %7B%0A%09%09%09%09HttpURLConnection httpConnection = (HttpURLConnection) apiConnection.openConnection();%0A%09%09%09%09httpConnection.setRequestMethod(%22POST%22);%0A%09%09%09%09connection = httpConnection;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09connection.setRequestProperty(%22Content-Length%22, Integer.toString(0));%0A%09%09%09connection.setRequestProperty(%22Content-Type%22, %22application/x-www-form-urlencoded%22);%0A%09%09%09connection.setDoOutput(true);%0A%09%09%09connection.addRequestProperty(%22User-Agent%22, %22Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)%22);%0A%0A%0A%09%09%09// Initialize output stream%0A%09%09%09DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());%0A%09%09%09// Write request%0A%09%09%09outputStream.writeBytes(%22%22);%0A%0A%09%09%09// Initialize input stream%0A%09%09%09InputStream inputStream = connection.getInputStream();%0A%0A%09%09%09// Handle response%0A%09%09%09BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, %22UTF-8%22));%0A%09%09%09StringBuilder responseBuilder = new StringBuilder();%0A%0A%09%09%09String responseString;%0A%09%09%09while ((responseString = streamReader.readLine()) != null)%0A%09%09%09%09responseBuilder.append(responseString);%0A%0A%09%09%09JsonObject response = new JsonObject();%0A%09%09%09JsonParser parser = new JsonParser();%0A%0A%09%09%09response = parser.parse(responseBuilder.toString()).getAsJsonObject();%0A%09%09%09%0A%09%09%09outputStream.flush();%0A%09%09%09outputStream.close();%0A%09%09%09inputStream.close();%0A%0A%09%09%09if (https) %7B%0A%09%09%09%09((HttpsURLConnection) connection).disconnect();%0A%09%09%09%7D else %7B%0A%09%09%09%09((HttpURLConnection) connection).disconnect();%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09if (response.has(%22success%22)%0A%09%09%09%09%09%7C%7C response.get(%22message%22).getAsString().equalsIgnoreCase(%22Invalid API method%22)) %7B%0A%09%09%09%09return null;%0A%09%09%09%7D else if (response.has(%22error%22)) %7B%0A%09%09%09%09return new NamelessException(response.get(%22message%22).getAsString());%0A%09%09%09%7D else %7B%0A%09%09%09%09return new NamelessException();%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09return new NamelessException(e);%0A%09%09%7D*/%0A
 %09%7D%0A%09

Remove unused code