From 7049bc1aeddc066def0615dd3666c4bc3f618918 Mon Sep 17 00:00:00 2001 From: Garry Taylor Date: Tue, 21 Nov 2023 09:48:56 +0000 Subject: [PATCH 1/8] Create middleware.md Document how to implement Middleware into Kiota --- OpenAPI/kiota/middleware.md | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 OpenAPI/kiota/middleware.md diff --git a/OpenAPI/kiota/middleware.md b/OpenAPI/kiota/middleware.md new file mode 100644 index 0000000..e8b5058 --- /dev/null +++ b/OpenAPI/kiota/middleware.md @@ -0,0 +1,51 @@ +--- +title: Middleware +description: Learn about implementing middleware into Kiota. +author: gpltaylor +ms.author: gpltaylor +ms.topic: overview +date: 21/11/2023 +--- + +# Implementing Middleware +Implement middleware to access the pure request. + +## Middleware class +Create your middleware class and add your business logic + +``` +public class SaveRequestHandler : DelegatingHandler +{ + protected override async Task 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 however, make sure to use the existing Middleware already implemented within Kiota.HttpClient. This includes Retry, redirect handling and more. + +``` +var delegates = KiotaClientFactory.CreateDefaultHandlers(); +delegates.Add(new SaveRequestHandler()); +``` +Next we create a Delegate chain so middleware is registered in the right order + +``` +var handler = + KiotaClientFactory.ChainHandlersCollectionAndGetFirstLink( + KiotaClientFactory.GetDefaultHttpMessageHandler(), + delegates.ToArray()); +``` + +Finally create the HttpClient and then the Adapter + +``` +var httpClient = new HttpClient(handler!); +var adapter = new HttpClientRequestAdapter(authProvider, httpClient:httpClient); +var client = new PostsClient(adapter); +``` From 5e3b442066217641b56bf879ebba3ea737835ca3 Mon Sep 17 00:00:00 2001 From: Garry Taylor Date: Tue, 21 Nov 2023 09:54:34 +0000 Subject: [PATCH 2/8] Update middleware.md Improve wording --- OpenAPI/kiota/middleware.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/OpenAPI/kiota/middleware.md b/OpenAPI/kiota/middleware.md index e8b5058..93a4ca9 100644 --- a/OpenAPI/kiota/middleware.md +++ b/OpenAPI/kiota/middleware.md @@ -8,10 +8,10 @@ date: 21/11/2023 --- # Implementing Middleware -Implement middleware to access the pure request. +Here we review implementing Middleware into Kiota. Using this example we are going to access the request before we proccess the request. This allows us to audio the request or to maker changes before submitting. ## Middleware class -Create your middleware class and add your business logic +Create your middleware class and add your business requirements. For example you may wish to add custom Headers to the request. ``` public class SaveRequestHandler : DelegatingHandler @@ -27,7 +27,7 @@ public class SaveRequestHandler : DelegatingHandler } ``` ## Register Middleware -Create a Middleware delegate array however, make sure to use the existing Middleware already implemented within Kiota.HttpClient. This includes Retry, redirect handling and more. +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. ``` var delegates = KiotaClientFactory.CreateDefaultHandlers(); From b529d09752e863169f0b9b23f7b4788bdb83164f Mon Sep 17 00:00:00 2001 From: Garry Taylor Date: Tue, 21 Nov 2023 10:29:35 +0000 Subject: [PATCH 3/8] Update middleware.md --- OpenAPI/kiota/middleware.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/OpenAPI/kiota/middleware.md b/OpenAPI/kiota/middleware.md index 93a4ca9..579ee18 100644 --- a/OpenAPI/kiota/middleware.md +++ b/OpenAPI/kiota/middleware.md @@ -1,5 +1,5 @@ --- -title: Middleware +title: Middleware1 description: Learn about implementing middleware into Kiota. author: gpltaylor ms.author: gpltaylor @@ -8,10 +8,10 @@ date: 21/11/2023 --- # Implementing Middleware -Here we review implementing Middleware into Kiota. Using this example we are going to access the request before we proccess the request. This allows us to audio the request or to maker changes before submitting. +By registering custom middleware delegates we can perform operations before a request is made. For example, auditing and filtering the request before we send. -## Middleware class -Create your middleware class and add your business requirements. For example you may wish to add custom Headers to the request. +## 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. ``` public class SaveRequestHandler : DelegatingHandler @@ -42,7 +42,7 @@ var handler = delegates.ToArray()); ``` -Finally create the HttpClient and then the Adapter +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. ``` var httpClient = new HttpClient(handler!); From 0ae0cfc430a1048fcc988818b6932b54a934db7d Mon Sep 17 00:00:00 2001 From: Garry Taylor Date: Tue, 21 Nov 2023 10:43:05 +0000 Subject: [PATCH 4/8] Update Concept Index Add Middleware to Concept page --- OpenAPI/kiota/index.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/OpenAPI/kiota/index.yml b/OpenAPI/kiota/index.yml index 7d4a638..cf421d9 100644 --- a/OpenAPI/kiota/index.yml +++ b/OpenAPI/kiota/index.yml @@ -29,6 +29,8 @@ landingContent: url: experience.md - text: Authentication url: authentication.md + - text: Middleware + url: middleware.md - title: Get started linkLists: From d7686e0acd1a1cda86be1b9d06e81ae7c12f8007 Mon Sep 17 00:00:00 2001 From: Garry Taylor Date: Tue, 21 Nov 2023 10:49:21 +0000 Subject: [PATCH 5/8] Update middleware.md --- OpenAPI/kiota/middleware.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/OpenAPI/kiota/middleware.md b/OpenAPI/kiota/middleware.md index 579ee18..c7d92c3 100644 --- a/OpenAPI/kiota/middleware.md +++ b/OpenAPI/kiota/middleware.md @@ -1,8 +1,7 @@ --- -title: Middleware1 +title: Middleware description: Learn about implementing middleware into Kiota. author: gpltaylor -ms.author: gpltaylor ms.topic: overview date: 21/11/2023 --- From 5d517742e844599000e3b4475c24b1ca8bc8ac22 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 21 Nov 2023 09:05:57 -0500 Subject: [PATCH 6/8] - linting --- OpenAPI/kiota/middleware.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/OpenAPI/kiota/middleware.md b/OpenAPI/kiota/middleware.md index c7d92c3..e2907d5 100644 --- a/OpenAPI/kiota/middleware.md +++ b/OpenAPI/kiota/middleware.md @@ -1,18 +1,21 @@ --- title: Middleware description: Learn about implementing middleware into Kiota. -author: gpltaylor +author: baywet +ms.author: vibiret 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. ## 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. -``` +```cs public class SaveRequestHandler : DelegatingHandler { protected override async Task SendAsync( @@ -26,15 +29,17 @@ public class SaveRequestHandler : DelegatingHandler } ``` ## 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. -``` +```cs var delegates = KiotaClientFactory.CreateDefaultHandlers(); delegates.Add(new SaveRequestHandler()); ``` -Next we create a Delegate chain so middleware is registered in the right order -``` +Next we create a Delegate chain so middleware is registered in the right order. + +```cs var handler = KiotaClientFactory.ChainHandlersCollectionAndGetFirstLink( KiotaClientFactory.GetDefaultHttpMessageHandler(), @@ -43,8 +48,7 @@ var handler = 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. -``` +```cs var httpClient = new HttpClient(handler!); var adapter = new HttpClientRequestAdapter(authProvider, httpClient:httpClient); var client = new PostsClient(adapter); -``` From bc8e2c01fa81f2365cdc271c747c373ba1d6bb16 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 21 Nov 2023 09:07:51 -0500 Subject: [PATCH 7/8] - adds missing triple backticks to code blocks Signed-off-by: Vincent Biret --- OpenAPI/kiota/middleware.md | 1 + 1 file changed, 1 insertion(+) diff --git a/OpenAPI/kiota/middleware.md b/OpenAPI/kiota/middleware.md index e2907d5..0ffccda 100644 --- a/OpenAPI/kiota/middleware.md +++ b/OpenAPI/kiota/middleware.md @@ -52,3 +52,4 @@ Finally we create Adapter using our HttpClient that's registered our Middleware. var httpClient = new HttpClient(handler!); var adapter = new HttpClientRequestAdapter(authProvider, httpClient:httpClient); var client = new PostsClient(adapter); +``` From 3d25edbde9e082928d7a3ea626e6571da3ee472d Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 21 Nov 2023 09:14:27 -0500 Subject: [PATCH 8/8] - applies microsoft active voice guidelines --- OpenAPI/kiota/middleware.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/OpenAPI/kiota/middleware.md b/OpenAPI/kiota/middleware.md index 0ffccda..f7ed0ef 100644 --- a/OpenAPI/kiota/middleware.md +++ b/OpenAPI/kiota/middleware.md @@ -9,7 +9,7 @@ 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. +By registering custom middleware delegates you can perform operations before a request is made. For example, auditing and filtering the request before the client sends it. ## Middleware @@ -21,35 +21,36 @@ public class SaveRequestHandler : DelegatingHandler protected override async Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { - string jsonContent = await request.Content.ReadAsStringAsync(); + var jsonContent = await request.Content.ReadAsStringAsync(cancellationToken); 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. ```cs -var delegates = KiotaClientFactory.CreateDefaultHandlers(); -delegates.Add(new SaveRequestHandler()); +var handlers = KiotaClientFactory.CreateDefaultHandlers(); +handlers.Add(new SaveRequestHandler()); ``` -Next we create a Delegate chain so middleware is registered in the right order. +Next you will need to create a Delegate chain so the middleware handlers are registered in the right order. ```cs -var handler = +var httpMessageHandler = KiotaClientFactory.ChainHandlersCollectionAndGetFirstLink( KiotaClientFactory.GetDefaultHttpMessageHandler(), - delegates.ToArray()); + handlers.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. +Finally, create a request adapter using a HttpClient with the message handler that was just created. This adapter can then be used when submitting requests from the generated client. This design means different adapters/middleware can be used when calling APIs and therefore gives flexibility to how and when a middleware handler is used. ```cs -var httpClient = new HttpClient(handler!); +var httpClient = new HttpClient(httpMessageHandler!); var adapter = new HttpClientRequestAdapter(authProvider, httpClient:httpClient); -var client = new PostsClient(adapter); +var client = new PostsClient(adapter); // the name of the client will vary based on your generation parameters ```