public async Task <OpenWeatherDto> GetWeather(LatLonParameters latLonParameters) { var lat = latLonParameters.Lat; var lon = latLonParameters.Lon; var requestUri = $"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={APIKeys.OpenWeatherApiKey}&units=imperial"; // Look for cached version if (_memoryCache.TryGetValue(requestUri, out OpenWeatherDto weatherDto)) { // We found it in cache, use this. return(weatherDto); } var request = new HttpRequestMessage(HttpMethod.Get, requestUri); // request.Headers.Add("Accept", "application/vnd.github.v3+json"); // request.Headers.Add("User-Agent", "HttpClientFactory-Sample"); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); if (!response.IsSuccessStatusCode) { return(null); } await using var responseStream = await response.Content.ReadAsStreamAsync(); weatherDto = await JsonSerializer.DeserializeAsync <OpenWeatherDto>(responseStream, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, }); // Save to cache var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetAbsoluteExpiration(TimeSpan .FromMinutes(1)); // Save data in cache. _memoryCache.Set(requestUri, weatherDto, cacheEntryOptions); return(weatherDto); }
public async Task <ActionResult <DashboardDto> > GetDashboardData([FromQuery] LatLonParameters latLonParameters) { BeachConditionsDto beachConditions = null; try { beachConditions = await _weatherService.GetBeachConditions(); } catch (Exception e) { _logger.LogWarning("Beach Conditions could not be grabbed: " + e.Message); } var uvDto = await _weatherService.GetCurrentUVIndex(latLonParameters); var weatherData = await _weatherService.GetWeather(latLonParameters); var usersFromRepo = await _beachBuddyRepository.GetUsers(); var dashboardDto = new DashboardDto { BeachConditions = beachConditions, DashboardUvDto = _mapper.Map <DashboardUVDto>(uvDto), // Todo: this can be deleted // DashboardUvDto = new DashboardUVDto // { // uv = 8.3, // uv_time = "2020-06-29T13:32:07.067Z", // uv_max = 12.1, // uv_max_time = "2020-06-29T16:32:07.067Z", // safe_exposure_time = new SafeExposureTimeDto // { // st1 = 10, // st2 = 20, // st3 = 30, // st4 = 40, // st5 = 50, // st6 = 60 // } // }, WeatherInfo = weatherData, Users = _mapper.Map <IEnumerable <UserDto> >(usersFromRepo), }; return(Ok(dashboardDto)); }
public async Task <OpenUVDto> GetCurrentUVIndex(LatLonParameters latLngParameters) { var lat = latLngParameters.Lat; var lon = latLngParameters.Lon; var requestUri = $"https://api.openuv.io/api/v1/uv?lat={lat}&lng={lon}"; // Look for cached version if (_memoryCache.TryGetValue(requestUri, out OpenUVDto openUvDto)) { // We found it in cache, use this. return(openUvDto); } var request = new HttpRequestMessage(HttpMethod.Get, requestUri); request.Headers.Add("x-access-token", APIKeys.OpenUVIndexApiKey); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); if (!response.IsSuccessStatusCode) { return(null); } await using var responseStream = await response.Content.ReadAsStreamAsync(); openUvDto = await JsonSerializer.DeserializeAsync <OpenUVDto>(responseStream, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, }); // Save to cache var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetAbsoluteExpiration(TimeSpan .FromMinutes( 30)); // We are allowed 50 hits per day. We will only ever hit this 48 times. (2 * 24 = 48) // Save data in cache. _memoryCache.Set(requestUri, openUvDto, cacheEntryOptions); return(openUvDto); }
public async Task <ActionResult <OpenWeatherDto> > GetCurrentUVForLatLong( [FromQuery] LatLonParameters latLonParameters) { return(Ok(await _weatherService.GetCurrentUVIndex(latLonParameters))); }