diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/ClientPipelineApi.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/ClientPipelineApi.cs index 9c18555bf2d..8efc7222d71 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/ClientPipelineApi.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/ClientPipelineApi.cs @@ -4,7 +4,6 @@ using System; using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Primitives; -using Microsoft.Generator.CSharp.Providers; using Microsoft.Generator.CSharp.Snippets; using Microsoft.Generator.CSharp.Statements; @@ -20,11 +19,10 @@ protected ClientPipelineApi(Type type, ValueExpression original) : base(type, or { } - public abstract ValueExpression CreateMessage(HttpRequestOptionsApi requestOptions, ValueExpression responseClassifier); - - public abstract MethodBodyStatement Send(HttpMessageApi message, HttpRequestOptionsApi options); + public abstract MethodBodyStatement[] ProcessMessage(HttpMessageApi message, HttpRequestOptionsApi options); + public abstract MethodBodyStatement[] ProcessMessageAsync(HttpMessageApi message, HttpRequestOptionsApi options); - public abstract MethodBodyStatement SendAsync(HttpMessageApi message, HttpRequestOptionsApi options); + public abstract ValueExpression CreateMessage(HttpRequestOptionsApi requestOptions, ValueExpression responseClassifier); public abstract ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpMessageApi.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpMessageApi.cs index 1ae33e935d3..d117351b30d 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpMessageApi.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/Abstractions/HttpMessageApi.cs @@ -25,8 +25,6 @@ protected HttpMessageApi(Type type, ValueExpression original) : base(type, origi public abstract MethodBodyStatement ApplyRequestOptions(HttpRequestOptionsApi options); - public abstract MethodBodyStatement[] ExtractResponse(); - public abstract HttpMessageApi FromExpression(ValueExpression original); public abstract HttpMessageApi ToExpression(); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineExtensionsDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineExtensionsDefinition.cs index 0dbf23902ec..0237b17e34f 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineExtensionsDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineExtensionsDefinition.cs @@ -8,7 +8,6 @@ using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Providers; -using Microsoft.Generator.CSharp.Snippets; using Microsoft.Generator.CSharp.Statements; using static Microsoft.Generator.CSharp.Snippets.Snippet; @@ -49,8 +48,7 @@ protected override MethodProvider[] BuildMethods() BuildProcessMessageAsync(), BuildProcessMessage(), ProcessHeadAsBoolMessageAsync(), - ProcessHeadAsBoolMessage(), - BuildExtractResponseContent() + ProcessHeadAsBoolMessage() ]; } @@ -120,28 +118,7 @@ private MethodSignature GetProcessHeadAsBoolMessageSignature(bool isAsync) private MethodProvider BuildProcessMessage() { MethodSignature signature = GetProcessMessageSignature(false); - - var clientErrorNoThrow = _options.NoThrow(); - return new MethodProvider(signature, new MethodBodyStatement[] - { - _pipeline.Send(_message, _options), - MethodBodyStatement.EmptyLine, - new IfStatement(_message.Response().IsError().And(new BinaryOperatorExpression("&", _options.NullConditional().Property("ErrorOptions"), clientErrorNoThrow).NotEqual(clientErrorNoThrow))) - { - Throw(New.Instance(ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseExceptionType, _message.Response())) - }, - MethodBodyStatement.EmptyLine, - Declare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, new TernaryConditionalExpression(_message.BufferResponse(), _message.Response(), Static().Invoke(ExtractResponseContentMethodName, [_messageParam])), out var response), - Return(response) - }, this); - } - - private const string ExtractResponseContentMethodName = "ExtractResponseContent"; - private MethodProvider BuildExtractResponseContent() - { - var signature = new MethodSignature(ExtractResponseContentMethodName, null, MethodSignatureModifiers.Private | MethodSignatureModifiers.Static, ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, null, [_messageParam]); - var body = _message.ExtractResponse(); - return new MethodProvider(signature, body, this); + return new MethodProvider(signature, _pipeline.ProcessMessage(_message, _options), this); } private MethodSignature GetProcessMessageSignature(bool isAsync) @@ -163,20 +140,7 @@ private MethodSignature GetProcessMessageSignature(bool isAsync) private MethodProvider BuildProcessMessageAsync() { MethodSignature signature = GetProcessMessageSignature(true); - - var clientErrorNoThrow = _options.NoThrow(); - return new MethodProvider(signature, new MethodBodyStatement[] - { - _pipeline.SendAsync(_message, _options), - MethodBodyStatement.EmptyLine, - new IfStatement(_message.Response().IsError().And(new BinaryOperatorExpression("&", _options.NullConditional().Property("ErrorOptions"), clientErrorNoThrow).NotEqual(clientErrorNoThrow))) - { - Throw(ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ToExpression().CreateAsync(_message.Response())) - }, - MethodBodyStatement.EmptyLine, - Declare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, new TernaryConditionalExpression(_message.BufferResponse(), _message.Response(), Static().Invoke(ExtractResponseContentMethodName, [_messageParam])), out var response), - Return(response) - }, this); + return new MethodProvider(signature, _pipeline.ProcessMessageAsync(_message, _options), this); } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineProvider.cs index 22c03078082..4ae5e0bcd39 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineProvider.cs @@ -5,6 +5,7 @@ using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Statements; +using Microsoft.Generator.CSharp.Snippets; using static Microsoft.Generator.CSharp.Snippets.Snippet; namespace Microsoft.Generator.CSharp.ClientModel.Providers @@ -36,12 +37,34 @@ public override ClientPipelineApi FromExpression(ValueExpression expression) public override ValueExpression PerRetryPolicy(params ValueExpression[] arguments) => Static().Invoke(nameof(ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy), arguments).As(); - public override MethodBodyStatement Send(HttpMessageApi message, HttpRequestOptionsApi options) - => Original.Invoke(nameof(ClientPipeline.Send), [message]).Terminate(); + public override ClientPipelineApi ToExpression() => this; - public override MethodBodyStatement SendAsync(HttpMessageApi message, HttpRequestOptionsApi options) - => Original.Invoke(nameof(ClientPipeline.SendAsync), [message], true).Terminate(); + public override MethodBodyStatement[] ProcessMessage(HttpMessageApi message, HttpRequestOptionsApi options) + => + [ + Original.Invoke(nameof(ClientPipeline.Send), [message]).Terminate(), + MethodBodyStatement.EmptyLine, + new IfStatement(message.Response().IsError().And(new BinaryOperatorExpression("&", options.NullConditional().Property("ErrorOptions"), options.NoThrow()).NotEqual(options.NoThrow()))) + { + Throw(New.Instance(ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseExceptionType, message.Response())) + }, + MethodBodyStatement.EmptyLine, + Declare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, new TernaryConditionalExpression(message.BufferResponse(), message.Response(), message.Invoke(nameof(PipelineMessage.ExtractResponse))), out var response), + Return(response) + ]; - public override ClientPipelineApi ToExpression() => this; + public override MethodBodyStatement[] ProcessMessageAsync(HttpMessageApi message, HttpRequestOptionsApi options) + => + [ + Original.Invoke(nameof(ClientPipeline.SendAsync), [message], true).Terminate(), + MethodBodyStatement.EmptyLine, + new IfStatement(message.Response().IsError().And(new BinaryOperatorExpression("&", options.NullConditional().Property("ErrorOptions"), options.NoThrow()).NotEqual(options.NoThrow()))) + { + Throw(ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ToExpression().CreateAsync(message.Response())) + }, + MethodBodyStatement.EmptyLine, + Declare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, new TernaryConditionalExpression(message.BufferResponse(), message.Response(), message.Invoke(nameof(PipelineMessage.ExtractResponse))), out var response), + Return(response) + ]; } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineMessageProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineMessageProvider.cs index 255acaebc42..f8c5dac5990 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineMessageProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/PipelineMessageProvider.cs @@ -34,9 +34,6 @@ public override MethodBodyStatement ApplyResponseClassifier(StatusCodeClassifier public override MethodBodyStatement ApplyRequestOptions(HttpRequestOptionsApi options) => Original.Invoke(nameof(PipelineMessage.Apply), options).Terminate(); - public override MethodBodyStatement[] ExtractResponse() - => [Return(Original.Invoke(nameof(PipelineMessage.ExtractResponse)))]; - public override HttpMessageApi FromExpression(ValueExpression original) => new PipelineMessageProvider(original); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/ClientPipelineApiTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/ClientPipelineApiTests.cs index fb5c2538b42..6d30f06eb75 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/ClientPipelineApiTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/ClientPipelineApiTests.cs @@ -67,14 +67,13 @@ public override ClientPipelineApi FromExpression(ValueExpression expression) public override ValueExpression PerRetryPolicy(params ValueExpression[] arguments) => Original.Invoke("GetFakePerRetryPolicy", arguments); - public override MethodBodyStatement Send(HttpMessageApi message, HttpRequestOptionsApi options) - => Original.Invoke("GetFakeSend", [message, options]).Terminate(); + public override ClientPipelineApi ToExpression() => this; - public override MethodBodyStatement SendAsync(HttpMessageApi message, HttpRequestOptionsApi options) - => Original.Invoke("GetFakeSendAsync", [message, options]).Terminate(); + public override MethodBodyStatement[] ProcessMessage(HttpMessageApi message, HttpRequestOptionsApi options) + => [Original.Invoke("GetFakeProcessMessage", [message, options]).Terminate()]; - public override ClientPipelineApi ToExpression() => this; + public override MethodBodyStatement[] ProcessMessageAsync(HttpMessageApi message, HttpRequestOptionsApi options) + => [Original.Invoke("GetFakeProcessMessageAsync", [message, options]).Terminate()]; } } - } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/HttpMessageApiTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/HttpMessageApiTests.cs index 2bd282029a3..aa01b341793 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/HttpMessageApiTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Abstractions/HttpMessageApiTests.cs @@ -6,7 +6,6 @@ using Microsoft.Generator.CSharp.Statements; using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; -using static Microsoft.Generator.CSharp.Snippets.Snippet; namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.Abstractions { @@ -60,9 +59,6 @@ public override MethodBodyStatement ApplyRequestOptions(HttpRequestOptionsApi op public override ValueExpression BufferResponse() => Original.Invoke("GetFakeBufferResponse"); - public override MethodBodyStatement[] ExtractResponse() - => [Return(Original.Invoke("GetFakeExtractResponse"))]; - public override HttpMessageApi FromExpression(ValueExpression original) => new TestHttpMessageApi(original); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Definitions/ClientPipelineExtensionsDefCustomizationTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Definitions/ClientPipelineExtensionsDefCustomizationTests.cs index 0ed26feadd6..fe39159437b 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Definitions/ClientPipelineExtensionsDefCustomizationTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/Definitions/ClientPipelineExtensionsDefCustomizationTests.cs @@ -22,7 +22,7 @@ public async Task CanReplaceMethod() // The definitions should not have the custom method var definitionMethods = definition!.Methods; - Assert.AreEqual(4, definitionMethods.Count); + Assert.AreEqual(3, definitionMethods.Count); Assert.IsFalse(definitionMethods.Any(m => m.Signature.Name == "ProcessMessageAsync")); // The custom code view should contain the method diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/Internal/ClientPipelineExtensions.cs b/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/Internal/ClientPipelineExtensions.cs index a11fb09675d..6ed3aa6f719 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/Internal/ClientPipelineExtensions.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/Internal/ClientPipelineExtensions.cs @@ -19,7 +19,7 @@ public static async ValueTask ProcessMessageAsync(this ClientP throw await ClientResultException.CreateAsync(message.Response).ConfigureAwait(false); } - PipelineResponse response = message.BufferResponse ? message.Response : ExtractResponseContent(message); + PipelineResponse response = message.BufferResponse ? message.Response : message.ExtractResponse(); return response; } @@ -32,7 +32,7 @@ public static PipelineResponse ProcessMessage(this ClientPipeline pipeline, Pipe throw new ClientResultException(message.Response); } - PipelineResponse response = message.BufferResponse ? message.Response : ExtractResponseContent(message); + PipelineResponse response = message.BufferResponse ? message.Response : message.ExtractResponse(); return response; } @@ -63,10 +63,5 @@ public static ClientResult ProcessHeadAsBoolMessage(this ClientPipeline pi return new ErrorResult(response, new ClientResultException(response)); } } - - private static PipelineResponse ExtractResponseContent(PipelineMessage message) - { - return message.ExtractResponse(); - } } }