-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Azure Redis Cache for weather data
- Loading branch information
Showing
3 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,43 @@ | ||
using HandlenettAPI.Models; | ||
using Azure.Identity; | ||
using HandlenettAPI.Models; | ||
using Newtonsoft.Json.Linq; | ||
using StackExchange.Redis; | ||
using System.Text.Json; | ||
|
||
namespace HandlenettAPI.Services | ||
{ | ||
public class WeatherService | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly ConnectionMultiplexer _redis; | ||
|
||
public WeatherService(HttpClient httpClient) | ||
public WeatherService(HttpClient httpClient, IConfiguration config) | ||
{ | ||
_httpClient = httpClient; | ||
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Miles Haugesund Handlenett/1.0 ([email protected])"); | ||
_redis = ConnectionMultiplexer.Connect(config.GetConnectionString("AzureRedisCache")); | ||
} | ||
|
||
public async Task<Weather> GetWeatherByCoordinatesAsync(double latitude, double longitude) | ||
{ | ||
Weather? weather = null; | ||
|
||
var cachedValue = GetCacheValue("Weather"); | ||
if (cachedValue != null) | ||
{ | ||
weather = JsonSerializer.Deserialize<Weather>(cachedValue.ToString()); | ||
} | ||
|
||
if (weather == null || weather.LastUpdated <= DateTime.UtcNow.AddHours(-1)) | ||
{ | ||
weather = await GetDataFromMet(latitude, longitude); | ||
weather.LastUpdated = DateTime.UtcNow; | ||
SetCacheValue("Weather", JsonSerializer.Serialize<Weather>(weather)); | ||
} | ||
return weather; | ||
} | ||
|
||
private async Task<Weather> GetDataFromMet(double latitude, double longitude) | ||
{ | ||
var returnModel = new Weather(); | ||
var url = $"https://api.met.no/weatherapi/locationforecast/2.0/compact?lat={latitude}&lon={longitude}"; | ||
|
@@ -27,16 +51,29 @@ public async Task<Weather> GetWeatherByCoordinatesAsync(double latitude, double | |
|
||
returnModel.Temperature = json["properties"]["timeseries"][0]["data"]["instant"]["details"]["air_temperature"].ToString(); | ||
var symbol = json["properties"]["timeseries"][0]["data"]["next_1_hours"]["summary"]["symbol_code"].ToString(); | ||
|
||
//TODO: add icons for displaying weather in web app | ||
//returnModel.ImageUri = new Uri($"https://api.met.no/weatherapi/weathericon/2.0/?symbol={symbol}&content_type=image/png"); | ||
//https://github.com/metno/weathericons | ||
|
||
return returnModel; | ||
} | ||
else | ||
{ | ||
throw new Exception ("Unable to retrieve weather data from MET Norway."); | ||
throw new Exception("Unable to retrieve weather data from MET Norway."); | ||
} | ||
} | ||
|
||
private void SetCacheValue(string key, string value) | ||
{ | ||
var db = _redis.GetDatabase(); | ||
db.StringSet(key, value); | ||
} | ||
|
||
private string? GetCacheValue(string key) | ||
{ | ||
var db = _redis.GetDatabase(); | ||
return db.StringGet(key); | ||
} | ||
} | ||
} |