From 2a1d30326e890ad780099f41fc9918e5e9e20252 Mon Sep 17 00:00:00 2001 From: Ellerbach Date: Tue, 27 Jun 2017 14:13:29 +0200 Subject: [PATCH] Added history support, Added samples for history request --- .../Models/DailySummary.cs | 149 ++++++++++++++++++ .../Models/History.cs | 16 ++ .../Models/HistoryDate.cs | 19 +++ .../Models/Observation.cs | 73 +++++++++ .../Models/QueryOptions.cs | 11 +- .../ResultModels/HistoryResponse.cs | 8 + .../Utilities/UrlBuilder.cs | 6 +- .../WeatherClient.cs | 12 ++ Sample/Program.cs | 13 +- 9 files changed, 302 insertions(+), 5 deletions(-) create mode 100644 CreativeGurus.Weather.Wunderground/Models/DailySummary.cs create mode 100644 CreativeGurus.Weather.Wunderground/Models/History.cs create mode 100644 CreativeGurus.Weather.Wunderground/Models/HistoryDate.cs create mode 100644 CreativeGurus.Weather.Wunderground/Models/Observation.cs create mode 100644 CreativeGurus.Weather.Wunderground/ResultModels/HistoryResponse.cs diff --git a/CreativeGurus.Weather.Wunderground/Models/DailySummary.cs b/CreativeGurus.Weather.Wunderground/Models/DailySummary.cs new file mode 100644 index 0000000..3c1999b --- /dev/null +++ b/CreativeGurus.Weather.Wunderground/Models/DailySummary.cs @@ -0,0 +1,149 @@ +using Newtonsoft.Json; + +namespace CreativeGurus.Weather.Wunderground.Models +{ + public class DailySummary + { + [JsonProperty("date")] + public Date Date { get; set; } + [JsonProperty("fog")] + public bool Fog { get; set; } + [JsonProperty("rain")] + public bool Rain { get; set; } + [JsonProperty("snow")] + public bool Snow { get; set; } + [JsonProperty("snowfallm")] + public float? SnowFallMetric { get; set; } + [JsonProperty("snowfalli")] + public float? SnowFallImperial { get; set; } + [JsonProperty("monthtodatesnowfallm")] + public float? MonthToDateSnowFallMetric { get; set; } + [JsonProperty("monthtodatesnowfalli")] + public float? MonthToDateSnowFallImperial { get; set; } + [JsonProperty("since1julsnowfallm")] + public float? Since1JulSnowFallMetric { get; set; } + [JsonProperty("since1julsnowfalli")] + public float? Since1JulSnowFallImperial { get; set; } + [JsonProperty("snowdepthm")] + public float? SnowDepthMetric { get; set; } + [JsonProperty("snowdepthi")] + public float? SnowDepthImperial { get; set; } + [JsonProperty("hail")] + public bool Hail { get; set; } + [JsonProperty("thunder")] + public bool Thunder { get; set; } + [JsonProperty("tornado")] + public bool Tornado { get; set; } + [JsonProperty("meantempm")] + public float? MeanTempMetric { get; set; } + [JsonProperty("meantempi")] + public float? MeanTempImperial { get; set; } + [JsonProperty("meandewptm")] + public float? MeanDewPointMetric { get; set; } + [JsonProperty("meandewpti")] + public float? MeanDewPopintImperial { get; set; } + [JsonProperty("meanpressurem")] + public float? MeanPressureMetric { get; set; } + [JsonProperty("meanpressurei")] + public float? MeanPressureImperial { get; set; } + [JsonProperty("meanwindspdm")] + public float? MeanWindSpeedMetric { get; set; } + [JsonProperty("meanwindspdi")] + public float? MeanWindSpeedImperial { get; set; } + [JsonProperty("meanwdire")] + public string MeanWindDirection { get; set; } + [JsonProperty("meanwdird")] + public int? MeanWindDirectionDegree { get; set; } + [JsonProperty("meanvism")] + public float? MeanVisibilityMetric { get; set; } + [JsonProperty("meanvisi")] + public float? MeanVisibilityImperial { get; set; } + [JsonProperty("humidity")] + public int? Humidity { get; set; } + [JsonProperty("maxtempm")] + public float? MaxTempMetric { get; set; } + [JsonProperty("maxtempi")] + public float? MaxTempImperial { get; set; } + [JsonProperty("mintempm")] + public float? MinTempMetric { get; set; } + [JsonProperty("mintempi")] + public float? MinTempImperial { get; set; } + [JsonProperty("maxhumidity")] + public int? MaxHumidity { get; set; } + [JsonProperty("minhumidity")] + public int? MinHumidity { get; set; } + [JsonProperty("maxdewptm")] + public float? MaxDewPointMetric { get; set; } + [JsonProperty("maxdewpti")] + public float? MaxDewPointImperial { get; set; } + [JsonProperty("mindewptm")] + public float? MinDewPointMetric { get; set; } + [JsonProperty("mindewpti")] + public float? MinDewPointImperial { get; set; } + [JsonProperty("maxpressurem")] + public float? MaxPressureMetric { get; set; } + [JsonProperty("maxpressurei")] + public float? MaxPressureImperial { get; set; } + [JsonProperty("minpressurem")] + public float? MinPressureMetric { get; set; } + [JsonProperty("minpressurei")] + public float? MinPressureImperial { get; set; } + [JsonProperty("maxwspdm")] + public float? MaxWindSpeedMetric { get; set; } + [JsonProperty("maxwspdi")] + public float? MaxWindSpeedImperial { get; set; } + [JsonProperty("minwspdm")] + public float? MinWindSpeedMetric { get; set; } + [JsonProperty("minwspdi")] + public float? MinWindSpeedImperial { get; set; } + [JsonProperty("maxvism")] + public float? MaxVisibilityMetric { get; set; } + [JsonProperty("maxvisi")] + public float? MaxVisibilityImperial { get; set; } + [JsonProperty("minvism")] + public float? MinVisibilityMetric { get; set; } + [JsonProperty("minvisi")] + public float? MinVisibilityImperial { get; set; } + [JsonProperty("gdegreedays")] + public int? GrowingDegreeDays { get; set; } + [JsonProperty("heatingdegreedays")] + public int? HeatingDegreeDays { get; set; } + [JsonProperty("coolingdegreedays")] + public int? CoolingDegreeDays { get; set; } + [JsonProperty("precipm")] + public float? PrecipitationMetric { get; set; } + [JsonProperty("precipi")] + public float? PrecipitationImperial { get; set; } + [JsonProperty("precipsource")] + public string PrecipitationSource { get; set; } + [JsonProperty("heatingdegreedaysnormal")] + public int? HeatingDegreeDaysNormal { get; set; } + [JsonProperty("monthtodateheatingdegreedays")] + public int? MonthToDateHeatingDegreeDays { get; set; } + [JsonProperty("monthtodateheatingdegreedaysnormal")] + public int? MonthToDateHeatingDegreeDaysNormal { get; set; } + [JsonProperty("since1sepheatingdegreedays")] + public int? Since1SepHeatingDegreeDays { get; set; } + [JsonProperty("since1sepheatingdegreedaysnormal")] + public int? Since1SepHeatingDegreeDaysNormal { get; set; } + [JsonProperty("since1julheatingdegreedays")] + public int? Since1JulHeatingDegreeDays { get; set; } + [JsonProperty("since1julheatingdegreedaysnormal")] + public int? Since1JulHeatingDegreeDaysNormal { get; set; } + [JsonProperty("coolingdegreedaysnormal")] + public int? CoolingDegreeDaysNormal { get; set; } + [JsonProperty("monthtodatecoolingdegreedays")] + public int? MonthToDateCoolingDegreeDays { get; set; } + [JsonProperty("monthtodatecoolingdegreedaysnormal")] + public int? MonthToDateCoolingDegreeDaysNormal { get; set; } + [JsonProperty("since1sepcoolingdegreedays")] + public int? Since1SepCoolingDegreeDays { get; set; } + [JsonProperty("since1sepcoolingdegreedaysnormal")] + public int? Since1SepCoolingDegreeDaysNormal { get; set; } + [JsonProperty("since1jancoolingdegreedays")] + public int? Since1JanCoolingDegreeDays { get; set; } + [JsonProperty("since1jancoolingdegreedaysnormal")] + public int? Since1JanCoolingDegreeDaysNormal { get; set; } + + } +} diff --git a/CreativeGurus.Weather.Wunderground/Models/History.cs b/CreativeGurus.Weather.Wunderground/Models/History.cs new file mode 100644 index 0000000..84cd2fa --- /dev/null +++ b/CreativeGurus.Weather.Wunderground/Models/History.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace CreativeGurus.Weather.Wunderground.Models +{ + public class History + { + [JsonProperty("date")] + public Date Date { get; set; } + [JsonProperty("utcdate")] + public Date UTCDATE { get; set; } + [JsonProperty("observations")] + public Observation[] Observations { get; set; } + [JsonProperty("dailysummary")] + public DailySummary[] DailySummaries { get; set; } + } +} diff --git a/CreativeGurus.Weather.Wunderground/Models/HistoryDate.cs b/CreativeGurus.Weather.Wunderground/Models/HistoryDate.cs new file mode 100644 index 0000000..d1ee299 --- /dev/null +++ b/CreativeGurus.Weather.Wunderground/Models/HistoryDate.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CreativeGurus.Weather.Wunderground.Models +{ + public class HistoryDate + { + public HistoryDate(int Year, int Month, int Day) + { + this.Year = Year; + this.Month = Month; + this.Day = Day; + } + public int Day { get; set; } + public int Year { get; set; } + public int Month { get; set; } + } +} diff --git a/CreativeGurus.Weather.Wunderground/Models/Observation.cs b/CreativeGurus.Weather.Wunderground/Models/Observation.cs new file mode 100644 index 0000000..d7cb97d --- /dev/null +++ b/CreativeGurus.Weather.Wunderground/Models/Observation.cs @@ -0,0 +1,73 @@ +using Newtonsoft.Json; + +namespace CreativeGurus.Weather.Wunderground.Models +{ + public class Observation + { + [JsonProperty("date")] + public Date Date { get; set; } + [JsonProperty("utcdate")] + public Date UTCDATE { get; set; } + [JsonProperty("tempm")] + public float? TempCelcius { get; set; } + [JsonProperty("tempi")] + public float? TempFahrenheit { get; set; } + [JsonProperty("dewptm")] + public float? DewPointMetric { get; set; } + [JsonProperty("dewpti")] + public float? DewPointImparial { get; set; } + [JsonProperty("hum")] + public int? Humidity { get; set; } + [JsonProperty("wspdm")] + public float? WindSpeedMetric { get; set; } + [JsonProperty("wspdi")] + public float? WindSpeedImperial { get; set; } + [JsonProperty("wgustm")] + public float? WindGustMetric { get; set; } + [JsonProperty("wgusti")] + public float? WindGurstImperial { get; set; } + [JsonProperty("wdird")] + public int? WindDirectionDegree { get; set; } + [JsonProperty("wdire")] + public string WindDirection { get; set; } + [JsonProperty("vism")] + public float? VisibilityMetric { get; set; } + [JsonProperty("visi")] + public float? VisibilityImperial { get; set; } + [JsonProperty("pressurem")] + public float? PressureMetric { get; set; } + [JsonProperty("pressurei")] + public float? PressureImperial { get; set; } + [JsonProperty("windchillm")] + public float? WindChillMetric { get; set; } + [JsonProperty("windchilli")] + public float? WindChillImperial { get; set; } + [JsonProperty("heatindexm")] + public float? HeatIndexMetric { get; set; } + [JsonProperty("heatindexi")] + public float? HeatIndexImperial { get; set; } + [JsonProperty("precipm")] + public float? PrecipitationMetric { get; set; } + [JsonProperty("precipi")] + public float? PrecipitationImperial { get; set; } + [JsonProperty("conds")] + public string Conditions { get; set; } + [JsonProperty("icon")] + public string Icon { get; set; } + [JsonProperty("fog")] + public bool Fog { get; set; } + [JsonProperty("rain")] + public bool Rain { get; set; } + [JsonProperty("snow")] + public bool Snow { get; set; } + [JsonProperty("hail")] + public bool Hail { get; set; } + [JsonProperty("thunder")] + public bool Thunder { get; set; } + [JsonProperty("tornado")] + public bool Tornado { get; set; } + [JsonProperty("metar")] + public string Metar { get; set; } + + } +} diff --git a/CreativeGurus.Weather.Wunderground/Models/QueryOptions.cs b/CreativeGurus.Weather.Wunderground/Models/QueryOptions.cs index e037998..c6fbae8 100644 --- a/CreativeGurus.Weather.Wunderground/Models/QueryOptions.cs +++ b/CreativeGurus.Weather.Wunderground/Models/QueryOptions.cs @@ -1,4 +1,5 @@ -using System; +using CreativeGurus.Weather.Wunderground.Models; +using System; namespace CreativeGurus.Weather.Wunderground { public class QueryOptions @@ -87,5 +88,11 @@ public QueryOptions() /// /// The PWSI. public string PWSId { get; set; } - } + + /// + /// Date for the history request + /// + /// The date + public HistoryDate HistoryDate { get; set; } + } } \ No newline at end of file diff --git a/CreativeGurus.Weather.Wunderground/ResultModels/HistoryResponse.cs b/CreativeGurus.Weather.Wunderground/ResultModels/HistoryResponse.cs new file mode 100644 index 0000000..17174f4 --- /dev/null +++ b/CreativeGurus.Weather.Wunderground/ResultModels/HistoryResponse.cs @@ -0,0 +1,8 @@ +namespace CreativeGurus.Weather.Wunderground.Models +{ + public class HistoryResponse + { + public Models.History History { get; set; } + public Models.Response Response { get; set; } + } +} diff --git a/CreativeGurus.Weather.Wunderground/Utilities/UrlBuilder.cs b/CreativeGurus.Weather.Wunderground/Utilities/UrlBuilder.cs index 7d180af..6463be2 100644 --- a/CreativeGurus.Weather.Wunderground/Utilities/UrlBuilder.cs +++ b/CreativeGurus.Weather.Wunderground/Utilities/UrlBuilder.cs @@ -10,8 +10,10 @@ internal static class UrlBuilder internal static string BuildUrl(string baseUrl, string apiKey, Feature feature, QueryType queryType, QueryOptions options) { StringBuilder sb = new StringBuilder(); - - sb.AppendFormat("{0}/{1}/{2}", baseUrl, apiKey, feature.ToString().ToLower()); + if (feature != Feature.History) + sb.AppendFormat("{0}/{1}/{2}", baseUrl, apiKey, feature.ToString().ToLower()); + else + sb.AppendFormat("{0}/{1}/{2}", baseUrl, apiKey, feature.ToString().ToLower() + "_" + options.HistoryDate.Year.ToString() + options.HistoryDate.Month.ToString() + options.HistoryDate.Day.ToString()); if (!string.IsNullOrWhiteSpace(options?.Language)) { sb.AppendFormat("/lang:{0}", options?.Language); } //Language if (options?.UsePWS == true) { sb.AppendFormat("/pws:{0}", options?.UsePWS); } // Use Personal weather station diff --git a/CreativeGurus.Weather.Wunderground/WeatherClient.cs b/CreativeGurus.Weather.Wunderground/WeatherClient.cs index a696c9e..4186288 100644 --- a/CreativeGurus.Weather.Wunderground/WeatherClient.cs +++ b/CreativeGurus.Weather.Wunderground/WeatherClient.cs @@ -74,6 +74,18 @@ public async Task GetForecastAsync(QueryType queryType, QueryO return await forecast.GetDataAsync(Feature.Forecast, queryType, options).ConfigureAwait(false); } + public HistoryResponse GetHistory(QueryType queryType, QueryOptions options = null) + { + Service history = new Service(_apiKey, _baseUrl); + return history.GetData(Feature.History, queryType, options); + } + + public async Task GetHistoryAsync(QueryType queryType, QueryOptions options = null) + { + Service history = new Service(_apiKey, _baseUrl); + return await history.GetDataAsync(Feature.Forecast, queryType, options).ConfigureAwait(false); + } + public GeoLookupResponse GetGeoLookup(QueryType queryType, QueryOptions options = null) { Service forecast = new Service(_apiKey, _baseUrl); diff --git a/Sample/Program.cs b/Sample/Program.cs index c8a8127..d41c0c2 100644 --- a/Sample/Program.cs +++ b/Sample/Program.cs @@ -19,7 +19,7 @@ private static async Task Examples() //List of supported languages //https://www.wunderground.com/weather/api/d/docs?d=language-support&MR=1 - string key = "{Your API Key Here}"; + string key = "{Insert your key here}"; WeatherClient client = new WeatherClient(key); @@ -49,6 +49,17 @@ private static async Task Examples() //var f7 = client.GetForecast(QueryType.USCity, new QueryOptions() { City = "Dallas", State = "TX" }); //Gets data for a US City //var f8 = client.GetForecast(QueryType.ZipCode, new QueryOptions() { ZipCode = "94107" }); //Gets data by US Zip Code + //Sample History + //var h1 = client.GetHistory(QueryType.AirportCode, new QueryOptions() { AirportCode = "DFW", HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data by Airport Code + //var h2 = client.GetHistory(QueryType.AutoIp, new QueryOptions() { HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data by the detected IP address of the calling client + //var h3 = client.GetHistory(QueryType.GlobalCity, new QueryOptions() { Country = "France", City = "Paris", HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data by country and city + //var h4 = client.GetHistory(QueryType.GPS, new QueryOptions() { Latitude = "37.776289", Longitude = "-122.395234", HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data by GPS coordinates + //var h5 = client.GetHistory(QueryType.LinkId, new QueryOptions() { LinkId = "00000.1.16172", HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data by Weather Underground Link ID + //var h6 = client.GetHistory(QueryType.PWSId, new QueryOptions() { PWSId = "KMNCHASK10", HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data from a personal weather station by ID + //var h7 = client.GetHistory(QueryType.USCity, new QueryOptions() { City = "Dallas", State = "TX", HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data for a US City + //var h8 = client.GetHistory(QueryType.ZipCode, new QueryOptions() { ZipCode = "94107", HistoryDate = new HistoryDate(2017, 06, 25) }); //Gets data by US Zip Code + + //Sample GeoLookups //var g1 = client.GetGeoLookup(QueryType.AirportCode, new QueryOptions() { AirportCode = "DFW" }); //Gets data by Airport Code //var g2 = client.GetGeoLookup(QueryType.AutoIp); //Gets data by the detected IP address of the calling client