public static IEnumerable <Route> ConstructRouteObjects(string origin, string dest, bool oPos, bool dPos, string currency = "USD") { var geolocation = ""; var address = ""; var url = ""; if (oPos || dPos) { var user = new UserHandler().GetUser(MessageHandler.SenderId); geolocation = user.Location; address = user.LocationString; if (string.IsNullOrEmpty(geolocation) || string.IsNullOrEmpty(address)) { MessageHandler.SendTextMessage( $"I am sorry, I do not know your current location. Please send it via Messenger"); return(null); } if (oPos) { url = $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oPos={geolocation}&dName={dest}¤cyCode={currency}"; } if (dPos) { url = $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oName={origin}&dPos={geolocation}¤cyCode={currency}"; } } url = string.IsNullOrEmpty(url) ? $"http://free.rome2rio.com/api/1.4/json/Search?key={R2RApiKey}&oName={origin}&dName={dest}¤cyCode={currency}" : url; var routesList = new List <Route>(); JObject responseJson; using (var client = new HttpClient()) { var response = client.GetAsync(url); if (response.Result.IsSuccessStatusCode) { responseJson = JObject.Parse(response.Result.Content.ReadAsStringAsync().Result); } else { return(null); } } var routes = (JArray)responseJson["routes"]; var places = (JArray)responseJson["places"]; var originLongName = places[0]["longName"]; var destLongName = places[1]["longName"]; foreach (JObject route in routes) { var indicativePrices = route["indicativePrices"] as JArray; var majorSegment = (JObject)((JArray)route["segments"]).OrderByDescending(s => (int)s["distance"]).First(); var name = (string)route["name"]; var pLow = indicativePrices != null ? (int?)indicativePrices[0]["priceLow"] : 0; var pHigh = indicativePrices != null ? (int?)indicativePrices[0]["priceHigh"] : 0; var duration = (int)route["totalDuration"]; var agencyTuple = ((string)majorSegment["segmentKind"]).Equals("surface") ? FetchAgencyImage(majorSegment, responseJson["agencies"] as JArray) : FetchAirlineImage(majorSegment, responseJson["airlines"] as JArray); var agencyName = agencyTuple.Item1; var agencyImage = agencyTuple.Item2; pLow = pLow ?? 0; pHigh = pHigh ?? 0; int?price = 0; if (pLow == 0 && pHigh == 0) { price = indicativePrices != null ? (int?)indicativePrices[0]["price"] : 0; } var routeObject = new Route(name, pLow ?? 0, pHigh ?? 0, duration, string.IsNullOrEmpty(agencyImage) && name.Contains("Drive") ? "http://i.imgur.com/PfC7OYk.jpg" : agencyImage, string.IsNullOrEmpty(agencyName) && name.Contains("Drive") ? "Self-Drive" : agencyName, !((string)majorSegment["segmentKind"]).Equals("surface"), price ?? 0); routesList.Add(routeObject); } var routeList = routesList.OrderBy(r => r.Duration).Take(Math.Min(routesList.Count, 6)).ToList(); var cheapestRoute = routesList.MinBy(x => x.Price); cheapestRoute.IsCheapest = true; var fastestRoute = routesList.MinBy(x => x.Duration); fastestRoute.IsFastest = true; if (!routeList.Contains(cheapestRoute)) { routeList.Add(cheapestRoute); } if (oPos) { MessageHandler.SendTextMessage( $"I have found {routeList.Count} routes from {address} to {destLongName}"); } else if (dPos) { MessageHandler.SendTextMessage( $"I have found {routeList.Count} routes from {originLongName} to {address}"); } else { MessageHandler.SendTextMessage( $"I have found {routeList.Count} routes from {originLongName} to {destLongName}"); } return(routeList); // Returns maximum of 7 routes. }