/// <summary> /// processing the data means checking if user has entered -1 /// if so then leave else check if number is negative then prompt the user again /// if number is positive then check if the api is valid and returns data then make /// the calculations and display the results, else give an error message and exit /// </summary> /// <param name="strdist">it is the user input for the distance to travle</param> /// <returns>true for success and false for failure</returns> public bool applyprocess(string strdist) { decimal dbldistance = 0; if (strdist.Trim() != "-1") { decimal.TryParse(strdist, out dbldistance); if (dbldistance > 0) { if (!_businesslogic.ProcessData(dbldistance)) { return(false); } else { return(true); } } else { _disp.displaymessage("Please enter a positive numeric number!", "info"); return(true); } } else { return(false); } }
/// <summary> /// getting data from the api /// </summary> /// <param name="strurl">the url to call the api if empty take the default address else use the url sent</param> /// <returns>an object of type starships</returns> public starships getstarshipsfromapi(string strurl) { string strresult; starships ships = null; using (WebClient client = new WebClient()) { ///using webclient in order to bring data from api, and need the data to be of type JSON client.Headers.Add("Content-Type: application/json"); client.Headers.Add("Accept: application/json"); ///if url empty take the default url if (strurl == "") { strurl = "https://swapi.co/api/starships"; } try { ///get data from the api strresult = client.DownloadString(strurl); ///convert JSON data into ojects of type starships ships = JsonConvert.DeserializeObject <starships>(strresult); } catch (Exception ex) { ///if an error or url not valid send back an error message _disp.displaymessage($"Error in Getting data { ex.Message }", "error"); } } return(ships); }