add RequestFunctions class
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package io.github.jack1424.realtimeweather.requests;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class RequestFunctions {
|
||||
public static Object makeRequest(String URLString) throws IOException, HTTPResponseException, ParseException {
|
||||
int responseCode = getResponseCode(URLString);
|
||||
if (responseCode > 399)
|
||||
throw new HTTPResponseException(responseCode);
|
||||
|
||||
Scanner scanner = new Scanner(new URL(URLString).openStream());
|
||||
StringBuilder response = new StringBuilder();
|
||||
while (scanner.hasNextLine())
|
||||
response.append(scanner.nextLine());
|
||||
scanner.close();
|
||||
|
||||
return new JSONParser().parse(response.toString());
|
||||
}
|
||||
|
||||
public static int getResponseCode(String URLString) throws IOException {
|
||||
URL url = new URL(URLString);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.connect();
|
||||
return con.getResponseCode();
|
||||
}
|
||||
|
||||
public static String getLatestVersion() throws Exception {
|
||||
return ((JSONObject) ((JSONArray) makeRequest("https://api.modrinth.com/v2/project/WRA6ODcm/version")).get(0)).get("version_number").toString();
|
||||
}
|
||||
|
||||
public static class HTTPResponseException extends Exception {
|
||||
public HTTPResponseException(int responseCode) {
|
||||
super(String.valueOf(responseCode));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,14 +1,10 @@
|
||||
package io.github.jack1424.realtimeweather.requests;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.IOException;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
@@ -20,23 +16,13 @@ public class SunriseSunsetRequestObject {
|
||||
private String sunriseTime, sunsetTime;
|
||||
|
||||
public SunriseSunsetRequestObject(TimeZone timeZone, String lat, String lon) throws IOException, ParseException, ConfigurationException {
|
||||
URL url = new URL(String.format("https://api.sunrisesunset.io/json?lat=%s&lng=%s&timezone=UTC", lat, lon));
|
||||
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.connect();
|
||||
int responseCode = con.getResponseCode();
|
||||
if (responseCode > 399)
|
||||
throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")");
|
||||
|
||||
Scanner scanner = new Scanner(url.openStream());
|
||||
StringBuilder data = new StringBuilder();
|
||||
while (scanner.hasNext()) {
|
||||
data.append(scanner.nextLine());
|
||||
JSONObject response;
|
||||
try {
|
||||
response = (JSONObject) ((JSONObject) RequestFunctions.makeRequest(String.format("https://api.sunrisesunset.io/json?lat=%s&lng=%s&timezone=UTC", lat, lon))).get("results");
|
||||
} catch (RequestFunctions.HTTPResponseException e) {
|
||||
throw new IOException("Server/client error (HTTP error " + e.getMessage() + ")");
|
||||
}
|
||||
scanner.close();
|
||||
|
||||
JSONObject response = (JSONObject) ((JSONObject) new JSONParser().parse(data.toString())).get("results");
|
||||
sunriseTime = response.get("sunrise").toString();
|
||||
sunsetTime = response.get("sunset").toString();
|
||||
|
||||
|
@@ -2,46 +2,34 @@ package io.github.jack1424.realtimeweather.requests;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.IOException;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class WeatherRequestObject {
|
||||
private boolean rain = false, thunder = false;
|
||||
|
||||
public WeatherRequestObject(String apiKey, String lat, String lon) throws IOException, ParseException, ConfigurationException {
|
||||
URL url = new URL(String.format("https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s", lat, lon, apiKey));
|
||||
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.connect();
|
||||
int responseCode = con.getResponseCode();
|
||||
JSONArray conditions;
|
||||
try {
|
||||
conditions = (JSONArray) ((JSONObject) RequestFunctions.makeRequest(String.format("https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s", lat, lon, apiKey))).get("weather");
|
||||
} catch (RequestFunctions.HTTPResponseException e) {
|
||||
int responseCode = Integer.parseInt(e.getMessage());
|
||||
if (responseCode > 499) {
|
||||
throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")");
|
||||
}
|
||||
else if (responseCode > 399) {
|
||||
} else if (responseCode > 399) {
|
||||
String message = "Error when getting weather information: ";
|
||||
|
||||
if (responseCode == 401)
|
||||
throw new ConfigurationException(message + "API key invalid. Check the Wiki for troubleshooting steps.");
|
||||
else
|
||||
throw new ProtocolException(message + "Unknown error");
|
||||
} else {
|
||||
throw new IOException("Server/client error (HTTP error " + e.getMessage() + ")");
|
||||
}
|
||||
|
||||
Scanner scanner = new Scanner(url.openStream());
|
||||
StringBuilder data = new StringBuilder();
|
||||
while (scanner.hasNext()) {
|
||||
data.append(scanner.nextLine());
|
||||
}
|
||||
scanner.close();
|
||||
|
||||
JSONArray conditions = (JSONArray) ((JSONObject) new JSONParser().parse(data.toString())).get("weather");
|
||||
|
||||
for (Object rawCondition : conditions) {
|
||||
int id = Integer.parseInt(String.valueOf(((JSONObject) rawCondition).get("id")));
|
||||
|
Reference in New Issue
Block a user