public async Task <FlightPlan> GetExternalFlightPlanAsync(string id) { FlightPlan flightPlan = null; try { //get the server that has the flightPlan string serverId = flightplanServer[id]; ServerFlight s = db.GetServerById(serverId); if (serverId == null) { return(null); } string url = "api/FlightPlan/" + id; //build new http client to new request HttpClient httpClient = BuildHttpClient(s); try { //get string response and convert to object flight plan string resp = await ResponceAsync(httpClient, url); flightPlan = JsonConvert.DeserializeObject <FlightPlan>(resp); } catch (Exception) { //Console.WriteLine("failed in external filghtPlan by id get response"); } } catch //no server has the flight plan { flightPlan = null; } return(flightPlan); }
//build new Http client for each request from extenal server private HttpClient BuildHttpClient(ServerFlight s) { HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(s.ServerUrl); //add header fields for jyson httpClient.DefaultRequestHeaders.Add("User-Agent", "C# console program"); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); return(httpClient); }
public ServerFlight GetServerById(string id) { //get server from DB by specific id ServerFlight f = null; string stm = "SELECT * FROM ServerTable WHERE id=\'" + id + "\'"; using var cmd1 = new SQLiteCommand(stm, connection); using SQLiteDataReader rdr = cmd1.ExecuteReader(); if (rdr.Read()) { f = new ServerFlight(rdr.GetString(0), rdr.GetString(1)); } return(f); }
public void AddServer(ServerFlight s) { using var cmd = new SQLiteCommand(connection); string a = " VALUES(" + "\'" + s.ServerId + "\',\'" + s.ServerUrl + "\')"; cmd.CommandText = "INSERT INTO ServerTable(id,url)" + a; int suc = cmd.ExecuteNonQuery(); //0 - not insert, -1 exception if (suc == 0 || suc == -1) { throw new Exception(); } }
public void AddServer(ServerFlight s) { //add new server to DB List <ServerFlight> list = db.GetServers(); foreach (ServerFlight server in list) { //same url - throw exception if (server.ServerUrl == s.ServerUrl) { throw new Exception(); } } if (s.ServerId == null || s.ServerUrl == null) { throw new Exception(); } db.AddServer(s); }