示例#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.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);
            }
        }