public async Task <IActionResult> Get([FromBody] TripCheckQuery query) { using (HttpClient client = new HttpClient()) { // Call asynchronous network methods in a try/catch block to handle exceptions try { var uriBuilder = new UriBuilder("https://maps.googleapis.com/maps/api/directions/json"); var uriQuery = HttpUtility.ParseQueryString(uriBuilder.Query); uriQuery["language"] = "ar"; uriQuery["key"] = "AIzaSyCezD7oizzzd8QqHw5tVSveIxjyemdpwVU"; uriQuery["origin"] = "" + query.OriginLat + "," + query.OriginLng; uriQuery["destination"] = "" + query.DestinationLat + "," + query.DestinationLng; uriBuilder.Query = uriQuery.ToString(); HttpResponseMessage response = await client.GetAsync(uriBuilder.ToString()); 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); var x = JsonConvert.DeserializeObject <Directions>(responseBody); x.Routes[0].Legs[0].Steps = null; return(Ok(new TripInfoDto { EncodedRoute = x.Routes[0].OverviewPolyline, Info = x.Routes[0].Legs[0], ExcpectedPayment = await TripHelper.calculateTripFeesRange(x) })); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } } return(Ok()); }
public async Task <Directions> getDirectionInfo(TripCheckQuery query) { using (HttpClient client = new HttpClient()) { // Call asynchronous network methods in a try/catch block to handle exceptions try { var uriBuilder = new UriBuilder("https://maps.googleapis.com/maps/api/directions/json"); var uriQuery = HttpUtility.ParseQueryString(uriBuilder.Query); uriQuery["language"] = "ar"; uriQuery["key"] = "AIzaSyCezD7oizzzd8QqHw5tVSveIxjyemdpwVU"; uriQuery["origin"] = "" + query.OriginLat + "," + query.OriginLng; uriQuery["destination"] = "" + query.DestinationLat + "," + query.DestinationLng; uriBuilder.Query = uriQuery.ToString(); HttpResponseMessage response = await client.GetAsync(uriBuilder.ToString()); 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); var x = JsonConvert.DeserializeObject <Directions>(responseBody); return(x); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } return(null); } }