public async Task <JsonResult> Index(string id) { string yelpKey = Environment.GetEnvironmentVariable("yelp_key"); if (!string.IsNullOrWhiteSpace(yelpKey)) { using HttpClient client = new HttpClient { BaseAddress = new Uri(string.Format(InitialRequestURLBase, id)) }; client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", yelpKey); HttpResponseMessage response = client.GetAsync(InitialRequestURLParameters).Result; if (response.IsSuccessStatusCode) { YelpReviewAnswer yelpInitialAnswer = await response.Content.ReadAsAsync <YelpReviewAnswer>(); List <ReviewResult> resultWithLocations = await GetLocations(yelpInitialAnswer, id, yelpKey); return(new JsonResult(resultWithLocations)); } else { return(new JsonResult(new { status_code = response.StatusCode, reason_phrase = response.ReasonPhrase })); } } else { return(new JsonResult(new { status_code = 500, reason_phrase = "Internal Server Error", further_help = "Yelp API key not provided." })); } }
private async Task <List <ReviewResult> > GetLocations(YelpReviewAnswer yelpInitialAnswer, string initialRequestBusinessId, string yelpKey) { List <ReviewResult> withLocations = new List <ReviewResult>(); BusinessLocation businessLocation = await GetBusinessLocation(initialRequestBusinessId, yelpKey); for (int i = 0; i < yelpInitialAnswer.Reviews.Length; i++) { withLocations.Add( new ReviewResult { Business_location = businessLocation, Content = yelpInitialAnswer.Reviews[i].Text, Rating = yelpInitialAnswer.Reviews[i].Rating, Reviewer = await GetReviewerInfo(yelpInitialAnswer.Reviews[i].User) } ); } return(withLocations); }