Merge pull request #2798 from khassel/cors

This commit is contained in:
Michael Teeuw
2022-01-28 11:22:43 +01:00
committed by GitHub
6 changed files with 53 additions and 31 deletions

View File

@@ -18,7 +18,8 @@ WeatherProvider.register("darksky", {
// Set the default config properties that is specific to this provider
defaults: {
apiBase: "https://cors-anywhere.herokuapp.com/https://api.darksky.net",
useCorsProxy: true,
apiBase: "https://api.darksky.net",
weatherEndpoint: "/forecast",
apiKey: "",
lat: 0,

View File

@@ -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 "https://thingproxy.freeboard.io/fetch/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";
},
//

View File

@@ -111,8 +111,17 @@ const WeatherProvider = Class.extend({
this.delegate.updateAvailable(this);
},
getCorsUrl: function () {
if (this.config.mockData || typeof this.config.useCorsProxy === "undefined" || !this.config.useCorsProxy) {
return "";
} else {
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) {
@@ -125,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);
}