Last active
April 4, 2018 10:32
-
-
Save antdimot/83ad0b90abf857d683b828fd00621b64 to your computer and use it in GitHub Desktop.
Flightstats API wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FlightManager | |
{ | |
string _protocol = "rest"; | |
string _format = "json"; | |
string _flightstatusBaseApiUrl; | |
string _flightscheduleBaseApiUrl; | |
public string AppId { get; private set; } | |
public string AppKey { get; private set; } | |
public FlightManager( string appId, string appKey ) | |
{ | |
AppId = appId; | |
AppKey = appKey; | |
_flightstatusBaseApiUrl = $"https://api.flightstats.com/flex/flightstatus/{_protocol}/v2/{_format}/flight/status/"; | |
_flightscheduleBaseApiUrl = $"https://api.flightstats.com/flex/schedules/{_protocol}/v1/{_format}/flight/"; | |
} | |
public async Task<FlightStatus> GetFlightStatusAsync( string carrier, int flightNumber, DateTime date ) | |
{ | |
var flightStatusByArriving = $"{carrier}/{flightNumber}/arr/{date.Year}/{date.Month}/{date.Day}"; | |
var uriBuilder = new UriBuilder( _flightstatusBaseApiUrl + flightStatusByArriving ) { | |
Query = $"appId={AppId}&appKey={AppKey}" | |
}; | |
try | |
{ | |
using( var client = new HttpClient() ) | |
{ | |
var response = await client.GetAsync( uriBuilder.Uri ); | |
response.EnsureSuccessStatusCode(); | |
var jsonResult = await response.Content.ReadAsStringAsync(); | |
dynamic obj = JsonConvert.DeserializeObject( jsonResult ); | |
return FlightStatus.Create( obj ); | |
} | |
} | |
catch( HttpRequestException ex ) | |
{ | |
// todo: add exception log here | |
throw ex; | |
} | |
} | |
public async Task<FlightSchedule> GetFlightScheduleAsync( string carrier, int flightNumber, DateTime date ) | |
{ | |
var flightScheduleByDeparture = $"{carrier}/{flightNumber}/departing/{date.Year}/{date.Month}/{date.Day}"; | |
var uriBuilder = new UriBuilder( _flightscheduleBaseApiUrl + flightScheduleByDeparture ) { | |
Query = $"appId={AppId}&appKey={AppKey}" | |
}; | |
try | |
{ | |
using( var client = new HttpClient() ) | |
{ | |
var response = await client.GetAsync( uriBuilder.Uri ); | |
response.EnsureSuccessStatusCode(); | |
var jsonResult = await response.Content.ReadAsStringAsync(); | |
dynamic obj = JsonConvert.DeserializeObject( jsonResult ); | |
return FlightSchedule.Create( obj ); | |
} | |
} | |
catch( HttpRequestException ex ) | |
{ | |
// todo: add exception log here | |
throw ex; | |
} | |
} | |
} | |
public class FlightStatus | |
{ | |
public string Status { get; set; } | |
public long FlightId { get; set; } | |
public string DepartureAirport { get; set; } | |
public string ArriveAirport { get; set; } | |
public int? DelayDepartureMinutes { get; set; } | |
public int? DelayArrivalMinutes { get; set; } | |
public bool HasError { get; set; } = false; | |
public string ErrorMessage { get; set; } | |
// create flight status from dynamic json object (response of flightstats api call) | |
public static FlightStatus Create( dynamic data ) | |
{ | |
var obj = new FlightStatus(); | |
try | |
{ | |
if( data.error != null ) | |
{ | |
obj.HasError = true; | |
obj.ErrorMessage = data.error.errorMessage; | |
return obj; | |
} | |
var flightStatuses = data.flightStatuses; | |
if( flightStatuses != null && flightStatuses.Count > 0 ) | |
{ | |
obj.FlightId = flightStatuses[0].flightId.Value; | |
obj.DepartureAirport = flightStatuses[0].departureAirportFsCode.Value; | |
obj.ArriveAirport = flightStatuses[0].arrivalAirportFsCode.Value; | |
obj.Status = flightStatuses[0].status.Value; | |
var delays = flightStatuses[0].delays; | |
if( delays != null ) | |
{ | |
obj.DelayDepartureMinutes = delays.departureGateDelayMinutes; | |
obj.DelayArrivalMinutes = delays.arrivalGateDelayMinutes; | |
} | |
} | |
} | |
catch( Exception ex ) | |
{ | |
throw ex; | |
} | |
return obj; | |
} | |
} | |
public class FlightSchedule | |
{ | |
public string DepartureAirport { get; set; } | |
public string ArriveAirport { get; set; } | |
public bool HasError { get; set; } = false; | |
public string ErrorMessage { get; set; } | |
public static FlightSchedule Create( dynamic data ) | |
{ | |
var obj = new FlightSchedule(); | |
try | |
{ | |
if( data.error != null ) | |
{ | |
obj.HasError = true; | |
obj.ErrorMessage = data.error.errorMessage; | |
return obj; | |
} | |
var scheduledFlights = data.scheduledFlights; | |
if( scheduledFlights != null && scheduledFlights.Count > 0 ) | |
{ | |
obj.DepartureAirport = scheduledFlights[0].departureAirportFsCode.Value; | |
obj.ArriveAirport = scheduledFlights[0].arrivalAirportFsCode.Value; | |
} | |
} | |
catch( Exception ex ) | |
{ | |
throw ex; | |
} | |
return obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment