public async Task GetMarketStackIndexes_HtttReponseNotContainsError_ReturnsValidContent()
        {
            Mock <IHttpClientHandler> _mockHttpClientHandler = new Mock <IHttpClientHandler>();
            Mock <IStreamHandler>     _mockStreamHandler     = new Mock <IStreamHandler>();

            _mockHttpClientHandler.Setup(httpClient => httpClient.GetAsync(It.IsAny <string>()))
            .ReturnsAsync(CreateHttpResponseMessage(HttpStatusCode.OK));

            _mockStreamHandler.Setup(stream => stream.DeserializeJsonFromStreamAsync <SPIndexDTO>(It.IsAny <Stream>()))
            .ReturnsAsync(new SPIndexDTO()
            {
                Data = new List <SPIndexValueDTO>()
                {
                    CreateMockedSPIndexValueDTO(3, 4),
                    CreateMockedSPIndexValueDTO(6, 7)
                },
                Pagination = CreateMockedSPIndexPaginationDTO(2, 100, 15)
            });

            MarketStackService marketStackService = new MarketStackService(_mockHttpClientHandler.Object, _mockStreamHandler.Object);

            HttpHeaderSetting setting = CreateHttpHeaderSetting("https://marketstack.com/", "testKey", "json");
            var url = "api/testURL";
            var res = await marketStackService.GetMarketStackIndexes(url, setting);

            Assert.Equal(2, res.Data.Count);

            Assert.Equal(3, res.Data[0].Low);
            Assert.Equal(4, res.Data[0].High);
            Assert.Equal(6, res.Data[1].Low);
            Assert.Equal(7, res.Data[1].High);
        }
        public async Task GetMarketStackIndexes_HtttReponseContainsError_ThrowsCustomException()
        {
            Mock <IHttpClientHandler> _mockHttpClientHandler = new Mock <IHttpClientHandler>();
            Mock <IStreamHandler>     _mockStreamHandler     = new Mock <IStreamHandler>();

            _mockHttpClientHandler.Setup(httpClient => httpClient.GetAsync(It.IsAny <string>()))
            .ReturnsAsync(CreateHttpResponseMessage(HttpStatusCode.InternalServerError));

            MarketStackService marketStackService = new MarketStackService(_mockHttpClientHandler.Object, _mockStreamHandler.Object);

            HttpHeaderSetting setting     = CreateHttpHeaderSetting("https://marketstack.com/", "testKey", "json");
            var       url                 = "api/testURL";
            const int internalServerError = 500;

            Func <Task <SPIndexDTO> > deleg = () => marketStackService.GetMarketStackIndexes(url, setting);

            var exceptionDetails = await Assert.ThrowsAsync <ApiException>(deleg);

            Assert.Equal(exceptionDetails.StatusCode, internalServerError);
        }
        public async Task <SPIndexDTO> GetMarketStackIndexes(string marketStackIndexProviderUrl,
                                                             HttpHeaderSetting httpHeaderSetting)
        {
            using (var response = await _httpClientHandler.GetAsync(marketStackIndexProviderUrl))
            {
                _httpClientHandler.SetHttpHeaderSettings(httpHeaderSetting);
                var stream = await _streamHandler.ReadAsStreamAsync(response);

                if (response.IsSuccessStatusCode)
                {
                    return(await _streamHandler.DeserializeJsonFromStreamAsync <SPIndexDTO>(stream));
                }

                var content = await _streamHandler.StreamToStringAsync(stream);

                throw new ApiException
                      {
                          StatusCode = (int)response.StatusCode,
                          Content    = content
                      };
            }
        }
 public void SetHttpHeaderSettings(HttpHeaderSetting headerSettings)
 {
     _httpClient.BaseAddress = new Uri(headerSettings.BaseAddress);
     _httpClient.DefaultRequestHeaders.Accept.Clear();
     _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(headerSettings.MediaType));
 }