init commit
This commit is contained in:
27
src/main/java/dev/clanders/weatherapi/Api.java
Normal file
27
src/main/java/dev/clanders/weatherapi/Api.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package dev.clanders.weatherapi;
|
||||
|
||||
import dev.clanders.weatherapi.request.SearchLocation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
public class Api {
|
||||
private static String API_KEY = "08Yqt7FJVrndj56jsasgceKdppJ4yu90";
|
||||
public static HttpResponse<String> getWeatherByLocation(SearchLocation searchLocation) throws IOException, InterruptedException {
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(String.format("https://api.tomorrow.io/v4/weather/forecast?location=%s %s&units=imperial&apikey=%s", searchLocation.city, searchLocation.state, API_KEY).replace(" ", "%20")))
|
||||
.header("accept", "application/json")
|
||||
.method("GET", HttpRequest.BodyPublishers.noBody())
|
||||
.build();
|
||||
|
||||
return HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
public static void setApiKey(String newApiKey) {
|
||||
API_KEY = newApiKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.clanders.weatherapi.request;
|
||||
|
||||
public class SearchLocation {
|
||||
public String city;
|
||||
public String state;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.clanders.weatherapi.request;
|
||||
|
||||
public class SearchLocationBuilder {
|
||||
public static SearchLocation createLocation(String locationStr) {
|
||||
SearchLocation searchLocation = new SearchLocation();
|
||||
String[] locationTokenized = locationStr.split(",");
|
||||
searchLocation.city = locationTokenized[0].trim();
|
||||
searchLocation.state = locationTokenized[1].trim(); // Refactor this to intake fullname or Abbrev.
|
||||
|
||||
return searchLocation;
|
||||
}
|
||||
}
|
||||
16
src/main/java/dev/clanders/weatherapi/response/Location.java
Normal file
16
src/main/java/dev/clanders/weatherapi/response/Location.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package dev.clanders.weatherapi.response;
|
||||
|
||||
public class Location {
|
||||
public double lat;
|
||||
public double lon;
|
||||
public String name;
|
||||
public String type;
|
||||
|
||||
public String getCountry() {
|
||||
// Probably not efficient to be splitting this string every time, but does
|
||||
// it really matter in an app this size????
|
||||
String[] locationValues = name.split(",");
|
||||
|
||||
return locationValues[locationValues.length - 1].trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.clanders.weatherapi.response;
|
||||
|
||||
public class TimeLineDailyValue {
|
||||
public double temperatureMax;
|
||||
public double temperatureMin;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.clanders.weatherapi.response;
|
||||
|
||||
public class TimelineDaily {
|
||||
public String time;
|
||||
public TimeLineDailyValue values;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.clanders.weatherapi.response;
|
||||
|
||||
public class TimelineHourly {
|
||||
public String time;
|
||||
public TimelineHourlyValue values;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dev.clanders.weatherapi.response;
|
||||
|
||||
public class TimelineHourlyValue {
|
||||
public double temperature;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.clanders.weatherapi.response;
|
||||
|
||||
public class Timelines {
|
||||
public TimelineDaily[] daily;
|
||||
public TimelineHourly[] hourly;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.clanders.weatherapi.response;
|
||||
|
||||
public class WeatherLocation {
|
||||
public Location location;
|
||||
public Timelines timelines;
|
||||
}
|
||||
9
src/main/java/dev/clanders/weatherapp/App.java
Normal file
9
src/main/java/dev/clanders/weatherapp/App.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package dev.clanders.weatherapp;
|
||||
|
||||
public class App {
|
||||
// For some reason main doesn't work within the Application class
|
||||
// So gotta put it here.
|
||||
public static void main(String[] args) {
|
||||
WeatherApp.run(args);
|
||||
}
|
||||
}
|
||||
27
src/main/java/dev/clanders/weatherapp/WeatherApp.java
Normal file
27
src/main/java/dev/clanders/weatherapp/WeatherApp.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package dev.clanders.weatherapp;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class WeatherApp extends Application {
|
||||
public static void run(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException {
|
||||
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/WeatherApp.fxml")));
|
||||
|
||||
Scene scene = new Scene(root, 600, 400);
|
||||
|
||||
stage.setTitle("FXML Welcome");
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package dev.clanders.weatherapp;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import dev.clanders.weatherapi.Api;
|
||||
import dev.clanders.weatherapi.request.SearchLocation;
|
||||
import dev.clanders.weatherapi.request.SearchLocationBuilder;
|
||||
import dev.clanders.weatherapi.response.WeatherLocation;
|
||||
import dev.clanders.weatherapp.error.ApiNotResponsiveException;
|
||||
import dev.clanders.weatherapp.error.InvalidCountryException;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.paint.Paint;
|
||||
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Objects;
|
||||
|
||||
public class WeatherAppController {
|
||||
@FXML
|
||||
private TextField locationTextField;
|
||||
@FXML
|
||||
private Label cityNameLabel;
|
||||
@FXML
|
||||
private Label locationLowLabel;
|
||||
@FXML
|
||||
private Label locationHighLabel;
|
||||
@FXML
|
||||
private Label locationCurrentLabel;
|
||||
@FXML
|
||||
private VBox forecastContainer;
|
||||
@FXML
|
||||
private TextField usersApiKeyTextField;
|
||||
@FXML
|
||||
private Label userApiKeyMessageLabel;
|
||||
|
||||
@FXML protected void handleLocationEnteredAction(ActionEvent event) {
|
||||
cityNameLabel.setTextFill(Color.BLACK);
|
||||
cityNameLabel.setStyle("-fx-font-size: 24;");
|
||||
|
||||
try {
|
||||
SearchLocation searchLocation = SearchLocationBuilder.createLocation(locationTextField.getText()); // This doesn't fail gracefully
|
||||
HttpResponse<String> res = Api.getWeatherByLocation(searchLocation);
|
||||
|
||||
Gson g = new Gson();
|
||||
WeatherLocation wLocation = g.fromJson(res.body(), WeatherLocation.class);
|
||||
|
||||
if (wLocation.location == null) {
|
||||
throw new ApiNotResponsiveException();
|
||||
}
|
||||
|
||||
if (!Objects.equals(wLocation.location.getCountry(), "United States")) {
|
||||
throw new InvalidCountryException();
|
||||
}
|
||||
|
||||
System.out.printf("Location: %s\n", wLocation.location.name);
|
||||
cityNameLabel.setText(wLocation.location.name);
|
||||
forecastContainer.setVisible(true);
|
||||
|
||||
locationLowLabel.setText(String.valueOf(wLocation.timelines.daily[0].values.temperatureMin));
|
||||
locationHighLabel.setText(String.valueOf(wLocation.timelines.daily[0].values.temperatureMax));
|
||||
locationCurrentLabel.setText((String.valueOf(wLocation.timelines.hourly[0].values.temperature)));
|
||||
} catch(ApiNotResponsiveException e) {
|
||||
cityNameLabel.setStyle("-fx-font-size: 12;");
|
||||
cityNameLabel.setTextFill(Color.RED);
|
||||
cityNameLabel.setText("API currently unresponsive or Invalid custom API Key. \nThis application uses the free tier of tomorrow.io.\n" +
|
||||
"Due to hourly limits of API calls, you will need to wait before making further requests. Or provide a new API Key below.");
|
||||
usersApiKeyTextField.setVisible(true);
|
||||
}
|
||||
catch (InvalidCountryException e) {
|
||||
cityNameLabel.setStyle("-fx-font-size: 12;");
|
||||
cityNameLabel.setTextFill(Color.RED);
|
||||
cityNameLabel.setText("City must be within United States.\n Please use format City, State without abbreviations.");
|
||||
}
|
||||
catch (Exception e) {
|
||||
cityNameLabel.setTextFill(Color.RED);
|
||||
cityNameLabel.setText("INVALID");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML protected void handleApiKeyEnteredAction(ActionEvent event) {
|
||||
Api.setApiKey(usersApiKeyTextField.getText());
|
||||
usersApiKeyTextField.setVisible(false);
|
||||
userApiKeyMessageLabel.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package dev.clanders.weatherapp.error;
|
||||
|
||||
public class ApiNotResponsiveException extends Exception {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dev.clanders.weatherapp.error;
|
||||
|
||||
public class InvalidCountryException extends Exception{
|
||||
|
||||
}
|
||||
66
src/main/resources/WeatherApp.fxml
Normal file
66
src/main/resources/WeatherApp.fxml
Normal file
@@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.scene.text.*?>
|
||||
|
||||
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dev.clanders.weatherapp.WeatherAppController">
|
||||
<children>
|
||||
<VBox prefWidth="100.0">
|
||||
<children>
|
||||
<Label text="Location">
|
||||
<font>
|
||||
<Font size="25.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<TextField fx:id="locationTextField" focusTraversable="false" onAction="#handleLocationEnteredAction" prefHeight="37.0" prefWidth="435.0" promptText="City, State" />
|
||||
</children>
|
||||
<VBox.margin>
|
||||
<Insets />
|
||||
</VBox.margin>
|
||||
</VBox>
|
||||
<Label fx:id="cityNameLabel" alignment="CENTER" contentDisplay="CENTER" maxHeight="300.0" maxWidth="400.0" prefHeight="150.0" textAlignment="CENTER" textOverrun="WORD_ELLIPSIS" wrapText="true">
|
||||
<font>
|
||||
<Font size="24.0" />
|
||||
</font>
|
||||
<padding>
|
||||
<Insets top="5.0" />
|
||||
</padding>
|
||||
</Label>
|
||||
<VBox fx:id="forecastContainer" alignment="CENTER" prefHeight="100.0" prefWidth="447.0" visible="false">
|
||||
<children>
|
||||
<Label text="Today's Forecast" />
|
||||
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
|
||||
<children>
|
||||
<VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0">
|
||||
<children>
|
||||
<Label text="High" />
|
||||
<Label fx:id="locationHighLabel" />
|
||||
</children></VBox>
|
||||
<VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0">
|
||||
<children>
|
||||
<Label text="Current" />
|
||||
<Label fx:id="locationCurrentLabel" />
|
||||
</children>
|
||||
</VBox>
|
||||
<VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0">
|
||||
<children>
|
||||
<Label text="Low" />
|
||||
<Label fx:id="locationLowLabel" />
|
||||
</children></VBox>
|
||||
</children>
|
||||
</HBox>
|
||||
</children>
|
||||
<VBox.margin>
|
||||
<Insets top="5.0" />
|
||||
</VBox.margin>
|
||||
</VBox>
|
||||
<Separator opacity="0.0" orientation="VERTICAL" prefHeight="200.0" />
|
||||
<TextField fx:id="usersApiKeyTextField" focusTraversable="false" onAction="#handleApiKeyEnteredAction" promptText="API KEY" visible="false" />
|
||||
<Label fx:id="userApiKeyMessageLabel" contentDisplay="CENTER" text="Using Custom API Key" visible="false" />
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
|
||||
</padding>
|
||||
</VBox>
|
||||
Reference in New Issue
Block a user