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

Update ClientPipelineApi, HttpMessageApi abstraction #4816

Merged
merged 16 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Snippets;

namespace Microsoft.Generator.CSharp.ClientModel.Providers
Expand All @@ -18,13 +19,11 @@ protected ClientPipelineApi(Type type, ValueExpression original) : base(type, or
{
}

public abstract HttpMessageApi CreateMessage();

public abstract ValueExpression CreateMessage(HttpRequestOptionsApi requestOptions, ValueExpression responseClassifier);

public abstract InvokeMethodExpression Send(HttpMessageApi message);
public abstract InvokeMethodExpression Send(HttpMessageApi message, HttpRequestOptionsApi options);

public abstract InvokeMethodExpression SendAsync(HttpMessageApi message);
public abstract InvokeMethodExpression SendAsync(HttpMessageApi message, HttpRequestOptionsApi options);

public abstract ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected HttpMessageApi(Type type, ValueExpression original) : base(type, origi

public abstract ValueExpression BufferResponse();

public abstract ValueExpression ResponseClassifier();
public abstract MethodBodyStatement Assign(StatusCodeClassifierApi statusCodeClassifier);
live1206 marked this conversation as resolved.
Show resolved Hide resolved

public abstract MethodBodyStatement Apply(ValueExpression options);
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ClientPipelineExtensionsDefinition()
{
_pipelineParam = new ParameterProvider("pipeline", FormattableStringHelpers.Empty, ClientModelPlugin.Instance.TypeFactory.ClientPipelineApi.ClientPipelineType);
_messageParam = new ParameterProvider("message", FormattableStringHelpers.Empty, ClientModelPlugin.Instance.TypeFactory.HttpMessageApi.HttpMessageType);
_requestOptionsParam = new ParameterProvider("options", FormattableStringHelpers.Empty, ClientModelPlugin.Instance.TypeFactory.HttpRequestOptionsApi.HttpRequestOptionsType);
_requestOptionsParam = new ParameterProvider(ClientModelPlugin.Instance.TypeFactory.HttpRequestOptionsApi.ParameterName, FormattableStringHelpers.Empty, ClientModelPlugin.Instance.TypeFactory.HttpRequestOptionsApi.HttpRequestOptionsType);
_pipeline = _pipelineParam.AsExpression.ToApi<ClientPipelineApi>();
_message = _messageParam.AsExpression.ToApi<HttpMessageApi>();
_options = _requestOptionsParam.AsExpression.ToApi<HttpRequestOptionsApi>();
Expand Down Expand Up @@ -124,7 +124,7 @@ private MethodProvider BuildProcessMessage()
var clientErrorNoThrow = _options.NoThrow();
return new MethodProvider(signature, new MethodBodyStatement[]
{
_pipeline.Send(_message).Terminate(),
_pipeline.Send(_message, _options).Terminate(),
MethodBodyStatement.EmptyLine,
new IfStatement(_message.Response().IsError().And(new BinaryOperatorExpression("&", _options.NullConditional().Property("ErrorOptions"), clientErrorNoThrow).NotEqual(clientErrorNoThrow)))
{
Expand Down Expand Up @@ -167,7 +167,7 @@ private MethodProvider BuildProcessMessageAsync()
var clientErrorNoThrow = _options.NoThrow();
return new MethodProvider(signature, new MethodBodyStatement[]
{
_pipeline.SendAsync(_message).Terminate(),
_pipeline.SendAsync(_message, _options).Terminate(),
MethodBodyStatement.EmptyLine,
new IfStatement(_message.Response().IsError().And(new BinaryOperatorExpression("&", _options.NullConditional().Property("ErrorOptions"), clientErrorNoThrow).NotEqual(clientErrorNoThrow)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ClientModel.Primitives;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

namespace Microsoft.Generator.CSharp.ClientModel.Providers
Expand All @@ -26,22 +27,19 @@ public ClientPipelineProvider(ValueExpression original) : base(typeof(ClientPipe
public override ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies)
=> Static<ClientPipeline>().Invoke(nameof(ClientPipeline.Create), [options, New.Array(ClientModelPlugin.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType), perRetryPolicies, New.Array(ClientModelPlugin.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType)]).As<ClientPipeline>();

public override HttpMessageApi CreateMessage()
=> new PipelineMessageProvider(Original.Invoke(nameof(ClientPipeline.CreateMessage)));

public override ValueExpression CreateMessage(HttpRequestOptionsApi requestOptions, ValueExpression responseClassifier)
=> Original.Invoke(nameof(ClientPipeline.CreateMessage), requestOptions, responseClassifier).As<PipelineMessage>();
=> new PipelineMessageProvider(Original.Invoke(nameof(ClientPipeline.CreateMessage)));

public override ClientPipelineApi FromExpression(ValueExpression expression)
=> new ClientPipelineProvider(expression);

public override ValueExpression PerRetryPolicy(params ValueExpression[] arguments)
=> Static<ApiKeyAuthenticationPolicy>().Invoke(nameof(ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy), arguments).As<ApiKeyAuthenticationPolicy>();

public override InvokeMethodExpression Send(HttpMessageApi message)
public override InvokeMethodExpression Send(HttpMessageApi message, HttpRequestOptionsApi options)
=> Original.Invoke(nameof(ClientPipeline.Send), [message]);

public override InvokeMethodExpression SendAsync(HttpMessageApi message)
public override InvokeMethodExpression SendAsync(HttpMessageApi message, HttpRequestOptionsApi options)
=> Original.Invoke(nameof(ClientPipeline.SendAsync), [message], true);

public override ClientPipelineApi ToExpression() => this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.ClientModel.Primitives;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Statements;
Expand All @@ -27,8 +28,8 @@ public override ValueExpression BufferResponse()
public override HttpResponseApi Response()
=> new PipelineResponseProvider(Original.Property(nameof(PipelineMessage.Response)));

public override ValueExpression ResponseClassifier()
=> Original.Property(nameof(PipelineMessage.ResponseClassifier));
public override MethodBodyStatement Assign(StatusCodeClassifierApi statusCodeClassifier)
=> Original.Property(nameof(PipelineMessage.ResponseClassifier)).Assign(statusCodeClassifier).Terminate();

public override MethodBodyStatement Apply(ValueExpression options)
=> Original.Invoke(nameof(PipelineMessage.Apply), options).Terminate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ private MethodProvider BuildCreateRequestMethod(InputOperation operation)
signature,
new MethodBodyStatements(
[
Declare("message", pipelineField.CreateMessage().ToApi<HttpMessageApi>(), out HttpMessageApi message),
message.ResponseClassifier().Assign(classifier).Terminate(),
Declare("request", message.Request().ToApi<HttpRequestApi>(), out HttpRequestApi request), // ScopedApi<PipelineRequest>
Declare("message", pipelineField.CreateMessage(options.AsExpression.ToApi<HttpRequestOptionsApi>(), classifier).ToApi<HttpMessageApi>(), out HttpMessageApi message),
message.Assign(((MemberExpression)classifier).ToApi<StatusCodeClassifierApi>()),
Declare("request", message.Request().ToApi<HttpRequestApi>(), out HttpRequestApi request),
request.SetMethod(operation.HttpMethod).Terminate(),
Declare("uri", New.Instance<ClientUriBuilderDefinition>(), out ScopedApi<ClientUriBuilderDefinition> uri),
uri.Reset(ClientProvider.EndpointField).Terminate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public void ValidateBodyOfRestClientOperationIsOverridden()

Assert.NotNull(method);
Assert.NotNull(method!.BodyStatements);
var test = method?.BodyStatements?.ToDisplayString();
Assert.AreEqual(Helpers.GetExpectedFromFile(), method!.BodyStatements!.ToDisplayString());
}

Expand Down Expand Up @@ -58,9 +57,6 @@ public TestClientPipelineApi(ValueExpression original) : base(typeof(string), or
public override ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies)
=> Original.Invoke("GetFakeCreate", [options, perRetryPolicies]);

public override HttpMessageApi CreateMessage()
=> Original.Invoke("GetFakeCreateMessage").ToApi<HttpMessageApi>();

public override ValueExpression CreateMessage(HttpRequestOptionsApi requestOptions, ValueExpression responseClassifier)
=> Original.Invoke("GetFakeCreateMessage", [requestOptions, responseClassifier]);

Expand All @@ -70,11 +66,11 @@ public override ClientPipelineApi FromExpression(ValueExpression expression)
public override ValueExpression PerRetryPolicy(params ValueExpression[] arguments)
=> Original.Invoke("GetFakePerRetryPolicy", arguments);

public override InvokeMethodExpression Send(HttpMessageApi message)
=> Original.Invoke("GetFakeSend", message);
public override InvokeMethodExpression Send(HttpMessageApi message, HttpRequestOptionsApi options)
=> Original.Invoke("GetFakeSend", [message, options]);

public override InvokeMethodExpression SendAsync(HttpMessageApi message)
=> Original.Invoke("GetFakeSendAsync", message);
public override InvokeMethodExpression SendAsync(HttpMessageApi message, HttpRequestOptionsApi options)
=> Original.Invoke("GetFakeSendAsync", [message, options]);

public override ClientPipelineApi ToExpression() => this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void ValidateBodyOfProcessMessageIsOverridden()

Assert.IsNotNull(method);
Assert.IsNotNull(method!.BodyStatements);
var test = method!.BodyStatements!.ToDisplayString();
Assert.AreEqual(Helpers.GetExpectedFromFile(), method!.BodyStatements!.ToDisplayString());
}

Expand Down Expand Up @@ -73,8 +72,8 @@ public override HttpRequestApi Request()
public override HttpResponseApi Response()
=> Original.Invoke("GetFakeResponse").ToApi<HttpResponseApi>();

public override ValueExpression ResponseClassifier()
=> Original.Invoke("GetFakeResponseClassifier");
public override MethodBodyStatement Assign(StatusCodeClassifierApi statusCodeClassifier)
=> Original.Invoke("GetFakeResponseClassifier", [statusCodeClassifier]).Terminate();

public override HttpMessageApi ToExpression() => this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global::System.ClientModel.Primitives.PipelineMessage message = Pipeline.GetFakeCreateMessage();
global::System.ClientModel.Primitives.PipelineMessage message = Pipeline.GetFakeCreateMessage(options, PipelineMessageClassifier200);
message.ResponseClassifier = PipelineMessageClassifier200;
global::System.ClientModel.Primitives.PipelineRequest request = message.Request;
request.Method = "GET";
Expand Down
Loading