Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
</ItemGroup>

<ItemGroup Condition="'$(IsGeneratorLibrary)' == 'true'">
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20241030.4" />
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20241111.4" />
</ItemGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class AzureClientPlugin : ClientModelPlugin
/// <inheritdoc/>
public override AzureTypeFactory TypeFactory { get; }

private AzureOutputLibrary? _azureOutputLibrary;
/// <inheritdoc/>
public override AzureOutputLibrary OutputLibrary => _azureOutputLibrary ??= new();

/// <summary>
/// The Azure client plugin to generate the Azure client SDK.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Generator.Providers;
using Microsoft.Generator.CSharp.ClientModel;
using Microsoft.Generator.CSharp.Providers;

namespace Azure.Generator
{
/// <inheritdoc/>
public class AzureOutputLibrary : ScmOutputLibrary
{
/// <inheritdoc/>
protected override TypeProvider[] BuildTypeProviders()
=> [
..base.BuildTypeProviders(),
new RequestContextExtensionsDefinition(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public HttpMessageProvider(ValueExpression original) : base(typeof(HttpMessage),
public override ValueExpression BufferResponse()
=> Original.Property(nameof(HttpMessage.BufferResponse));

public override MethodBodyStatement[] ExtractResponse()
=> [Original.Invoke(nameof(HttpMessage.ExtractResponseContent)).Terminate(), Return(Original.Property(nameof(HttpMessage.Response)))];

public override HttpMessageApi FromExpression(ValueExpression original)
=> new HttpMessageProvider(original);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
using Microsoft.Generator.CSharp.ClientModel.Providers;
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 System.Threading;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

namespace Azure.Generator.Providers.Abstraction
Expand Down Expand Up @@ -40,10 +43,40 @@ public override ValueExpression PerRetryPolicy(params ValueExpression[] argument

public override ClientPipelineApi ToExpression() => this;

public override MethodBodyStatement Send(HttpMessageApi message, HttpRequestOptionsApi options)
=> Original.Invoke(nameof(HttpPipeline.Send), [message, Default]).Terminate();
public override MethodBodyStatement[] ProcessMessage(HttpMessageApi message, HttpRequestOptionsApi options)
{
var userCacellationToken = new ParameterProvider("userCancellationToken", $"", new CSharpType(typeof(CancellationToken)));
var statusOption = new ParameterProvider("statusOption", $"", new CSharpType(typeof(ErrorOptions)));
return new MethodBodyStatement[]
{
new VariableTupleExpression(false, userCacellationToken, statusOption).Assign(options.Invoke("Parse")).Terminate(),
Original.Invoke(nameof(HttpPipeline.Send), [message, userCacellationToken]).Terminate(),
MethodBodyStatement.EmptyLine,
new IfStatement(message.Response().IsError().And(new BinaryOperatorExpression("&", options.NullConditional().Property("ErrorOptions"), options.NoThrow()).NotEqual(options.NoThrow())))
{
Throw(New.Instance(AzureClientPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseExceptionType, message.Response()))
},
MethodBodyStatement.EmptyLine,
Return(message.Response())
};
}

public override MethodBodyStatement SendAsync(HttpMessageApi message, HttpRequestOptionsApi options)
=> Original.Invoke(nameof(HttpPipeline.SendAsync), [message, Default], true).Terminate();
public override MethodBodyStatement[] ProcessMessageAsync(HttpMessageApi message, HttpRequestOptionsApi options)
{
var userCacellationToken = new ParameterProvider("userCancellationToken", $"", new CSharpType(typeof(CancellationToken)));
var statusOption = new ParameterProvider("statusOption", $"", new CSharpType(typeof(ErrorOptions)));
return new MethodBodyStatement[]
{
new VariableTupleExpression(false, userCacellationToken, statusOption).Assign(options.Invoke("Parse")).Terminate(),
Original.Invoke(nameof(HttpPipeline.SendAsync), [message, userCacellationToken], true).Terminate(),
MethodBodyStatement.EmptyLine,
new IfStatement(message.Response().IsError().And(new BinaryOperatorExpression("&", options.NullConditional().Property("ErrorOptions"), options.NoThrow()).NotEqual(options.NoThrow())))
{
Throw(AzureClientPlugin.Instance.TypeFactory.ClientResponseApi.ToExpression().CreateAsync(message.Response()))
},
MethodBodyStatement.EmptyLine,
Return(message.Response())
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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 System.IO;
using System.Threading;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

namespace Azure.Generator.Providers
{
internal class RequestContextExtensionsDefinition : TypeProvider
{
protected override TypeSignatureModifiers GetDeclarationModifiers() => TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;

protected override string BuildName() => "RequestContextExtensions";

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override MethodProvider[] BuildMethods()
{
return
[
BuildParse()
];
}

private MethodProvider BuildParse()
{
var requestContextParameter = new ParameterProvider("requestContext", $"", typeof(RequestContext));
var signature = new MethodSignature(
"Parse",
null,
MethodSignatureModifiers.Public | MethodSignatureModifiers.Static | MethodSignatureModifiers.Extension,
new CSharpType(typeof((CancellationToken, ErrorOptions))),
null,
[requestContextParameter]);

var method = new MethodProvider(signature, new MethodBodyStatement[]
{
new IfStatement(requestContextParameter.Equal(Null))
{
Return(new TupleExpression(Static<CancellationToken>().Property(nameof(CancellationToken.None)), Static<ErrorOptions>().Property(nameof(ErrorOptions.Default))))
},
Return(new TupleExpression(requestContextParameter.Property(nameof(RequestContext.CancellationToken)), requestContextParameter.Property(nameof(RequestContext.ErrorOptions))))
}, this);
return method;
}
}
}
Loading