Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forecast information to library #20

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import me.adamcraftmaster.enums.SpeedScaleEnum;
import me.adamcraftmaster.enums.TempScaleEnum;
import me.adamcraftmaster.exceptions.JSONGetException;
import me.adamcraftmaster.schema.currentweather.CurrentWeather;
import me.adamcraftmaster.mapper.currentweather.CurrentWeather;
import me.adamcraftmaster.utils.JSONParserUtil;

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public CurrentWeatherLib(String apiKey, String localJson) {
}

/**
* Automatically deserialize the current.json using given API key and region
* Automatically convert to object the current.json using given API key and region
*
* @param region the region, can be given as US Zipcode, UK Postcode, Canada Postalcode, IP
* address, Latitude/Longitude (decimal degree) or city name
Expand Down
197 changes: 197 additions & 0 deletions WeatherAPIcomLib/src/main/java/me/adamcraftmaster/ForecastLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package me.adamcraftmaster;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import me.adamcraftmaster.enums.TempScaleEnum;
import me.adamcraftmaster.exceptions.JSONGetException;
import me.adamcraftmaster.mapper.currentforecast.CurrentForecast;
import me.adamcraftmaster.utils.JSONParserUtil;

/**
* A class that handles all the current weather information.
*
* <p>this class uses the current.json from weatherapi.com.
*/
public class ForecastLib {

private final String apiKey;
private final int daysInFuture;
private final String localJson;

/**
* Creates a new ForecastLib.
*
* @param apiKey a valid API key from weatherapi.com
* @param daysInFuture how many days in the future to check, between 1 and 10 days.
*/
public ForecastLib(String apiKey, int daysInFuture) throws IOException {
this.apiKey = apiKey;
if (daysInFuture > 10 || daysInFuture < 1) {
throw new IOException("daysInFuture range is invalid");
}
this.daysInFuture = daysInFuture;
this.localJson = "null";
}

/**
* Creates a new ForecastLib with an existing JSON file for information.
*
* <p>This constructor is primarily used for testing, as it takes advantage of local given, and
* potentially outdated data.
*
* @param apiKey a valid API key from weatherapi.com
* @param localJson a json as a String containing the current weather data from a forecast.json
* from weatherapi.com
*/
public ForecastLib(String apiKey, int daysInFuture, String localJson) {
this.apiKey = apiKey;
this.daysInFuture = daysInFuture;
this.localJson = localJson;
}

/**
* Automatically convert to object the forecast.json using given API key and region
*
* @param region the region, can be given as US Zipcode, UK Postcode, Canada Postalcode, IP
* address, Latitude/Longitude (decimal degree) or city name
* @return a deserialized JSON inside the CurrentWeather object
* @throws JsonProcessingException something went wrong with mapping the JSON from weatherapi.com
* @throws JSONGetException something went wrong with getting the JSON from weatherapi.com
*/
private CurrentForecast forecastInfoDataSource(String region)
throws JsonProcessingException, JSONGetException {
ObjectMapper objectMapper = new ObjectMapper();
if (localJson.equals("null")) {
return objectMapper.readValue(
JSONParserUtil.urlToJson(
"https://api.weatherapi.com/v1/forecast.json?key="
+ apiKey
+ "&q="
+ region
+ "&days="
+ daysInFuture
+ "&aqi=no"
+ "&alerts=no"),
CurrentForecast.class);
} else {
return objectMapper.readValue(localJson, CurrentForecast.class);
}
}

/**
* Gets the current predicted average current weather conditions for that day
*
* @param region the region, can be given as US Zipcode, UK Postcode, Canada Postalcode, IP
* address, Latitude/Longitude (decimal degree) or city name
* @param day which day in the future to check, where 0 is today, 1 is tomorrow, and so on.
* @return the weather condition as a String.
* @throws JSONGetException something went wrong with getting the JSON from weatherapi.com
* @throws JsonProcessingException something went wrong with mapping the JSON from weatherapi.com
*/
public final String getWeatherConditions(String region, int day)
throws JSONGetException, JsonProcessingException {
return forecastInfoDataSource(region)
.getForecast()
.getForecastday()
.get(day)
.getDay()
.getCondition()
.getText();
}

/**
* Gets the current predicted average weather conditions for that day, and that hour.
*
* @param region the region, can be given as US Zipcode, UK Postcode, Canada Postalcode, IP
* address, Latitude/Longitude (decimal degree) or city name
* @param day which day in the future to check, where 0 is today, 1 is tomorrow, and so on.
* @param hour which hour in the future to check, from 0 to 23.
* @return the weather condition as a String.
* @throws JSONGetException something went wrong with getting the JSON from weatherapi.com
* @throws JsonProcessingException something went wrong with mapping the JSON from weatherapi.com
*/
public final String getWeatherConditions(String region, int day, int hour)
throws JSONGetException, JsonProcessingException {
return forecastInfoDataSource(region)
.getForecast()
.getForecastday()
.get(day)
.getHour()
.get(hour)
.getCondition()
.getText();
}

/**
* Gets the current predicted average temperature for that day.
*
* @param region the region, can be given as US Zipcode, UK Postcode, Canada Postalcode, IP
* address, Latitude/Longitude (decimal degree) or city name
* @param day which day in the future to check, where 0 is today, 1 is tomorrow, and so on.
* @param tempScale the scale of temperature, use the TempScaleEnum enum for temperature scale,
* defaults to Celsius if invalid tempScale given
* @return the average temperature of that day as a double in either Fahrenheit or Celsius, based
* on the tempScale
* @throws JSONGetException something went wrong with getting the JSON from weatherapi.com
* @throws JsonProcessingException something went wrong with mapping the JSON from weatherapi.com
*/
public final double getAverageTemperature(String region, int day, TempScaleEnum tempScale)
throws JSONGetException, JsonProcessingException {
switch (tempScale) {
case FARENHEIGHT:
return forecastInfoDataSource(region)
.getForecast()
.getForecastday()
.get(day)
.getDay()
.getAvgtempF();
case CELCIUS:
default:
// defaults to Celsius if no valid tempScale given
return forecastInfoDataSource(region)
.getForecast()
.getForecastday()
.get(day)
.getDay()
.getAvgtempC();
}
}

/**
* Gets the current predicted temperature for that hour, of that day.
*
* @param region the region, can be given as US Zipcode, UK Postcode, Canada Postalcode, IP
* address, Latitude/Longitude (decimal degree) or city name
* @param day which day in the future to check, where 0 is today, 1 is tomorrow, and so on.
* @param hour which hour in the future to check, from 0 to 23.
* @param tempScale the scale of temperature, use the TempScaleEnum enum for temperature scale,
* defaults to Celsius if invalid tempScale given
* @return the temperature of that hour as a double in either Fahrenheit or Celsius, based on the
* tempScale
* @throws JSONGetException something went wrong with getting the JSON from weatherapi.com
* @throws JsonProcessingException something went wrong with mapping the JSON from weatherapi.com
*/
public final double getTemperature(String region, int day, int hour, TempScaleEnum tempScale)
throws JSONGetException, JsonProcessingException {
switch (tempScale) {
case FARENHEIGHT:
return forecastInfoDataSource(region)
.getForecast()
.getForecastday()
.get(day)
.getHour()
.get(hour)
.getTempF();
case CELCIUS:
default:
return forecastInfoDataSource(region)
.getForecast()
.getForecastday()
.get(day)
.getHour()
.get(hour)
.getTempC();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import me.adamcraftmaster.exceptions.JSONGetException;
import me.adamcraftmaster.schema.currentweather.CurrentWeather;
import me.adamcraftmaster.mapper.currentweather.CurrentWeather;
import me.adamcraftmaster.utils.JSONParserUtil;

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ public LocationInfoLib(String apiKey, String localJson) {
}

/**
* Automatically deserialize the current.json using given API key and region.
* Automatically convert to object the current.json using given API key and region.
*
* @param region the region, can be given as US Zipcode, UK Postcode, Canada Postalcode, IP
* address, Latitude/Longitude (decimal degree) or city name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@

package me.adamcraftmaster.mapper.currentforecast;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"sunrise",
"sunset",
"moonrise",
"moonset",
"moon_phase",
"moon_illumination"
})
@Generated("jsonschema2pojo")
public class Astro {

@JsonProperty("sunrise")
private String sunrise;
@JsonProperty("sunset")
private String sunset;
@JsonProperty("moonrise")
private String moonrise;
@JsonProperty("moonset")
private String moonset;
@JsonProperty("moon_phase")
private String moonPhase;
@JsonProperty("moon_illumination")
private String moonIllumination;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("sunrise")
public String getSunrise() {
return sunrise;
}

@JsonProperty("sunrise")
public void setSunrise(String sunrise) {
this.sunrise = sunrise;
}

public Astro withSunrise(String sunrise) {
this.sunrise = sunrise;
return this;
}

@JsonProperty("sunset")
public String getSunset() {
return sunset;
}

@JsonProperty("sunset")
public void setSunset(String sunset) {
this.sunset = sunset;
}

public Astro withSunset(String sunset) {
this.sunset = sunset;
return this;
}

@JsonProperty("moonrise")
public String getMoonrise() {
return moonrise;
}

@JsonProperty("moonrise")
public void setMoonrise(String moonrise) {
this.moonrise = moonrise;
}

public Astro withMoonrise(String moonrise) {
this.moonrise = moonrise;
return this;
}

@JsonProperty("moonset")
public String getMoonset() {
return moonset;
}

@JsonProperty("moonset")
public void setMoonset(String moonset) {
this.moonset = moonset;
}

public Astro withMoonset(String moonset) {
this.moonset = moonset;
return this;
}

@JsonProperty("moon_phase")
public String getMoonPhase() {
return moonPhase;
}

@JsonProperty("moon_phase")
public void setMoonPhase(String moonPhase) {
this.moonPhase = moonPhase;
}

public Astro withMoonPhase(String moonPhase) {
this.moonPhase = moonPhase;
return this;
}

@JsonProperty("moon_illumination")
public String getMoonIllumination() {
return moonIllumination;
}

@JsonProperty("moon_illumination")
public void setMoonIllumination(String moonIllumination) {
this.moonIllumination = moonIllumination;
}

public Astro withMoonIllumination(String moonIllumination) {
this.moonIllumination = moonIllumination;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public Astro withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}

}
Loading