public async Task <ActionResult> HandleForm(GoogleSearchRequest googlesearchrequest) { GoogleSearchAPIRequest obj_APIRequest = new GoogleSearchAPIRequest(googlesearchrequest.SearchTerms, 1); GoogleSearchResponse obj_Response = new GoogleSearchResponse(googlesearchrequest.UrlToFind); bool b_NoSearchError = true; // flag for any errors picked up during GoofgleSearchAPI requests. int int_StartIndex = 1; // index in Google Search is 1-based not zero-based. So we start at 1 instead of zero. while (int_StartIndex <= 91 && b_NoSearchError) { await obj_APIRequest.FireAPISeachRequest(); while (obj_APIRequest.RequestInProcess()) { } if (obj_APIRequest.APIRequestState == GoogleSearchAPIRequest.GoogleSearchAPIRequestState.ResponseComplete) // if request returned successfully, process the result { // Process the actual JSON string returned from the GoogleSearchAPI. // Parse it into a custom object we can utitlise to access the results more easily. obj_Response.AddResultsFromAPI_JSONResponse(obj_APIRequest.GetAPISearchResponse()); } else { b_NoSearchError = false; //_lbl_ResponseAsText.Text = _obj_SearchRequest.GetAPISearchError(); } int_StartIndex += obj_APIRequest.ResultsPerRequest; } return(View("Results", obj_Response)); }
public async Task <IActionResult> SearchGoogle([FromBody] GoogleSearchRequest request) { if (!ModelState.IsValid) { return(BadRequest("Invalid request")); } if (!Uri.IsWellFormedUriString(request.UrlToFind, UriKind.Absolute)) { return(BadRequest("Invalid URL to find")); } try { var result = await searchService.GetSearchResults(request); return(Ok(result)); } catch (HttpRequestException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { logger.LogError(ex, "An error has occurred"); return(StatusCode(500)); } }
public async Task <IActionResult> SearchGoogle([FromForm] GoogleSearchRequest request) { if (!ModelState.IsValid) { ViewData["SearchResults"] = "Invalid request"; return(View("Index")); } if (!Uri.IsWellFormedUriString(request.UrlToFind, UriKind.Absolute)) { ViewData["SearchResults"] = "Invalid URL to find"; return(View("Index")); } try { var result = await searchService.GetSearchResults(request); ViewData["SearchResults"] = string.Join(", ", result); return(View("Index")); } catch (HttpRequestException ex) { ViewData["SearchResults"] = ex.Message; return(View("Index")); } catch (Exception ex) { logger.LogError(ex, "An error has occurred"); return(StatusCode(500)); } }
public async Task TestGetSearchResults_FindsRequiredOccurrences() { string file; var assembly = typeof(TestSearchService).GetTypeInfo().Assembly; using (var stream = assembly.GetManifestResourceStream("Tests.TestFiles.NetflixResults.txt")) { using (var reader = new StreamReader(stream)) { file = reader.ReadToEnd(); } } var keywords = new[] { "this", "is", "a", "test" }; var request = new GoogleSearchRequest { Keywords = keywords, UrlToFind = "https://www.netflix.com" }; var mockClient = new Mock <IGoogleSearchClient>(); mockClient .Setup(x => x.GetGoogleSearchResultsAsync("/search?q=this+is+a+test&num=100")) .ReturnsAsync(file); var service = new SearchService(mockClient.Object, LoggerFactory); var result = await service.GetSearchResults(request); Assert.AreEqual(2, result.Length); Assert.AreEqual("11", result[0]); Assert.AreEqual("12", result[1]); mockClient.VerifyAll(); }
public async Task OnPostAsync() { var request = new GoogleSearchRequest { Keywords = SplitKeywords(), UrlToFind = URL }; SearchResults = await service.GetSearchResults(request); }
public Task <GoogleSearchResponse> Search(string request) { var googleRequest = new GoogleSearchRequest { Cx = _googleSearchSettings.Cx, Key = _googleSearchSettings.ApiKey, Num = 10, Q = request }; return(_restClient.GetAsync <GoogleSearchRequest, GoogleSearchResponse>(_googleSearchSettings.Endpoint, googleRequest)); }
public async Task <string[]> GetSearchResults(GoogleSearchRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var numberOfResultsToReturn = request.NumberOfResults == 0 ? 100 : request.NumberOfResults; var searchString = $"/search?q={string.Join('+', request.Keywords)}&num={numberOfResultsToReturn}"; using (logger.BeginScope(nameof(GetSearchResults))) { try { var rawSearchResultsHtml = await client.GetGoogleSearchResultsAsync(searchString); if (TryProcessSearchResults(rawSearchResultsHtml, request.UrlToFind, out string[] results))
public async Task TestGetSearchResults_CorrectSearchStringPassed() { var keywords = new[] { "this", "is", "a", "test" }; var request = new GoogleSearchRequest { Keywords = keywords, UrlToFind = "http://www.test.com" }; var mockClient = new Mock <IGoogleSearchClient>(); mockClient .Setup(x => x.GetGoogleSearchResultsAsync("/search?q=this+is+a+test&num=100")) .ReturnsAsync("Success"); var service = new SearchService(mockClient.Object, LoggerFactory); var result = await service.GetSearchResults(request); Assert.AreEqual(1, result.Length); Assert.AreEqual("0", result[0]); mockClient.VerifyAll(); }
public async Task TestRazorPage_CorrectlyProcessesKeywords(string keywords, string[] splitKeywords, string[] results) { var mockService = new Mock <IGoogleSearchService>(); var page = new IndexModel(mockService.Object); page.Keywords = keywords; page.URL = "http://www.google.com"; var request = new GoogleSearchRequest { Keywords = splitKeywords, NumberOfResults = 0, UrlToFind = "http://www.google.com" }; mockService .Setup(x => x.GetSearchResults(It.Is <GoogleSearchRequest>(r => new GoogleSearchRequestComparer().Equals(r, request)))) .ReturnsAsync(results); await page.OnPostAsync(); Assert.AreEqual(results, page.SearchResults); mockService.VerifyAll(); }