/// <summary> /// this function will get data from api and then in case there are no results then will return /// to the main project that there are nothing to show /// </summary> /// <param name="distance">the distance the user wishes to travel by starship</param> /// <returns>it will return true if all calculations done and displayed or false in case of failure</returns> public bool ProcessData(decimal distance) { ///bringing data from the api starships ships = _dataapi.getstarships(); int nbstops = 0; if (ships == null) { return(false); } ///checking if api has returned results if (ships.results.Count() != 0) { ///looping the startships, calculating the number of stops and displaying the results foreach (starship ship in ships.results) { ///calculating the number of stops nbstops = _calc.getnumberofstops(distance, ship.MGLT, ship.consumables); if (nbstops > -1) { ///displaying the results _disp.displaylistshipandstop(ship, nbstops); } } } return(true); }
/// <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); }
public void testgetstarships_wrongurl() { //act starships ships = dataapi.getstarshipsfromapi("myapi"); //assert Assert.IsNull(ships); }
public void testgetstarships_success() { //act starships ships = dataapi.getstarshipsfromapi(""); //assert Assert.IsNotNull(ships); }
public void testgetstarships_null() { //act starships ships = dataapi.getstarshipsfromapi(null); //assert Assert.IsNull(ships); }