示例#1
0
        public async Task GetProductByIdAsync_ShouldReturnProduct_WhenExternalServiceResponseIsValid()
        {
            HttpResponseMessage response = GetSuccessMockResponse();

            _httpClientFactoryWrapper.SendAsync(Arg.Any <HttpRequestMessage>(), "product").Returns(response);

            //Act
            var sut     = new ProductRest(_httpClientFactoryWrapper, _configuration, _loggerFactory);
            var product = await sut.GetProductByIdAsync(_productId);

            //Assert
            Assert.NotNull(product);
        }
示例#2
0
        public async Task <WishListProduct> GetProductByIdAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("Product id not provided");
            }

            var request = new HttpRequestMessage(HttpMethod.Get, $"{_configuration["ProductAPI:Url"]}/{id}");

            HttpResponseMessage response;

            try
            {
                response = await _httpClientFactoryWrapper.SendAsync(request, "product");
            }
            catch (BrokenCircuitException ex)
            {
                _logger.LogError($"Get Product by Id: {ex.Message}");
                throw new HttpRequestException("Unable to get product informations.");
            }
            catch (TimeoutRejectedException)
            {
                _logger.LogError($"Unable to get product informations.Timeout was reached");
                throw new HttpRequestException("Unable to get product informations.");
            }

            if (response.IsSuccessStatusCode)
            {
                return(await ProcessResponse(response));
            }

            throw new HttpRequestException("Unable to get product informations");
        }