Beispiel #1
0
        public async Task <int> GetBikeCountInStation(string station)
        {
            var httpClient = new System.Net.Http.HttpClient();

            var toast = await httpClient.GetByteArrayAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental");

            string utfString = System.Text.Encoding.UTF8.GetString(toast);
            BikeRentalStationList stationInfo = Newtonsoft.Json.JsonConvert.DeserializeObject <BikeRentalStationList>(utfString);

            try
            {
                if (stationInfo.stations.Length != 0)
                {
                    for (int i = 0; i < stationInfo.stations.Length; ++i)
                    {
                        if (station.ToLower() == stationInfo.stations[i].name.ToLower())
                        {
                            _bikesAvailable = stationInfo.stations[i].bikesAvailable;
                            Console.WriteLine(stationInfo.stations[i].name + " has " + _bikesAvailable + " bike(s) available.\n");
                            return(_bikesAvailable);
                        }
                    }
                }
                throw new NotFoundException("Station with that name was not found");
            }
            catch (NotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
            return(0);
        }
Beispiel #2
0
        public async Task <int> GetBikeCountInStation(string stationName)
        {
            try
            {
                if (stationName.Any(char.IsDigit))
                {
                    throw new ArgumentException();
                }

                HttpClient client  = new HttpClient();
                string     message = await client.GetStringAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental");

                BikeRentalStationList helkama = JsonConvert.DeserializeObject <BikeRentalStationList>(message);

                int  bikes = 0;
                bool found = false;

                foreach (var station in helkama.stations)
                {
                    if (station.name == stationName)
                    {
                        bikes = station.bikesAvailable;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    throw new NotFoundException(stationName);
                }
                return(bikes);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("Invalid argument: " + ex.Message);
                return(0);
            }

            catch (NotFoundException ex)
            {
                Console.WriteLine("Not found: " + ex.Message);
                return(0);
            }
        }
        public async Task <int> GetBikeCountInStation(string stationName)
        {
            if (stationName.Any(c => char.IsDigit(c)))
            {
                throw new NotImplementedException(String.Format("Station name '{0}' should not contain numbers", stationName));
            }

            try
            {
                /*HttpResponseMessage response = await client.GetAsync(api);
                 * response.EnsureSuccessStatusCode();
                 * string responseBody = await response.Content.ReadAsStringAsync();*/
                string responseBody = await client.GetStringAsync(api);

                //convert response to BikeRentalStationList
                BikeRentalStationList stationList = JsonConvert.DeserializeObject <BikeRentalStationList>(responseBody);

                foreach (Station s in stationList.stations)
                {
                    Console.WriteLine("- Station " + s.name);
                    if (s.name.Equals(stationName))
                    {
                        //Console.WriteLine("Station " + stationName + " has " + s.bikesAvailable + " bikes available");
                        return(s.bikesAvailable);
                    }
                }
                throw new NotImplementedException(String.Format("Could not find station {0}", stationName));
                //throw NotFoundException(String.Format("Could not find station {0}", stationName));
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
                throw new NotImplementedException("Could not get stations");
            }
            throw new NotImplementedException(String.Format("Something happened while fetching station {0}", stationName));
        }