Skip to content

Commit

Permalink
Merge pull request #44 from dotnet-presentations/dotnetaspire81
Browse files Browse the repository at this point in the history
improve the live zone data code
  • Loading branch information
jamesmontemagno authored Jul 31, 2024
2 parents 3660353 + fbb68f5 commit 407a238
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
18 changes: 12 additions & 6 deletions complete/Api/Data/NwsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ public class NwsManager(HttpClient httpClient, IMemoryCache cache)

public async Task<Zone[]?> GetZonesAsync()
{
// To get the live zone data from NWS, uncomment the following code and comment out the return statement below
//var response = await httpClient.GetAsync("https://api.weather.gov/zones?type=forecast");
//response.EnsureSuccessStatusCode();
//var content = await response.Content.ReadAsStringAsync();
//return JsonSerializer.Deserialize<ZonesResponse>(content, options);

return await cache.GetOrCreateAsync("zones", async entry =>
{
if (entry is null)
return [];
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
// To get the live zone data from NWS, uncomment the following code and comment out the return statement below. This is required if you are deploying to ACA
//var response = await httpClient.GetAsync("https://api.weather.gov/zones?type=forecast");
//response.EnsureSuccessStatusCode();
//var content = await response.Content.ReadAsStringAsync();
//var zones = JsonSerializer.Deserialize<ZonesResponse>(content, options);
//return zones?.Features
// ?.Where(f => f.Properties?.ObservationStations?.Count > 0)
// .Select(f => (Zone)f)
// .Distinct()
// .ToArray() ?? [];
// Deserialize the zones.json file from the wwwroot folder
var zonesJson = File.Open("wwwroot/zones.json", FileMode.Open);
if (zonesJson is null)
Expand Down
22 changes: 13 additions & 9 deletions start-with-api/Api/Data/NwsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ public class NwsManager(HttpClient httpClient, IMemoryCache cache)
PropertyNameCaseInsensitive = true
};


public async Task<Zone[]?> GetZonesAsync()
{



// To get the live zone data from NWS, uncomment the following code and comment out the return statement below
//var response = await httpClient.GetAsync("https://api.weather.gov/zones?type=forecast");
//response.EnsureSuccessStatusCode();
//var content = await response.Content.ReadAsStringAsync();
//return JsonSerializer.Deserialize<ZonesResponse>(content, options);

return await cache.GetOrCreateAsync("zones", async entry =>
{
if (entry is null)
return [];
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
// To get the live zone data from NWS, uncomment the following code and comment out the return statement below. This is required if you are deploying to ACA
//var response = await httpClient.GetAsync("https://api.weather.gov/zones?type=forecast");
//response.EnsureSuccessStatusCode();
//var content = await response.Content.ReadAsStringAsync();
//var zones = JsonSerializer.Deserialize<ZonesResponse>(content, options);
//return zones?.Features
// ?.Where(f => f.Properties?.ObservationStations?.Count > 0)
// .Select(f => (Zone)f)
// .Distinct()
// .ToArray() ?? [];
// Deserialize the zones.json file from the wwwroot folder
var zonesJson = File.Open("wwwroot/zones.json", FileMode.Open);
if (zonesJson is null)
Expand Down

0 comments on commit 407a238

Please sign in to comment.