diff --git a/modules/default/weather/providers/darksky.js b/modules/default/weather/providers/darksky.js index 01bd79cd..04595d1a 100755 --- a/modules/default/weather/providers/darksky.js +++ b/modules/default/weather/providers/darksky.js @@ -18,6 +18,7 @@ WeatherProvider.register("darksky", { // Set the default config properties that is specific to this provider defaults: { + useCorsProxy: true, apiBase: "https://api.darksky.net", weatherEndpoint: "/forecast", apiKey: "", @@ -67,7 +68,7 @@ WeatherProvider.register("darksky", { // Create a URL from the config and base URL. getUrl() { const units = this.units[this.config.units] || "auto"; - return this.getCorsUrl() + `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`; + return `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`; }, // Implement WeatherDay generator. diff --git a/modules/default/weather/providers/envcanada.js b/modules/default/weather/providers/envcanada.js index 8cbf313c..e293edb4 100644 --- a/modules/default/weather/providers/envcanada.js +++ b/modules/default/weather/providers/envcanada.js @@ -40,6 +40,7 @@ WeatherProvider.register("envcanada", { // Set the default config properties that is specific to this provider defaults: { + useCorsProxy: true, siteCode: "s1234567", provCode: "ON" }, @@ -73,7 +74,7 @@ WeatherProvider.register("envcanada", { // Override the fetchCurrentWeather method to query EC and construct a Current weather object // fetchCurrentWeather() { - this.fetchData(this.getUrl(), "GET") + this.fetchData(this.getUrl(), "GET", "xml") .then((data) => { if (!data) { // Did not receive usable new data. @@ -93,7 +94,7 @@ WeatherProvider.register("envcanada", { // Override the fetchWeatherForecast method to query EC and construct Forecast weather objects // fetchWeatherForecast() { - this.fetchData(this.getUrl(), "GET") + this.fetchData(this.getUrl(), "GET", "xml") .then((data) => { if (!data) { // Did not receive usable new data. @@ -113,7 +114,7 @@ WeatherProvider.register("envcanada", { // Override the fetchWeatherHourly method to query EC and construct Forecast weather objects // fetchWeatherHourly() { - this.fetchData(this.getUrl(), "GET") + this.fetchData(this.getUrl(), "GET", "xml") .then((data) => { if (!data) { // Did not receive usable new data. @@ -129,26 +130,6 @@ WeatherProvider.register("envcanada", { .finally(() => this.updateAvailable()); }, - // - // Override fetchData function to handle XML document (base function assumes JSON) - // - fetchData: function (url, method = "GET", data = null) { - return new Promise(function (resolve, reject) { - const request = new XMLHttpRequest(); - request.open(method, url, true); - request.onreadystatechange = function () { - if (this.readyState === 4) { - if (this.status === 200) { - resolve(this.responseXML); - } else { - reject(request); - } - } - }; - request.send(); - }); - }, - ////////////////////////////////////////////////////////////////////////////////// // // Environment Canada methods - not part of the standard Provider methods @@ -160,11 +141,8 @@ WeatherProvider.register("envcanada", { // URL defaults to the Englsih version simply because there is no language dependancy in the data // being accessed. This is only pertinent when using the EC data elements that contain a textual forecast. // - // Also note that access is supported through a proxy service (thingproxy.freeboard.io) to mitigate - // CORS errors when accessing EC - // getUrl() { - return this.getCorsUrl() + "https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml"; + return "https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml"; }, // diff --git a/modules/default/weather/providers/openweathermap.js b/modules/default/weather/providers/openweathermap.js index 4d3238f6..c1258d3c 100755 --- a/modules/default/weather/providers/openweathermap.js +++ b/modules/default/weather/providers/openweathermap.js @@ -116,7 +116,7 @@ WeatherProvider.register("openweathermap", { * Gets the complete url for the request */ getUrl() { - return this.getCorsUrl() + this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams(); + return this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams(); }, /* diff --git a/modules/default/weather/providers/smhi.js b/modules/default/weather/providers/smhi.js index adc5f07a..603aa814 100644 --- a/modules/default/weather/providers/smhi.js +++ b/modules/default/weather/providers/smhi.js @@ -91,7 +91,7 @@ WeatherProvider.register("smhi", { getURL() { let lon = this.config.lon; let lat = this.config.lat; - return this.getCorsUrl() + `https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/${lon}/lat/${lat}/data.json`; + return `https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/${lon}/lat/${lat}/data.json`; }, /** diff --git a/modules/default/weather/providers/ukmetoffice.js b/modules/default/weather/providers/ukmetoffice.js index 195e9d2a..158592bd 100755 --- a/modules/default/weather/providers/ukmetoffice.js +++ b/modules/default/weather/providers/ukmetoffice.js @@ -73,7 +73,7 @@ WeatherProvider.register("ukmetoffice", { * Gets the complete url for the request */ getUrl(forecastType) { - return this.getCorsUrl() + this.config.apiBase + this.config.locationID + this.getParams(forecastType); + return this.config.apiBase + this.config.locationID + this.getParams(forecastType); }, /* diff --git a/modules/default/weather/providers/ukmetofficedatahub.js b/modules/default/weather/providers/ukmetofficedatahub.js index 86ede528..bab1220d 100644 --- a/modules/default/weather/providers/ukmetofficedatahub.js +++ b/modules/default/weather/providers/ukmetofficedatahub.js @@ -63,7 +63,7 @@ WeatherProvider.register("ukmetofficedatahub", { queryStrings += "&includeLocationName=" + true; // Return URL, making sure there is a trailing "/" in the base URL. - return this.getCorsUrl() + this.config.apiBase + (this.config.apiBase.endsWith("/") ? "" : "/") + forecastType + queryStrings; + return this.config.apiBase + (this.config.apiBase.endsWith("/") ? "" : "/") + forecastType + queryStrings; }, // Build the list of headers for the request diff --git a/modules/default/weather/providers/weatherbit.js b/modules/default/weather/providers/weatherbit.js index 9023f742..17f27812 100644 --- a/modules/default/weather/providers/weatherbit.js +++ b/modules/default/weather/providers/weatherbit.js @@ -72,7 +72,7 @@ WeatherProvider.register("weatherbit", { // Create a URL from the config and base URL. getUrl() { const units = this.units[this.config.units] || "auto"; - return this.getCorsUrl() + `${this.config.apiBase}${this.config.weatherEndpoint}?lat=${this.config.lat}&lon=${this.config.lon}&units=${units}&key=${this.config.apiKey}`; + return `${this.config.apiBase}${this.config.weatherEndpoint}?lat=${this.config.lat}&lon=${this.config.lon}&units=${units}&key=${this.config.apiKey}`; }, // Implement WeatherDay generator. diff --git a/modules/default/weather/providers/weathergov.js b/modules/default/weather/providers/weathergov.js index cf7912c3..31934784 100755 --- a/modules/default/weather/providers/weathergov.js +++ b/modules/default/weather/providers/weathergov.js @@ -40,7 +40,7 @@ WeatherProvider.register("weathergov", { // Called to set the config, this config is the same as the weather module's config. setConfig: function (config) { this.config = config; - (this.config.apiBase = this.getCorsUrl() + "https://api.weather.gov"), this.fetchWxGovURLs(this.config); + (this.config.apiBase = "https://api.weather.gov"), this.fetchWxGovURLs(this.config); }, // Called when the weather provider is about to start. diff --git a/modules/default/weather/weatherprovider.js b/modules/default/weather/weatherprovider.js index 4acfe47e..6533b472 100644 --- a/modules/default/weather/weatherprovider.js +++ b/modules/default/weather/weatherprovider.js @@ -112,20 +112,16 @@ const WeatherProvider = Class.extend({ }, getCorsUrl: function () { - if (this.config.mockData) { + if (this.config.mockData || typeof this.config.useCorsProxy === "undefined" || !this.config.useCorsProxy) { return ""; } else { - const url = window.config.address + ":" + window.config.port + "/cors?url="; - if (window.config.useHttps) { - return "https://" + url; - } else { - return "http://" + url; - } + return location.protocol + "//" + location.host + "/cors?url="; } }, // A convenience function to make requests. It returns a promise. - fetchData: function (url, method = "GET", data = null) { + fetchData: function (url, method = "GET", type = "json") { + url = this.getCorsUrl() + url; const getData = function (mockData) { return new Promise(function (resolve, reject) { if (mockData) { @@ -138,7 +134,11 @@ const WeatherProvider = Class.extend({ request.onreadystatechange = function () { if (this.readyState === 4) { if (this.status === 200) { - resolve(JSON.parse(this.response)); + if (type === "xml") { + resolve(this.responseXML); + } else { + resolve(JSON.parse(this.response)); + } } else { reject(request); }