示例#1
0
        private async Task <T> GetApiResponse <T>(OpenWeatherMapClientParam parameter, string path) where T : class
        {
            var queryParams = parameter.ToQueryString();

            var uriBuilder = new UriBuilder(_config.ApiBaseUrl)
            {
                Path = path, Query = queryParams
            };

            using var httpResponse = await _client.GetAsync(uriBuilder.ToString(), HttpCompletionOption.ResponseHeadersRead);

            if (httpResponse.IsSuccessStatusCode)
            {
                var contentStream = await httpResponse.Content.ReadAsStreamAsync();

                return(await JsonSerializer.DeserializeAsync <T>(contentStream));
            }

            if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new UnauthorizedAccessException();
            }

            return(null);
        }
示例#2
0
        public async Task <CurrentWeatherResponse> GetCurrentWeatherByZipcode(string zipcode, string country, string unit)
        {
            var parameter = new OpenWeatherMapClientParam(_config);

            parameter.AddFilterByZipParams(zipcode, country, unit);

            return(await GetApiResponse <CurrentWeatherResponse>(parameter, _config.CurrentWeatherRoute));
        }
示例#3
0
        public async Task <WeatherForecastResponse> GetForecastByCity(string city, string country, string unit)
        {
            var parameter = new OpenWeatherMapClientParam(_config);

            parameter.AddFilterByCityParams(city, country, unit);

            return(await GetApiResponse <WeatherForecastResponse>(parameter, _config.ForecastRoute));
        }
示例#4
0
        public void Add_Config_Param_Config_Non_Empty()
        {
            // Arrange
            _openWeatherMapClientParam = new OpenWeatherMapClientParam(new OpenWeatherMapConfig()
            {
                DefaultCountryCode = "DE", ApiKey = "AppId"
            });
            _openWeatherMapClientParam.AddFilterByZipParams(_fakeZip, "", _fakeUnit);
            var expectedResult = $"zip={_fakeZip},DE&units={_fakeUnit}&appid=AppId";

            // Act
            var actualResult = _openWeatherMapClientParam.ToQueryString();

            // Assert
            Assert.AreEqual(expectedResult, actualResult);
        }
示例#5
0
 public OpenWeatherMapClientParamTest()
 {
     _mockConfig = new OpenWeatherMapConfig();
     _openWeatherMapClientParam = new OpenWeatherMapClientParam(_mockConfig);
 }