private Weather GetWeatheryWithPolly(string city)
        {
            AsyncCircuitBreakerPolicy breaker = Policy
                                                .Handle <OpenWeatherMap.OpenWeatherMapException>()
                                                .CircuitBreakerAsync(
                exceptionsAllowedBeforeBreaking: 2,
                durationOfBreak: TimeSpan.FromMinutes(1)
                );

            var timeoutPolicy = Policy
                                .TimeoutAsync(new TimeSpan(0, 0, 2), TimeoutStrategy.Pessimistic); // Pessimistic strategy as openWeatherClient doesn't support CancellationToken

            var fallBackPolicy = Policy <OpenWeatherMap.CurrentWeatherResponse>
                                 .Handle <Exception>(IsFallbackPolicyEnabled)
                                 .FallbackAsync <OpenWeatherMap.CurrentWeatherResponse>((ct) => DoFallbackAction(ct, city));

            var simmyPolicy = GetSimmyLatencyPolicy();

            var policyWrap = fallBackPolicy // fallbackPolicy must be the innermost to work for all cases!!
                             .WrapAsync(breaker)
                             .WrapAsync(timeoutPolicy)
                             .WrapAsync(simmyPolicy); // simmyPolicy must be the outermost to trigger all internal policies!

            var result = policyWrap.ExecuteAsync(async()
                                                 => await openWeatherClient.CurrentWeather.GetByName(city, OpenWeatherMap.MetricSystem.Metric)).Result;
            var weather = WeatherConverter.GetWeatherFromResponse(result);

            return(weather);
        }
示例#2
0
        public ActionResult <Weather> Get(string city)
        {
            Console.WriteLine("Call Get; city:" + city);

            var response = openWeatherClient.CurrentWeather.GetByName(city, OpenWeatherMap.MetricSystem.Metric).Result;
            var weather  = WeatherConverter.GetWeatherFromResponse(response);

            return(weather);
        }
示例#3
0
        public void OnGet()
        {
            cityid = _weathersettings.Value.Id;
            apikey = _weathersettings.Value.Appid;

            apicall = $"https://api.openweathermap.org/data/2.5/weather?id={cityid}&units=metric&appid={apikey}";

            // Convert all weather properties as .NET objects.
            WeatherConverter converter = new WeatherConverter(apicall);

            Weather = converter.DeserializeJsonFile();
        }
示例#4
0
        public WeatherResult GetWeather(string location)
        {
            var client   = new RestClient("http://localhost:17855/api");
            var request  = new RestRequest("weather/abcd");
            var response = client.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(new WeatherResult {
                    Status = response.StatusCode
                });
            }

            var weatherResult = JsonConvert.DeserializeObject <WeatherResult>(response.Content);

            //convert for other units
            weatherResult.TemperatureFahrenheit = WeatherConverter.Fahrenheit(weatherResult.TemperatureCelsius);
            weatherResult.WindSpeedMph          = WeatherConverter.Mph(weatherResult.WindSpeedKph);
            weatherResult.Status = response.StatusCode;

            return(weatherResult);
        }
        public void OnGet()
        {
            cityid = _weathersettings.Value.Id;
            apikey = _weathersettings.Value.Appid;

            apicall = $"https://api.openweathermap.org/data/2.5/weather?id={cityid}&units=metric&appid={apikey}";

            // Convert all weather properties as .NET objects.
            WeatherConverter converter = new WeatherConverter(apicall);

            Weather = converter.DeserializeJsonFile();

            // Use the properties as weather widgets
            WeatherWidget widget = new WeatherWidget(Weather);

            // Get the temperature with the celsius symbol for a small widget.
            SmallWidget = widget.SmallWidget();

            // Get the temperature and weather icon for a medium size widget.
            MediumWidget = widget.MediumWidget();

            // Get the city name, temperature, weather icon and weather details for a big widget.
            BigWidget = widget.BigWidget();
        }