Skip to content

Commit

Permalink
Merge pull request #12 from dotnet-presentations/update-api-comlete
Browse files Browse the repository at this point in the history
Refactor projects and update JSON handling
  • Loading branch information
jamesmontemagno authored Jun 14, 2024
2 parents 333be06 + 1ae1843 commit 6ce3dfd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
9 changes: 0 additions & 9 deletions complete/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RuntimeIdentifiers>linux-x64</RuntimeIdentifiers>
<EnableSdkContainerDebugging>True</EnableSdkContainerDebugging>
<UserSecretsId>7b8931a0-bda7-4546-b99d-dc41f97af10e</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,11 +12,6 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<ContainerEnvironmentVariable Include="ASPNETCORE_HTTPS_PORTS">
<Value>8081</Value>
</ContainerEnvironmentVariable>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
Expand Down
10 changes: 0 additions & 10 deletions complete/MyWeatherHub/MyWeatherHub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<OpenApiReference Include="OpenAPIs\swagger.json" CodeGenerator="NSwagCSharp" Options="/UseBaseUrl:false" ClassName="NwsManager">
<SourceUri>https://localhost:7032/swagger/v1/swagger.json</SourceUri>
</OpenApiReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.QuickGrid" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NSwag.ApiDescription.Client" Version="13.18.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
36 changes: 36 additions & 0 deletions complete/MyWeatherHub/NwsManager.cs
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);
9 changes: 0 additions & 9 deletions start-with-api/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RuntimeIdentifiers>linux-x64</RuntimeIdentifiers>
<EnableSdkContainerDebugging>True</EnableSdkContainerDebugging>
<UserSecretsId>7b8931a0-bda7-4546-b99d-dc41f97af10e</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<ContainerEnvironmentVariable Include="ASPNETCORE_HTTPS_PORTS">
<Value>8081</Value>
</ContainerEnvironmentVariable>
</ItemGroup>

</Project>

0 comments on commit 6ce3dfd

Please sign in to comment.