Skip to content

Commit c74adcc

Browse files
authored
Apply RequestContext for ProcessMessage (#47049)
1 parent 24c69a5 commit c74adcc

19 files changed

+161
-44
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@
252252
</ItemGroup>
253253

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

258258
<!--

eng/packages/http-client-csharp/generator/Azure.Generator/src/AzureClientPlugin.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public class AzureClientPlugin : ClientModelPlugin
2424
/// <inheritdoc/>
2525
public override AzureTypeFactory TypeFactory { get; }
2626

27+
private AzureOutputLibrary? _azureOutputLibrary;
28+
/// <inheritdoc/>
29+
public override AzureOutputLibrary OutputLibrary => _azureOutputLibrary ??= new();
30+
2731
/// <summary>
2832
/// The Azure client plugin to generate the Azure client SDK.
2933
/// </summary>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Generator.Providers;
5+
using Microsoft.Generator.CSharp.ClientModel;
6+
using Microsoft.Generator.CSharp.Providers;
7+
8+
namespace Azure.Generator
9+
{
10+
/// <inheritdoc/>
11+
public class AzureOutputLibrary : ScmOutputLibrary
12+
{
13+
/// <inheritdoc/>
14+
protected override TypeProvider[] BuildTypeProviders() => [.. base.BuildTypeProviders(), new RequestContextExtensionsDefinition()];
15+
}
16+
}

eng/packages/http-client-csharp/generator/Azure.Generator/src/Providers/Abstraction/HttpMessageProvider.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ public HttpMessageProvider(ValueExpression original) : base(typeof(HttpMessage),
2424
public override ValueExpression BufferResponse()
2525
=> Original.Property(nameof(HttpMessage.BufferResponse));
2626

27-
public override MethodBodyStatement[] ExtractResponse()
28-
=> [Original.Invoke(nameof(HttpMessage.ExtractResponseContent)).Terminate(), Return(Original.Property(nameof(HttpMessage.Response)))];
29-
3027
public override HttpMessageApi FromExpression(ValueExpression original)
3128
=> new HttpMessageProvider(original);
3229

eng/packages/http-client-csharp/generator/Azure.Generator/src/Providers/Abstraction/HttpPipelineProvider.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
using Microsoft.Generator.CSharp.ClientModel.Providers;
77
using Microsoft.Generator.CSharp.Expressions;
88
using Microsoft.Generator.CSharp.Primitives;
9+
using Microsoft.Generator.CSharp.Providers;
10+
using Microsoft.Generator.CSharp.Snippets;
911
using Microsoft.Generator.CSharp.Statements;
12+
using System.Threading;
1013
using static Microsoft.Generator.CSharp.Snippets.Snippet;
1114

1215
namespace Azure.Generator.Providers.Abstraction
@@ -40,10 +43,30 @@ public override ValueExpression PerRetryPolicy(params ValueExpression[] argument
4043

4144
public override ClientPipelineApi ToExpression() => this;
4245

43-
public override MethodBodyStatement Send(HttpMessageApi message, HttpRequestOptionsApi options)
44-
=> Original.Invoke(nameof(HttpPipeline.Send), [message, Default]).Terminate();
46+
public override MethodBodyStatement[] ProcessMessage(HttpMessageApi message, HttpRequestOptionsApi options)
47+
=> BuildProcessMessage(message, options, false);
4548

46-
public override MethodBodyStatement SendAsync(HttpMessageApi message, HttpRequestOptionsApi options)
47-
=> Original.Invoke(nameof(HttpPipeline.SendAsync), [message, Default], true).Terminate();
49+
public override MethodBodyStatement[] ProcessMessageAsync(HttpMessageApi message, HttpRequestOptionsApi options)
50+
=> BuildProcessMessage(message, options, true);
51+
52+
private MethodBodyStatement[] BuildProcessMessage(HttpMessageApi message, HttpRequestOptionsApi options, bool isAsync)
53+
{
54+
var userCancellationToken = new ParameterProvider("userCancellationToken", $"", new CSharpType(typeof(CancellationToken)));
55+
var statusOption = new ParameterProvider("statusOption", $"", new CSharpType(typeof(ErrorOptions)));
56+
return
57+
[
58+
new VariableTupleExpression(false, userCancellationToken, statusOption).Assign(options.Invoke("Parse")).Terminate(),
59+
Original.Invoke(isAsync ? nameof(HttpPipeline.SendAsync) : nameof(HttpPipeline.Send), [message, userCancellationToken], isAsync).Terminate(),
60+
MethodBodyStatement.EmptyLine,
61+
new IfStatement(message.Response().IsError().And(new BinaryOperatorExpression("&", options.NullConditional().Property("ErrorOptions"), options.NoThrow()).NotEqual(options.NoThrow())))
62+
{
63+
isAsync
64+
? Throw(AzureClientPlugin.Instance.TypeFactory.ClientResponseApi.ToExpression().CreateAsync(message.Response()))
65+
: Throw(New.Instance(AzureClientPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseExceptionType, message.Response()))
66+
},
67+
MethodBodyStatement.EmptyLine,
68+
Return(message.Response())
69+
];
70+
}
4871
}
4972
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Generator.CSharp.Expressions;
5+
using Microsoft.Generator.CSharp.Primitives;
6+
using Microsoft.Generator.CSharp.Providers;
7+
using Microsoft.Generator.CSharp.Snippets;
8+
using Microsoft.Generator.CSharp.Statements;
9+
using System.IO;
10+
using System.Threading;
11+
using static Microsoft.Generator.CSharp.Snippets.Snippet;
12+
13+
namespace Azure.Generator.Providers
14+
{
15+
internal class RequestContextExtensionsDefinition : TypeProvider
16+
{
17+
protected override TypeSignatureModifiers GetDeclarationModifiers() => TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
18+
19+
protected override string BuildName() => "RequestContextExtensions";
20+
21+
protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");
22+
23+
protected override MethodProvider[] BuildMethods() => [BuildParse()];
24+
25+
private MethodProvider BuildParse()
26+
{
27+
var requestContextParameter = new ParameterProvider("requestContext", $"", typeof(RequestContext));
28+
var signature = new MethodSignature(
29+
"Parse",
30+
null,
31+
MethodSignatureModifiers.Public | MethodSignatureModifiers.Static | MethodSignatureModifiers.Extension,
32+
new CSharpType(typeof((CancellationToken, ErrorOptions))),
33+
null,
34+
[requestContextParameter]);
35+
36+
var method = new MethodProvider(signature, new MethodBodyStatement[]
37+
{
38+
new IfStatement(requestContextParameter.Equal(Null))
39+
{
40+
Return(new TupleExpression(Static<CancellationToken>().Property(nameof(CancellationToken.None)), Static<ErrorOptions>().Property(nameof(ErrorOptions.Default))))
41+
},
42+
Return(new TupleExpression(requestContextParameter.Property(nameof(RequestContext.CancellationToken)), requestContextParameter.Property(nameof(RequestContext.ErrorOptions))))
43+
}, this);
44+
return method;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)