Skip to content

Commit e1622c2

Browse files
authored
[Core.Experimental] Add RequestOptions (Azure#20641)
For the low level client APIs, we want to have some knobs that help you control the behavior of the requests. This class holds these options.
1 parent ecdd772 commit e1622c2

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

sdk/core/Azure.Core.Experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 0.1.0-preview.12 (Unreleased)
44

5+
### New Features
6+
- Added `RequestOptions`.
57

68
## 0.1.0-preview.11 (2021-03-22)
79

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
namespace Azure
2+
{
3+
public partial class RequestOptions
4+
{
5+
public RequestOptions() { }
6+
public RequestOptions(Azure.ResponseStatusOption statusOption) { }
7+
public RequestOptions(System.Action<Azure.Core.HttpMessage> perCall) { }
8+
public System.Threading.CancellationToken CancellationToken { get { throw null; } set { } }
9+
public Azure.Core.Pipeline.HttpPipelinePolicy? PerCallPolicy { get { throw null; } set { } }
10+
public Azure.ResponseStatusOption StatusOption { get { throw null; } set { } }
11+
public static implicit operator Azure.RequestOptions (Azure.ResponseStatusOption option) { throw null; }
12+
}
13+
public enum ResponseStatusOption
14+
{
15+
Default = 0,
16+
NoThrow = 1,
17+
}
18+
}
119
namespace Azure.Core
220
{
321
[System.Diagnostics.DebuggerDisplayAttribute("Content: {_body}")]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading;
6+
using Azure.Core;
7+
using Azure.Core.Pipeline;
8+
9+
namespace Azure
10+
{
11+
/// <summary>
12+
/// Options which can be used to control the behavior of a request sent by a client.
13+
/// </summary>
14+
public class RequestOptions
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="RequestOptions"/> class.
18+
/// </summary>
19+
public RequestOptions() { }
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="RequestOptions"/> class using the given <see cref="RequestOptions"/>.
23+
/// </summary>
24+
/// <param name="statusOption"></param>
25+
public RequestOptions(ResponseStatusOption statusOption) => StatusOption = statusOption;
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="RequestOptions"/> class.
29+
/// </summary>
30+
/// <param name="perCall"></param>
31+
public RequestOptions(Action<HttpMessage> perCall) => PerCallPolicy = new ActionPolicy(perCall);
32+
33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="RequestOptions"/> class using the given <see cref="ResponseStatusOption"/>.
35+
/// </summary>
36+
/// <param name="option"></param>
37+
public static implicit operator RequestOptions(ResponseStatusOption option) => new RequestOptions(option);
38+
39+
/// <summary>
40+
/// The token to check for cancellation.
41+
/// </summary>
42+
public CancellationToken CancellationToken { get; set; } = CancellationToken.None;
43+
44+
/// <summary>
45+
/// Controls under what conditions the operation raises an exception if the underlying response indicates a failure.
46+
/// </summary>
47+
public ResponseStatusOption StatusOption { get; set; } = ResponseStatusOption.Default;
48+
49+
/// <summary>
50+
/// A <see cref="HttpPipelinePolicy"/> to use as part of this operation. This policy will be applied at the start
51+
/// of the underlying <see cref="HttpPipeline"/>.
52+
/// </summary>
53+
public HttpPipelinePolicy? PerCallPolicy { get; set; }
54+
55+
/// <summary>
56+
/// An <see cref="HttpPipelineSynchronousPolicy"/> which invokes an action when a request is being sent.
57+
/// </summary>
58+
internal class ActionPolicy : HttpPipelineSynchronousPolicy
59+
{
60+
private Action<HttpMessage> Action { get; }
61+
62+
public ActionPolicy(Action<HttpMessage> action) => Action = action;
63+
64+
public override void OnSendingRequest(HttpMessage message) => Action.Invoke(message);
65+
}
66+
}
67+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Azure
5+
{
6+
/// <summary>
7+
/// ResponseStatusOption controls the behavior of an operation based on the status code of a response.
8+
/// </summary>
9+
public enum ResponseStatusOption
10+
{
11+
/// <summary>
12+
/// Indicates that an operation should throw an exception when the response indicates a failure.
13+
/// </summary>
14+
Default = 0,
15+
/// <summary>
16+
/// Indicates that an operation should not throw an exception when the response indicates a failure.
17+
/// </summary>
18+
NoThrow = 1,
19+
}
20+
}

0 commit comments

Comments
 (0)