Skip to content

A set of libraries for auto-generating API clients using Kiota

License

Notifications You must be signed in to change notification settings

ellizio/Kiota.Autogen

Repository files navigation

Kiota.Autogen

A set of libraries for auto-generating API clients using Kiota


Libraries list

Basic Usage

  1. Add a Class Library project to the solution that contains WebApi project
  2. Add WebApi project reference with ExcludeAssets="All"
<ItemGroup>
    <ProjectReference Include="..\ExampleService.Api\ExampleService.Api.csproj" ExcludeAssets="All" />
</ItemGroup>
  1. Install Kiota.Autogen.Swagger with PrivateAssets="All"
<ItemGroup>
    <PackageReference Include="Kiota.Autogen.Swagger" Version="1.16.1" PrivateAssets="All" />
</ItemGroup>
  1. 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>
  1. 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"
  }
]
  1. Set up Class library project to build as a nuget package
<PropertyGroup>
    ... other properties

    <IsPackable>true</IsPackable>
    <PackageId>ExampleService.Client</PackageId>
</PropertyGroup>
  1. Pack Class library project
  2. 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

Advanced Usage

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

More Examples

See more examples here

Additional References