pluginapi/src/main/java/xyz/etztech/core/web/Http.java

135 lines
4.3 KiB
Java

package xyz.etztech.core.web;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
import java.util.logging.Logger;
/**
* A static class used for HTTP calls within a plugin
*/
public class Http {
private static BukkitScheduler schedule = null;
private static final Logger logger = Logger.getLogger("Minecraft");
static {
try {
schedule = Bukkit.getScheduler();
} catch (Exception ex) {
logger.warning("Could not get scheduler. Is this running outside Minecraft?");
}
}
/**
* @param plugin The calling plugin
* @param url The URL to call
* @param data A map of parameters
*/
public static void asyncGet(final Plugin plugin, final String url, final Map<String, String> data) {
schedule.runTaskAsynchronously(plugin, () -> {
try {
HTTP(url, Method.GET, data);
} catch (Exception ex) {
logger.warning("GET request failed. (" + url + ")");
}
});
}
/**
* @param plugin The calling plugin
* @param url The URL to call
* @param data A map of parameters
* @param callback A class implementing an ICallBack to run upon completion
* @see ICallback
*/
public static void asyncGetCallback(final Plugin plugin, final String url, final Map<String, String> data, final ICallback callback) {
schedule.runTaskAsynchronously(plugin, () -> {
try {
String result = HTTP(url, Method.GET, data);
callback.invoke(result);
} catch (Exception ex) {
logger.warning("GET request failed. (" + url + ")");
}
});
}
/**
* @param plugin The calling plugin
* @param url The URL to call
* @param data A map of parameters
*/
public static void asyncPost(final Plugin plugin, final String url, final Map<String, String> data) {
schedule.runTaskAsynchronously(plugin, () -> {
try {
HTTP(url, Method.POST, data);
} catch (Exception ex) {
logger.warning("POST request failed. (" + url + ")");
}
});
}
/**
* @param plugin The calling plugin
* @param url The URL to call
* @param data A map of parameters
* @param callback A class implementing an ICallBack to run upon completion
* @see ICallback
*/
public static void asyncPostCallback(final Plugin plugin, final String url, final Map<String, String> data, final ICallback callback) {
schedule.runTaskAsynchronously(plugin, () -> {
try {
String result = HTTP(url, Method.POST, data);
callback.invoke(result);
} catch (Exception ex) {
logger.warning("POST request failed. (" + url + ")");
}
});
}
/**
* Available HTTP methods to use with the raw HTTP method
*/
public enum Method {
GET, POST
}
public static String HTTP(String endpoint, Method method, Map<String, String> data) throws Exception {
if (method == Method.POST) {
MultipartUtility request = new MultipartUtility(endpoint, "utf-8");
for (String key : data.keySet()) {
request.addFormField(key, data.get(key));
}
return request.finish();
}
// Set up data
StringJoiner sj = new StringJoiner("&");
for(Map.Entry<String,String> entry : data.entrySet())
sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "="
+ URLEncoder.encode(entry.getValue(), "UTF-8"));
// Set up URL
endpoint += "?" + sj.toString();
URL url = new URL(endpoint);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
StringBuilder result = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
result.append(inputLine);
}
http.disconnect();
return result.toString();
}
}