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); }
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)); }
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)); }
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); }
public OpenWeatherMapClientParamTest() { _mockConfig = new OpenWeatherMapConfig(); _openWeatherMapClientParam = new OpenWeatherMapClientParam(_mockConfig); }