/// <summary> /// Method to receive data from API end point as a collection of objects /// /// JsonConvert parses the JSON string into classes /// </summary> /// <returns></returns> public Places GetPlaces() { string NATIONAL_PARK_API_PATH = BASE_URL + API_KEY; string placeData = ""; string nextpagetoken = ""; Places places = null; httpClient.BaseAddress = new Uri(NATIONAL_PARK_API_PATH); // It can take a few requests to get back a prompt response, if the API has not received // calls in the recent past and the server has put the service on hibernation try { HttpResponseMessage response = httpClient.GetAsync(NATIONAL_PARK_API_PATH).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { placeData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } if (!placeData.Equals("")) { // Deserialize Placedata string into the Attractions Object. //places = JsonConvert.DeserializeObject<Root>(placedata); //response_parse = JsonConvert.DeserializeObject<Dictionary<string, object>>(Convert.ToString(placesdata)); while (true) { placeData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); places = JsonConvert.DeserializeObject <Places>(placeData); object has_next_page = places.GetType().GetProperty("next_page_token"); if (!has_next_page.Equals("") || has_next_page is null) /// Check how to handle Null, should we use is null while comparing string null (actual NULL)?? . { Console.WriteLine("the value is:" + has_next_page); break; } else { nextpagetoken = Convert.ToString(places.next_page_token); string URL_Copy = NATIONAL_PARK_API_PATH; string URL_Final = URL_Copy + "&pagetoken=" + nextpagetoken; response = httpClient.GetAsync(URL_Final).GetAwaiter().GetResult(); } } } } catch (Exception e) { // This is a useful place to insert a breakpoint and observe the error message Console.WriteLine(e.Message); } return(places); }
public void PlacesConstructor_CreatesInstanceOfPlaces_Places() { Places newPlace = new Places("name", "picture", "staylength", "time traveled"); Assert.AreEqual(typeof(Places), newPlace.GetType()); }
public void PlacesConstructor_CreatesInstanceOfPlaces_Places() { Places newPlace = new Places("test", "test", "test"); Assert.AreEqual(typeof(Places), newPlace.GetType()); }
public ActionResult Plan(String user1, String password1, String destination) { List <Places> FinalResult = new List <Places>(); DataTable dt = new DataTable(); List <object> iData_Attraction = new List <object>(); List <object> iData_Rating = new List <object>(); var list = (from cust in dbContext.saveSearch where cust.user.Equals(user1) group cust by cust.destination into grp select new { destination = grp.Key, Count = grp.Count() }).ToList(); if (!String.IsNullOrEmpty(user1) || !String.IsNullOrEmpty(password1) && password1 != "") { Console.WriteLine("Inside Login Call " + user1); Console.WriteLine("user1 " + user1); Console.WriteLine("usedestinationr1 " + destination); int usrCount = (from usr in dbContext.userlogin where usr.user.Equals(user1) && usr.password.Equals(password1) select usr).Count(); if (usrCount == 0) { ViewBag.MandatoryCheck = "Please enter a valid Username or Password."; return(View("UserLogin")); } else { ViewBag.rep = JsonConvert.SerializeObject(list.Select(x => x.Count)); ViewBag.hod = JsonConvert.SerializeObject(list.Select(x => x.destination)); return(View(FinalResult)); } } httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Add("X-Api-Key", key); saveSearch save = new saveSearch(); save.user = user1; save.destination = destination; dbContext.Add(save); dbContext.SaveChanges(); httpClient.DefaultRequestHeaders.Accept.Add( new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); string URL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=%27" + destination + "%27+point+of+interest&language=en&key=" + key; string Google_Path = URL; string parksdata = ""; string nextpagetoken = ""; Places places = null; httpClient.BaseAddress = new Uri(Google_Path); try { PlanValidation Plan = new PlanValidation(); Plan.location2 = destination; if (!String.IsNullOrEmpty(Plan.location2)) { HttpResponseMessage response = httpClient.GetAsync(Google_Path).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { // Asynchronous for wait till get result, common for remote operation parksdata = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } if (!parksdata.Equals("")) { // Deserialize parksdata string into the Parks Object. //parks = JsonConvert.DeserializeObject<Root>(parksdata); //response_parse = JsonConvert.DeserializeObject<Dictionary<string, object>>(Convert.ToString(parksdata)); while (true) { parksdata = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); places = JsonConvert.DeserializeObject <Places>(parksdata); FinalResult.Add(places); object has_next_page = places.GetType().GetProperty("next_page_token"); if (!has_next_page.Equals("") || has_next_page is null) /// Check how to handle Null, should we use === while comparing string null (actual NULL)?? . { Console.WriteLine("the value is:" + has_next_page); break; } else { nextpagetoken = Convert.ToString(places.next_page_token); string URL_Copy = URL; string URL_Final = URL_Copy + "&pagetoken=" + nextpagetoken; response = httpClient.GetAsync(URL_Final).GetAwaiter().GetResult(); } } } ViewBag.TopAttraction = "Here are the best places to visit in " + Plan.location2 + "."; foreach (ISM6225FinalProject.Models.Places p in FinalResult) { foreach (Place r in p.results) { iData_Attraction.Add(r.name); } } var json_Attraction = JsonConvert.SerializeObject(iData_Attraction); Console.WriteLine("json_Attraction " + json_Attraction); ViewBag.iData_Attraction = json_Attraction; foreach (ISM6225FinalProject.Models.Places p in FinalResult) { foreach (Place r in p.results) { iData_Rating.Add(r.user_ratings_total); } } var json_Rating = JsonConvert.SerializeObject(iData_Rating); Console.WriteLine("json_Rating " + json_Rating); ViewBag.iData_Rating = json_Rating; list = (from cust in dbContext.saveSearch where cust.user.Equals(user1) group cust by cust.destination into grp select new { destination = grp.Key, Count = grp.Count() }).ToList(); ViewBag.rep = JsonConvert.SerializeObject(list.Select(x => x.Count)); ViewBag.hod = JsonConvert.SerializeObject(list.Select(x => x.destination)); return(View(FinalResult)); } else { return(View(FinalResult)); } } catch (Exception e) { Console.WriteLine(e.Message); } return(View(FinalResult)); }