Skip to content

Commit 35066c4

Browse files
authored
Remove RequestOptions from Azure.Core (Azure#27359)
* Remove RequestOptions * export API
1 parent 4b1550c commit 35066c4

File tree

9 files changed

+144
-435
lines changed

9 files changed

+144
-435
lines changed

sdk/core/Azure.Core/api/Azure.Core.net461.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,14 @@ public RequestConditions() { }
189189
public System.DateTimeOffset? IfModifiedSince { get { throw null; } set { } }
190190
public System.DateTimeOffset? IfUnmodifiedSince { get { throw null; } set { } }
191191
}
192-
public partial class RequestContext : Azure.RequestOptions
192+
public partial class RequestContext
193193
{
194194
public RequestContext() { }
195-
public RequestContext(Azure.RequestOptions options) { }
196195
public System.Threading.CancellationToken CancellationToken { get { throw null; } set { } }
196+
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
197+
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
198+
public void AddClassifier(int statusCode, bool isError) { }
199+
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
197200
public static implicit operator Azure.RequestContext (Azure.ErrorOptions options) { throw null; }
198201
}
199202
public partial class RequestFailedException : System.Exception, System.Runtime.Serialization.ISerializable
@@ -209,16 +212,6 @@ public RequestFailedException(string message, System.Exception? innerException)
209212
public int Status { get { throw null; } }
210213
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
211214
}
212-
public partial class RequestOptions
213-
{
214-
public RequestOptions() { }
215-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
216-
protected RequestOptions(Azure.RequestOptions options) { }
217-
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
218-
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
219-
public void AddClassifier(int statusCode, bool isError) { }
220-
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
221-
}
222215
public abstract partial class Response : System.IDisposable
223216
{
224217
protected Response() { }

sdk/core/Azure.Core/api/Azure.Core.net5.0.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,14 @@ public RequestConditions() { }
189189
public System.DateTimeOffset? IfModifiedSince { get { throw null; } set { } }
190190
public System.DateTimeOffset? IfUnmodifiedSince { get { throw null; } set { } }
191191
}
192-
public partial class RequestContext : Azure.RequestOptions
192+
public partial class RequestContext
193193
{
194194
public RequestContext() { }
195-
public RequestContext(Azure.RequestOptions options) { }
196195
public System.Threading.CancellationToken CancellationToken { get { throw null; } set { } }
196+
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
197+
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
198+
public void AddClassifier(int statusCode, bool isError) { }
199+
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
197200
public static implicit operator Azure.RequestContext (Azure.ErrorOptions options) { throw null; }
198201
}
199202
public partial class RequestFailedException : System.Exception, System.Runtime.Serialization.ISerializable
@@ -209,16 +212,6 @@ public RequestFailedException(string message, System.Exception? innerException)
209212
public int Status { get { throw null; } }
210213
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
211214
}
212-
public partial class RequestOptions
213-
{
214-
public RequestOptions() { }
215-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
216-
protected RequestOptions(Azure.RequestOptions options) { }
217-
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
218-
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
219-
public void AddClassifier(int statusCode, bool isError) { }
220-
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
221-
}
222215
public abstract partial class Response : System.IDisposable
223216
{
224217
protected Response() { }

sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,14 @@ public RequestConditions() { }
189189
public System.DateTimeOffset? IfModifiedSince { get { throw null; } set { } }
190190
public System.DateTimeOffset? IfUnmodifiedSince { get { throw null; } set { } }
191191
}
192-
public partial class RequestContext : Azure.RequestOptions
192+
public partial class RequestContext
193193
{
194194
public RequestContext() { }
195-
public RequestContext(Azure.RequestOptions options) { }
196195
public System.Threading.CancellationToken CancellationToken { get { throw null; } set { } }
196+
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
197+
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
198+
public void AddClassifier(int statusCode, bool isError) { }
199+
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
197200
public static implicit operator Azure.RequestContext (Azure.ErrorOptions options) { throw null; }
198201
}
199202
public partial class RequestFailedException : System.Exception, System.Runtime.Serialization.ISerializable
@@ -209,16 +212,6 @@ public RequestFailedException(string message, System.Exception? innerException)
209212
public int Status { get { throw null; } }
210213
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
211214
}
212-
public partial class RequestOptions
213-
{
214-
public RequestOptions() { }
215-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
216-
protected RequestOptions(Azure.RequestOptions options) { }
217-
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
218-
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
219-
public void AddClassifier(int statusCode, bool isError) { }
220-
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
221-
}
222215
public abstract partial class Response : System.IDisposable
223216
{
224217
protected Response() { }

sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,14 @@ public RequestConditions() { }
189189
public System.DateTimeOffset? IfModifiedSince { get { throw null; } set { } }
190190
public System.DateTimeOffset? IfUnmodifiedSince { get { throw null; } set { } }
191191
}
192-
public partial class RequestContext : Azure.RequestOptions
192+
public partial class RequestContext
193193
{
194194
public RequestContext() { }
195-
public RequestContext(Azure.RequestOptions options) { }
196195
public System.Threading.CancellationToken CancellationToken { get { throw null; } set { } }
196+
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
197+
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
198+
public void AddClassifier(int statusCode, bool isError) { }
199+
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
197200
public static implicit operator Azure.RequestContext (Azure.ErrorOptions options) { throw null; }
198201
}
199202
public partial class RequestFailedException : System.Exception, System.Runtime.Serialization.ISerializable
@@ -209,16 +212,6 @@ public RequestFailedException(string message, System.Exception? innerException)
209212
public int Status { get { throw null; } }
210213
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
211214
}
212-
public partial class RequestOptions
213-
{
214-
public RequestOptions() { }
215-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
216-
protected RequestOptions(Azure.RequestOptions options) { }
217-
public Azure.ErrorOptions ErrorOptions { get { throw null; } set { } }
218-
public void AddClassifier(Azure.Core.ResponseClassificationHandler classifier) { }
219-
public void AddClassifier(int statusCode, bool isError) { }
220-
public void AddPolicy(Azure.Core.Pipeline.HttpPipelinePolicy policy, Azure.Core.HttpPipelinePosition position) { }
221-
}
222215
public abstract partial class Response : System.IDisposable
223216
{
224217
protected Response() { }

sdk/core/Azure.Core/src/ChainingClassifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Azure.Core
1313
/// characteristics.
1414
/// The classifier chain is a series of <see cref="ResponseClassificationHandler"/> classifiers
1515
/// followed by the "end-of-chain" <see cref="ResponseClassifier"/>. The handlers are
16-
/// added to the chain via <see cref="RequestContext"/> or <see cref="RequestOptions"/>,
16+
/// added to the chain via <see cref="RequestContext"/>,
1717
/// and all of them are applied starting with the most recently added handler and
1818
/// iterating over the list to the least-recently added handler, then applying status code
1919
/// classification, and finally by applying the "end-of-chain" classifier.

sdk/core/Azure.Core/src/Pipeline/HttpPipeline.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public class HttpPipeline
3030

3131
/// <summary>
3232
/// The pipeline index where <see cref="HttpPipelinePosition.PerCall"/> policies will be added,
33-
/// if any are specified using <see cref="RequestOptions.AddPolicy(HttpPipelinePolicy, HttpPipelinePosition)"/>.
33+
/// if any are specified using <see cref="RequestContext.AddPolicy(HttpPipelinePolicy, HttpPipelinePosition)"/>.
3434
/// </summary>
3535
private readonly int _perCallIndex;
3636

3737
/// <summary>
3838
/// The pipeline index where <see cref="HttpPipelinePosition.PerRetry"/> policies will be added,
39-
/// if any are specified using <see cref="RequestOptions.AddPolicy(HttpPipelinePolicy, HttpPipelinePosition)"/>.
39+
/// if any are specified using <see cref="RequestContext.AddPolicy(HttpPipelinePolicy, HttpPipelinePosition)"/>.
4040
/// </summary>
4141
private readonly int _perRetryIndex;
4242

Lines changed: 121 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
5+
using System.Collections.Generic;
46
using System.Threading;
7+
using Azure.Core;
8+
using Azure.Core.Pipeline;
59

610
namespace Azure
711
{
812
/// <summary>
913
/// Options that can be used to control the behavior of a request sent by a client.
1014
/// </summary>
11-
public class RequestContext : RequestOptions
15+
public class RequestContext
1216
{
17+
private bool _frozen;
18+
19+
private (int Status, bool IsError)[]? _statusCodes;
20+
internal (int Status, bool IsError)[]? StatusCodes => _statusCodes;
21+
22+
private ResponseClassificationHandler[]? _handlers;
23+
internal ResponseClassificationHandler[]? Handlers => _handlers;
24+
25+
internal List<(HttpPipelinePosition Position, HttpPipelinePolicy Policy)>? Policies { get; private set; }
26+
1327
/// <summary>
14-
/// Initializes a new instance of the <see cref="RequestContext"/> class.
28+
/// Controls under what conditions the operation raises an exception if the underlying response indicates a failure.
1529
/// </summary>
16-
public RequestContext() : base()
17-
{
18-
}
30+
public ErrorOptions ErrorOptions { get; set; } = ErrorOptions.Default;
31+
32+
/// <summary>
33+
/// The token to check for cancellation.
34+
/// </summary>
35+
public CancellationToken CancellationToken { get; set; } = CancellationToken.None;
1936

2037
/// <summary>
21-
/// Initializes a new instance of the <see cref="RequestContext"/> class from
22-
/// a <see cref="RequestOptions"/> instance.
38+
/// Initializes a new instance of the <see cref="RequestContext"/> class.
2339
/// </summary>
24-
public RequestContext(RequestOptions options) : base(options)
40+
public RequestContext()
2541
{
2642
}
2743

@@ -32,8 +48,103 @@ public RequestContext(RequestOptions options) : base(options)
3248
public static implicit operator RequestContext(ErrorOptions options) => new RequestContext { ErrorOptions = options };
3349

3450
/// <summary>
35-
/// The token to check for cancellation.
51+
/// Adds an <see cref="HttpPipelinePolicy"/> into the pipeline for the duration of this request.
52+
/// The position of policy in the pipeline is controlled by <paramref name="position"/> parameter.
53+
/// If you want the policy to execute once per client request use <see cref="HttpPipelinePosition.PerCall"/>
54+
/// otherwise use <see cref="HttpPipelinePosition.PerRetry"/> to run the policy for every retry.
3655
/// </summary>
37-
public CancellationToken CancellationToken { get; set; } = CancellationToken.None;
56+
/// <param name="policy">The <see cref="HttpPipelinePolicy"/> instance to be added to the pipeline.</param>
57+
/// <param name="position">The position of the policy in the pipeline.</param>
58+
public void AddPolicy(HttpPipelinePolicy policy, HttpPipelinePosition position)
59+
{
60+
Policies ??= new();
61+
Policies.Add((position, policy));
62+
}
63+
64+
/// <summary>
65+
/// Customizes the <see cref="ResponseClassifier"/> for this operation to change
66+
/// the default <see cref="Response"/> classification behavior so that it considers
67+
/// the passed-in status code to be an error or not, as specified.
68+
/// Status code classifiers are applied after all <see cref="ResponseClassificationHandler"/> classifiers.
69+
/// This is useful for cases where you'd like to prevent specific response status codes from being treated as errors by
70+
/// logging and distributed tracing policies -- that is, if a response is not classified as an error, it will not appear as an error in
71+
/// logs or distributed traces.
72+
/// </summary>
73+
/// <param name="statusCode">The status code to customize classification for.</param>
74+
/// <param name="isError">Whether the passed-in status code should be classified as an error.</param>
75+
/// <exception cref="ArgumentOutOfRangeException">statusCode is not between 100 and 599 (inclusive).</exception>
76+
/// <exception cref="InvalidOperationException">If this method is called after the <see cref="RequestContext"/> has been
77+
/// used in a method call.</exception>
78+
public void AddClassifier(int statusCode, bool isError)
79+
{
80+
Argument.AssertInRange(statusCode, 100, 599, nameof(statusCode));
81+
82+
if (_frozen)
83+
{
84+
throw new InvalidOperationException("Cannot modify classifiers after this type has been used in a method call.");
85+
}
86+
87+
int length = _statusCodes == null ? 0 : _statusCodes.Length;
88+
Array.Resize(ref _statusCodes, length + 1);
89+
Array.Copy(_statusCodes, 0, _statusCodes, 1, length);
90+
_statusCodes[0] = (statusCode, isError);
91+
}
92+
93+
/// <summary>
94+
/// Customizes the <see cref="ResponseClassifier"/> for this operation.
95+
/// Adding a <see cref="ResponseClassificationHandler"/> changes the classification
96+
/// behavior so that it first tries to classify a response via the handler, and if
97+
/// the handler doesn't have an opinion, it instead uses the default classifier.
98+
/// Handlers are applied in order so the most recently added takes precedence.
99+
/// This is useful for cases where you'd like to prevent specific response status codes from being treated as errors by
100+
/// logging and distributed tracing policies -- that is, if a response is not classified as an error, it will not appear as an error in
101+
/// logs or distributed traces.
102+
/// </summary>
103+
/// <param name="classifier">The custom classifier.</param>
104+
/// <exception cref="InvalidOperationException">If this method is called after the <see cref="RequestContext"/> has been
105+
/// used in a method call.</exception>
106+
public void AddClassifier(ResponseClassificationHandler classifier)
107+
{
108+
if (_frozen)
109+
{
110+
throw new InvalidOperationException("Cannot modify classifiers after this type has been used in a method call.");
111+
}
112+
113+
int length = _handlers == null ? 0 : _handlers.Length;
114+
Array.Resize(ref _handlers, length + 1);
115+
Array.Copy(_handlers, 0, _handlers, 1, length);
116+
_handlers[0] = classifier;
117+
}
118+
119+
internal void Freeze()
120+
{
121+
_frozen = true;
122+
}
123+
124+
internal ResponseClassifier Apply(ResponseClassifier classifier)
125+
{
126+
if (_statusCodes == null && _handlers == null)
127+
{
128+
return classifier;
129+
}
130+
131+
if (classifier is CoreResponseClassifier coreClassifier)
132+
{
133+
CoreResponseClassifier clone = coreClassifier.Clone();
134+
clone.Handlers = _handlers;
135+
136+
if (_statusCodes != null)
137+
{
138+
foreach (var classification in _statusCodes)
139+
{
140+
clone.AddClassifier(classification.Status, classification.IsError);
141+
}
142+
}
143+
144+
return clone;
145+
}
146+
147+
return new ChainingClassifier(_statusCodes, _handlers, classifier);
148+
}
38149
}
39150
}

0 commit comments

Comments
 (0)