mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
introduce useCorsProxy, per default only enabled for darksky and envcanada
This commit is contained in:
parent
9f9c17ea8a
commit
f04dd6b6cd
@ -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.
|
||||
|
@ -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";
|
||||
},
|
||||
|
||||
//
|
||||
|
@ -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();
|
||||
},
|
||||
|
||||
/*
|
||||
|
@ -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`;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
},
|
||||
|
||||
/*
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user