Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve the live zone data code #44

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 Expand Up @@ -110,7 +116,7 @@
var forecasts = await manager.GetForecastByZoneAsync(zoneId);
return TypedResults.Ok(forecasts);
}
catch (HttpRequestException ex)

Check warning on line 119 in complete/Api/Data/NwsManager.cs

View workflow job for this annotation

GitHub Actions / Build Projects (complete)

The variable 'ex' is declared but never used

Check warning on line 119 in complete/Api/Data/NwsManager.cs

View workflow job for this annotation

GitHub Actions / Build Projects (complete)

The variable 'ex' is declared but never used
{
return TypedResults.NotFound();
}
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 @@
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 Expand Up @@ -119,7 +123,7 @@
var forecasts = await manager.GetForecastByZoneAsync(zoneId);
return TypedResults.Ok(forecasts);
}
catch (HttpRequestException ex)

Check warning on line 126 in start-with-api/Api/Data/NwsManager.cs

View workflow job for this annotation

GitHub Actions / Build Projects (start-with-api)

The variable 'ex' is declared but never used

Check warning on line 126 in start-with-api/Api/Data/NwsManager.cs

View workflow job for this annotation

GitHub Actions / Build Projects (start-with-api)

The variable 'ex' is declared but never used
{
return TypedResults.NotFound();
}
Expand Down