public void ReturnItemDetailForSingleProduct() { //Arrange WalmartApiConfig urls = ApiTestDataHelper.GetApiConfig(); IApiRequestBuilder requestbuilder = new ApiRequestBuilder(); var mockWalmartApiRequestManager = new Mock <IHttpApiRequestManager>(); var responseDataSample = ApiTestDataHelper .GetApiResponseDataFromResource("Walmart.Api.Client.Tests.ProductLookupSingleItemSampleResponse.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); var itemId = 10294196; ProductLookupRequestInput request = new ProductLookupRequestInput { ids = new List <int> { itemId } }; //Act var result = productService.GetProductDetailAsync(request).Result; //Assert Assert.False(result.HasError); Assert.Single <FullResponseItem>(result.Response.items); Assert.Equal(itemId, result.Response.items[0].ItemId); mockWalmartApiRequestManager.VerifyAll(); }
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)); }
GetProductDetailAsync(ProductLookupRequestInput productLookupParameters) { var queryString = _apiRequestBuilder.GetProductLookupApiQueryString(productLookupParameters, _walmartApiConfig.ApiKey); var apiUrl = $"{_walmartApiConfig.ProductLookupUrl}?{queryString}"; return(GetDataFromApi <ProductLookupResult>(apiUrl)); }
public string GetProductLookupApiQueryString(ProductLookupRequestInput request, string apiKey) { string result = ""; result = result .AppendValue <string>(nameof(apiKey), apiKey) .AppendValue <string>(nameof(request.ids), request.ids.GetCommaSeparatedItems()); return(result); }
public void BuildProductLookupMultipleItemRequestQuery() { //Arrange IApiRequestBuilder builder = new ApiRequestBuilder(); ProductLookupRequestInput request = new ProductLookupRequestInput { ids = new List <int> { 93874234, 238479023 } }; //Act var result = builder.GetProductLookupApiQueryString(request, "test"); //Assert var expectedValue = @"apiKey=test&ids=93874234%2C238479023"; Assert.Equal(expectedValue, result); }
public void BuildProductLookupRequestQuery() { //Arrange IApiRequestBuilder builder = new ApiRequestBuilder(); ProductLookupRequestInput request = new ProductLookupRequestInput { ids = new List <int> { 29372934 } }; //Act var result = builder.GetProductLookupApiQueryString(request, "test"); //Assert var expectedValue = @"apiKey=test&ids=29372934"; Assert.Equal(expectedValue, result); }
public async Task <IActionResult> ProductLookup(int id) { var productList = new List <ProductInfoFull>(); if (id < 1) { return(Ok(productList)); } var productLookupParams = new ProductLookupRequestInput { ids = new List <int> { id } }; var productLookupResponse = await _walmartApiService.GetProductDetailAsync(productLookupParams); productList = ProcessProductLookupResponse(productLookupResponse); var item = productList.First(); return(Ok(item)); }
public void ReturnItemDetailsForMultipleProducts() { //Arrange WalmartApiConfig urls = ApiTestDataHelper.GetApiConfig(); IApiRequestBuilder requestbuilder = new ApiRequestBuilder(); var mockWalmartApiRequestManager = new Mock <IHttpApiRequestManager>(); var responseDataSample = ApiTestDataHelper .GetApiResponseDataFromResource("Walmart.Api.Client.Tests.ProductLookupMultiItemSampleResponse.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); var itemIds = new List <int> { 10294196, 893337, 817612485, 39661524, 654023063, 10294197, 10403812, 156205102, 20691516, 10324477 }; ProductLookupRequestInput request = new ProductLookupRequestInput { ids = itemIds }; //Act var result = productService.GetProductDetailAsync(request).Result; //Assert Assert.False(result.HasError); Assert.Equal(10, result.Response.items.Count); itemIds.ForEach(item => { Assert.Single(result.Response.items, p => p.ItemId == item); }); mockWalmartApiRequestManager.VerifyAll(); }
public async Task <IActionResult> ProductRecommendations(int id) { var productList = new List <ProductInfoFull>(); if (id < 1) { return(Ok(productList)); } var productRecommendationResponse = await _walmartApiService.GetProductRecommendationAsync(id); if ((!productRecommendationResponse.HasError) && (productRecommendationResponse.Response?.Count() > 0)) { var productLookupParams = new ProductLookupRequestInput { ids = GetTopNItemIds(productRecommendationResponse.Response, 10) }; var productLookupResponse = await _walmartApiService.GetProductDetailAsync(productLookupParams); productList = ProcessProductLookupResponse(productLookupResponse); } return(Ok(productList)); }