Example #1
0
        private WeatherTestDTO GetJSONWeatherFromUrl(string url)
        {
            WeatherTestDTO weatherTestDTO = null;

            var request = HttpWebRequest.Create(@url);

            request.ContentType = "application/json";
            request.Method      = "GET";

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
                }
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    var content = reader.ReadToEnd();
                    if (string.IsNullOrWhiteSpace(content))
                    {
                        Console.Out.WriteLine("Response contained empty body...");
                    }
                    else
                    {
                        Console.Out.WriteLine("Response Body: \r\n {0}", content);
                    }
                    weatherTestDTO = JsonConvert.DeserializeObject <WeatherTestDTO>(content);
                }
            }
            return(weatherTestDTO);
        }
Example #2
0
        public WeatherTestDTO GetWeather(string location)
        {
            List <WeatherTestDTO> collWeatherTests = new List <WeatherTestDTO>();

            collWeatherTests.Add(GetAccuWeather(location));
            collWeatherTests.Add(GetBBCWeather(location));

            var weatherTestDTO = new WeatherTestDTO();

            weatherTestDTO.TemperatureCelsius = GetAverageValue(collWeatherTests, nameof(WeatherTestDTO.TemperatureCelsius));
            weatherTestDTO.WindSpeedKph       = GetAverageValue(collWeatherTests, nameof(WeatherTestDTO.WindSpeedKph));
            return(weatherTestDTO);
        }
Example #3
0
        private WeatherTestDTO GetWeatherFromURL(string url)
        {
            var weatherTestDTO = new WeatherTestDTO();

            using (var webClient = new WebClient())
            {
                string  str = webClient.DownloadString(url);
                JObject o   = JObject.Parse(str);
                weatherTestDTO.Location              = (string)o["Location"];
                weatherTestDTO.TemperatureCelsius    = o["TemperatureCelsius"] != null ? (double)o["TemperatureCelsius"] : 0.0;
                weatherTestDTO.TemperatureFahrenheit = o["TemperatureFahrenheit"] != null ? (double)o["TemperatureFahrenheit"] : 0.0;
                Console.WriteLine(o["Location"]);
            }
            return(weatherTestDTO);
        }
Example #4
0
        private WeatherTestDTO GetAccuWeather(string location)
        {
            WeatherTestDTO weatherTestDto = GetJSONWeatherFromUrl(string.Format(@"http://localhost:60827/weather/{0}", location));

            return(weatherTestDto);
        }