A set of libraries for auto-generating API clients using Kiota
Kiota.Autogen.Swagger for generating API client based on Swashbuckle.AspNetCore
- Add a
Class Library
project to the solution that contains WebApi project - Add WebApi project reference with
ExcludeAssets="All"
<ItemGroup>
<ProjectReference Include="..\ExampleService.Api\ExampleService.Api.csproj" ExcludeAssets="All" />
</ItemGroup>
- Install
Kiota.Autogen.Swagger
withPrivateAssets="All"
<ItemGroup>
<PackageReference Include="Kiota.Autogen.Swagger" Version="1.16.1" PrivateAssets="All" />
</ItemGroup>
- Install following
Kiota
packages
<ItemGroup>
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.9.11" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.9.11" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.9.11" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.9.11" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.9.11" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.9.11" />
</ItemGroup>
- Create a
gensettings.json
file with the following structure
[
{
"name": "WeatherClient", // API client name to be generated
"namespace": "Weather.Client", // API client namespace
"version": "v1" // WebApi Swagger document version
},
{
"name": "WeatherClientNew",
"namespace": "Weather.Client.New",
"version": "v2"
}
]
- Set up
Class library
project to build as a nuget package
<PropertyGroup>
... other properties
<IsPackable>true</IsPackable>
<PackageId>ExampleService.Client</PackageId>
</PropertyGroup>
- Pack
Class library
project - Enjoy using API client
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Weather.Client;
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://your_service_url");
var provider = new AnonymousAuthenticationProvider();
using var adapter = new HttpClientRequestAdapter(provider, httpClient: httpClient);
var client = new WeatherClient(adapter);
var forecasts = await client.Weatherforecast.GetAsync();
See full example
You can provide your own implementations of Microsoft.Kiota.Abstractions
from the Class library
project if you need
Usage:
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Weather.Client;
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://your_service_url");
var provider = new AnonymousAuthenticationProvider();
using var adapter = new HttpClientRequestAdapter(provider, new WeatherParseNodeFactory(), new WeatherSerializationWriterFactory(), httpClient: httpClient);
var client = new WeatherClient(adapter);
var forecasts = await client.Weatherforecast.GetAsync();
See full example
See more examples here