Link Flights with Airports JSON

Jassim Al Rahma 1,616 Reputation points
2022-07-28T15:18:35.28+00:00

Hi,

I am using airlabs web services to get the flight schedules but the schedules API doesn't have the airport name.. it only has the airport code.

So I am thinking to link it with the Airports API

then link the dep_iata and arr_iata from the schedules API with the airport API to get the city for both

below is my current code..

Kindy help...

var client = new HttpClient();  
  
// client.Timeout = TimeSpan.FromSeconds(App.HttpClient_TimeOut);  
client.BaseAddress = new Uri("https://airlabs.co/api/v9/schedules");  
  
var content = new FormUrlEncodedContent(new[]  
{  
    new KeyValuePair<string, string>("api_key", App.WebServiceAppID),  
    new KeyValuePair<string, string>(what, airport),  
    new KeyValuePair<string, string>("_fields", "arr_time, flight_iata, status"),  
});  
  
var response = await client.PostAsync("https://airlabs.co/api/v9/schedules", content);  
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. ShuaiHua Du 636 Reputation points
    2022-07-30T04:23:42.657+00:00

    Hi,

    Please refer below code:

    namespace MSTestProject  
    {  
        [TestClass]  
        public class TestLinkFlightClass  
        {  
      
            [TestMethod]  
            public void TestLinkFlight()  
            {  
                var airportResponse = GetAirportResponse();  
      
                var airports = Enumerable.Empty<Airport>().ToList();  
      
                if (airportResponse != null && airportResponse.Airports != null && airportResponse.Airports.Count > 0)  
                {  
                    airports = airportResponse.Airports.DistinctBy(x => x.iata_code).ToList();  
                }  
      
                var flightScheduleResposne = GetFlightScheduleResponse();  
      
                var flightSchedules = Enumerable.Empty<FlightSchedule>().ToList();  
      
                if (flightScheduleResposne != null && flightScheduleResposne.FlightSchedules != null && flightScheduleResposne.FlightSchedules.Count > 0)  
                {  
                    var flightScheduleWithAirports = SetFlightScheduleAirport(flightScheduleResposne.FlightSchedules, airports);  
      
                    foreach (var item in flightScheduleWithAirports)  
                    {  
      
                        Console.WriteLine($"The name of dep airport of {item.dep_iata}:");  
                        Console.WriteLine(item.DepAirport.name);  
                        Console.WriteLine(item.DepAirport.names.ar);//name for mutiple language  
      
      
                        Console.WriteLine($"The name of arr airport of {item.arr_iata}:");  
                        Console.WriteLine(item.ArrAirport.name);  
                        Console.WriteLine(item.ArrAirport.names.cs);//name for mutiple language  
                    }  
                }  
            }  
      
            IList<FlightScheduleWithAirport> SetFlightScheduleAirport(IList<FlightSchedule> flightSchedules, IList<Airport> airports)  
            {  
                var flightScheduleWithAirport = flightSchedules.Select(x => x.ToFlightScheduleWithName()).ToList();  
                foreach (var item in flightScheduleWithAirport)  
                {  
                    item.DepAirport = airports.FirstOrDefault(x => item.dep_iata == x.iata_code);  
                    item.ArrAirport = airports.FirstOrDefault(x => item.arr_iata == x.iata_code);  
                }  
      
                return flightScheduleWithAirport;  
            }  
      
      
            AirportResponse GetAirportResponse()  
            {  
                //You logic for get the AirportResponse from API  
      
                //Below code is only for test  
                return new AirportResponse()  
                {  
                    Airports = new List<Airport>  
                    {  
                         new Airport  
                         {  
                            name = "DepAirport1",  
                            iata_code ="DepCode1",  
                            names = new Names{ ar = "DepAirport1_ar", cs = "DepAirport1_cs" }//...othe names property  
                         },  
                         new Airport  
                         {  
                            name = "DepAirport2",  
                            iata_code ="DepCode2",  
                            names = new Names{ ar = "DepAirport2_ar", cs = "DepAirport2_cs" }//...othe names property  
                         },  
                         new Airport  
                         {  
                            name = "ArrAirport1",  
                            iata_code ="ArrCode1",  
                            names = new Names{ ar = "ArrAirport1_ar", cs = "ArrAirport1_cs" }//...othe names property  
                         },  
                         new Airport  
                         {  
                            name = "ArrAirport2",  
                            iata_code ="ArrCode2",  
                            names = new Names{ ar = "ArrAirport2_ar", cs = "ArrAirport2_cs" }//...othe names property  
                         }  
                    }  
                };  
            }  
      
            FlightScheduleResponse GetFlightScheduleResponse()  
            {  
                //You logic for get the FlightScheduleResponse from API  
      
                //Below code is only for test  
                return new FlightScheduleResponse  
                {  
                    FlightSchedules = new List<FlightSchedule>  
                    {  
                         new FlightSchedule{ dep_iata ="DepCode1",arr_iata="ArrCode1" },  
                         new FlightSchedule{ dep_iata ="DepCode2",arr_iata="ArrCode2" }  
                    }  
                };  
      
            }  
        }  
      
        public class FlightScheduleResponse  
        {  
            public IList<FlightSchedule> FlightSchedules { get; set; }  
        }  
      
        public class FlightScheduleWithAirport : FlightSchedule  
        {  
            public Airport DepAirport { get; set; }  
            public Airport ArrAirport { get; set; }  
        }  
      
        public class FlightSchedule  
        {  
            public string airline_iata { get; set; }  
            public string airline_icao { get; set; }  
            public string flight_iata { get; set; }  
            public string flight_icao { get; set; }  
            public string flight_number { get; set; }  
            public string cs_airline_iata { get; set; }  
            public string cs_flight_number { get; set; }  
            public string cs_flight_iata { get; set; }  
            public string dep_iata { get; set; }  
            public string dep_icao { get; set; }  
            public string dep_terminal { get; set; }  
            public string dep_gate { get; set; }  
            public string dep_time { get; set; }  
            public int dep_time_ts { get; set; }  
            public string dep_time_utc { get; set; }  
            public string dep_estimated { get; set; }  
            public int dep_estimated_ts { get; set; }  
            public string dep_estimated_utc { get; set; }  
            public string arr_iata { get; set; }  
            public string arr_icao { get; set; }  
            public string arr_terminal { get; set; }  
            public string arr_gate { get; set; }  
            public string arr_baggage { get; set; }  
            public string arr_time { get; set; }  
            public int arr_time_ts { get; set; }  
            public string arr_time_utc { get; set; }  
            public string arr_estimated { get; set; }  
            public int arr_estimated_ts { get; set; }  
            public string arr_estimated_utc { get; set; }  
            public string status { get; set; }  
            public int duration { get; set; }  
            public int delayed { get; set; }  
      
            public FlightScheduleWithAirport ToFlightScheduleWithName()  
            {  
                return new FlightScheduleWithAirport  
                {  
                    dep_iata = dep_iata,  
                    arr_iata = arr_iata,  
                    ///....Other property to set  
                    ///...  
                    ///...  
                    ///...  
                    ///...  
                    ///...  
                    DepAirport = default,  
                    ArrAirport = default  
                };  
            }  
        }  
      
      
      
        public class AirportResponse  
        {  
            public IList<Airport> Airports { get; set; }  
        }  
      
        public class Airport  
        {  
            public string name { get; set; }  
            public string iata_code { get; set; }  
            public string icao_code { get; set; }  
            public float lat { get; set; }  
            public float lng { get; set; }  
            public int alt { get; set; }  
            public string city { get; set; }  
            public string city_code { get; set; }  
            public string un_locode { get; set; }  
            public string timezone { get; set; }  
            public string country_code { get; set; }  
            public string phone { get; set; }  
            public string website { get; set; }  
            public string facebook { get; set; }  
            public string instagram { get; set; }  
            public string linkedin { get; set; }  
            public string twitter { get; set; }  
            public Names names { get; set; }  
            public int runways { get; set; }  
            public int departures { get; set; }  
            public int connections { get; set; }  
            public bool is_major { get; set; }  
            public int is_international { get; set; }  
            public string slug { get; set; }  
        }  
      
        public class Names  
        {  
            public string no { get; set; }  
            public string de { get; set; }  
            public string hi { get; set; }  
            public string ln { get; set; }  
            public string ru { get; set; }  
            public string fi { get; set; }  
            public string pt { get; set; }  
            public string jv { get; set; }  
            public string fr { get; set; }  
            public string hu { get; set; }  
            public string wuu { get; set; }  
            public string uk { get; set; }  
            public string sk { get; set; }  
            public string id { get; set; }  
            public string mk { get; set; }  
            public string sv { get; set; }  
            public string ko { get; set; }  
            public string pnb { get; set; }  
            public string mr { get; set; }  
            public string el { get; set; }  
            public string en { get; set; }  
            public string _is { get; set; }  
            public string it { get; set; }  
            public string ta { get; set; }  
            public string es { get; set; }  
            public string cs { get; set; }  
            public string ar { get; set; }  
            public string vi { get; set; }  
            public string th { get; set; }  
            public string ja { get; set; }  
            public string fa { get; set; }  
            public string pl { get; set; }  
            public string ro { get; set; }  
            public string he { get; set; }  
            public string tr { get; set; }  
            public string nl { get; set; }  
        }  
      
    }  
    

    If right, please Accept.
    Enjoy programming!!!

    0 comments No comments

  2. Neha Jangid 0 Reputation points
    2023-12-06T12:10:51.54+00:00

    Hi Jassim,

    If you want flight schedule data with the airport name.

    Instead of linking 2 different APIs, you can simply use, this API that provides flight schedule and status data with the airport name. For more information check out the documentation.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.