Exemplo n.º 1
0
        public async Task <IActionResult> Get([FromQuery] WeatherRequest weatherViewModel)
        {
            var weather =
                await _weatherClient.GetWeatherAsync(weatherViewModel.City, weatherViewModel.Language, weatherViewModel.Unit);

            return(Ok(weather));
        }
        public async Task <IActionResult> Get(string loc)
        {
            Dictionary <string, string> query = new Dictionary <string, string>();

            query.Add("Location", loc);
            query.Add("APIKEY", _openWeatherSettings.APIKEY);

            string respAsString = await _weatherClient.GetWeatherAsync(query);

            var        obj = JsonSerializer.Deserialize <OpenWeatherResponse>(respAsString);
            WeatherDTO cleanWeatherDesiredObject = _mapper.Map <OpenWeatherResponse, WeatherDTO>(obj);

            return(Ok(cleanWeatherDesiredObject));
        }
Exemplo n.º 3
0
        public WeatherModule(IWeatherCommandParser parser, IWeatherClient client)
        {
            Post["/weather", true] = async (_, token) =>
            {
                var request = this.Bind<SlackRequest>();

                try
                {
                    Command c = null;
                    if (parser.TryParse(request, out c))
                    {
                        var postalCode = c.Arguments.First();
                        var units = c.Arguments.Count() == 2
                            ? (Units) Enum.Parse(typeof (Units), c.Arguments.ElementAt(1), true)
                            : Units.Imperial;

                        var responseType =
                            c.Arguments.Any(arg => arg.Equals(ShareArgument, StringComparison.OrdinalIgnoreCase))
                                ? ResponseType.InChannel
                                : ResponseType.Ephemeral;

                        var weatherData = await client.GetWeatherAsync(postalCode, units);
                        var currentCondition = weatherData.WeatcherCondition.FirstOrDefault();

                        var unitString = units == Units.Imperial ? "F" : "C";
                        var currentConditionIconUrl = string.Empty;
                        var currentConditionText = string.Empty;

                        if (currentCondition != null)
                        {
                            currentConditionIconUrl = string.Format(IconUrlFormat, currentCondition.IconId);
                            currentConditionText = $"{currentCondition.Main} - {currentCondition.Description}";
                        }

                        return
                            new SlackResponse()
                            {
                                ResponseType = ResponseType.Ephemeral,
                                Text = $"Here's the current weather for {weatherData.CityName}!",
                                Attachments = new[]
                                {
                                    new SlackResponseAttachment()
                                    {
                                        Title = "Current Conditions",
                                        ThumbUrl = currentConditionIconUrl,
                                        Text = currentConditionText,
                                        Fields = new []
                                        {
                                            new SlackField()
                                            {
                                                Title = "Temperature",
                                                Value = $"{Math.Round(weatherData.Weather.Temperature, 2)}° {unitString}",
                                                IsShort = true
                                            },
                                            new SlackField()
                                            {
                                                Title = "Humidity",
                                                Value = $"{weatherData.Weather.Humidity}%",
                                                IsShort = true
                                            }, 
                                            new SlackField()
                                            {
                                                Title = "Pressure",
                                                Value = $"{weatherData.Weather.Pressure} hPa",
                                                IsShort = true
                                            }, 
                                        }
                                    }
                                }
                            };
                    }
                }
                catch
                {
                    // TODO: don't filter critical exception types
                    return DefaultResponse();
                }

                return DefaultResponse();
            };
        }