Create a REST client

This commit is contained in:
Manuel Thalmann 2024-06-06 17:11:36 +02:00
parent 2b25012888
commit 2b6ba2e095
4 changed files with 110 additions and 0 deletions

View file

@ -0,0 +1,66 @@
{
"openapi": "3.0.1",
"info": {
"title": "Ranking API",
"description": "List rankings",
"version": "v1"
},
"paths": {
"/Rankings": {
"get": {
"tags": [
"Rankings"
],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Competitor"
}
}
},
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Competitor"
}
}
},
"text/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Competitor"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Competitor": {
"type": "object",
"properties": {
"name": {
"type": "string",
"nullable": true
},
"time": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}

View file

@ -0,0 +1,10 @@
// See https://aka.ms/new-console-template for more information
using RankingService;
RankingClient client = new RankingClient("http://localhost:5072", new HttpClient());
List<Competitor> competitors = (await client.RankingsAsync()).ToList();
for (int i = 0; i < competitors.Count; i++)
{
Console.WriteLine($"{i + 1}. {competitors[i].Name}, {competitors[i].Time}");
}

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<OpenApiReference Include="OpenAPIs\swagger.json" CodeGenerator="NSwagCSharp" Namespace="RankingService" ClassName="RankingClient">
<SourceUri>http://localhost:5072/swagger/v1/swagger.json</SourceUri>
</OpenApiReference>
</ItemGroup>
<ItemGroup>
<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>
</Project>

View file

@ -5,6 +5,8 @@ VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RankingService", "RankingService\RankingService.csproj", "{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RankingService", "RankingService\RankingService.csproj", "{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RankingClient", "RankingClient\RankingClient.csproj", "{6F9D2832-93DB-4CF5-91B4-51345035C30D}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Debug|Any CPU.Build.0 = Debug|Any CPU {BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Release|Any CPU.ActiveCfg = Release|Any CPU {BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Release|Any CPU.Build.0 = Release|Any CPU {BC31F4B3-2FAF-4E74-BDFB-B642F8F74D35}.Release|Any CPU.Build.0 = Release|Any CPU
{6F9D2832-93DB-4CF5-91B4-51345035C30D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F9D2832-93DB-4CF5-91B4-51345035C30D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F9D2832-93DB-4CF5-91B4-51345035C30D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F9D2832-93DB-4CF5-91B4-51345035C30D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE