Exemplo n.º 1
0
        public static CityCoordinates build(WeatherCitiesEnum city)
        {
            switch (city)
            {
            case WeatherCitiesEnum.Moskow:
                return(new CityCoordinates("Moskow", 55.78892895, 37.58972168));
            }

            throw new CityIsNotDefined();
        }
        public async Task <Entity.WeatherForecast> getWeatherByCity(WeatherCitiesEnum city)
        {
            var cityCoordinates = CityCoordinatesBuilder.build(city);

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(apiBaseUrl);
                client.DefaultRequestHeaders.Add("X-Yandex-API-Key", _weatherConfig["YandexApiKey"]);

                HttpResponseMessage response = await client.GetAsync(getUri(cityCoordinates));

                if (response.IsSuccessStatusCode)
                {
                    dynamic o = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());

                    return(new Entity.WeatherForecast(cityCoordinates.city, Convert.ToDouble(o.fact.temp)));
                }

                throw new HttpRequestException($"returned status code is {response.StatusCode}");
            }
        }