/// <summary> /// Polls the current session for quotes. /// <returns> /// The <c>Quote</c> instances for this search. /// </returns> /// </summary> private async Task <IList <Quote> > PollCurrentSession() { // Flag to track if TripAdvisor API has completed compiling results bool complete = false; // Container for the flight quotes var flightCollection = new List <Quote>(); // Build request URL for current session string url = TripAdvisorUrlBuilder.BuildPollUrl(_sessionId); do { // Wait 1 second to avoid excessive API calls. // The free TripAdvisor API has a limit, so we need to avoid // wasting calls. Thread.Sleep(1000); // Query TripAdvisor API var jsonResponse = await MakeHTTPRequestRaw(url); // Build quote parser for json payload var flightParser = new TripAdvisorPayloadParser(jsonResponse); // Parse the payload into quote instances var flights = flightParser.GetQuoteData(); // Add the batch of flight quotes flightCollection.AddRange(flights); // Check if the TripAdvisor API has signaled completion if (flightParser.IsComplete()) { complete = true; } } while (!complete); // Iterate until TripAdvisor is done // Return the flight quotes, dropping duplicates return(flightCollection .GroupBy(x => new { x.Key, x.Cost.CurrencyType, x.Cost.Value, x.DepartureAirline, x.DepartureArrivalTime, x.DepartureStopCount, x.DepartureTakeoffTime, x.ReturnAirline, x.ReturnArrivalTime, x.ReturnStopCount, x.ReturnTakeoffTime, x.ReturnFlightNumber, x.DepartureFlightNumber }) .Select(x => x.First()) .ToList()); }
/// <summary> /// Creates a session for the search parameters. /// <param name="origin">The name of the origin </param> /// <param name="destination">The name of the destination airport.</param> /// <param name="departureDate">The date that the flight will depart from the origin airport.</param> /// <returns> /// An awaitable Task instance. /// </returns> /// </summary> private async Task CreateSession(string origin, string destination, DateTime departureDate) { // Get airport objects from names var originAirport = GetAirportFromAirportName(origin); var destinationAirport = GetAirportFromAirportName(destination); // Build Url to request the creation of the session string url = TripAdvisorUrlBuilder.BuildSessionUrl( originAirport.IataCode, destinationAirport.IataCode, departureDate); // Send request to create session var response = await MakeHTTPRequest <CreateSessionResponse>(url); // Extract session Id for later polling _sessionId = response.search_params.sid; }