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

Using Middleware #38

Merged
merged 8 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions OpenAPI/kiota/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ landingContent:
url: experience.md
- text: Authentication
url: authentication.md
- text: Middleware
url: middleware.md

- title: Get started
linkLists:
Expand Down
50 changes: 50 additions & 0 deletions OpenAPI/kiota/middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: Middleware
description: Learn about implementing middleware into Kiota.
author: gpltaylor
baywet marked this conversation as resolved.
Show resolved Hide resolved
ms.topic: overview
date: 21/11/2023
---

# Implementing Middleware
By registering custom middleware delegates we can perform operations before a request is made. For example, auditing and filtering the request before we send.
baywet marked this conversation as resolved.
Show resolved Hide resolved

## Middleware
Create your middleware class and add your business requirements. For example you may wish to add custom Headers to the request or filter headers and content.
baywet marked this conversation as resolved.
Show resolved Hide resolved

```
baywet marked this conversation as resolved.
Show resolved Hide resolved
public class SaveRequestHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
string jsonContent = await request.Content.ReadAsStringAsync();
Console.WriteLine($"Request: {jsonContent}");

return await base.SendAsync(request, cancellationToken);
}
}
```
## Register Middleware
Create a Middleware delegate array and use the existing Middleware already implemented within Kiota.HttpClient that includes existing Delegates like Retry, redirect handling and more.
baywet marked this conversation as resolved.
Show resolved Hide resolved

```
baywet marked this conversation as resolved.
Show resolved Hide resolved
var delegates = KiotaClientFactory.CreateDefaultHandlers();
delegates.Add(new SaveRequestHandler());
```
Next we create a Delegate chain so middleware is registered in the right order
baywet marked this conversation as resolved.
Show resolved Hide resolved

```
baywet marked this conversation as resolved.
Show resolved Hide resolved
var handler =
KiotaClientFactory.ChainHandlersCollectionAndGetFirstLink(
KiotaClientFactory.GetDefaultHttpMessageHandler(),
delegates.ToArray());
```

Finally we create Adapter using our HttpClient that's registered our Middleware. This adapter can then be using when submitting requests. The power of this design means that different adapters/middleware can be used when calling Endpoints and therefore gives flexibility to how and when Middleware is used.

```
baywet marked this conversation as resolved.
Show resolved Hide resolved
var httpClient = new HttpClient(handler!);
var adapter = new HttpClientRequestAdapter(authProvider, httpClient:httpClient);
var client = new PostsClient(adapter);
```
baywet marked this conversation as resolved.
Show resolved Hide resolved