//static BikeRentalStationList list = new BikeRentalStationList(); public async Task <int> GetBikeCountInStation(string stationName) { try { HttpResponseMessage response = await client.GetAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); response.EnsureSuccessStatusCode(); string stations = await response.Content.ReadAsStringAsync(); BikeRentalStationList list = JsonConvert.DeserializeObject <BikeRentalStationList>(stations); for (int i = 0; i < list.stations.Length; i++) { if (list.stations[i].name.Equals(stationName)) { return(list.stations[i].bikesAvailable); } } } catch (HttpRequestException) { throw new HttpRequestException("Error: Can't get data"); } throw new ArgumentException("Station could not be found"); /* * catch (NotFoundException) * { * throw new NotFoundException("Not found: Station"); * } */ return(0); }
public async Task <int> GetBikeCountInStation(string stationName) { HttpClient client = new HttpClient(); try { string response = await client.GetStringAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); BikeRentalStationList list = Newtonsoft.Json.JsonConvert.DeserializeObject <BikeRentalStationList>(response); for (int i = 0; i < list.stations.Length; i++) { if (list.stations[i].name.Equals(stationName)) { return(list.stations[i].bikesAvailable); } } throw new NotFoundException(); } catch (ArgumentException e) { Console.WriteLine("That's not a name / " + e); } catch (NotFoundException e) { Console.WriteLine("Station " + stationName + " " + e); } return(0); }
public async Task <int> GetBikeCountInStation(string stationName) { HttpClient httpClient = new HttpClient(); String jsonString = await httpClient.GetStringAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); BikeRentalStationList list = JsonConvert.DeserializeObject <BikeRentalStationList>(jsonString); Station temp = Array.Find(list.stations, x => x.name == stationName); if (temp == null) { throw new NotFoundException(String.Format("Station name ({0}) is not found!", stationName), stationName); } return(temp.bikesAvailable); }
async Task <int> GetBikeCountInStation(string stationName) { try { HttpResponseMessage response = await client.GetAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); // Above three lines can be replaced with new helper method below // string responseBody = await client.GetStringAsync(uri); BikeRentalStationList list = JsonConvert.DeserializeObject <BikeRentalStationList>(stations); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } }
public OfflineCityBikeDataFetcher() { stationList = new BikeRentalStationList(); string fileName = "bikedata.txt"; string[] lines = File.ReadAllLines(fileName); List <BikeRentalStationList.Station> st = new List <BikeRentalStationList.Station>(); foreach (string line in lines) { string[] split = line.Split(':'); st.Add(new BikeRentalStationList.Station() { name = split[0].Trim(), bikesAvailable = Int32.Parse(split[1]) }); } stationList.stations = st.ToArray(); }
public async Task <int> GetBikeCountInStation(string stationName) { for (int i = 0; i < stationName.Length; i++) { if (Char.IsDigit(stationName, i)) { throw new ArgumentException("Invalid argument: " + stationName); } } try { HttpResponseMessage response = await client.GetAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); // Above three lines can be replaced with new helper method below // string responseBody = await client.GetStringAsync(uri); stationList = JsonConvert.DeserializeObject <BikeRentalStationList>(responseBody); foreach (BikeRentalStationList.Station s in stationList.stations) { if (s.name == stationName) { return(s.bikesAvailable); } } throw new NotFoundException("Not found: " + stationName); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } return(0); }
public async Task <int> GetBikeCountInStation(string stationName) { try { if (stationName.Any(char.IsDigit)) { throw new ArgumentException(String.Format("stationName can't contain a number")); } HttpClient client = new System.Net.Http.HttpClient(); // HttpResponseMessage response1 = await client.GetAsync(URL); string response = await client.GetStringAsync(URL); BikeRentalStationList list = JsonConvert.DeserializeObject <BikeRentalStationList>(response); foreach (Station station in list.stations) { if (stationName == station.name) { Console.WriteLine("Bikes available: " + station.bikesAvailable); return(station.bikesAvailable); } } throw new NotFoundException(String.Format(stationName)); } catch (ArgumentException e) { Console.WriteLine("Invalid argument: " + e.Message); } catch (NotFoundException e) { Console.WriteLine("Not found: " + e.Message); } return(0); }
public async Task <int> GetBikeCountInStation(string stationName) { HttpClient client = new HttpClient(); try { string json = await client.GetStringAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); BikeRentalStationList bikeList = JsonConvert.DeserializeObject <BikeRentalStationList>(json); for (int i = 0; i < bikeList.Stations.Length; i++) { if (bikeList.Stations[i].Name == stationName) { bikeCount = bikeList.Stations[i].BikesAvailable; Console.WriteLine("There are " + bikeCount + " bikes at the station"); return(0); } } foreach (char c in stationName) { if (char.IsDigit(c)) { System.ArgumentException argEx = new System.ArgumentException("Contains numbers"); throw argEx; } } NotFoundException notFound = new NotFoundException("Station not found."); throw notFound; } catch (ArgumentException ex) { Console.WriteLine("Invalid argument: " + ex.Message); } catch (NotFoundException ex) { Console.WriteLine("Not Found: " + ex.Message); } return(0); }
public async Task <int> GetBikeCountInStation(string stationName) { HttpClient bikeamountstringretriever = new HttpClient(); try { foreach (char c in stationName) { if (char.IsDigit(c)) { System.ArgumentException argEx = new System.ArgumentException("Contains numbers"); throw argEx; } } string responseBody = await bikeamountstringretriever.GetStringAsync("https://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); //Console.WriteLine(responseBody); BikeRentalStationList bikes = JsonConvert.DeserializeObject <BikeRentalStationList>(responseBody); for (int i = 0; i < bikes.stations.Length; i++) { if (bikes.stations[i].name == stationName) { bikeCount = bikes.stations[i].bikesAvailable; Console.WriteLine("there are " + bikeCount + " bikes at the " + stationName + " station"); return(1); } } NotFoundException not = new NotFoundException("Station is not on the list. Check if you typed correctly"); throw not; } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); return(1); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Please provide at least one argument"); } catch (FormatException ex) { Console.WriteLine("Invalid argument: " + ex.Message); } catch (ArgumentException ex) { Console.WriteLine("Invalid argument: " + ex.Message); } catch (NotFoundException ex) { Console.WriteLine("Not found: " + ex.Message); } catch (Exception ex) { Console.WriteLine("just an exception"); } return(1); }
// Task<int> bikeCount; public async Task <int> GetBikeCountInStation(string stationName) { HttpClient bikeamountstringretriever = new HttpClient(); try { string responseBody = await bikeamountstringretriever.GetStringAsync("https://api.digitransit.fi/routing/v1/routers/hsl/bike_rental"); foreach (char c in stationName) { if (char.IsDigit(c)) { System.ArgumentException argEx = new System.ArgumentException("Contains numbers"); throw argEx; } } // Console.WriteLine(responseBody); BikeRentalStationList bikes = JsonConvert.DeserializeObject <BikeRentalStationList>(responseBody); for (int i = 0; i < bikes.stations.Length; i++) { if (bikes.stations[i].name == stationName) { int bikeCount = Convert.ToInt32(bikes.stations[i].bikesAvailable); Console.WriteLine("there are " + bikeCount + " bikes at the station"); return(1); } } System.Exception argEx2 = new NotFoundException(); throw argEx2; } catch (NotFoundException exx) { Console.WriteLine("Object not found", exx.Message); } catch (System.IndexOutOfRangeException exx) { Console.WriteLine("Please provide at least one argument", exx.Message); } catch (System.FormatException e) { Console.WriteLine("That is not a letter!", e.Message); } catch (HttpRequestException ex) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", ex.Message); } catch (ArgumentException ex) { Console.WriteLine("Invalid argument: " + ex.Message); } catch (System.Exception ex) { Console.WriteLine("We caught something else", ex.Message); } return(1); }