/// <summary>
        /// In case this service provides forecasts within the call of the Weather service
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual List <ForecastModel> Forecast(IWeatherParameters parameters)
        {
            if (this.ProvidesForecastWithoutAnotherCall)
            {
                var weatherInfo = this.Weather(parameters);
                return(weatherInfo.Forecast);
            }

            throw new NotImplementedException();
        }
        public override WeatherModel Weather(IWeatherParameters parameters)
        {
            if (parameters != null)
            {
                var url = parameters.Latitude.HasValue && parameters.Longitude.HasValue ?
                          $"http://api.openweathermap.org/data/2.5/weather?lat={parameters.Latitude}&lon={parameters.Longitude}&appid={ApiKey}&units=metric&mode=json" :
                          $"http://api.openweathermap.org/data/2.5/weather?q={parameters.City},{parameters.CountryCode}&appid={ApiKey}&units=metric&mode=json";

                try
                {
                    var res = WebRequest.CreateHttp(url).GetResponse();
                    using (res)
                    {
                        var reader = new StreamReader(res.GetResponseStream());
                        var data   = reader.ReadToEnd();
                        var json   = JObject.Parse(data);

                        //closes the stream reader
                        reader.Close();
                        reader.Dispose();

                        //adds one call
                        this.NumberOfQueriesMade += 1;

                        var weather = ToWeather(json);

                        //uses parameters to set city and country
                        weather.City        = parameters.City;
                        weather.Region      = parameters.Region;
                        weather.RegionCode  = parameters.RegionCode;
                        weather.Country     = parameters.Country;
                        weather.CountryCode = parameters.CountryCode;

                        //converts
                        return(weather);
                    }
                }
                catch
                {
                    this.UnsuccessfulCalls += 1;
                    throw;
                }
            }

            throw new ArgumentNullException("parameters");
        }
Ejemplo n.º 3
0
        public override WeatherModel Weather(IWeatherParameters parameters)
        {
            if (parameters != null)
            {
                try
                {
                    var url = $"https://api.darksky.net/forecast/{ApiKey}/{parameters.Latitude},{parameters.Longitude}?units=si";
                    var res = WebRequest.CreateHttp(url).GetResponse();

                    using (res)
                    {
                        var reader = new StreamReader(res.GetResponseStream());
                        var data   = reader.ReadToEnd();
                        var json   = JObject.Parse(data);

                        //closes the stream reader
                        reader.Close();
                        reader.Dispose();

                        //adds one call
                        this.NumberOfQueriesMade += 1;

                        //parses json to weathermodel
                        var weather = ToWeather(json);

                        //uses parameters to set city and country
                        weather.City        = parameters.City;
                        weather.Region      = parameters.Region;
                        weather.RegionCode  = parameters.RegionCode;
                        weather.Country     = parameters.Country;
                        weather.CountryCode = parameters.CountryCode;

                        //converts
                        return(weather);
                    }
                }
                catch
                {
                    this.UnsuccessfulCalls += 1;
                    throw;
                }
            }

            throw new ArgumentNullException("parameters");
        }
 public abstract WeatherModel Weather(IWeatherParameters parameters);