forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Response abstraction for Azure (Azure#46530)
- Loading branch information
Showing
38 changed files
with
1,083 additions
and
631 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...csharp/generator/Azure.Generator/src/Providers/Abstraction/AzureClientResponseProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Snippets; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Azure.Generator.Providers | ||
{ | ||
internal record AzureClientResponseProvider : ClientResponseApi | ||
{ | ||
private static ClientResponseApi? _instance; | ||
internal static ClientResponseApi Instance => _instance ??= new AzureClientResponseProvider(Empty); | ||
|
||
public AzureClientResponseProvider(ValueExpression original) : base(typeof(Response), original) | ||
{ | ||
} | ||
|
||
public override CSharpType ClientResponseType => typeof(Response); | ||
|
||
public override CSharpType ClientResponseOfTType => typeof(Response<>); | ||
|
||
public override CSharpType ClientResponseExceptionType => typeof(RequestFailedException); | ||
|
||
public override ValueExpression CreateAsync(HttpResponseApi response) | ||
=> New.Instance(ClientResponseExceptionType, [response]); | ||
|
||
public override ClientResponseApi FromExpression(ValueExpression original) | ||
=> new AzureClientResponseProvider(original); | ||
|
||
public override ValueExpression FromResponse(ValueExpression valueExpression) => valueExpression; | ||
|
||
public override ValueExpression FromValue(ValueExpression valueExpression, HttpResponseApi response) | ||
=> Static(ClientResponseType).Invoke(nameof(FromValue), [valueExpression, response]); | ||
|
||
public override ValueExpression FromValue<ValueType>(ValueExpression valueExpression, HttpResponseApi response) | ||
=> Static(ClientResponseType).Invoke(nameof(FromValue), [valueExpression, response], [typeof(ValueType)], false); | ||
|
||
public override HttpResponseApi GetRawResponse() | ||
=> new AzureResponseProvider(GetRawResponseExpression()); | ||
|
||
public override ClientResponseApi ToExpression() => this; | ||
|
||
private ScopedApi<Response> GetRawResponseExpression() => Original.As<Response>(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...lient-csharp/generator/Azure.Generator/src/Providers/Abstraction/AzureResponseProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Snippets; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Azure.Generator.Providers | ||
{ | ||
internal record AzureResponseProvider : HttpResponseApi | ||
{ | ||
private static HttpResponseApi? _instance; | ||
internal static HttpResponseApi Instance => _instance ??= new AzureResponseProvider(Empty); | ||
|
||
public AzureResponseProvider(ValueExpression original) : base(typeof(Response), original) | ||
{ | ||
} | ||
|
||
public override CSharpType HttpResponseType => typeof(Response); | ||
|
||
public override ScopedApi<BinaryData> Content() | ||
=> Original.Property(nameof(Response.Content)).As<BinaryData>(); | ||
|
||
public override ScopedApi<Stream> ContentStream() | ||
=> Original.Property(nameof(Response.ContentStream)).As<Stream>(); | ||
|
||
public override HttpResponseApi FromExpression(ValueExpression original) | ||
=> new AzureResponseProvider(original); | ||
|
||
public override ScopedApi<bool> IsError() | ||
=> Original.Property(nameof(Response.IsError)).As<bool>(); | ||
|
||
public override HttpResponseApi ToExpression() => this; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...-client-csharp/generator/Azure.Generator/src/Providers/Abstraction/HttpMessageProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core; | ||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Statements; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Azure.Generator.Providers | ||
{ | ||
internal record HttpMessageProvider : HttpMessageApi | ||
{ | ||
private static HttpMessageApi? _instance; | ||
internal static HttpMessageApi Instance => _instance ??= new HttpMessageProvider(Empty); | ||
|
||
public HttpMessageProvider(ValueExpression original) : base(typeof(HttpMessage), original) | ||
{ | ||
} | ||
|
||
public override CSharpType HttpMessageType => typeof(HttpMessage); | ||
|
||
public override MethodBodyStatement Apply(ValueExpression options) | ||
=> MethodBodyStatement.Empty; | ||
|
||
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); | ||
|
||
public override HttpRequestApi Request() | ||
=> new HttpRequestProvider(Original.Property(nameof(HttpMessage.Request))); | ||
|
||
public override HttpResponseApi Response() | ||
=> new AzureResponseProvider(Original.Property(nameof(HttpMessage.Response))); | ||
|
||
public override ValueExpression ResponseClassifier() | ||
=> Original.Property(nameof(HttpMessage.ResponseClassifier)); | ||
|
||
public override HttpMessageApi ToExpression() => this; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...client-csharp/generator/Azure.Generator/src/Providers/Abstraction/HttpPipelineProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core; | ||
using Azure.Core.Pipeline; | ||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Azure.Generator.Providers.Abstraction | ||
{ | ||
internal record HttpPipelineProvider : ClientPipelineApi | ||
{ | ||
private static ClientPipelineApi? _instance; | ||
internal static ClientPipelineApi Instance => _instance ??= new HttpPipelineProvider(Empty); | ||
|
||
public HttpPipelineProvider(ValueExpression original) : base(typeof(HttpPipeline), original) | ||
{ | ||
} | ||
|
||
public override CSharpType ClientPipelineType => typeof(HttpPipeline); | ||
|
||
public override CSharpType ClientPipelineOptionsType => typeof(ClientOptions); | ||
|
||
public override CSharpType PipelinePolicyType => typeof(HttpPipelinePolicy); | ||
|
||
public override ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies) | ||
=> Static(typeof(HttpPipelineBuilder)).Invoke(nameof(HttpPipelineBuilder.Build), [options, perRetryPolicies]); | ||
|
||
public override HttpMessageApi CreateMessage() | ||
=> new HttpMessageProvider(Original.Invoke(nameof(HttpPipeline.CreateMessage))); | ||
|
||
public override ValueExpression CreateMessage(HttpRequestOptionsApi requestOptions, ValueExpression responseClassifier) | ||
=> Original.Invoke(nameof(HttpPipeline.CreateMessage), requestOptions, responseClassifier).As<HttpMessage>(); | ||
|
||
public override ClientPipelineApi FromExpression(ValueExpression expression) | ||
=> new HttpPipelineProvider(expression); | ||
|
||
public override ValueExpression PerRetryPolicy(params ValueExpression[] arguments) | ||
=> Empty; // TODO: implement with default retry policy for Azure | ||
|
||
public override InvokeMethodExpression Send(HttpMessageApi message) | ||
=> Original.Invoke(nameof(HttpPipeline.Send), [message, Default]); | ||
|
||
public override InvokeMethodExpression SendAsync(HttpMessageApi message) | ||
=> Original.Invoke(nameof(HttpPipeline.SendAsync), [message, Default], true); | ||
|
||
public override ClientPipelineApi ToExpression() => this; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...-csharp/generator/Azure.Generator/src/Providers/Abstraction/HttpRequestOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Azure.Generator.Providers.Abstraction | ||
{ | ||
internal record HttpRequestOptionsProvider : HttpRequestOptionsApi | ||
{ | ||
private static HttpRequestOptionsApi? _instance; | ||
internal static HttpRequestOptionsApi Instance => _instance ??= new HttpRequestOptionsProvider(Empty); | ||
|
||
public HttpRequestOptionsProvider(ValueExpression original) : base(typeof(RequestContext), original) | ||
{ | ||
} | ||
|
||
public override CSharpType HttpRequestOptionsType => typeof(RequestContext); | ||
|
||
public override string ParameterName => "context"; | ||
|
||
public override ValueExpression ErrorOptions() | ||
=> Original.NullConditional().Property(nameof(RequestContext.ErrorOptions)); | ||
|
||
public override HttpRequestOptionsApi FromExpression(ValueExpression original) | ||
=> new HttpRequestOptionsProvider(original); | ||
|
||
public override ValueExpression NoThrow() | ||
=> FrameworkEnumValue(Azure.ErrorOptions.NoThrow); | ||
|
||
public override HttpRequestOptionsApi ToExpression() => this; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...-client-csharp/generator/Azure.Generator/src/Providers/Abstraction/HttpRequestProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core; | ||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Snippets; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Azure.Generator.Providers | ||
{ | ||
internal record HttpRequestProvider : HttpRequestApi | ||
{ | ||
private static HttpRequestApi? _instance; | ||
internal static HttpRequestApi Instance => _instance ??= new HttpRequestProvider(Empty); | ||
|
||
public HttpRequestProvider(ValueExpression original) : base(typeof(Request), original) | ||
{ | ||
} | ||
|
||
public override ValueExpression Content() | ||
=> Original.Property(nameof(Request.Content)); | ||
|
||
public override HttpRequestApi FromExpression(ValueExpression original) | ||
=> new HttpRequestProvider(original); | ||
|
||
public override InvokeMethodExpression SetHeaders(IReadOnlyList<ValueExpression> arguments) | ||
=> Original.Property(nameof(PipelineRequest.Headers)).Invoke(nameof(RequestHeaders.Add), arguments); | ||
|
||
public override AssignmentExpression SetMethod(string httpMethod) | ||
=> Original.Property(nameof(PipelineRequest.Method)).Assign(New.Instance(typeof(RequestMethod), [Literal(httpMethod)])); | ||
|
||
public override AssignmentExpression SetUri(ValueExpression value) | ||
=> Original.Property("Uri").Assign(value); | ||
|
||
public override HttpRequestApi ToExpression() => this; | ||
} | ||
} |
Oops, something went wrong.