2022-01-17 13:54:11 +00:00
|
|
|
package fi.vanced.utils.requests;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
public class Requester {
|
|
|
|
private Requester() {}
|
|
|
|
|
|
|
|
public static HttpURLConnection getConnectionFromRoute(String apiUrl, Route route, String... params) throws IOException {
|
|
|
|
String url = apiUrl + route.compile(params).getCompiledRoute();
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
|
connection.setRequestMethod(route.getMethod().name());
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String parseJson(HttpURLConnection connection) throws IOException {
|
2022-01-24 14:14:56 +00:00
|
|
|
return parseJson(connection.getInputStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String parseJson(InputStream inputStream) throws IOException {
|
2022-01-17 13:54:11 +00:00
|
|
|
StringBuilder jsonBuilder = new StringBuilder();
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
2022-01-24 14:14:56 +00:00
|
|
|
jsonBuilder.append(line).append("\n");
|
2022-01-17 13:54:11 +00:00
|
|
|
}
|
|
|
|
inputStream.close();
|
|
|
|
return jsonBuilder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONObject getJSONObject(HttpURLConnection connection) throws Exception {
|
|
|
|
return new JSONObject(parseJsonAndDisconnect(connection));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONArray getJSONArray(HttpURLConnection connection) throws Exception {
|
|
|
|
return new JSONArray(parseJsonAndDisconnect(connection));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String parseJsonAndDisconnect(HttpURLConnection connection) throws IOException {
|
|
|
|
String json = parseJson(connection);
|
|
|
|
connection.disconnect();
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
}
|