예제 #1
0
        /// <summary>
        /// Get Wind speed
        /// Call WeatherMap web Api
        /// </summary>
        /// <returns></returns>
        public static WeatherMapResponse GetWindSpeed()
        {
            WeatherMapResponse weatherMapResponse = new WeatherMapResponse();

            try
            {
                using (var client = new HttpClient())
                {
                    WeatherMapRequest weatherMapRequest = new WeatherMapRequest()
                    {
                        CityName = WeatherMapConstants.City
                    };
                    var responseTask = client.GetAsync(ApiConstant.WebApUrl + ApiConstant.WindSpeedAction + weatherMapRequest.CityName);
                    responseTask.Wait();

                    var result = responseTask.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var readTask = result.Content.ReadAsStringAsync();
                        readTask.Wait();
                        var speed = readTask.Result;

                        var weatherInfo = JObject.Parse(speed).ToString();

                        var response = JsonConvert.DeserializeObject <WeatherMap>(weatherInfo);
                        weatherMapResponse.WindSpeed = response.Result.Data[0].Wind.Speed;
                        return(weatherMapResponse);
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionLogger exceptionLogger = new ExceptionLogger()
                {
                    ExceptionStackTrace = e.StackTrace,
                    ExceptionMessage    = e.Message,
                    LogTime             = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
                };
                weatherMapResponse.ExceptionLog = exceptionLogger;
            }

            return(weatherMapResponse);
        }
        public async Task <ActionResult <SpotifyResponse> > CreatePlaylist([FromServices] IPlaylistService playlistService, [FromQuery] WeatherMapRequest weather)
        {
            var result = string.IsNullOrEmpty(weather.City) ? await playlistService.CreatePlaylist(weather.Lat, weather.Long) : await playlistService.CreatePlaylist(weather.City);

            if (result.Weather.Error != null)
            {
                return(BadRequest(_mapper.Map <WeatherErrorResponse>(result.Weather.Error)));
            }

            if (result.Spotify.Data != null)
            {
                return(Ok(_mapper.Map <SpotifyResponse>(result.Spotify.Data)));
            }

            return(BadRequest(_mapper.Map <SpotifyErrorResponse>(result.Spotify.Error)));
        }