public async Task <AirlineRootObject> ReturnSchedule(string startDate, string endDate, string origin) { using (HttpClient client = new HttpClient()) { // Basic authentication headers are added to the HttpClient 'client' before a request is made. // This tells Flight Aware which username and API key should be used for the request. client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, apiKey)))); //int startDate = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; //int endDate = startDate + 43200; // GET request URL if (origin != "*") { Uri url = new Uri($"http://flightxml.flightaware.com/json/FlightXML2/AirlineFlightSchedules?startDate={startDate}&endDate={endDate}&origin={origin}&destination=EGPF&howMany=10&offset=0"); HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false); // The client is passed the url for the request. // The response from Flight Aware is stored as a HttpResponseMessage. string json; // This takes the HTTP response and extracts the content. using (HttpContent content = response.Content) { // This is then assigned to the 'json' string json = await content.ReadAsStringAsync().ConfigureAwait(false); } AirlineRootObject result = JsonConvert.DeserializeObject <AirlineRootObject>(json); return(result); } else { Uri url = new Uri($"http://flightxml.flightaware.com/json/FlightXML2/AirlineFlightSchedules?startDate={startDate}&endDate={endDate}&destination=EGPF&howMany=10&offset=0"); HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false); string json; using (HttpContent content = response.Content) { json = await content.ReadAsStringAsync().ConfigureAwait(false); } AirlineRootObject result = JsonConvert.DeserializeObject <AirlineRootObject>(json); return(result); } } }
public async Task <IActionResult> GetAirlineSchedule(string startDate, string endDate, string origin) { AirlineRootObject result = await _flightService.GetAirlineSchedule(startDate, endDate, origin); return(Ok(result)); }