Error if WeatherProvider API key is not provided
This commit is contained in:
@@ -6,14 +6,23 @@ import { WeatherProvider } from "./WeatherProvider";
|
|||||||
|
|
||||||
export default class DarkSkyWeatherProvider extends WeatherProvider {
|
export default class DarkSkyWeatherProvider extends WeatherProvider {
|
||||||
|
|
||||||
|
private readonly API_KEY: string;
|
||||||
|
|
||||||
|
public constructor() {
|
||||||
|
super();
|
||||||
|
this.API_KEY = process.env.DARKSKY_API_KEY;
|
||||||
|
if (!this.API_KEY) {
|
||||||
|
throw "DARKSKY_API_KEY environment variable is not defined.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
||||||
// The Unix timestamp of 24 hours ago.
|
// The Unix timestamp of 24 hours ago.
|
||||||
const yesterdayTimestamp: number = moment().subtract( 1, "day" ).unix();
|
const yesterdayTimestamp: number = moment().subtract( 1, "day" ).unix();
|
||||||
const todayTimestamp: number = moment().unix();
|
const todayTimestamp: number = moment().unix();
|
||||||
|
|
||||||
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
|
const yesterdayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`,
|
||||||
yesterdayUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`,
|
todayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ todayTimestamp }?exclude=currently,minutely,daily,alerts,flags`;
|
||||||
todayUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ todayTimestamp }?exclude=currently,minutely,daily,alerts,flags`;
|
|
||||||
|
|
||||||
let yesterdayData, todayData;
|
let yesterdayData, todayData;
|
||||||
try {
|
try {
|
||||||
@@ -62,8 +71,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
||||||
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
|
const forecastUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] }?exclude=minutely,alerts,flags`;
|
||||||
forecastUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] }?exclude=minutely,alerts,flags`;
|
|
||||||
|
|
||||||
let forecast;
|
let forecast;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -4,9 +4,18 @@ import { WeatherProvider } from "./WeatherProvider";
|
|||||||
|
|
||||||
export default class OWMWeatherProvider extends WeatherProvider {
|
export default class OWMWeatherProvider extends WeatherProvider {
|
||||||
|
|
||||||
|
private readonly API_KEY: string;
|
||||||
|
|
||||||
|
public constructor() {
|
||||||
|
super();
|
||||||
|
this.API_KEY = process.env.OWM_API_KEY;
|
||||||
|
if (!this.API_KEY) {
|
||||||
|
throw "OWM_API_KEY environment variable is not defined.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
||||||
const OWM_API_KEY = process.env.OWM_API_KEY,
|
const forecastUrl = `http://api.openweathermap.org/data/2.5/forecast?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`;
|
||||||
forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ];
|
|
||||||
|
|
||||||
// Perform the HTTP request to retrieve the weather data
|
// Perform the HTTP request to retrieve the weather data
|
||||||
let forecast;
|
let forecast;
|
||||||
@@ -43,9 +52,8 @@ export default class OWMWeatherProvider extends WeatherProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
||||||
const OWM_API_KEY = process.env.OWM_API_KEY,
|
const currentUrl = `http://api.openweathermap.org/data/2.5/weather?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`,
|
||||||
currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ],
|
forecastDailyUrl = `http://api.openweathermap.org/data/2.5/forecast/daily?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`;
|
||||||
forecastDailyUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ];
|
|
||||||
|
|
||||||
let current, forecast;
|
let current, forecast;
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user