Beispiel #1
0
        public async Task <IHttpActionResult> WeatherFromAllServices([FromUri] string country, [FromUri] string city)
        {
            if (string.IsNullOrWhiteSpace(country))
            {
                throw new ArgumentNullException(nameof(country));
            }
            if (string.IsNullOrWhiteSpace(city))
            {
                throw new ArgumentNullException(nameof(city));
            }

            var tasks = _weatherServices.Select(x =>
            {
                return(new Func <Task <KeyValuePair <string, WeatherResponse> > >(async() =>
                {
                    var result = await x.GetByCity(country, city);

                    WeatherResponse res = null;
                    if (!result.Errored)
                    {
                        res = WeatherModelMapper.ToWeatherResponse(result);
                    }

                    return new KeyValuePair <string, WeatherResponse>(x.Identifier, res);
                })());
            });

            try
            {
                var results = await Task.WhenAll(tasks);

                //TODO: Avoid dynamics, use proper models

                var mapped = results.Where(x => x.Value != null)
                             .Select(x => new
                {
                    x.Key,
                    x.Value
                });

                var responseObj = new
                {
                    Results = mapped
                };

                return(Ok(responseObj));
            }
            catch (Exception ex)
            {
                Log.Error(ex, "WeatherFromAllServices failed. Country : {country}, City: {city}", country, city);
                throw;
            }
        }
Beispiel #2
0
        public async Task <IHttpActionResult> WeatherFromService([FromUri] string service, [FromUri] string country, [FromUri] string city)
        {
            if (string.IsNullOrWhiteSpace(service))
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (string.IsNullOrWhiteSpace(country))
            {
                throw new ArgumentNullException(nameof(country));
            }
            if (string.IsNullOrWhiteSpace(city))
            {
                throw new ArgumentNullException(nameof(city));
            }

            var targetService = _weatherServices.FirstOrDefault(x => x.Identifier.Equals(service, StringComparison.OrdinalIgnoreCase));

            if (targetService == null)
            {
                return(BadRequest($"Could not find a service matching the specified service identifier '{service}'"));
            }

            try
            {
                var model = await targetService.GetByCity(country, city);

                if (!model.Errored)
                {
                    var res = WeatherModelMapper.ToWeatherResponse(model);
                    return(Ok(res));
                }
                else
                {
                    return(Content(HttpStatusCode.InternalServerError, model.Error.ErrorMessage));
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "WeatherFromService failed. Country : {country}, City: {city}", country, city);
                throw;
            }
        }