From 118cc5bcfc47d77efcf66184a7bbe05ff7e0b539 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:29:19 +0000 Subject: [PATCH] .Net: RestApiOperationRunner refactoring (#10080) ### Motivation, Context and Description Small refactoring of the `RestApiOperationRunner` class to simplify the review of the next PR related to the customization of operation response building. --- .../RestApiOperationRunner.cs | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/dotnet/src/Functions/Functions.OpenApi/RestApiOperationRunner.cs b/dotnet/src/Functions/Functions.OpenApi/RestApiOperationRunner.cs index a6427e58061c..c32a3811e6bb 100644 --- a/dotnet/src/Functions/Functions.OpenApi/RestApiOperationRunner.cs +++ b/dotnet/src/Functions/Functions.OpenApi/RestApiOperationRunner.cs @@ -175,7 +175,7 @@ public Task RunAsync( var (Payload, Content) = this._payloadFactory?.Invoke(operation, arguments, this._enableDynamicPayload, this._enablePayloadNamespacing, options) ?? this.BuildOperationPayload(operation, arguments); - return this.SendAsync(url, operation.Method, headers, Payload, Content, operation.Responses.ToDictionary(item => item.Key, item => item.Value.Schema), options, cancellationToken); + return this.SendAsync(operation, url, headers, Payload, Content, options, cancellationToken); } #region private @@ -183,26 +183,24 @@ public Task RunAsync( /// /// Sends an HTTP request. /// + /// The REST API operation. /// The url to send request to. - /// The HTTP request method. /// Headers to include into the HTTP request. /// HTTP request payload. /// HTTP request content. - /// The dictionary of expected response schemas. /// Options for REST API operation run. /// The cancellation token. /// Response content and content type private async Task SendAsync( + RestApiOperation operation, Uri url, - HttpMethod method, IDictionary? headers = null, object? payload = null, HttpContent? requestContent = null, - IDictionary? expectedSchemas = null, RestApiOperationRunOptions? options = null, CancellationToken cancellationToken = default) { - using var requestMessage = new HttpRequestMessage(method, url); + using var requestMessage = new HttpRequestMessage(operation.Method, url); #if NET5_0_OR_GREATER requestMessage.Options.Set(OpenApiKernelFunctionContext.KernelFunctionContextKey, new OpenApiKernelFunctionContext(options?.Kernel, options?.KernelFunction, options?.KernelArguments)); @@ -237,9 +235,7 @@ private async Task SendAsync( { responseMessage = await this._httpClient.SendWithSuccessCheckAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); - response = await this.ReadContentAndCreateOperationResponseAsync(requestMessage, responseMessage, payload, cancellationToken).ConfigureAwait(false); - - response.ExpectedSchema ??= GetExpectedSchema(expectedSchemas, responseMessage.StatusCode); + response = await this.BuildResponseAsync(operation, requestMessage, responseMessage, payload, cancellationToken).ConfigureAwait(false); return response; } @@ -570,5 +566,23 @@ private Uri BuildsOperationUrl(RestApiOperation operation, IDictionary + /// Builds the operation response. + /// + /// The REST API operation. + /// The HTTP request message. + /// The HTTP response message. + /// The payload sent in the HTTP request. + /// The cancellation token. + /// The operation response. + private async Task BuildResponseAsync(RestApiOperation operation, HttpRequestMessage requestMessage, HttpResponseMessage responseMessage, object? payload, CancellationToken cancellationToken) + { + var response = await this.ReadContentAndCreateOperationResponseAsync(requestMessage, responseMessage, payload, cancellationToken).ConfigureAwait(false); + + response.ExpectedSchema ??= GetExpectedSchema(operation.Responses.ToDictionary(item => item.Key, item => item.Value.Schema), responseMessage.StatusCode); + + return response; + } + #endregion }