Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Kiota #2509

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Uno.Extensions-packageonly.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"src\\Uno.Extensions.Core\\Uno.Extensions.Core.csproj",
"src\\Uno.Extensions.Hosting.UI\\Uno.Extensions.Hosting.WinUI.csproj",
"src\\Uno.Extensions.Hosting\\Uno.Extensions.Hosting.csproj",
"src\\Uno.Extensions.Http.Kiota\\Uno.Extensions.Http.Kiota.csproj",
"src\\Uno.Extensions.Http.Refit\\Uno.Extensions.Http.Refit.csproj",
"src\\Uno.Extensions.Http.UI\\Uno.Extensions.Http.WinUI.csproj",
"src\\Uno.Extensions.Http\\Uno.Extensions.Http.csproj",
Expand Down
19 changes: 19 additions & 0 deletions Uno.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Uno.Extensions.RuntimeTests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Uno.Extensions.RuntimeTests.Core", "src\Uno.Extensions.RuntimeTests\Uno.Extensions.RuntimeTests.Core\Uno.Extensions.RuntimeTests.Core.csproj", "{869C9E5B-0F85-4316-BC4B-CB6CBFCC02A3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Uno.Extensions.Http.Kiota", "src\Uno.Extensions.Http.Kiota\Uno.Extensions.Http.Kiota.csproj", "{C9827C80-312B-4E81-B539-2D305D893A6C}"
Kunal22shah marked this conversation as resolved.
Show resolved Hide resolved
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -732,6 +734,22 @@ Global
{869C9E5B-0F85-4316-BC4B-CB6CBFCC02A3}.Release|x64.Build.0 = Release|Any CPU
{869C9E5B-0F85-4316-BC4B-CB6CBFCC02A3}.Release|x86.ActiveCfg = Release|Any CPU
{869C9E5B-0F85-4316-BC4B-CB6CBFCC02A3}.Release|x86.Build.0 = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|arm64.ActiveCfg = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|arm64.Build.0 = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|x64.ActiveCfg = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|x64.Build.0 = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|x86.ActiveCfg = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Debug|x86.Build.0 = Debug|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|Any CPU.Build.0 = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|arm64.ActiveCfg = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|arm64.Build.0 = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|x64.ActiveCfg = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|x64.Build.0 = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|x86.ActiveCfg = Release|Any CPU
{C9827C80-312B-4E81-B539-2D305D893A6C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -778,6 +796,7 @@ Global
{B72698C5-6706-4275-8A2B-A1D39FE9B13E} = {2197ADCE-59C4-465A-B380-0B06BF68BBBC}
{A42362AF-8A61-4BBA-AA8A-E43323D5A063} = {FB399485-A0B1-4416-A494-E19AC7F5A665}
{869C9E5B-0F85-4316-BC4B-CB6CBFCC02A3} = {FB399485-A0B1-4416-A494-E19AC7F5A665}
{C9827C80-312B-4E81-B539-2D305D893A6C} = {45179294-70DC-47E8-AD22-1296F897B594}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6E7B035D-9A64-4D95-89AA-9D4653F17C42}
Expand Down
114 changes: 114 additions & 0 deletions doc/Learn/Http/HowTo-Kiota.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
uid: Uno.Extensions.Http.HowToKiota
---
# How-To: Quickly create and register a Kiota Client for an API

When working with APIs in your application, having a strongly-typed client can simplify communication and reduce boilerplate code. **Kiota** is a tool that generates strongly-typed API clients from Swagger/OpenAPI definitions. With Uno.Extensions, you can easily register and use Kiota clients in your Uno Platform app without additional setup.

## Step-by-Step Guide

> [!IMPORTANT]
> This guide assumes you used the template wizard or `dotnet new unoapp` to create your solution. If not, it is recommended that you follow the [**Creating an application with Uno.Extensions** documentation](xref:Uno.Extensions.HowToGettingStarted) to create an application from the template.

### 1. Installation

* Add `Http` to the `<UnoFeatures>` property in the Class Library (`.csproj`) file:

```diff
<UnoFeatures>
Material;
Extensions;
+ Http;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about this. I see there is no separate UnoFeature for Refit, it just comes as part of the Http feature. I know @francoistanguay mentioned we may replace Refit with Kiota rather than having both. We could instead split this as HttpRefit and HttpKiota feature?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Along with that, @jeromelaban we would need to make some changes to the uno.sdk to include the Uno.Extensions.Kiota packages as part of the Http UnoFeature in order for this to work as is, correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends. If this package is transitively included, there's no need. If it's optional, then yes, we'll need to.

Toolkit;
MVUX;
</UnoFeatures>
```

### 2. Enable Http in Host Builder

* Add the UseHttp method to the `IHostBuilder`:

```csharp
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
var appBuilder = this.CreateBuilder(args)
.Configure(hostBuilder =>
{
hostBuilder.UseHttp();
});
...
}
```

### 3. Generate the Kiota Client

* Install the Kiota tool:

```xml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```xml
```

dotnet tool install --global Microsoft.OpenApi.Kiota
```

* Generate the Client using the OpenAPI specification URL:

```xml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not XML :)

Suggested change
```xml
```

kiota generate -l CSharp -c MyApiClient -n MyApp.Client -d PATH_TO_YOUR_API_SPEC.json -o ./Client
```

This will create a client named `MyApiClient` in the Client folder.

### 4. Register the Kiota Client

* Register the generated client in the IHostBuilder using `AddKiotaClient` from `Uno.Extensions`:

```csharp

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
var appBuilder = this.CreateBuilder(args)
.Configure(hostBuilder =>
{
hostBuilder.UseHttp((context, services) =>
services.AddKiotaClient<MyApiClient>(
context,
options: new EndpointOptions { Url = "https://localhost:5002" }
)
);
});
}
```

### 5. Use the Kiota Client in Your Code

* Inject the ChefsApiClient into your view model or service and make API requests:

```csharp
public class MyViewModel
{
private readonly MyApiClient _apiClient;

public MyViewModel(MyApiClient apiClient)
{
_apiClient = apiClient;
}

public async Task GetAll()
{
var something = await _apiClient.Api.GetAsync();
Console.WriteLine($"Retrieved {something?.Count}.");
}
}

```

## Important Considerations

* With `Uno.Extensions.Authentication`, the HttpClient automatically includes the **Authorization** header. You don't need to manually handle token injection. The middleware ensures the access token is included in each request.

* Ensure your server is running and the swagger.json file is accessible at the specified URL when generating the Kiota client.

## See also

* [Overview: What is Kiota?](https://learn.microsoft.com/en-us/openapi/kiota/)
* [Overview: HTTP](xref:Uno.Extensions.Http.Overview)
* [How-To: Consume a web API with HttpClient](xref:Uno.Development.ConsumeWebApi)
* [How-To: Register an Endpoint for HTTP Requests](xref:Uno.Extensions.Http.HowToHttp)
2 changes: 2 additions & 0 deletions doc/Learn/Http/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
href: xref:Uno.Extensions.Http.HowToRefit
- name: "How-To: Configure with endpoint options"
href: xref:Uno.Extensions.Http.HowToEndpointOptions
- name: "How-To: Use create Kiota Client"
href: Uno.Extensions.Http.HowToKiota
136 changes: 70 additions & 66 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,76 +1,80 @@
<Project ToolsVersion="15.0">
<!--
<!--
Due to an issue with duplicate references on output for Windows we need to ensure that when building for MAUI Embedding that we use the same version
of both WinUI and Uno.WinUI across Extensions and the MauiEmbedding Sample Project.
-->
<!-- <PropertyGroup Condition="$(SolutionName) != 'MauiEmbedding'">
<!-- <PropertyGroup Condition="$(SolutionName) != 'MauiEmbedding'">
<UnoVersion Condition="$(UnoVersion) == '' AND !$(MSBuildProjectName.Contains('Maui'))">5.2.175</UnoVersion>
<UnoVersion Condition="$(UnoVersion) == '' AND $(MSBuildProjectName.Contains('Maui'))">5.2.175</UnoVersion>
<WinAppSdkVersion Condition="$(WinAppSdkVersion) == ''">1.5.240311000</WinAppSdkVersion>
</PropertyGroup> -->

<!--<PropertyGroup>
<!--<PropertyGroup>
<UnoToolkitVersion>5.0.15</UnoToolkitVersion>
</PropertyGroup>-->

<ItemGroup>
<PackageVersion Include="CommunityToolkit.Mvvm" Version="7.0.1" />
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.1.1" />
<PackageVersion Include="FluentAssertions" Version="6.7.0" />
<PackageVersion Include="IdentityModel.OidcClient" Version="5.0.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="2.2.9" />
<PackageVersion Include="MSTest.TestFramework" Version="2.2.9" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.61.3" />
<PackageVersion Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.61.3" />
<PackageVersion Include="Microsoft.Maui.Controls" Version="8.0.3" />
<PackageVersion Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.2.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.UI.Xaml" Version="2.7.1" />
<PackageVersion Include="Microsoft.Graphics.Win2D" Version="1.2.0" />
<PackageVersion Include="Moq" Version="4.17.2" />
<PackageVersion Include="Refit" Version="6.3.2" />
<PackageVersion Include="Refit.HttpClientFactory" Version="6.3.2" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="4.2.0" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageVersion Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageVersion Include="Serilog.Sinks.Xamarin" Version="1.0.0" />
<PackageVersion Include="System.Collections.Immutable" Version="1.3.1" /> <!--Lower version breaks build of reactive.<UI|WinUI>-->
<PackageVersion Include="System.Linq.Async" Version="4.0.0" />
<PackageVersion Include="System.Net.Http.WinHttpHandler" Version="6.0.1" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="Uno.Core" Version="4.1.1" />
<PackageVersion Include="Uno.Core.Extensions" Version="4.1.1" />
<PackageVersion Include="Uno.Core.Extensions.Collections" Version="4.1.1" />
<PackageVersion Include="Uno.Core.Extensions.Logging.Singleton" Version="4.1.1" />
<PackageVersion Include="Uno.Extensions.Logging.OSLog" Version="1.7.0" />
<PackageVersion Include="Uno.Extensions.Logging.WebAssembly.Console" Version="1.7.0" />
<PackageVersion Include="Uno.Extensions.Markup.Generators" Version="5.3.12" />
<PackageVersion Include="Uno.Extensions.Markup.WinUI" Version="5.3.12" />
<PackageVersion Include="Uno.Roslyn" Version="1.3.0-dev.12" />
<PackageVersion Include="Uno.Toolkit" Version="6.1.7" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="$(UnoVersion)"/>

<PackageVersion Include="Uno.UI.RuntimeTests.Engine" Version="0.37.0-dev.126" />
<PackageVersion Include="Uno.WinUI" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.Markup" Version="5.3.12" />
<PackageVersion Include="Uno.WinUI.MSAL" Version="$(UnoVersion)" />
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
<PackageVersion Include="xunit" Version="2.4.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="FluentValidation" Version="11.4.0" />
</ItemGroup>
</Project>
<ItemGroup>
<PackageVersion Include="CommunityToolkit.Mvvm" Version="7.0.1" />
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.1.1" />
<PackageVersion Include="FluentAssertions" Version="6.7.0" />
<PackageVersion Include="IdentityModel.OidcClient" Version="5.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Kiota.Abstractions" Version="1.11.0" />
<PackageVersion Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.11.0" />
<PackageVersion Include="Microsoft.Kiota.Serialization.Form" Version="1.11.0" />
<PackageVersion Include="Microsoft.Kiota.Serialization.Json" Version="1.11.0" />
<PackageVersion Include="Microsoft.Kiota.Serialization.Multipart" Version="1.11.0" />
<PackageVersion Include="Microsoft.Kiota.Serialization.Text" Version="1.11.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="2.2.9" />
<PackageVersion Include="MSTest.TestFramework" Version="2.2.9" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.61.3" />
<PackageVersion Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.61.3" />
<PackageVersion Include="Microsoft.Maui.Controls" Version="8.0.3" />
<PackageVersion Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.2.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.UI.Xaml" Version="2.7.1" />
<PackageVersion Include="Microsoft.Graphics.Win2D" Version="1.2.0" />
<PackageVersion Include="Moq" Version="4.17.2" />
<PackageVersion Include="Refit" Version="6.3.2" />
<PackageVersion Include="Refit.HttpClientFactory" Version="6.3.2" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="4.2.0" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageVersion Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageVersion Include="Serilog.Sinks.Xamarin" Version="1.0.0" />
<PackageVersion Include="System.Collections.Immutable" Version="1.3.1" /> <!--Lower version breaks build of reactive.<UI|WinUI>-->
<PackageVersion Include="System.Linq.Async" Version="4.0.0" />
<PackageVersion Include="System.Net.Http.WinHttpHandler" Version="6.0.1" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="Uno.Core" Version="4.1.1" />
<PackageVersion Include="Uno.Core.Extensions" Version="4.1.1" />
<PackageVersion Include="Uno.Core.Extensions.Collections" Version="4.1.1" />
<PackageVersion Include="Uno.Core.Extensions.Logging.Singleton" Version="4.1.1" />
<PackageVersion Include="Uno.Extensions.Logging.OSLog" Version="1.7.0" />
<PackageVersion Include="Uno.Extensions.Logging.WebAssembly.Console" Version="1.7.0" />
<PackageVersion Include="Uno.Extensions.Markup.Generators" Version="5.3.12" />
<PackageVersion Include="Uno.Extensions.Markup.WinUI" Version="5.3.12" />
<PackageVersion Include="Uno.Roslyn" Version="1.3.0-dev.12" />
<PackageVersion Include="Uno.Toolkit" Version="6.1.7" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.UI.RuntimeTests.Engine" Version="0.37.0-dev.126" />
<PackageVersion Include="Uno.WinUI" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.Markup" Version="5.3.12" />
<PackageVersion Include="Uno.WinUI.MSAL" Version="$(UnoVersion)" />
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
<PackageVersion Include="xunit" Version="2.4.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="FluentValidation" Version="11.4.0" />
</ItemGroup>
</Project>
Loading