191 lines
5.3 KiB
Java
191 lines
5.3 KiB
Java
package netneutrality.http;
|
|
|
|
/*
|
|
* Copyright (c) anthorob
|
|
*
|
|
* https://discord.gg/qUa6zNFZsd
|
|
*/
|
|
|
|
import io.sentry.Breadcrumb;
|
|
import io.sentry.Sentry;
|
|
import netneutrality.NetNeutrality;
|
|
import netneutrality.core.LogHandler;
|
|
import okhttp3.*;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.TreeMap;
|
|
|
|
public class WebModule {
|
|
|
|
private final String url;
|
|
private final OkHttpClient client = new OkHttpClient();
|
|
private Response response;
|
|
//private final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 OPR/82.0.4227.43";
|
|
private static final String userAgent = String.format("Net Neutrality/%s (Discord bot, [email protected])", NetNeutrality.VERSION);
|
|
|
|
|
|
public WebModule(String url) {
|
|
this.url = url;
|
|
}
|
|
|
|
/**
|
|
* Send a request to the specified URL in the constructor.
|
|
* <br>Needs to be executed before any other method call
|
|
*
|
|
* @return an instance of the WebModule class with the Response of the Web Request
|
|
*/
|
|
public WebModule request() {
|
|
Sentry.addBreadcrumb(Breadcrumb.http(url, "get"));
|
|
Request request = new Request.Builder().url(this.url)
|
|
.addHeader("User-Agent", userAgent)
|
|
.build();
|
|
try {
|
|
response = client.newCall(request).execute();
|
|
Sentry.addBreadcrumb(Breadcrumb.http(url, "get", response.code()));
|
|
} catch (IOException e) {
|
|
Sentry.captureException(e);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public WebModule request(Map<String, String> headers) throws IOException {
|
|
Sentry.addBreadcrumb(Breadcrumb.http(url, "get"));
|
|
Request.Builder builder = new Request.Builder().url(this.url)
|
|
.addHeader("User-Agent", userAgent);
|
|
|
|
for (Map.Entry<String, String> header : headers.entrySet()) {
|
|
builder.addHeader(header.getKey(), header.getValue());
|
|
}
|
|
|
|
Request request = builder.build();
|
|
|
|
try {
|
|
response = client.newCall(request).execute();
|
|
Sentry.addBreadcrumb(Breadcrumb.http(url, "get", response.code()));
|
|
} catch (IOException e) {
|
|
Sentry.captureException(e);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public WebModule post(TreeMap<String, String> params, Map<String, String> headers) {
|
|
Sentry.addBreadcrumb(Breadcrumb.http(url, "post"));
|
|
RequestBody body = RequestBody.create(createJson(params), MediaType.parse("application/json; charset=utf-8"));
|
|
Request.Builder builder = new Request.Builder().url(this.url)
|
|
.addHeader("User-Agent", userAgent);
|
|
|
|
for (Map.Entry<String, String> header : headers.entrySet()) {
|
|
builder.addHeader(header.getKey(), header.getValue());
|
|
}
|
|
Request request = builder.post(body).build();
|
|
try {
|
|
response = client.newCall(request).execute();
|
|
Sentry.addBreadcrumb(Breadcrumb.http(url, "post", response.code()));
|
|
} catch (IOException e) {
|
|
Sentry.captureException(e);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public static String createJson(TreeMap<String, String> params) {
|
|
StringBuilder json = new StringBuilder();
|
|
|
|
for (Map.Entry<String, String> param : params.entrySet()) {
|
|
String str = "\"" + param.getKey() + "\":\"" + param.getValue() + "\"";
|
|
json.append(str);
|
|
json.append(",");
|
|
}
|
|
json.insert(0, "{");
|
|
|
|
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "}" : "");
|
|
}
|
|
|
|
public static String createJsonObject(List<String> key, List<String> value) {
|
|
if (key.size() != value.size())
|
|
throw new IllegalArgumentException("Key list size not equal to value list size");
|
|
|
|
StringBuilder json = new StringBuilder();
|
|
|
|
for (int index = 0; index < key.size();) {
|
|
json.append(String.format("\"%s\":\"%s\"", key.get(index), value.get(index)));
|
|
json.append(",");
|
|
}
|
|
json.insert(0, "{");
|
|
|
|
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "}" : "");
|
|
}
|
|
|
|
public static String createJsonArray(List<String> list) {
|
|
StringBuilder json = new StringBuilder();
|
|
|
|
for (String str : list) {
|
|
json.append("\"").append(str).append("\"");
|
|
json.append(",");
|
|
}
|
|
json.insert(0, "[");
|
|
|
|
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "]" : "");
|
|
}
|
|
|
|
public static String createJsonArray(TreeMap<String, String> users) {
|
|
StringBuilder json = new StringBuilder();
|
|
|
|
for (Map.Entry<String, String> param : users.entrySet()) {
|
|
String str = "{\"" + param.getKey() + "\":\"" + param.getValue() + "\"}";
|
|
json.append(str);
|
|
json.append(",");
|
|
}
|
|
json.insert(0, "[");
|
|
|
|
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "]" : "[]");
|
|
}
|
|
|
|
|
|
/**
|
|
* @return the response as a JSONObject
|
|
*/
|
|
public JSONObject getAsObject() {
|
|
if (response == null)
|
|
throw new RuntimeException("Request has not been sent");
|
|
|
|
try {
|
|
ResponseBody body = response.body();
|
|
return new JSONObject(body.string());
|
|
} catch (IOException | NullPointerException e) {
|
|
LogHandler.log(e);
|
|
return new JSONObject();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Use if your response start with an array : [ ]
|
|
*
|
|
* @return the response as a JSONArray
|
|
*/
|
|
public JSONArray getAsArray() throws JSONException, IOException {
|
|
if (response == null)
|
|
throw new RuntimeException("Request has not been sent");
|
|
|
|
return new JSONArray(response.body().string());
|
|
}
|
|
|
|
/**
|
|
* @return the response as a String
|
|
*/
|
|
public String getRawData() throws IOException {
|
|
if (response == null)
|
|
throw new RuntimeException("Request has not been sent");
|
|
|
|
return response.body().string();
|
|
}
|
|
|
|
}
|