/// <summary> /// /// </summary> /// <param name="city"></param> /// <param name="unit"></param> /// <param name="language"></param> /// <returns></returns> public async Task <WeatherConditions> GetCurrentWeather( string city, OWUnit unit = OWUnit.Standard, Language language = Language.English) { ThrowExceptionIfApiKeyInvalid(); try { var url = string.Format(WeatherCityUri, city, unit.ToString().ToLower(), language.ToValue().ToLower(), _apiKey); HttpResponseMessage response = await _httpClient.GetAsync(url).ConfigureAwait(false); ThrowExceptionIfResponseError(response); #if DEBUG var json = await response.Content.ReadAsStringAsync(); #endif using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { return(ParseJsonFromStream <WeatherConditions>(responseStream, new MyAlertConverter())); } } catch (WeatherException) { throw; } catch (HttpRequestException ex) { throw new WeatherException( WeatherException.HttpError, !string.IsNullOrWhiteSpace(ex.Message) ? ex.Message : "Couldn't retrieve data"); } catch (Exception ex) { throw new WeatherException(ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <param name="unit"></param> /// <param name="language"></param> /// <returns></returns> public async Task <ForecastDSL> GetForecastDSL( double latitude, double longitude, OWUnit unit = OWUnit.Standard, Language language = Language.English) { ThrowExceptionIfApiKeyInvalid(); try { var url = string.Format(ForecastOneCallUri, latitude, longitude, unit.ToString().ToLower(), language.ToValue(), _apiKey); HttpResponseMessage response = await _httpClient.GetAsync(url).ConfigureAwait(false); ThrowExceptionIfResponseError(response); #if DEBUG var json = await response.Content.ReadAsStringAsync(); #endif using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { var output = ParseJsonFromStream <ForecastDSL>(responseStream, new MyAlertConverter()); // patch a bit the output and return it return(PatchReturnedData(output)); } } catch (WeatherException) { throw; } catch (HttpRequestException ex) { throw new WeatherException( WeatherException.HttpError, !string.IsNullOrWhiteSpace(ex.Message) ? ex.Message : "Couldn't retrieve data"); } catch (Exception ex) { throw new WeatherException(ex.Message, ex); } }