-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dotnet-presentations/update-api-comlete
Refactor projects and update JSON handling
- Loading branch information
Showing
4 changed files
with
36 additions
and
28 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Text.Json; | ||
|
||
namespace MyWeatherHub; | ||
|
||
public class NwsManager(HttpClient client) | ||
{ | ||
readonly JsonSerializerOptions options = new() | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
public async Task<Zone[]> GetZonesAsync() | ||
{ | ||
var response = await client.GetAsync("zones"); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var content = await response.Content.ReadAsStringAsync(); | ||
var zones = JsonSerializer.Deserialize<Zone[]>(content, options); | ||
|
||
return zones ?? []; | ||
} | ||
|
||
public async Task<Forecast[]> GetForecastByZoneAsync(string zoneId) | ||
{ | ||
var response = await client.GetAsync($"forecast/{zoneId}"); | ||
response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
var forecast = JsonSerializer.Deserialize<Forecast[]>(content, options); | ||
|
||
return forecast ?? []; | ||
} | ||
} | ||
|
||
public record Zone(string Key, string Name, string State); | ||
|
||
public record Forecast(string Name, string DetailedForecast); |
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