| | | 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.Globalization; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Azure.Core; |
| | | 10 | | using Azure.Core.Pipeline; |
| | | 11 | | |
| | | 12 | | namespace Azure.Data.AppConfiguration |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The client to use for interacting with the Azure Configuration Store. |
| | | 16 | | /// </summary> |
| | | 17 | | public partial class ConfigurationClient |
| | | 18 | | { |
| | | 19 | | private readonly Uri _endpoint; |
| | | 20 | | private readonly HttpPipeline _pipeline; |
| | | 21 | | private readonly ClientDiagnostics _clientDiagnostics; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Protected constructor to allow mocking. |
| | | 25 | | /// </summary> |
| | 324 | 26 | | protected ConfigurationClient() |
| | | 27 | | { |
| | 324 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="ConfigurationClient"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="connectionString">Connection string with authentication option and related parameters.</param> |
| | | 34 | | public ConfigurationClient(string connectionString) |
| | 0 | 35 | | : this(connectionString, new ConfigurationClientOptions()) |
| | | 36 | | { |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new instance of the <see cref="ConfigurationClient"/> class. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="connectionString">Connection string with authentication option and related parameters.</param> |
| | | 43 | | /// <param name="options">Options that allow configuration of requests sent to the configuration store.</param> |
| | 320 | 44 | | public ConfigurationClient(string connectionString, ConfigurationClientOptions options) |
| | | 45 | | { |
| | 320 | 46 | | if (connectionString == null) |
| | 0 | 47 | | throw new ArgumentNullException(nameof(connectionString)); |
| | 320 | 48 | | if (options == null) |
| | 0 | 49 | | throw new ArgumentNullException(nameof(options)); |
| | | 50 | | |
| | 320 | 51 | | ParseConnectionString(connectionString, out _endpoint, out var credential, out var secret); |
| | | 52 | | |
| | 320 | 53 | | _pipeline = CreatePipeline(options, new AuthenticationPolicy(credential, secret)); |
| | | 54 | | |
| | 320 | 55 | | _clientDiagnostics = new ClientDiagnostics(options); |
| | 320 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Initializes a new instance of the <see cref="ConfigurationClient"/> class. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="endpoint">The <see cref="Uri"/> referencing the app configuration storage.</param> |
| | | 62 | | /// <param name="credential">The token credential used to sign requests.</param> |
| | | 63 | | public ConfigurationClient(Uri endpoint, TokenCredential credential) |
| | 0 | 64 | | : this(endpoint, credential, new ConfigurationClientOptions()) |
| | | 65 | | { |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Initializes a new instance of the <see cref="ConfigurationClient"/> class. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="endpoint">The <see cref="Uri"/> referencing the app configuration storage.</param> |
| | | 72 | | /// <param name="credential">The token credential used to sign requests.</param> |
| | | 73 | | /// <param name="options">Options that allow configuration of requests sent to the configuration store.</param> |
| | 4 | 74 | | public ConfigurationClient(Uri endpoint, TokenCredential credential, ConfigurationClientOptions options) |
| | | 75 | | { |
| | 4 | 76 | | Argument.AssertNotNull(endpoint, nameof(endpoint)); |
| | 4 | 77 | | Argument.AssertNotNull(credential, nameof(credential)); |
| | | 78 | | |
| | 4 | 79 | | _endpoint = endpoint; |
| | 4 | 80 | | _pipeline = CreatePipeline(options, new BearerTokenAuthenticationPolicy(credential, GetDefaultScope(endpoint |
| | | 81 | | |
| | 4 | 82 | | _clientDiagnostics = new ClientDiagnostics(options); |
| | 4 | 83 | | } |
| | | 84 | | |
| | | 85 | | private static HttpPipeline CreatePipeline(ConfigurationClientOptions options, HttpPipelinePolicy authentication |
| | 324 | 86 | | => HttpPipelineBuilder.Build(options, |
| | 324 | 87 | | new HttpPipelinePolicy[] { new CustomHeadersPolicy(), new ApiVersionPolicy(options.GetVersionString()) } |
| | 324 | 88 | | new HttpPipelinePolicy[] { authenticationPolicy, new SyncTokenPolicy() }, |
| | 324 | 89 | | new ResponseClassifier()); |
| | | 90 | | |
| | | 91 | | private static string GetDefaultScope(Uri uri) |
| | 4 | 92 | | => $"{uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped)}/.default"; |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Creates a <see cref="ConfigurationSetting"/> if the setting, uniquely identified by key and label, does not |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 98 | | /// <param name="value">The configuration setting's value.</param> |
| | | 99 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 100 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 101 | | /// <returns>A response containing the added <see cref="ConfigurationSetting"/>.</returns> |
| | | 102 | | public virtual async Task<Response<ConfigurationSetting>> AddConfigurationSettingAsync(string key, string value, |
| | | 103 | | { |
| | 8 | 104 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 8 | 105 | | return await AddConfigurationSettingAsync(new ConfigurationSetting(key, value, label), cancellationToken).Co |
| | 8 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Creates a <see cref="ConfigurationSetting"/> if the setting, uniquely identified by key and label, does not |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 112 | | /// <param name="value">The configuration setting's value.</param> |
| | | 113 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 114 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 115 | | /// <returns>A response containing the added <see cref="ConfigurationSetting"/>.</returns> |
| | | 116 | | public virtual Response<ConfigurationSetting> AddConfigurationSetting(string key, string value, string label = d |
| | | 117 | | { |
| | 8 | 118 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 8 | 119 | | return AddConfigurationSetting(new ConfigurationSetting(key, value, label), cancellationToken); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Creates a <see cref="ConfigurationSetting"/> only if the setting does not already exist in the configuration |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to create.</param> |
| | | 126 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 127 | | /// <returns>A response containing the added <see cref="ConfigurationSetting"/>.</returns> |
| | | 128 | | public virtual async Task<Response<ConfigurationSetting>> AddConfigurationSettingAsync(ConfigurationSetting sett |
| | | 129 | | { |
| | 54 | 130 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(AddConf |
| | 54 | 131 | | scope.AddAttribute("key", setting?.Key); |
| | 54 | 132 | | scope.Start(); |
| | | 133 | | |
| | | 134 | | try |
| | | 135 | | { |
| | 54 | 136 | | using Request request = CreateAddRequest(setting); |
| | 54 | 137 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 138 | | |
| | 54 | 139 | | switch (response.Status) |
| | | 140 | | { |
| | | 141 | | case 200: |
| | | 142 | | case 201: |
| | 50 | 143 | | return await CreateResponseAsync(response, cancellationToken).ConfigureAwait(false); |
| | | 144 | | case 412: |
| | 4 | 145 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, "Setting was already |
| | | 146 | | default: |
| | 0 | 147 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | | 148 | | } |
| | | 149 | | } |
| | 4 | 150 | | catch (Exception e) |
| | | 151 | | { |
| | 4 | 152 | | scope.Failed(e); |
| | 4 | 153 | | throw; |
| | | 154 | | } |
| | 50 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Creates a <see cref="ConfigurationSetting"/> only if the setting does not already exist in the configuration |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to create.</param> |
| | | 161 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 162 | | /// <returns>A response containing the added <see cref="ConfigurationSetting"/>.</returns> |
| | | 163 | | public virtual Response<ConfigurationSetting> AddConfigurationSetting(ConfigurationSetting setting, Cancellation |
| | | 164 | | { |
| | 54 | 165 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(AddConf |
| | 54 | 166 | | scope.AddAttribute("key", setting?.Key); |
| | 54 | 167 | | scope.Start(); |
| | | 168 | | |
| | | 169 | | try |
| | | 170 | | { |
| | 54 | 171 | | using Request request = CreateAddRequest(setting); |
| | 54 | 172 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | | 173 | | |
| | 54 | 174 | | switch (response.Status) |
| | | 175 | | { |
| | | 176 | | case 200: |
| | | 177 | | case 201: |
| | 50 | 178 | | return CreateResponse(response); |
| | | 179 | | case 412: |
| | 4 | 180 | | throw _clientDiagnostics.CreateRequestFailedException(response, "Setting was already present."); |
| | | 181 | | default: |
| | 0 | 182 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | | 183 | | } |
| | | 184 | | } |
| | 4 | 185 | | catch (Exception e) |
| | | 186 | | { |
| | 4 | 187 | | scope.Failed(e); |
| | 4 | 188 | | throw; |
| | | 189 | | } |
| | 50 | 190 | | } |
| | | 191 | | |
| | | 192 | | private Request CreateAddRequest(ConfigurationSetting setting) |
| | | 193 | | { |
| | 108 | 194 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 108 | 195 | | Argument.AssertNotNullOrEmpty(setting.Key, $"{nameof(setting)}.{nameof(setting.Key)}"); |
| | | 196 | | |
| | 108 | 197 | | Request request = _pipeline.CreateRequest(); |
| | | 198 | | |
| | 108 | 199 | | ReadOnlyMemory<byte> content = ConfigurationServiceSerializer.SerializeRequestBody(setting); |
| | | 200 | | |
| | 108 | 201 | | request.Method = RequestMethod.Put; |
| | | 202 | | |
| | 108 | 203 | | BuildUriForKvRoute(request.Uri, setting); |
| | | 204 | | |
| | 108 | 205 | | MatchConditions requestOptions = new MatchConditions { IfNoneMatch = ETag.All }; |
| | 108 | 206 | | ConditionalRequestOptionsExtensions.ApplyHeaders(request, requestOptions); |
| | | 207 | | |
| | 108 | 208 | | request.Headers.Add(s_mediaTypeKeyValueApplicationHeader); |
| | 108 | 209 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| | 108 | 210 | | request.Content = RequestContent.Create(content); |
| | | 211 | | |
| | 108 | 212 | | return request; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Creates a <see cref="ConfigurationSetting"/>, uniquely identified by key and label, if it doesn't exist or o |
| | | 217 | | /// </summary> |
| | | 218 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 219 | | /// <param name="value">The configuration setting's value.</param> |
| | | 220 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 221 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 222 | | /// <returns>A response containing the <see cref="ConfigurationSetting"/> written to the configuration store.</r |
| | | 223 | | public virtual async Task<Response<ConfigurationSetting>> SetConfigurationSettingAsync(string key, string value, |
| | | 224 | | { |
| | 6 | 225 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 6 | 226 | | return await SetConfigurationSettingAsync(new ConfigurationSetting(key, value, label), false, cancellationTo |
| | 4 | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Creates a <see cref="ConfigurationSetting"/>, uniquely identified by key and label, if it doesn't exist or o |
| | | 231 | | /// </summary> |
| | | 232 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 233 | | /// <param name="value">The configuration setting's value.</param> |
| | | 234 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 235 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 236 | | /// <returns>A response containing the <see cref="ConfigurationSetting"/> written to the configuration store.</r |
| | | 237 | | public virtual Response<ConfigurationSetting> SetConfigurationSetting(string key, string value, string label = d |
| | | 238 | | { |
| | 6 | 239 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 6 | 240 | | return SetConfigurationSetting(new ConfigurationSetting(key, value, label), false, cancellationToken); |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Creates a <see cref="ConfigurationSetting"/> if it doesn't exist or overwrites the existing setting in the c |
| | | 245 | | /// </summary> |
| | | 246 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to create.</param> |
| | | 247 | | /// <param name="onlyIfUnchanged">If set to true and the configuration setting exists in the configuration store |
| | | 248 | | /// if the passed-in <see cref="ConfigurationSetting"/> is the same version as the one in the configuration stor |
| | | 249 | | /// are the same if their ETag fields match. If the two settings are different versions, this method will throw |
| | | 250 | | /// that the setting in the configuration store was modified since it was last obtained by the client.</param> |
| | | 251 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 252 | | /// <returns>A response containing the <see cref="ConfigurationSetting"/> written to the configuration store.</r |
| | | 253 | | public virtual async Task<Response<ConfigurationSetting>> SetConfigurationSettingAsync(ConfigurationSetting sett |
| | | 254 | | { |
| | 90 | 255 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 90 | 256 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(SetConf |
| | 90 | 257 | | scope.AddAttribute("key", setting?.Key); |
| | 90 | 258 | | scope.Start(); |
| | | 259 | | |
| | | 260 | | try |
| | | 261 | | { |
| | 90 | 262 | | using Request request = CreateSetRequest(setting, onlyIfUnchanged); |
| | 90 | 263 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 264 | | |
| | 90 | 265 | | return response.Status switch |
| | 90 | 266 | | { |
| | 172 | 267 | | 200 => await CreateResponseAsync(response, cancellationToken).ConfigureAwait(false), |
| | 94 | 268 | | 409 => throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, "The setting is re |
| | 90 | 269 | | |
| | 90 | 270 | | // Throws on 412 if resource was modified. |
| | 94 | 271 | | _ => throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false |
| | 90 | 272 | | }; |
| | | 273 | | } |
| | 8 | 274 | | catch (Exception e) |
| | | 275 | | { |
| | 8 | 276 | | scope.Failed(e); |
| | 8 | 277 | | throw; |
| | | 278 | | } |
| | 82 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Creates a <see cref="ConfigurationSetting"/> if it doesn't exist or overwrites the existing setting in the c |
| | | 283 | | /// </summary> |
| | | 284 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to create.</param> |
| | | 285 | | /// <param name="onlyIfUnchanged">If set to true and the configuration setting exists in the configuration store |
| | | 286 | | /// if the passed-in <see cref="ConfigurationSetting"/> is the same version as the one in the configuration stor |
| | | 287 | | /// are the same if their ETag fields match. If the two settings are different versions, this method will throw |
| | | 288 | | /// that the setting in the configuration store was modified since it was last obtained by the client.</param> |
| | | 289 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 290 | | /// <returns>A response containing the <see cref="ConfigurationSetting"/> written to the configuration store.</r |
| | | 291 | | public virtual Response<ConfigurationSetting> SetConfigurationSetting(ConfigurationSetting setting, bool onlyIfU |
| | | 292 | | { |
| | 90 | 293 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 90 | 294 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(SetConf |
| | 90 | 295 | | scope.AddAttribute("key", setting?.Key); |
| | 90 | 296 | | scope.Start(); |
| | | 297 | | |
| | | 298 | | try |
| | | 299 | | { |
| | 90 | 300 | | using Request request = CreateSetRequest(setting, onlyIfUnchanged); |
| | 90 | 301 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | | 302 | | |
| | 90 | 303 | | return response.Status switch |
| | 90 | 304 | | { |
| | 172 | 305 | | 200 => CreateResponse(response), |
| | 94 | 306 | | 409 => throw _clientDiagnostics.CreateRequestFailedException(response, "The setting is read only"), |
| | 90 | 307 | | |
| | 90 | 308 | | // Throws on 412 if resource was modified. |
| | 94 | 309 | | _ => throw _clientDiagnostics.CreateRequestFailedException(response), |
| | 90 | 310 | | }; |
| | | 311 | | } |
| | 8 | 312 | | catch (Exception e) |
| | | 313 | | { |
| | 8 | 314 | | scope.Failed(e); |
| | 8 | 315 | | throw; |
| | | 316 | | } |
| | 82 | 317 | | } |
| | | 318 | | |
| | | 319 | | private Request CreateSetRequest(ConfigurationSetting setting, bool onlyIfChanged) |
| | | 320 | | { |
| | 180 | 321 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 180 | 322 | | Argument.AssertNotNullOrEmpty(setting.Key, $"{nameof(setting)}.{nameof(setting.Key)}"); |
| | | 323 | | |
| | 180 | 324 | | Request request = _pipeline.CreateRequest(); |
| | 180 | 325 | | ReadOnlyMemory<byte> content = ConfigurationServiceSerializer.SerializeRequestBody(setting); |
| | | 326 | | |
| | 180 | 327 | | request.Method = RequestMethod.Put; |
| | 180 | 328 | | BuildUriForKvRoute(request.Uri, setting); |
| | 180 | 329 | | request.Headers.Add(s_mediaTypeKeyValueApplicationHeader); |
| | 180 | 330 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| | | 331 | | |
| | 180 | 332 | | if (onlyIfChanged) |
| | | 333 | | { |
| | 16 | 334 | | ConditionalRequestOptionsExtensions.ApplyHeaders(request, new MatchConditions { IfMatch = setting.ETag } |
| | | 335 | | } |
| | | 336 | | |
| | 180 | 337 | | request.Content = RequestContent.Create(content); |
| | 180 | 338 | | return request; |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Delete a <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 343 | | /// </summary> |
| | | 344 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 345 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 346 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 347 | | /// <returns>A response indicating the success of the operation.</returns> |
| | | 348 | | public virtual async Task<Response> DeleteConfigurationSettingAsync(string key, string label = default, Cancella |
| | | 349 | | { |
| | 104 | 350 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 104 | 351 | | return await DeleteConfigurationSettingAsync(key, label, default, cancellationToken).ConfigureAwait(false); |
| | 100 | 352 | | } |
| | | 353 | | |
| | | 354 | | /// <summary> |
| | | 355 | | /// Delete a <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 356 | | /// </summary> |
| | | 357 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 358 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 359 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 360 | | /// <returns>A response indicating the success of the operation.</returns> |
| | | 361 | | public virtual Response DeleteConfigurationSetting(string key, string label = default, CancellationToken cancell |
| | | 362 | | { |
| | 104 | 363 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 104 | 364 | | return DeleteConfigurationSetting(key, label, default, cancellationToken); |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// Delete a <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 369 | | /// </summary> |
| | | 370 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to delete.</param> |
| | | 371 | | /// <param name="onlyIfUnchanged">If set to true and the configuration setting exists in the configuration store |
| | | 372 | | /// if the passed-in <see cref="ConfigurationSetting"/> is the same version as the one in the configuration stor |
| | | 373 | | /// are the same if their ETag fields match. If the two settings are different versions, this method will throw |
| | | 374 | | /// that the setting in the configuration store was modified since it was last obtained by the client.</param> |
| | | 375 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 376 | | /// <returns>A response indicating the success of the operation.</returns> |
| | | 377 | | public virtual async Task<Response> DeleteConfigurationSettingAsync(ConfigurationSetting setting, bool onlyIfUnc |
| | | 378 | | { |
| | 18 | 379 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 18 | 380 | | MatchConditions requestOptions = onlyIfUnchanged ? new MatchConditions { IfMatch = setting.ETag } : default; |
| | 18 | 381 | | return await DeleteConfigurationSettingAsync(setting.Key, setting.Label, requestOptions, cancellationToken). |
| | 12 | 382 | | } |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Delete a <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 386 | | /// </summary> |
| | | 387 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to delete.</param> |
| | | 388 | | /// <param name="onlyIfUnchanged">If set to true and the configuration setting exists in the configuration store |
| | | 389 | | /// if the passed-in <see cref="ConfigurationSetting"/> is the same version as the one in the configuration stor |
| | | 390 | | /// are the same if their ETag fields match. If the two settings are different versions, this method will throw |
| | | 391 | | /// that the setting in the configuration store was modified since it was last obtained by the client.</param> |
| | | 392 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 393 | | /// <returns>A response indicating the success of the operation.</returns> |
| | | 394 | | public virtual Response DeleteConfigurationSetting(ConfigurationSetting setting, bool onlyIfUnchanged = false, C |
| | | 395 | | { |
| | 18 | 396 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 18 | 397 | | MatchConditions requestOptions = onlyIfUnchanged ? new MatchConditions { IfMatch = setting.ETag } : default; |
| | 18 | 398 | | return DeleteConfigurationSetting(setting.Key, setting.Label, requestOptions, cancellationToken); |
| | | 399 | | } |
| | | 400 | | |
| | | 401 | | private async Task<Response> DeleteConfigurationSettingAsync(string key, string label, MatchConditions requestOp |
| | | 402 | | { |
| | 122 | 403 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(DeleteC |
| | 122 | 404 | | scope.AddAttribute("key", key); |
| | 122 | 405 | | scope.Start(); |
| | | 406 | | |
| | | 407 | | try |
| | | 408 | | { |
| | 122 | 409 | | using Request request = CreateDeleteRequest(key, label, requestOptions); |
| | 122 | 410 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 411 | | |
| | 122 | 412 | | return response.Status switch |
| | 122 | 413 | | { |
| | 232 | 414 | | 200 => response, |
| | 124 | 415 | | 204 => response, |
| | 126 | 416 | | 409 => throw _clientDiagnostics.CreateRequestFailedException(response, "The setting is read only"), |
| | 122 | 417 | | |
| | 122 | 418 | | // Throws on 412 if resource was modified. |
| | 128 | 419 | | _ => throw _clientDiagnostics.CreateRequestFailedException(response) |
| | 122 | 420 | | }; |
| | | 421 | | } |
| | 10 | 422 | | catch (Exception e) |
| | | 423 | | { |
| | 10 | 424 | | scope.Failed(e); |
| | 10 | 425 | | throw; |
| | | 426 | | } |
| | 112 | 427 | | } |
| | | 428 | | |
| | | 429 | | private Response DeleteConfigurationSetting(string key, string label, MatchConditions requestOptions, Cancellati |
| | | 430 | | { |
| | 122 | 431 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(DeleteC |
| | 122 | 432 | | scope.AddAttribute("key", key); |
| | 122 | 433 | | scope.Start(); |
| | | 434 | | |
| | | 435 | | try |
| | | 436 | | { |
| | 122 | 437 | | using Request request = CreateDeleteRequest(key, label, requestOptions); |
| | 122 | 438 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | | 439 | | |
| | 122 | 440 | | return response.Status switch |
| | 122 | 441 | | { |
| | 232 | 442 | | 200 => response, |
| | 124 | 443 | | 204 => response, |
| | 126 | 444 | | 409 => throw _clientDiagnostics.CreateRequestFailedException(response, "The setting is read only."), |
| | 122 | 445 | | |
| | 122 | 446 | | // Throws on 412 if resource was modified. |
| | 128 | 447 | | _ => throw _clientDiagnostics.CreateRequestFailedException(response) |
| | 122 | 448 | | }; |
| | | 449 | | } |
| | 10 | 450 | | catch (Exception e) |
| | | 451 | | { |
| | 10 | 452 | | scope.Failed(e); |
| | 10 | 453 | | throw; |
| | | 454 | | } |
| | 112 | 455 | | } |
| | | 456 | | |
| | | 457 | | private Request CreateDeleteRequest(string key, string label, MatchConditions requestOptions) |
| | | 458 | | { |
| | 244 | 459 | | Request request = _pipeline.CreateRequest(); |
| | 244 | 460 | | request.Method = RequestMethod.Delete; |
| | 244 | 461 | | BuildUriForKvRoute(request.Uri, key, label); |
| | | 462 | | |
| | 244 | 463 | | if (requestOptions != null) |
| | | 464 | | { |
| | 16 | 465 | | ConditionalRequestOptionsExtensions.ApplyHeaders(request, requestOptions); |
| | | 466 | | } |
| | | 467 | | |
| | 244 | 468 | | return request; |
| | | 469 | | } |
| | | 470 | | |
| | | 471 | | /// <summary> |
| | | 472 | | /// Retrieve an existing <see cref="ConfigurationSetting"/>, uniquely identified by key and label, from the conf |
| | | 473 | | /// </summary> |
| | | 474 | | /// <param name="key">The primary identifier of the configuration setting to retrieve.</param> |
| | | 475 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 476 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 477 | | /// <returns>A response containing the retrieved <see cref="ConfigurationSetting"/>.</returns> |
| | | 478 | | public virtual async Task<Response<ConfigurationSetting>> GetConfigurationSettingAsync(string key, string label |
| | | 479 | | { |
| | 26 | 480 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 26 | 481 | | return await GetConfigurationSettingAsync(key, label, acceptDateTime: default, requestOptions: default, canc |
| | 18 | 482 | | } |
| | | 483 | | |
| | | 484 | | /// <summary> |
| | | 485 | | /// Retrieve an existing <see cref="ConfigurationSetting"/>, uniquely identified by key and label, from the conf |
| | | 486 | | /// </summary> |
| | | 487 | | /// <param name="key">The primary identifier of the configuration setting to retrieve.</param> |
| | | 488 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 489 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 490 | | /// <returns>A response containing the retrieved <see cref="ConfigurationSetting"/>.</returns> |
| | | 491 | | public virtual Response<ConfigurationSetting> GetConfigurationSetting(string key, string label = default, Cancel |
| | | 492 | | { |
| | 26 | 493 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | 26 | 494 | | return GetConfigurationSetting(key, label, acceptDateTime: default, requestOptions: default, cancellationTok |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | /// <summary> |
| | | 498 | | /// Retrieve an existing <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 499 | | /// </summary> |
| | | 500 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to retrieve.</param> |
| | | 501 | | /// <param name="onlyIfChanged">If set to true, only retrieve the setting from the configuration store if it has |
| | | 502 | | /// It is determined to have changed if the ETag field on the passed-in <see cref="ConfigurationSetting"/> is di |
| | | 503 | | /// configuration store. If it has not changed, the returned response will have have no value, and will throw i |
| | | 504 | | /// check the status code on the response to avoid triggering the exception.</param> |
| | | 505 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 506 | | /// <returns>A response containing the retrieved <see cref="ConfigurationSetting"/>.</returns> |
| | | 507 | | public virtual async Task<Response<ConfigurationSetting>> GetConfigurationSettingAsync(ConfigurationSetting sett |
| | | 508 | | { |
| | 8 | 509 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 8 | 510 | | MatchConditions requestOptions = onlyIfChanged ? new MatchConditions { IfNoneMatch = setting.ETag } : defaul |
| | 8 | 511 | | return await GetConfigurationSettingAsync(setting.Key, setting.Label, acceptDateTime: default, requestOption |
| | 8 | 512 | | } |
| | | 513 | | |
| | | 514 | | /// <summary> |
| | | 515 | | /// Retrieve an existing <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 516 | | /// </summary> |
| | | 517 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to retrieve.</param> |
| | | 518 | | /// <param name="onlyIfChanged">If set to true, only retrieve the setting from the configuration store if it has |
| | | 519 | | /// It is determined to have changed if the ETag field on the passed-in <see cref="ConfigurationSetting"/> is di |
| | | 520 | | /// configuration store. If it has not changed, the returned response will have have no value, and will throw i |
| | | 521 | | /// check the status code on the response to avoid triggering the exception.</param> |
| | | 522 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 523 | | /// <returns>A response containing the retrieved <see cref="ConfigurationSetting"/>.</returns> |
| | | 524 | | public virtual Response<ConfigurationSetting> GetConfigurationSetting(ConfigurationSetting setting, bool onlyIfC |
| | | 525 | | { |
| | 8 | 526 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 8 | 527 | | MatchConditions requestOptions = onlyIfChanged ? new MatchConditions { IfNoneMatch = setting.ETag } : defaul |
| | 8 | 528 | | return GetConfigurationSetting(setting.Key, setting.Label, acceptDateTime: default, requestOptions, cancella |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <summary> |
| | | 532 | | /// Retrieve an existing <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 533 | | /// </summary> |
| | | 534 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to retrieve.</param> |
| | | 535 | | /// <param name="acceptDateTime">The setting will be retrieved exactly as it existed at the provided time.</para |
| | | 536 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 537 | | /// <returns>A response containing the retrieved <see cref="ConfigurationSetting"/>.</returns> |
| | | 538 | | public virtual async Task<Response<ConfigurationSetting>> GetConfigurationSettingAsync(ConfigurationSetting sett |
| | | 539 | | { |
| | 4 | 540 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 4 | 541 | | return await GetConfigurationSettingAsync(setting.Key, setting.Label, acceptDateTime, requestOptions: defaul |
| | 4 | 542 | | } |
| | | 543 | | |
| | | 544 | | /// <summary> |
| | | 545 | | /// Retrieve an existing <see cref="ConfigurationSetting"/> from the configuration store. |
| | | 546 | | /// </summary> |
| | | 547 | | /// <param name="setting">The <see cref="ConfigurationSetting"/> to retrieve.</param> |
| | | 548 | | /// <param name="acceptDateTime">The setting will be retrieved exactly as it existed at the provided time.</para |
| | | 549 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 550 | | /// <returns>A response containing the retrieved <see cref="ConfigurationSetting"/>.</returns> |
| | | 551 | | public virtual Response<ConfigurationSetting> GetConfigurationSetting(ConfigurationSetting setting, DateTimeOffs |
| | | 552 | | { |
| | 4 | 553 | | Argument.AssertNotNull(setting, nameof(setting)); |
| | 4 | 554 | | return GetConfigurationSetting(setting.Key, setting.Label, acceptDateTime, requestOptions: default, cancella |
| | | 555 | | } |
| | | 556 | | |
| | | 557 | | private async Task<Response<ConfigurationSetting>> GetConfigurationSettingAsync(string key, string label, DateTi |
| | | 558 | | { |
| | 38 | 559 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(GetConf |
| | 38 | 560 | | scope.AddAttribute(nameof(key), key); |
| | 38 | 561 | | scope.Start(); |
| | | 562 | | |
| | | 563 | | try |
| | | 564 | | { |
| | 38 | 565 | | using Request request = CreateGetRequest(key, label, acceptDateTime, requestOptions); |
| | 38 | 566 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 567 | | |
| | 38 | 568 | | return response.Status switch |
| | 38 | 569 | | { |
| | 64 | 570 | | 200 => await CreateResponseAsync(response, cancellationToken).ConfigureAwait(false), |
| | 42 | 571 | | 304 => CreateResourceModifiedResponse(response), |
| | 46 | 572 | | _ => throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false |
| | 38 | 573 | | }; |
| | | 574 | | } |
| | 8 | 575 | | catch (Exception e) |
| | | 576 | | { |
| | 8 | 577 | | scope.Failed(e); |
| | 8 | 578 | | throw; |
| | | 579 | | } |
| | 30 | 580 | | } |
| | | 581 | | |
| | | 582 | | private Response<ConfigurationSetting> GetConfigurationSetting(string key, string label, DateTimeOffset acceptDa |
| | | 583 | | { |
| | 38 | 584 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(GetConf |
| | 38 | 585 | | scope.AddAttribute(nameof(key), key); |
| | 38 | 586 | | scope.Start(); |
| | | 587 | | |
| | | 588 | | try |
| | | 589 | | { |
| | 38 | 590 | | using Request request = CreateGetRequest(key, label, acceptDateTime, requestOptions); |
| | 38 | 591 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | | 592 | | |
| | 38 | 593 | | return response.Status switch |
| | 38 | 594 | | { |
| | 64 | 595 | | 200 => CreateResponse(response), |
| | 42 | 596 | | 304 => CreateResourceModifiedResponse(response), |
| | 46 | 597 | | _ => throw _clientDiagnostics.CreateRequestFailedException(response), |
| | 38 | 598 | | }; |
| | | 599 | | } |
| | 8 | 600 | | catch (Exception e) |
| | | 601 | | { |
| | 8 | 602 | | scope.Failed(e); |
| | 8 | 603 | | throw; |
| | | 604 | | } |
| | 30 | 605 | | } |
| | | 606 | | |
| | | 607 | | /// <summary> |
| | | 608 | | /// Retrieves one or more <see cref="ConfigurationSetting"/> entities that match the options specified in the pa |
| | | 609 | | /// </summary> |
| | | 610 | | /// <param name="selector">Options used to select a set of <see cref="ConfigurationSetting"/> entities from the |
| | | 611 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 612 | | /// <returns>An enumerable collection containing the retrieved <see cref="ConfigurationSetting"/> entities.</ret |
| | | 613 | | public virtual AsyncPageable<ConfigurationSetting> GetConfigurationSettingsAsync(SettingSelector selector, Cance |
| | | 614 | | { |
| | 30 | 615 | | Argument.AssertNotNull(selector, nameof(selector)); |
| | 80 | 616 | | return PageResponseEnumerator.CreateAsyncEnumerable(nextLink => GetConfigurationSettingsPageAsync(selector, |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | /// <summary> |
| | | 620 | | /// Retrieves one or more <see cref="ConfigurationSetting"/> entities that match the options specified in the pa |
| | | 621 | | /// </summary> |
| | | 622 | | /// <param name="selector">Set of options for selecting <see cref="ConfigurationSetting"/> from the configuratio |
| | | 623 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 624 | | public virtual Pageable<ConfigurationSetting> GetConfigurationSettings(SettingSelector selector, CancellationTok |
| | | 625 | | { |
| | 30 | 626 | | Argument.AssertNotNull(selector, nameof(selector)); |
| | 80 | 627 | | return PageResponseEnumerator.CreateEnumerable(nextLink => GetConfigurationSettingsPage(selector, nextLink, |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | /// <summary> |
| | | 631 | | /// Retrieves the revisions of one or more <see cref="ConfigurationSetting"/> entities that match the specified |
| | | 632 | | /// </summary> |
| | | 633 | | /// <param name="keyFilter">Key filter that will be used to select a set of <see cref="ConfigurationSetting"/> e |
| | | 634 | | /// <param name="labelFilter">Label filter that will be used to select a set of <see cref="ConfigurationSetting" |
| | | 635 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 636 | | public virtual AsyncPageable<ConfigurationSetting> GetRevisionsAsync(string keyFilter, string labelFilter = defa |
| | | 637 | | { |
| | 2 | 638 | | Argument.AssertNotNullOrEmpty(keyFilter, nameof(keyFilter)); |
| | 2 | 639 | | return GetRevisionsAsync(new SettingSelector { KeyFilter = keyFilter, LabelFilter = labelFilter }, cancellat |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | /// <summary> |
| | | 643 | | /// Retrieves the revisions of one or more <see cref="ConfigurationSetting"/> entities that match the specified |
| | | 644 | | /// </summary> |
| | | 645 | | /// <param name="keyFilter">Key filter that will be used to select a set of <see cref="ConfigurationSetting"/> e |
| | | 646 | | /// <param name="labelFilter">Label filter that will be used to select a set of <see cref="ConfigurationSetting" |
| | | 647 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 648 | | public virtual Pageable<ConfigurationSetting> GetRevisions(string keyFilter, string labelFilter = default, Cance |
| | | 649 | | { |
| | 2 | 650 | | Argument.AssertNotNullOrEmpty(keyFilter, nameof(keyFilter)); |
| | 2 | 651 | | return GetRevisions(new SettingSelector { KeyFilter = keyFilter, LabelFilter = labelFilter }, cancellationTo |
| | | 652 | | } |
| | | 653 | | |
| | | 654 | | /// <summary> |
| | | 655 | | /// Retrieves the revisions of one or more <see cref="ConfigurationSetting"/> entities that satisfy the options |
| | | 656 | | /// </summary> |
| | | 657 | | /// <param name="selector">Set of options for selecting <see cref="ConfigurationSetting"/> from the configuratio |
| | | 658 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 659 | | public virtual AsyncPageable<ConfigurationSetting> GetRevisionsAsync(SettingSelector selector, CancellationToken |
| | | 660 | | { |
| | 4 | 661 | | Argument.AssertNotNull(selector, nameof(selector)); |
| | 8 | 662 | | return PageResponseEnumerator.CreateAsyncEnumerable(nextLink => GetRevisionsPageAsync(selector, nextLink, ca |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | /// <summary> |
| | | 666 | | /// Retrieves the revisions of one or more <see cref="ConfigurationSetting"/> entities that satisfy the options |
| | | 667 | | /// </summary> |
| | | 668 | | /// <param name="selector">Set of options for selecting <see cref="ConfigurationSetting"/> from the configuratio |
| | | 669 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 670 | | public virtual Pageable<ConfigurationSetting> GetRevisions(SettingSelector selector, CancellationToken cancellat |
| | | 671 | | { |
| | 4 | 672 | | Argument.AssertNotNull(selector, nameof(selector)); |
| | 8 | 673 | | return PageResponseEnumerator.CreateEnumerable(nextLink => GetRevisionsPage(selector, nextLink, cancellation |
| | | 674 | | } |
| | | 675 | | |
| | | 676 | | private Request CreateGetRequest(string key, string label, DateTimeOffset acceptDateTime, MatchConditions reques |
| | | 677 | | { |
| | 76 | 678 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | | 679 | | |
| | 76 | 680 | | Request request = _pipeline.CreateRequest(); |
| | 76 | 681 | | request.Method = RequestMethod.Get; |
| | 76 | 682 | | BuildUriForKvRoute(request.Uri, key, label); |
| | 76 | 683 | | request.Headers.Add(s_mediaTypeKeyValueApplicationHeader); |
| | | 684 | | |
| | 76 | 685 | | if (acceptDateTime != default) |
| | | 686 | | { |
| | 8 | 687 | | var dateTime = acceptDateTime.UtcDateTime.ToString(AcceptDateTimeFormat, CultureInfo.InvariantCulture); |
| | 8 | 688 | | request.Headers.SetValue(AcceptDatetimeHeader, dateTime); |
| | | 689 | | } |
| | | 690 | | |
| | 76 | 691 | | if (requestOptions != null) |
| | | 692 | | { |
| | 16 | 693 | | ConditionalRequestOptionsExtensions.ApplyHeaders(request, requestOptions); |
| | | 694 | | } |
| | | 695 | | |
| | 76 | 696 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| | 76 | 697 | | return request; |
| | | 698 | | } |
| | | 699 | | |
| | | 700 | | /// <summary> |
| | | 701 | | /// Fetches the <see cref="ConfigurationSetting"/> from the configuration store that match the options selected |
| | | 702 | | /// </summary> |
| | | 703 | | /// <param name="selector">Set of options for selecting settings from the configuration store.</param> |
| | | 704 | | /// <param name="pageLink"></param> |
| | | 705 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 706 | | private async Task<Page<ConfigurationSetting>> GetConfigurationSettingsPageAsync(SettingSelector selector, strin |
| | | 707 | | { |
| | 50 | 708 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(GetConf |
| | 50 | 709 | | scope.Start(); |
| | | 710 | | |
| | | 711 | | try |
| | | 712 | | { |
| | 50 | 713 | | using Request request = CreateBatchRequest(selector, pageLink); |
| | 50 | 714 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 715 | | |
| | 50 | 716 | | switch (response.Status) |
| | | 717 | | { |
| | | 718 | | case 200: |
| | | 719 | | case 206: |
| | 50 | 720 | | SettingBatch settingBatch = await ConfigurationServiceSerializer.ParseBatchAsync(response, cance |
| | 50 | 721 | | return Page<ConfigurationSetting>.FromValues(settingBatch.Settings, settingBatch.NextBatchLink, |
| | | 722 | | default: |
| | 0 | 723 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | | 724 | | } |
| | | 725 | | } |
| | 0 | 726 | | catch (Exception e) |
| | | 727 | | { |
| | 0 | 728 | | scope.Failed(e); |
| | 0 | 729 | | throw; |
| | | 730 | | } |
| | 50 | 731 | | } |
| | | 732 | | |
| | | 733 | | /// <summary> |
| | | 734 | | /// Fetches the <see cref="ConfigurationSetting"/> from the configuration store that match the options selected |
| | | 735 | | /// </summary> |
| | | 736 | | /// <param name="selector">Set of options for selecting settings from the configuration store.</param> |
| | | 737 | | /// <param name="pageLink"></param> |
| | | 738 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 739 | | private Page<ConfigurationSetting> GetConfigurationSettingsPage(SettingSelector selector, string pageLink, Cance |
| | | 740 | | { |
| | 50 | 741 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(GetConf |
| | 50 | 742 | | scope.Start(); |
| | | 743 | | |
| | | 744 | | try |
| | | 745 | | { |
| | 50 | 746 | | using Request request = CreateBatchRequest(selector, pageLink); |
| | 50 | 747 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | | 748 | | |
| | 50 | 749 | | switch (response.Status) |
| | | 750 | | { |
| | | 751 | | case 200: |
| | | 752 | | case 206: |
| | 50 | 753 | | SettingBatch settingBatch = ConfigurationServiceSerializer.ParseBatch(response); |
| | 50 | 754 | | return Page<ConfigurationSetting>.FromValues(settingBatch.Settings, settingBatch.NextBatchLink, |
| | | 755 | | default: |
| | 0 | 756 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | | 757 | | } |
| | | 758 | | } |
| | 0 | 759 | | catch (Exception e) |
| | | 760 | | { |
| | 0 | 761 | | scope.Failed(e); |
| | 0 | 762 | | throw; |
| | | 763 | | } |
| | 50 | 764 | | } |
| | | 765 | | |
| | | 766 | | private Request CreateBatchRequest(SettingSelector selector, string pageLink) |
| | | 767 | | { |
| | 100 | 768 | | Request request = _pipeline.CreateRequest(); |
| | 100 | 769 | | request.Method = RequestMethod.Get; |
| | 100 | 770 | | BuildUriForGetBatch(request.Uri, selector, pageLink); |
| | 100 | 771 | | request.Headers.Add(s_mediaTypeKeyValueApplicationHeader); |
| | 100 | 772 | | if (selector.AcceptDateTime.HasValue) |
| | | 773 | | { |
| | 0 | 774 | | var dateTime = selector.AcceptDateTime.Value.UtcDateTime.ToString(AcceptDateTimeFormat, CultureInfo.Inva |
| | 0 | 775 | | request.Headers.SetValue(AcceptDatetimeHeader, dateTime); |
| | | 776 | | } |
| | | 777 | | |
| | 100 | 778 | | return request; |
| | | 779 | | } |
| | | 780 | | |
| | | 781 | | /// <summary> |
| | | 782 | | /// Lists chronological/historical representation of <see cref="ConfigurationSetting"/> from the configuration s |
| | | 783 | | /// </summary> |
| | | 784 | | /// <remarks>Revisions are provided in descending order from their respective <see cref="ConfigurationSetting.La |
| | | 785 | | /// <param name="selector">Set of options for selecting settings from the configuration store.</param> |
| | | 786 | | /// <param name="pageLink"></param> |
| | | 787 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 788 | | private async Task<Page<ConfigurationSetting>> GetRevisionsPageAsync(SettingSelector selector, string pageLink, |
| | | 789 | | { |
| | 4 | 790 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(GetRevi |
| | 4 | 791 | | scope.Start(); |
| | | 792 | | |
| | | 793 | | try |
| | | 794 | | { |
| | 4 | 795 | | using Request request = CreateGetRevisionsRequest(selector, pageLink); |
| | 4 | 796 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 4 | 797 | | switch (response.Status) |
| | | 798 | | { |
| | | 799 | | case 200: |
| | | 800 | | case 206: |
| | 4 | 801 | | SettingBatch settingBatch = await ConfigurationServiceSerializer.ParseBatchAsync(response, cance |
| | 4 | 802 | | return Page<ConfigurationSetting>.FromValues(settingBatch.Settings, settingBatch.NextBatchLink, |
| | | 803 | | default: |
| | 0 | 804 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | | 805 | | } |
| | | 806 | | } |
| | 0 | 807 | | catch (Exception e) |
| | | 808 | | { |
| | 0 | 809 | | scope.Failed(e); |
| | 0 | 810 | | throw; |
| | | 811 | | } |
| | 4 | 812 | | } |
| | | 813 | | |
| | | 814 | | /// <summary> |
| | | 815 | | /// Lists chronological/historical representation of <see cref="ConfigurationSetting"/> from the configuration s |
| | | 816 | | /// </summary> |
| | | 817 | | /// <remarks>Revisions are provided in descending order from their respective <see cref="ConfigurationSetting.La |
| | | 818 | | /// <param name="selector">Set of options for selecting settings from the configuration store.</param> |
| | | 819 | | /// <param name="pageLink"></param> |
| | | 820 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 821 | | private Page<ConfigurationSetting> GetRevisionsPage(SettingSelector selector, string pageLink, CancellationToken |
| | | 822 | | { |
| | 4 | 823 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(GetRevi |
| | 4 | 824 | | scope.Start(); |
| | | 825 | | |
| | | 826 | | try |
| | | 827 | | { |
| | 4 | 828 | | using Request request = CreateGetRevisionsRequest(selector, pageLink); |
| | 4 | 829 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 4 | 830 | | switch (response.Status) |
| | | 831 | | { |
| | | 832 | | case 200: |
| | | 833 | | case 206: |
| | 4 | 834 | | SettingBatch settingBatch = ConfigurationServiceSerializer.ParseBatch(response); |
| | 4 | 835 | | return Page<ConfigurationSetting>.FromValues(settingBatch.Settings, settingBatch.NextBatchLink, |
| | | 836 | | default: |
| | 0 | 837 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | | 838 | | } |
| | | 839 | | } |
| | 0 | 840 | | catch (Exception e) |
| | | 841 | | { |
| | 0 | 842 | | scope.Failed(e); |
| | 0 | 843 | | throw; |
| | | 844 | | } |
| | 4 | 845 | | } |
| | | 846 | | |
| | | 847 | | private Request CreateGetRevisionsRequest(SettingSelector selector, string pageLink) |
| | | 848 | | { |
| | 8 | 849 | | Request request = _pipeline.CreateRequest(); |
| | 8 | 850 | | request.Method = RequestMethod.Get; |
| | 8 | 851 | | BuildUriForRevisions(request.Uri, selector, pageLink); |
| | 8 | 852 | | request.Headers.Add(s_mediaTypeKeyValueApplicationHeader); |
| | 8 | 853 | | if (selector.AcceptDateTime.HasValue) |
| | | 854 | | { |
| | 4 | 855 | | var dateTime = selector.AcceptDateTime.Value.UtcDateTime.ToString(AcceptDateTimeFormat, CultureInfo.Inva |
| | 4 | 856 | | request.Headers.SetValue(AcceptDatetimeHeader, dateTime); |
| | | 857 | | } |
| | | 858 | | |
| | 8 | 859 | | return request; |
| | | 860 | | } |
| | | 861 | | |
| | | 862 | | /// <summary> |
| | | 863 | | /// Sets an existing <see cref="ConfigurationSetting"/> to read only or read write state in the configuration st |
| | | 864 | | /// </summary> |
| | | 865 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 866 | | /// <param name="isReadOnly">If true, the <see cref="ConfigurationSetting"/> will be set to read only in the con |
| | | 867 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 868 | | public virtual Task<Response<ConfigurationSetting>> SetReadOnlyAsync(string key, bool isReadOnly, CancellationTo |
| | 12 | 869 | | => SetReadOnlyAsync(key, label: default, isReadOnly, cancellationToken); |
| | | 870 | | |
| | | 871 | | /// <summary> |
| | | 872 | | /// Sets an existing <see cref="ConfigurationSetting"/> to read only or read write state in the configuration st |
| | | 873 | | /// </summary> |
| | | 874 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 875 | | /// <param name="isReadOnly">If true, the <see cref="ConfigurationSetting"/> will be set to read only in the con |
| | | 876 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 877 | | public virtual Response<ConfigurationSetting> SetReadOnly(string key, bool isReadOnly, CancellationToken cancell |
| | 12 | 878 | | => SetReadOnly(key, label: default, isReadOnly, cancellationToken); |
| | | 879 | | |
| | | 880 | | /// <summary> |
| | | 881 | | /// Sets an existing <see cref="ConfigurationSetting"/> to read only or read write state in the configuration st |
| | | 882 | | /// </summary> |
| | | 883 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 884 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 885 | | /// <param name="isReadOnly">If true, the <see cref="ConfigurationSetting"/> will be set to read only in the con |
| | | 886 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 887 | | public virtual async Task<Response<ConfigurationSetting>> SetReadOnlyAsync(string key, string label, bool isRead |
| | | 888 | | { |
| | 30 | 889 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | | 890 | | |
| | 30 | 891 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(SetRead |
| | 30 | 892 | | scope.AddAttribute("key", key); |
| | 30 | 893 | | scope.Start(); |
| | | 894 | | |
| | | 895 | | try |
| | | 896 | | { |
| | 30 | 897 | | using Request request = CreateSetReadOnlyRequest(key, label, isReadOnly); |
| | 30 | 898 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 899 | | |
| | 30 | 900 | | switch (response.Status) |
| | | 901 | | { |
| | | 902 | | case 200: |
| | 22 | 903 | | return CreateResponse(response); |
| | | 904 | | default: |
| | 8 | 905 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | | 906 | | } |
| | | 907 | | } |
| | 8 | 908 | | catch (Exception e) |
| | | 909 | | { |
| | 8 | 910 | | scope.Failed(e); |
| | 8 | 911 | | throw; |
| | | 912 | | } |
| | 22 | 913 | | } |
| | | 914 | | |
| | | 915 | | /// <summary> |
| | | 916 | | /// Sets an existing <see cref="ConfigurationSetting"/> to read only or read write state in the configuration st |
| | | 917 | | /// </summary> |
| | | 918 | | /// <param name="key">The primary identifier of the configuration setting.</param> |
| | | 919 | | /// <param name="label">A label used to group this configuration setting with others.</param> |
| | | 920 | | /// <param name="isReadOnly">If true, the <see cref="ConfigurationSetting"/> will be set to read only in the con |
| | | 921 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 922 | | public virtual Response<ConfigurationSetting> SetReadOnly(string key, string label, bool isReadOnly, Cancellatio |
| | | 923 | | { |
| | 30 | 924 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| | | 925 | | |
| | 30 | 926 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ConfigurationClient)}.{nameof(SetRead |
| | 30 | 927 | | scope.AddAttribute("key", key); |
| | 30 | 928 | | scope.Start(); |
| | | 929 | | |
| | | 930 | | try |
| | | 931 | | { |
| | 30 | 932 | | using Request request = CreateSetReadOnlyRequest(key, label, isReadOnly); |
| | 30 | 933 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | | 934 | | |
| | 30 | 935 | | switch (response.Status) |
| | | 936 | | { |
| | | 937 | | case 200: |
| | 22 | 938 | | return CreateResponse(response); |
| | | 939 | | default: |
| | 8 | 940 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | | 941 | | } |
| | | 942 | | } |
| | 8 | 943 | | catch (Exception e) |
| | | 944 | | { |
| | 8 | 945 | | scope.Failed(e); |
| | 8 | 946 | | throw; |
| | | 947 | | } |
| | 22 | 948 | | } |
| | | 949 | | |
| | | 950 | | private Request CreateSetReadOnlyRequest(string key, string label, bool isReadOnly) |
| | | 951 | | { |
| | 60 | 952 | | Request request = _pipeline.CreateRequest(); |
| | 60 | 953 | | request.Method = isReadOnly ? RequestMethod.Put : RequestMethod.Delete; |
| | 60 | 954 | | BuildUriForLocksRoute(request.Uri, key, label); |
| | | 955 | | |
| | 60 | 956 | | return request; |
| | | 957 | | } |
| | | 958 | | } |
| | | 959 | | } |