private string CreateSession(FlightApiSearchParams flightApiSearchParam) { log.Info(" CreateSession with flightApiSearchParam : " + flightApiSearchParam.ToString()); string sessionId = null; using (var client = new HttpClient()) { using (var request = new HttpRequestMessage { RequestUri = new Uri("https://skyscanner-skyscanner-flight-search-v1.p.mashape.com/apiservices/pricing/v1.0"), Method = HttpMethod.Post }) { Dictionary <string, string> requestParams = new Dictionary <string, string>(); requestParams.Add("Country", "IL-sky"); requestParams.Add("Currency", "USD"); requestParams.Add("Locale", "enl-US"); requestParams.Add("OriginPlace", "TLV-sky"); requestParams.Add("DestinationPlace", this.GetDestinationPlaceApiCode(flightApiSearchParam.DestinationPlace)); requestParams.Add("OutboundDate", flightApiSearchParam.OutboundDate.ToString("yyyy-MM-dd")); requestParams.Add("InboundDate", flightApiSearchParam.InboundDate != null ? flightApiSearchParam.InboundDate.ToString("yyyy-MM-dd") : ""); requestParams.Add("CabinClass", "economy"); requestParams.Add("Ddults", "1"); requestParams.Add("Children", "0"); requestParams.Add("Infants", "0"); requestParams.Add("IncludeCarriers", ""); requestParams.Add("ExcludeCarriers", ""); request.Content = new FormUrlEncodedContent(requestParams); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); request.Headers.Add("X-Mashape-Key", "Your RapidApi KEY !!!"); request.Headers.Add("X-Mashape-Host", "skyscanner-skyscanner-flight-search-v1.p.mashape.com"); HttpResponseMessage result = client.SendAsync(request).Result; if (result.IsSuccessStatusCode == false) { string jsonErr = result.Content.ReadAsStringAsync().Result; log.Info("Error on getting session id " + jsonErr); throw new Exception(jsonErr); } else { log.Info("Session id was got successfully"); } sessionId = result.Headers.Location.ToString().Split('/').Last(); } } return(sessionId); }
public IEnumerable <Flight> GetFlights(FlightSearchParams flightSearchParam, FlightSerachFilter flightSerachFilter) { IEnumerable <Flight> flightsResponse = null; try { FlightApiSearchParams flightApiSearchParam = new FlightApiSearchParams(); flightApiSearchParam.DestinationPlace = flightSearchParam.DestinationPlace; flightApiSearchParam.InboundDate = flightSearchParam.InboundDate; flightApiSearchParam.OutboundDate = flightSearchParam.OutboundDate; FlightsRetriever flightsRetriever = new FlightsRetriever(); List <Flight> flights = flightsRetriever.GetFlightsBySearchParams(flightSearchParam.DestinationPlace, flightSearchParam.InboundDate, flightSearchParam.OutboundDate).ToList(); // check if flights exist in db if (flights.Any() == false) { log.Info(string.Format("Flights with params : {0},{1},{2} not exist in db ", flightSearchParam.DestinationPlace, flightSearchParam.OutboundDate.ToString("dd/MM/yyyy"), flightSearchParam.InboundDate.ToString("dd/MM/yyyy"))); string sessionId = this.CreateSession(flightApiSearchParam); FlightApiResponse flightApiResponse = this.PullSessionResult(sessionId); flights = this.ConvertFlightApiResponseToFlight(flightApiResponse, flightSearchParam.DestinationPlace, flightSearchParam.InboundDate, flightSearchParam.OutboundDate).ToList(); // store flight in db flightsRetriever.InsertFlights(flights); } else { log.Info(string.Format("Flights with params : {0},{1},{2} found in db ", flightSearchParam.DestinationPlace, flightSearchParam.OutboundDate.ToString("dd/MM/yyyy"), flightSearchParam.InboundDate.ToString("dd/MM/yyyy"))); } FlightFilterProcessor flightFilterProcessor = new FlightFilterProcessor(); flightsResponse = flightFilterProcessor.FilterFlights(flights, flightSerachFilter); } catch (Exception e) { log.Info(string.Format("GetFlights error with params : {0},{1},{2} ", flightSearchParam.DestinationPlace, flightSearchParam.InboundDate.ToString("dd/MM/yyyy"), flightSearchParam.OutboundDate.ToString("dd/MM/yyyy"))); log.Error(string.Format("Error is : {0} stack trace is : {1} ", e.Message, e.StackTrace)); } return(flightsResponse); }