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 Libraryproject 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.SwaggerwithPrivateAssets="All"
<ItemGroup>
<PackageReference Include="Kiota.Autogen.Swagger" Version="1.18.0" PrivateAssets="All" />
</ItemGroup>- Install following
Kiotapackages
<ItemGroup>
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.12.4" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.12.4" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.12.4" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.12.4" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.12.4" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.12.4" />
</ItemGroup>- Create a
gensettings.jsonfile with the following structure
- Set up
Class libraryproject to build as a nuget package
<PropertyGroup>
... other properties
<IsPackable>true</IsPackable>
<PackageId>ExampleService.Client</PackageId>
</PropertyGroup>- Pack
Class libraryproject - 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
[ { "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" } ]