Exemplo n.º 1
0
        public static async Task <IActionResult> GetAllStationsForecast(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "stations/all/forecast")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation($"Get station forecast for all stations");

            LuasApi api = new LuasApi();

            var stations             = api.GetAllStations();
            var stationAbbreviations = stations.Select(s => s.Abbreviation);

            try
            {
                var allForecasts =
                    await Task.WhenAll(
                        stationAbbreviations.Select(
                            abbreviation => api.GetForecastAsync(abbreviation)))
                    .ConfigureAwait(false);

                var allForecastsDictionary = allForecasts.Select(forecast => new { forecast.Station.Abbreviation, forecast });

                return(new OkObjectResult(allForecastsDictionary));
            }
            catch (StationNotFoundException ex)
            {
                log.LogWarning($"StationNotFoundException for '{ex.StationThatWasNotFound}'. Exception: {ex}");
                return(new NotFoundObjectResult($"Unable to find forecast for: '{ex.StationThatWasNotFound}'"));
            }
            catch (Exception ex)
            {
                log.LogError($"Exception thrown in GetStationForecast. Exception: {ex}");
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
Exemplo n.º 2
0
        public static async Task <IActionResult> GetStationForecast(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "stations/{stationAbbreviation}/forecast")] HttpRequest req,
            string stationAbbreviation,
            ILogger log)
        {
            log.LogInformation($"Get station forecast for {stationAbbreviation}");

            LuasApi api = new LuasApi();

            try
            {
                var forecast = await api.GetForecastAsync(stationAbbreviation).ConfigureAwait(false);

                return(new OkObjectResult(forecast));
            }
            catch (StationNotFoundException ex)
            {
                log.LogWarning($"StationNotFoundException for '{stationAbbreviation}'. Exception: {ex}");
                return(new NotFoundObjectResult($"Unable to find forecast for: '{stationAbbreviation}'"));
            }
            catch (Exception ex)
            {
                log.LogError($"Exception thrown in GetStationForecast. Exception: {ex}");
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
Exemplo n.º 3
0
        public long ForecastAsync(string station)
        {
            Stopwatch.Restart();

            StationForecast sfAsync = Api.GetForecastAsync(station).Result;

            Stopwatch.Stop();
            Console.WriteLine($"{station}: {sfAsync.InboundTrams[0].Minutes} ({Stopwatch.ElapsedMilliseconds})");

            return(Stopwatch.ElapsedMilliseconds);
        }