public async Task <IActionResult> Search(string query, SearchRequestSortType sort = SearchRequestSortType.relevance) { var searchRequestInput = new SearchRequestInput { query = query, format = SearchRequestFormat.json, sort = sort }; var productList = new List <ProductInfoFull>(); var searchResponse = await _walmartApiService.SearchProductsAsync(searchRequestInput); if ((!searchResponse.HasError) && (searchResponse.Response.items?.Count() > 0)) { var productLookupParams = new ProductLookupRequestInput { ids = GetTopNItemIds(searchResponse.Response.items, 10) }; var productLookupResponse = await _walmartApiService.GetProductDetailAsync(productLookupParams); productList = ProcessProductLookupResponse(productLookupResponse); } return(Ok(productList)); }
public void ReturnItemsForQuery() { //Arrange WalmartApiConfig urls = ApiTestDataHelper.GetApiConfig(); IApiRequestBuilder requestbuilder = new ApiRequestBuilder(); var mockWalmartApiRequestManager = new Mock <IHttpApiRequestManager>(); var responseDataSample = ApiTestDataHelper .GetApiResponseDataFromResource("Walmart.Api.Client.Tests.SearchApiSampleResponse.json"); mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>())) .Returns(Task.FromResult <string>(responseDataSample)); var mockLogger = new Mock <ILogger <ApiServiceBase> >(); IWalmartApiService productService = new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object); SearchRequestInput request = new SearchRequestInput { query = "Ben & Jerry's ice cream", sort = SearchRequestSortType.bestseller }; //Act var result = productService.SearchProductsAsync(request).Result; //Assert Assert.False(result.HasError); Assert.Equal(10, result.Response.items.Count); mockWalmartApiRequestManager.VerifyAll(); }
SearchProductsAsync(SearchRequestInput searchParameters) { var queryString = _apiRequestBuilder .GetSearchApiQueryString(searchParameters, _walmartApiConfig.ApiKey); var apiUrl = $"{_walmartApiConfig.ProductSearchUrl}?{queryString}"; return(GetDataFromApi <SearchResult <FullResponseItem> >(apiUrl)); }
public string GetSearchApiQueryString(SearchRequestInput request, string apiKey) { string result = ""; result = result .AppendValue <string>(nameof(apiKey), apiKey) .AppendValue <string>(nameof(request.query), request.query) .AppendValue <int>(nameof(request.numItems), request.numItems) .AppendValue <SearchRequestSortType>(nameof(request.sort), request.sort) .AppendValue <int>(nameof(request.categoryId), request.categoryId) .AppendValue <string>(nameof(request.responseGroup), ResponseGroupValueBase); return(result); }
public void BuildSearchRequestQuery() { //Arrange IApiRequestBuilder builder = new ApiRequestBuilder(); SearchRequestInput request = new SearchRequestInput { query = "Ben & Jerry's ice cream", sort = SearchRequestSortType.bestseller }; //Act var result = builder.GetSearchApiQueryString(request, "test"); //Assert var expectedValue = @"apiKey=test&query=Ben+%26+Jerry%27s+ice+cream&numItems=10&sort=bestseller&categoryId=0&responseGroup=base"; Assert.Equal(expectedValue, result); }
public void ReturnErrorCodeWhenApiCallFails() { //Arrange WalmartApiConfig urls = ApiTestDataHelper.GetApiConfig(); IApiRequestBuilder requestbuilder = new ApiRequestBuilder(); var mockWalmartApiRequestManager = new Mock <IHttpApiRequestManager>(); mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>())) .Returns(Task.FromResult <string>("")); var mockWebResponse = new Mock <HttpWebResponse>(); mockWebResponse.Setup(res => res.StatusCode).Returns(HttpStatusCode.BadRequest); mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>())) .Throws( new WebException("Missing search query", null, WebExceptionStatus.UnknownError, mockWebResponse.Object)); var mockLogger = new Mock <ILogger <ApiServiceBase> >(); IWalmartApiService productService = new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object); SearchRequestInput request = new SearchRequestInput { query = "Ben & Jerry's ice cream", sort = SearchRequestSortType.bestseller }; //Act var result = productService.SearchProductsAsync(request).Result; //Assert Assert.True(result.HasError); Assert.Equal("400", result.ErrorCode); mockWalmartApiRequestManager.VerifyAll(); mockWebResponse.VerifyAll(); }