public override async Task <Weather> GetWeather(string location_query) { Weather weather = null; string queryAPI = null; Uri weatherURL = null; #if WINDOWS_UWP var userlang = Windows.System.UserProfile.GlobalizationPreferences.Languages.First(); var culture = new System.Globalization.CultureInfo(userlang); #else var culture = System.Globalization.CultureInfo.CurrentCulture; #endif string locale = LocaleToLangCode(culture.TwoLetterISOLanguageName, culture.Name); string key = Settings.UsePersonalKey ? Settings.API_KEY : GetAPIKey(); queryAPI = "https://api.wunderground.com/api/" + key + "/astronomy/conditions/forecast10day/hourly/alerts/lang:" + locale; string options = ".json"; weatherURL = new Uri(queryAPI + location_query + options); HttpClient webClient = new HttpClient(); WeatherException wEx = null; try { // Get response HttpResponseMessage response = await webClient.GetAsync(weatherURL); response.EnsureSuccessStatusCode(); Stream contentStream = null; #if WINDOWS_UWP contentStream = WindowsRuntimeStreamExtensions.AsStreamForRead(await response.Content.ReadAsInputStreamAsync()); #elif __ANDROID__ contentStream = await response.Content.ReadAsStreamAsync(); #endif // Reset exception wEx = null; // Load weather Rootobject root = null; await Task.Run(() => { root = JSONParser.Deserializer <Rootobject>(contentStream); }); // Check for errors if (root.response.error != null) { switch (root.response.error.type) { case "querynotfound": wEx = new WeatherException(WeatherUtils.ErrorStatus.QueryNotFound); break; case "keynotfound": wEx = new WeatherException(WeatherUtils.ErrorStatus.InvalidAPIKey); break; default: break; } } // End Stream if (contentStream != null) { contentStream.Dispose(); } weather = new Weather(root); // Add weather alerts if available if (root.alerts != null && root.alerts.Length > 0) { if (weather.weather_alerts == null) { weather.weather_alerts = new List <WeatherAlert>(); } foreach (Alert result in root.alerts) { weather.weather_alerts.Add(new WeatherAlert(result)); } } } catch (Exception ex) { weather = null; #if WINDOWS_UWP if (WebError.GetStatus(ex.HResult) > WebErrorStatus.Unknown) #elif __ANDROID__ if (ex is WebException || ex is HttpRequestException) #endif { wEx = new WeatherException(WeatherUtils.ErrorStatus.NetworkError); } Logger.WriteLine(LoggerLevel.Error, ex, "WeatherUndergroundProvider: error getting weather data"); } // End Stream webClient.Dispose(); if (weather == null || !weather.IsValid()) { wEx = new WeatherException(WeatherUtils.ErrorStatus.NoWeather); } else if (weather != null) { if (SupportsWeatherLocale) { weather.locale = locale; } weather.query = location_query; } if (wEx != null) { throw wEx; } return(weather); }
public override async Task <bool> IsKeyValid(string key) { string queryAPI = "https://api.wunderground.com/api/"; string query = "/q/NY/New_York.json"; Uri queryURL = new Uri(queryAPI + key + query); bool isValid = false; WeatherException wEx = null; try { if (String.IsNullOrWhiteSpace(key)) { throw (wEx = new WeatherException(WeatherUtils.ErrorStatus.InvalidAPIKey)); } // Connect to webstream HttpClient webClient = new HttpClient(); HttpResponseMessage response = await webClient.GetAsync(queryURL); response.EnsureSuccessStatusCode(); Stream contentStream = null; #if WINDOWS_UWP contentStream = WindowsRuntimeStreamExtensions.AsStreamForRead(await response.Content.ReadAsInputStreamAsync()); #elif __ANDROID__ contentStream = await response.Content.ReadAsStreamAsync(); #endif // Reset exception wEx = null; // End Stream webClient.Dispose(); // Load data Rootobject root = await JSONParser.DeserializerAsync <Rootobject>(contentStream); // Check for errors if (root.response.error != null) { switch (root.response.error.type) { case "keynotfound": wEx = new WeatherException(WeatherUtils.ErrorStatus.InvalidAPIKey); isValid = false; break; } } else { isValid = true; } // End Stream if (contentStream != null) { contentStream.Dispose(); } } catch (Exception) { isValid = false; } if (wEx != null) { #if WINDOWS_UWP await Toast.ShowToastAsync(wEx.Message, ToastDuration.Short); #elif __ANDROID__ new Android.OS.Handler(Android.OS.Looper.MainLooper).Post(() => { Toast.MakeText(Application.Context, wEx.Message, ToastLength.Short).Show(); }); #endif } return(isValid); }