| | 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.Linq; |
| | 7 | | using System.Reflection; |
| | 8 | |
|
| | 9 | | namespace Azure.Core.Pipeline |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Factory for creating instances of <see cref="HttpPipeline"/> populated with default policies. |
| | 13 | | /// </summary> |
| | 14 | | public static class HttpPipelineBuilder |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Creates an instance of <see cref="HttpPipeline"/> populated with default policies, customer provided policie |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="options">The customer provided client options object.</param> |
| | 20 | | /// <param name="perRetryPolicies">Client provided per-retry policies.</param> |
| | 21 | | /// <returns>A new instance of <see cref="HttpPipeline"/></returns> |
| | 22 | | public static HttpPipeline Build(ClientOptions options, params HttpPipelinePolicy[] perRetryPolicies) |
| | 23 | | { |
| 54 | 24 | | return Build(options, Array.Empty<HttpPipelinePolicy>(), perRetryPolicies, new ResponseClassifier()); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Creates an instance of <see cref="HttpPipeline"/> populated with default policies, customer provided policie |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="options">The customer provided client options object.</param> |
| | 31 | | /// <param name="perCallPolicies">Client provided per-call policies.</param> |
| | 32 | | /// <param name="perRetryPolicies">Client provided per-retry policies.</param> |
| | 33 | | /// <param name="responseClassifier">The client provided response classifier.</param> |
| | 34 | | /// <returns>A new instance of <see cref="HttpPipeline"/></returns> |
| | 35 | | public static HttpPipeline Build(ClientOptions options, HttpPipelinePolicy[] perCallPolicies, HttpPipelinePolicy |
| | 36 | | { |
| 56 | 37 | | if (perCallPolicies == null) |
| | 38 | | { |
| 0 | 39 | | throw new ArgumentNullException(nameof(perCallPolicies)); |
| | 40 | | } |
| | 41 | |
|
| 56 | 42 | | if (perRetryPolicies == null) |
| | 43 | | { |
| 0 | 44 | | throw new ArgumentNullException(nameof(perRetryPolicies)); |
| | 45 | | } |
| | 46 | |
|
| 56 | 47 | | var policies = new List<HttpPipelinePolicy>(); |
| | 48 | |
|
| 56 | 49 | | bool isDistributedTracingEnabled = options.Diagnostics.IsDistributedTracingEnabled; |
| | 50 | |
|
| 56 | 51 | | policies.Add(ReadClientRequestIdPolicy.Shared); |
| | 52 | |
|
| 56 | 53 | | policies.AddRange(perCallPolicies); |
| | 54 | |
|
| 56 | 55 | | policies.AddRange(options.PerCallPolicies); |
| | 56 | |
|
| 56 | 57 | | policies.Add(ClientRequestIdPolicy.Shared); |
| | 58 | |
|
| 56 | 59 | | DiagnosticsOptions diagnostics = options.Diagnostics; |
| 56 | 60 | | if (diagnostics.IsTelemetryEnabled) |
| | 61 | | { |
| 56 | 62 | | policies.Add(CreateTelemetryPolicy(options)); |
| | 63 | | } |
| | 64 | |
|
| 56 | 65 | | RetryOptions retryOptions = options.Retry; |
| 56 | 66 | | policies.Add(new RetryPolicy(retryOptions.Mode, retryOptions.Delay, retryOptions.MaxDelay, retryOptions.MaxR |
| | 67 | |
|
| 56 | 68 | | policies.AddRange(perRetryPolicies); |
| | 69 | |
|
| 56 | 70 | | policies.AddRange(options.PerRetryPolicies); |
| | 71 | |
|
| 56 | 72 | | if (diagnostics.IsLoggingEnabled) |
| | 73 | | { |
| 56 | 74 | | string assemblyName = options.GetType().Assembly.GetName().Name; |
| | 75 | |
|
| 56 | 76 | | policies.Add(new LoggingPolicy(diagnostics.IsLoggingContentEnabled, diagnostics.LoggedContentSizeLimit, |
| 56 | 77 | | diagnostics.LoggedHeaderNames.ToArray(), diagnostics.LoggedQueryParameters.ToArray(), assemblyName)) |
| | 78 | | } |
| | 79 | |
|
| 56 | 80 | | policies.Add(new ResponseBodyPolicy(options.Retry.NetworkTimeout)); |
| | 81 | |
|
| 56 | 82 | | policies.Add(new RequestActivityPolicy(isDistributedTracingEnabled, ClientDiagnostics.GetResourceProviderNam |
| | 83 | |
|
| 458 | 84 | | policies.RemoveAll(policy => policy == null); |
| | 85 | |
|
| 56 | 86 | | return new HttpPipeline(options.Transport, |
| 56 | 87 | | policies.ToArray(), |
| 56 | 88 | | responseClassifier); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | // internal for testing |
| | 92 | | internal static TelemetryPolicy CreateTelemetryPolicy(ClientOptions options) |
| | 93 | | { |
| | 94 | | const string PackagePrefix = "Azure."; |
| | 95 | |
|
| 58 | 96 | | Assembly clientAssembly = options.GetType().Assembly; |
| | 97 | |
|
| 58 | 98 | | AssemblyInformationalVersionAttribute versionAttribute = clientAssembly.GetCustomAttribute<AssemblyInformati |
| 58 | 99 | | if (versionAttribute == null) |
| | 100 | | { |
| 0 | 101 | | throw new InvalidOperationException($"{nameof(AssemblyInformationalVersionAttribute)} is required on cli |
| | 102 | | } |
| | 103 | |
|
| 58 | 104 | | string version = versionAttribute.InformationalVersion; |
| | 105 | |
|
| 58 | 106 | | string assemblyName = clientAssembly.GetName().Name; |
| 58 | 107 | | if (assemblyName.StartsWith(PackagePrefix, StringComparison.Ordinal)) |
| | 108 | | { |
| 58 | 109 | | assemblyName = assemblyName.Substring(PackagePrefix.Length); |
| | 110 | | } |
| | 111 | |
|
| 58 | 112 | | int hashSeparator = version.IndexOf('+'); |
| 58 | 113 | | if (hashSeparator != -1) |
| | 114 | | { |
| 58 | 115 | | version = version.Substring(0, hashSeparator); |
| | 116 | | } |
| | 117 | |
|
| 58 | 118 | | return new TelemetryPolicy(assemblyName, version, options.Diagnostics.ApplicationId); |
| | 119 | | } |
| | 120 | | } |
| | 121 | | } |