コード例 #1
0
        static async Task GetRoutesAync()
        {
            List <AirportItem> Airports = new List <AirportItem>();

            try
            {
                var            context         = new TemplateEntities1();
                AirportService _airportService = new AirportService(context);
                _airportService.DeleteAirportsTripProvider(Providers.RyanAir);

                var client      = new HttpClient();
                var queryString = HttpUtility.ParseQueryString(string.Empty);


                var uri      = "https://apigateway.ryanair.com/pub/v1/core/3/airports?apikey=" + Key;
                var response = await client.GetAsync(uri);


                if (response != null)
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var contents = await response.Content.ReadAsStringAsync();

                        if (!String.IsNullOrWhiteSpace(contents))
                        {
                            List <RyanAirAirports> AirportsList = JsonConvert.DeserializeObject <List <RyanAirAirports> >(contents);
                            if (AirportsList != null)
                            {
                                foreach (RyanAirAirports ResultItem in AirportsList)
                                {
                                    string FromAirportCode = ResultItem.iataCode.ToUpper();
                                    foreach (string route in ResultItem.routes)
                                    {
                                        if (route != null && route.ToLower().Contains("airport"))
                                        {
                                            _airportService.AddAirportsTripProviderItem(FromAirportCode, route.Split(':')[1].ToUpper(), Providers.RyanAir);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    FlightsEngine.Utils.Logger.GenerateInfo("RyanAir Response null ");
                }
            }
            catch (Exception e)
            {
                FlightsEngine.Utils.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
        }
コード例 #2
0
        static bool GetAirportsRoutes(string Key)
        {
            bool resultRoutes           = true;;
            List <AirportItem> Airports = new List <AirportItem>();

            try
            {
                var            context         = new TemplateEntities1();
                AirportService _airportService = new AirportService(context);
                //   _airportService.DeleteAirportsTripProvider(Providers.BritishAirways);
                List <AirportItem> AirportsList = getBADestinations(Key);
                AirportsList = AirportsList.OrderBy(a => a.Code).ToList();
                AirportsTrip lastAirportsTrip = _airportService.GetAirportsTripForProviderRoute(Providers.BritishAirways);

                bool result               = false;
                int  AttemptsNumber       = 0;
                int  MaxRequestAttempts   = FlightsEngine.Models.Constants.Constants.MaxRequestAttempts;
                bool NeedToContinueSearch = true;



                foreach (var fromAirport in AirportsList)
                {
                    foreach (var toAirport in AirportsList)
                    {
                        if (lastAirportsTrip != null)
                        {
                            int test  = string.Compare(lastAirportsTrip.Airport.Code, fromAirport.Code);
                            int tets2 = string.Compare(lastAirportsTrip.Airport1.Code, toAirport.Code);
                        }
                        if (!String.IsNullOrWhiteSpace(fromAirport.Code) && !String.IsNullOrWhiteSpace(toAirport.Code) && (lastAirportsTrip == null || (string.Compare(lastAirportsTrip.Airport.Code, fromAirport.Code) <= 0 && string.Compare(lastAirportsTrip.Airport1.Code, toAirport.Code) <= 0)))
                        {
                            result               = false;
                            AttemptsNumber       = 0;
                            NeedToContinueSearch = true;
                            while (!result && AttemptsNumber <= MaxRequestAttempts && NeedToContinueSearch)
                            {
                                AttemptsNumber++;
                                var client      = new HttpClient();
                                var queryString = HttpUtility.ParseQueryString(string.Empty);


                                var url            = "https://api.ba.com/rest-v1/v1/flightOfferBasic;departureCity=" + fromAirport.CityCode + ";arrivalCity=" + toAirport.CityCode + ";cabin=economy;journeyType=roundTrip;range=yearLow";
                                var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                                // httpWebRequest.ContentType = "application/json";
                                httpWebRequest.Method = "GET";
                                httpWebRequest.Headers.Add("client-key", Key);

                                HttpWebResponse httpResponse = null;
                                try
                                {
                                    AttemptsNumber++;
                                    httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                                    result       = true;
                                }
                                catch (WebException e)
                                {
                                    result = false;
                                    if (!e.ToString().Contains("404"))
                                    {
                                        string webError = FlightsEngine.Utils.Logger.GenerateWebError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "FromAirportId = " + fromAirport.Id + " and toAirportId = " + toAirport.Id);
                                    }
                                    else
                                    {
                                        NeedToContinueSearch = false;
                                    }
                                }


                                if (result)
                                {
                                    NeedToContinueSearch = false;
                                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                                    {
                                        var requestResult = streamReader.ReadToEnd();

                                        if (!String.IsNullOrWhiteSpace(requestResult))
                                        {
                                            _airportService.AddAirportsTripProviderItem(fromAirport.Code, toAirport.Code, Providers.BritishAirways);
                                        }
                                    }
                                }
                                System.Threading.Thread.Sleep(1000);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                resultRoutes = false;
                FlightsEngine.Utils.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
            return(resultRoutes);
        }
コード例 #3
0
        static async Task GetRoutesAync()
        {
            List <AirportItem> Airports = new List <AirportItem>();

            try
            {
                var            context         = new TemplateEntities1();
                AirportService _airportService = new AirportService(context);
                _airportService.DeleteAirportsTripProvider(Providers.Transavia);
                Airports = _airportService.GetActiveAirports();
                foreach (AirportItem fromAirport in Airports)
                {
                    try
                    {
                        var client      = new HttpClient();
                        var queryString = HttpUtility.ParseQueryString(string.Empty);

                        // Request headers
                        client.DefaultRequestHeaders.Add("apikey", Key);

                        var uri      = "https://api.transavia.com/v2/airports/" + fromAirport.Code.ToUpper() + "?" + queryString;
                        var response = await client.GetAsync(uri);


                        if (response != null)
                        {
                            if (response.IsSuccessStatusCode && !response.StatusCode.ToString().Contains("NoContent"))
                            {
                                var contents = await response.Content.ReadAsStringAsync();

                                if (!String.IsNullOrWhiteSpace(contents))
                                {
                                    var clientRoutes      = new HttpClient();
                                    var queryStringRoutes = HttpUtility.ParseQueryString(string.Empty);

                                    // Request headers
                                    clientRoutes.DefaultRequestHeaders.Add("apikey", Key);
                                    queryStringRoutes["Origin"] = fromAirport.Code.ToUpper();

                                    var uriRoutes      = "https://api.transavia.com/v3/routes/?" + queryStringRoutes;
                                    var responseRoutes = await clientRoutes.GetAsync(uriRoutes);


                                    if (responseRoutes != null)
                                    {
                                        if (responseRoutes.IsSuccessStatusCode && !responseRoutes.StatusCode.ToString().Contains("NoContent"))
                                        {
                                            var contentsRoutes = await responseRoutes.Content.ReadAsStringAsync();

                                            if (!String.IsNullOrWhiteSpace(contentsRoutes))
                                            {
                                                var     serializer = new JavaScriptSerializer();
                                                dynamic data       = serializer.Deserialize(contentsRoutes, typeof(object));
                                                if (data != null && data.Length > 0)
                                                {
                                                    foreach (var route in data)
                                                    {
                                                        _airportService.AddAirportsTripProviderItem(route["id"].Split('-')[0], route["id"].Split('-')[1], Providers.Transavia);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        FlightsEngine.Utils.Logger.GenerateInfo("Transavia Response null | FromAirport : " + fromAirport.Code);
                                    }
                                }
                            }
                        }
                        else
                        {
                            FlightsEngine.Utils.Logger.GenerateInfo("Transavia Response null | FromAirport : " + fromAirport.Code);
                        }
                    }
                    catch (Exception ex2)
                    {
                        FlightsEngine.Utils.Logger.GenerateError(ex2, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "FromAirport : " + fromAirport.Code);
                    }
                }
            }
            catch (Exception e)
            {
                FlightsEngine.Utils.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
        }