| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using Azure.Core; |
| | 7 | | using Azure.Core.Pipeline; |
| | 8 | | #if EXPERIMENTAL_SERIALIZER |
| | 9 | | using Azure.Core.Serialization; |
| | 10 | | #endif |
| | 11 | |
|
| | 12 | | #pragma warning disable SA1402 // File may only contain a single type |
| | 13 | |
|
| | 14 | | namespace Azure.Search.Documents |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Provides the client configuration options for connecting to Azure |
| | 18 | | /// Cognitive Search. |
| | 19 | | /// </summary> |
| | 20 | | public class SearchClientOptions : ClientOptions |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// The versions of Azure Cognitive Search supported by this client |
| | 24 | | /// library. For more, see |
| | 25 | | /// <see href="https://docs.microsoft.com/azure/search/search-api-versions" />. |
| | 26 | | /// </summary> |
| | 27 | | public enum ServiceVersion |
| | 28 | | { |
| | 29 | | #pragma warning disable CA1707 // Identifiers should not contain underscores |
| | 30 | | /// <summary> |
| | 31 | | /// The 2020_06_30 version of the Azure Cognitive Search |
| | 32 | | /// service. |
| | 33 | | /// </summary> |
| | 34 | | V2020_06_30 = 1 |
| | 35 | | #pragma warning restore CA1707 |
| | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The Latest service version supported by this client library. |
| | 40 | | /// </summary> |
| | 41 | | internal const ServiceVersion LatestVersion = ServiceVersion.V2020_06_30; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The service version to use when creating continuation tokens that |
| | 45 | | /// can be passed between different client libraries. Changing this |
| | 46 | | /// value requires updating <see cref="Azure.Search.Documents.Models.SearchContinuationToken"/>. |
| | 47 | | /// </summary> |
| | 48 | | internal const ServiceVersion ContinuationTokenVersion = ServiceVersion.V2020_06_30; |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Gets the <see cref="ServiceVersion"/> of the service API used when |
| | 52 | | /// making requests. For more, see |
| | 53 | | /// <see href="https://docs.microsoft.com/azure/search/search-api-versions" />. |
| | 54 | | /// </summary> |
| | 55 | | public ServiceVersion Version { get; } |
| | 56 | |
|
| | 57 | | #if EXPERIMENTAL_SERIALIZER |
| | 58 | | /// <summary> |
| | 59 | | /// Gets or sets an <see cref="ObjectSerializer"/> that can be used to |
| | 60 | | /// customize the serialization of strongly typed models. The |
| | 61 | | /// serializer needs to support JSON and <see cref="JsonObjectSerializer"/> |
| | 62 | | /// will be used if no value is provided. |
| | 63 | | /// </summary> |
| | 64 | | public ObjectSerializer Serializer { get; set; } |
| | 65 | | #endif |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Initializes a new instance of the <see cref="SearchClientOptions"/> |
| | 69 | | /// class. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="version"> |
| | 72 | | /// An optional <see cref="ServiceVersion"/> to specify the version of |
| | 73 | | /// the REST API to use. For more, see |
| | 74 | | /// <see href="https://docs.microsoft.com/azure/search/search-api-versions" />. |
| | 75 | | /// |
| | 76 | | /// If not provided, the <paramref name="version"/> will default to the |
| | 77 | | /// latest supported by this client library. It is recommended that |
| | 78 | | /// application authors allow the version to float to the latest and |
| | 79 | | /// library authors pin to a specific version. |
| | 80 | | /// </param> |
| | 81 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 82 | | /// Thrown when the <paramref name="version"/> is not supported by this |
| | 83 | | /// client library. |
| | 84 | | /// </exception> |
| | 85 | | public SearchClientOptions(ServiceVersion version = LatestVersion) |
| | 86 | | { |
| | 87 | | Version = version.Validate(); |
| | 88 | | AddLoggingHeaders(); |
| | 89 | | AddLoggingQueryParameters(); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Create an <see cref="HttpPipeline"/> to send requests to the Search |
| | 94 | | /// Service. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="credential"> |
| | 97 | | /// The <see cref="AzureKeyCredential"/> to authenticate requests. |
| | 98 | | /// </param> |
| | 99 | | /// <returns>An <see cref="HttpPipeline"/> to send requests.</returns> |
| | 100 | | internal HttpPipeline Build(AzureKeyCredential credential) |
| | 101 | | { |
| | 102 | | Debug.Assert(credential != null); |
| | 103 | | return HttpPipelineBuilder.Build( |
| | 104 | | options: this, |
| | 105 | | perCallPolicies: new[] { new AzureKeyCredentialPolicy(credential, Constants.ApiKeyHeaderName) }, |
| | 106 | | perRetryPolicies: Array.Empty<HttpPipelinePolicy>(), |
| | 107 | | responseClassifier: null); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Add the allow list headers to the <see cref="DiagnosticsOptions"/> |
| | 112 | | /// that are considered safe for logging/exceptions by default. |
| | 113 | | /// </summary> |
| | 114 | | private void AddLoggingHeaders() |
| | 115 | | { |
| | 116 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Allow-Credentials"); |
| | 117 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Allow-Headers"); |
| | 118 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Allow-Methods"); |
| | 119 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Allow-Origin"); |
| | 120 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Expose-Headers"); |
| | 121 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Max-Age"); |
| | 122 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Request-Headers"); |
| | 123 | | Diagnostics.LoggedHeaderNames.Add("Access-Control-Request-Method"); |
| | 124 | | Diagnostics.LoggedHeaderNames.Add("client-request-id"); |
| | 125 | | Diagnostics.LoggedHeaderNames.Add("elapsed-time"); |
| | 126 | | Diagnostics.LoggedHeaderNames.Add("Location"); |
| | 127 | | Diagnostics.LoggedHeaderNames.Add("OData-MaxVersion"); |
| | 128 | | Diagnostics.LoggedHeaderNames.Add("OData-Version"); |
| | 129 | | Diagnostics.LoggedHeaderNames.Add("Origin"); |
| | 130 | | Diagnostics.LoggedHeaderNames.Add("Prefer"); |
| | 131 | | Diagnostics.LoggedHeaderNames.Add("request-id"); |
| | 132 | | Diagnostics.LoggedHeaderNames.Add("return-client-request-id"); |
| | 133 | | Diagnostics.LoggedHeaderNames.Add("throttle-reason"); |
| | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Add the allow list query parameters to the |
| | 138 | | /// <see cref="DiagnosticsOptions"/> that are considered safe for |
| | 139 | | /// logging/exceptions by default. |
| | 140 | | /// </summary> |
| | 141 | | private void AddLoggingQueryParameters() |
| | 142 | | { |
| | 143 | | Diagnostics.LoggedQueryParameters.Add("api-version"); |
| | 144 | | Diagnostics.LoggedQueryParameters.Add("allowIndexDowntime"); |
| | 145 | | } |
| | 146 | | } |
| | 147 | |
|
| | 148 | | /// <summary> |
| | 149 | | /// Search extension methods. |
| | 150 | | /// </summary> |
| | 151 | | internal static partial class SearchExtensions |
| | 152 | | { |
| | 153 | | /// <summary> |
| | 154 | | /// Validate a <see cref="SearchClientOptions.ServiceVersion"/>. |
| | 155 | | /// </summary> |
| | 156 | | /// <param name="version"> |
| | 157 | | /// The <see cref="SearchClientOptions.ServiceVersion"/> to validate. |
| | 158 | | /// </param> |
| | 159 | | /// <returns> |
| | 160 | | /// The validated version. |
| | 161 | | /// </returns> |
| | 162 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 163 | | /// Thrown when the <paramref name="version"/> is not supported by this |
| | 164 | | /// client library. |
| | 165 | | /// </exception> |
| | 166 | | public static SearchClientOptions.ServiceVersion Validate(this SearchClientOptions.ServiceVersion version) => |
| 313 | 167 | | version switch |
| 313 | 168 | | { |
| 626 | 169 | | SearchClientOptions.ServiceVersion.V2020_06_30 => version, |
| 0 | 170 | | _ => throw CreateInvalidVersionException(version) |
| 313 | 171 | | }; |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Get a version string, like "2019-05-06", corresponding to a given |
| | 175 | | /// <see cref="SearchClientOptions.ServiceVersion"/> value. |
| | 176 | | /// </summary> |
| | 177 | | /// <param name="version"> |
| | 178 | | /// The <see cref="SearchClientOptions.ServiceVersion"/> value to |
| | 179 | | /// convert into a version string. |
| | 180 | | /// </param> |
| | 181 | | /// <returns> |
| | 182 | | /// The version string. |
| | 183 | | /// </returns> |
| | 184 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 185 | | /// Thrown when the <paramref name="version"/> is not supported by this |
| | 186 | | /// client library. |
| | 187 | | /// </exception> |
| | 188 | | public static string ToVersionString(this SearchClientOptions.ServiceVersion version) => |
| 432 | 189 | | version switch |
| 432 | 190 | | { |
| 864 | 191 | | SearchClientOptions.ServiceVersion.V2020_06_30 => "2020-06-30", |
| 0 | 192 | | _ => throw CreateInvalidVersionException(version) |
| 432 | 193 | | }; |
| | 194 | |
|
| | 195 | | /// <summary> |
| | 196 | | /// Create an <see cref="ArgumentOutOfRangeException"/> to throw when |
| | 197 | | /// an invalid <see cref="SearchClientOptions.ServiceVersion"/> value |
| | 198 | | /// is provided. |
| | 199 | | /// </summary> |
| | 200 | | /// <param name="version">The invalid version value.</param> |
| | 201 | | /// <returns>An exception to throw.</returns> |
| | 202 | | private static ArgumentOutOfRangeException CreateInvalidVersionException(SearchClientOptions.ServiceVersion vers |
| 0 | 203 | | new ArgumentOutOfRangeException( |
| 0 | 204 | | nameof(version), |
| 0 | 205 | | version, |
| 0 | 206 | | $"The {nameof(SearchClientOptions)}.{nameof(SearchClientOptions.ServiceVersion)} specified is not suppor |
| | 207 | | } |
| | 208 | | } |