| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | | using Azure.Core.Pipeline; |
| | 8 | |
|
| | 9 | | namespace Azure.Core |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Base type for all client option types, exposes various common client options like <see cref="Diagnostics"/>, <se |
| | 13 | | /// </summary> |
| | 14 | | public abstract class ClientOptions |
| | 15 | | { |
| 94 | 16 | | private HttpPipelineTransport _transport = GetDefaultTransport(); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Creates a new instance of <see cref="ClientOptions"/>. |
| | 20 | | /// </summary> |
| 94 | 21 | | protected ClientOptions() |
| | 22 | | { |
| 94 | 23 | | Retry = new RetryOptions(); |
| 94 | 24 | | Diagnostics = new DiagnosticsOptions(); |
| 94 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The <see cref="HttpPipelineTransport"/> to be used for this client. Defaults to an instance of <see cref="Ht |
| | 29 | | /// </summary> |
| | 30 | | public HttpPipelineTransport Transport |
| | 31 | | { |
| 58 | 32 | | get => _transport; |
| 40 | 33 | | set => _transport = value ?? throw new ArgumentNullException(nameof(value)); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets the client diagnostic options. |
| | 38 | | /// </summary> |
| 260 | 39 | | public DiagnosticsOptions Diagnostics { get; } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets the client retry options. |
| | 43 | | /// </summary> |
| 156 | 44 | | public RetryOptions Retry { get; } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Adds an <see cref="HttpPipeline"/> policy into the client pipeline. The position of policy in the pipeline i |
| | 48 | | /// If you want the policy to execute once per client request use <see cref="HttpPipelinePosition.PerCall"/> oth |
| | 49 | | /// to run the policy for every retry. Note that the same instance of <paramref name="policy"/> would be added t |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="policy">The <see cref="HttpPipelinePolicy"/> instance to be added to the pipeline.</param> |
| | 52 | | /// <param name="position">The position of policy in the pipeline.</param> |
| | 53 | | public void AddPolicy(HttpPipelinePolicy policy, HttpPipelinePosition position) |
| | 54 | | { |
| | 55 | | switch (position) |
| | 56 | | { |
| | 57 | | case HttpPipelinePosition.PerCall: |
| 8 | 58 | | PerCallPolicies.Add(policy); |
| 8 | 59 | | break; |
| | 60 | | case HttpPipelinePosition.PerRetry: |
| 4 | 61 | | PerRetryPolicies.Add(policy); |
| 4 | 62 | | break; |
| | 63 | | default: |
| 0 | 64 | | throw new ArgumentOutOfRangeException(nameof(position), position, null); |
| | 65 | | } |
| | 66 | | } |
| | 67 | |
|
| 158 | 68 | | internal IList<HttpPipelinePolicy> PerCallPolicies { get; } = new List<HttpPipelinePolicy>(); |
| | 69 | |
|
| 154 | 70 | | internal IList<HttpPipelinePolicy> PerRetryPolicies { get; } = new List<HttpPipelinePolicy>(); |
| | 71 | |
|
| | 72 | | /// <inheritdoc /> |
| | 73 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 74 | | public override bool Equals(object? obj) => base.Equals(obj); |
| | 75 | |
|
| | 76 | | /// <inheritdoc /> |
| | 77 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 78 | | public override int GetHashCode() => base.GetHashCode(); |
| | 79 | |
|
| | 80 | | /// <inheritdoc /> |
| | 81 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 82 | | public override string ToString() => base.ToString(); |
| | 83 | |
|
| | 84 | |
|
| | 85 | | private static HttpPipelineTransport GetDefaultTransport() |
| | 86 | | { |
| | 87 | | // TODO: Uncomment after release |
| | 88 | | #if false && NETFRAMEWORK |
| | 89 | | bool GetSwitchValue(string switchName, string envVariable) |
| | 90 | | { |
| | 91 | | if (!AppContext.TryGetSwitch(switchName, out bool ret)) |
| | 92 | | { |
| | 93 | | string? switchValue = Environment.GetEnvironmentVariable(envVariable); |
| | 94 | | if (switchValue != null) |
| | 95 | | { |
| | 96 | | ret = string.Equals("true", switchValue, StringComparison.InvariantCultureIgnoreCase) || |
| | 97 | | switchValue.Equals("1", StringComparison.InvariantCultureIgnoreCase); |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| | 101 | | return ret; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | if (!GetSwitchValue("Azure.Core.Pipeline.DisableHttpWebRequestTransport", "AZURE_CORE_DISABLE_HTTPWEBREQUEST |
| | 105 | | { |
| | 106 | | return HttpWebRequestTransport.Shared; |
| | 107 | | } |
| | 108 | | #endif |
| 94 | 109 | | return HttpClientTransport.Shared; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | } |
| | 113 | | } |