1414 */
1515
1616using System . Collections . Concurrent ;
17+ using System . Text . Json . Nodes ;
1718using Amazon ;
1819using Amazon . AppConfigData ;
1920using Amazon . AppConfigData . Model ;
@@ -35,32 +36,32 @@ public class AppConfigProvider : ParameterProvider<AppConfigProviderConfiguratio
3536 /// The default application Id.
3637 /// </summary>
3738 private string _defaultApplicationId = string . Empty ;
38-
39+
3940 /// <summary>
4041 /// The default environment Id.
4142 /// </summary>
4243 private string _defaultEnvironmentId = string . Empty ;
43-
44+
4445 /// <summary>
4546 /// The default configuration profile Id.
4647 /// </summary>
4748 private string _defaultConfigProfileId = string . Empty ;
48-
49+
4950 /// <summary>
5051 /// Instance of datetime wrapper.
5152 /// </summary>
5253 private readonly IDateTimeWrapper _dateTimeWrapper ;
53-
54+
5455 /// <summary>
5556 /// Thread safe dictionary to store results.
5657 /// </summary>
5758 private readonly ConcurrentDictionary < string , AppConfigResult > _results = new ( StringComparer . OrdinalIgnoreCase ) ;
58-
59+
5960 /// <summary>
6061 /// The client instance.
6162 /// </summary>
6263 private IAmazonAppConfigData ? _client ;
63-
64+
6465 /// <summary>
6566 /// Gets the client instance.
6667 /// </summary>
@@ -88,7 +89,7 @@ internal AppConfigProvider(
8889 }
8990
9091 #region IParameterProviderConfigurableClient implementation
91-
92+
9293 /// <summary>
9394 /// Use a custom client
9495 /// </summary>
@@ -238,7 +239,7 @@ public IAppConfigProvider ConfigureClient(string awsAccessKeyId, string awsSecre
238239 _client = new AmazonAppConfigDataClient ( awsAccessKeyId , awsSecretAccessKey , awsSessionToken , config ) ;
239240 return this ;
240241 }
241-
242+
242243 #endregion
243244
244245 /// <summary>
@@ -253,7 +254,7 @@ public IAppConfigProvider DefaultApplication(string applicationId)
253254 _defaultApplicationId = applicationId ;
254255 return this ;
255256 }
256-
257+
257258 /// <summary>
258259 /// Sets the default environment ID or name.
259260 /// </summary>
@@ -266,7 +267,7 @@ public IAppConfigProvider DefaultEnvironment(string environmentId)
266267 _defaultEnvironmentId = environmentId ;
267268 return this ;
268269 }
269-
270+
270271 /// <summary>
271272 /// Sets the default configuration profile ID or name.
272273 /// </summary>
@@ -318,7 +319,7 @@ protected override AppConfigProviderConfigurationBuilder NewConfigurationBuilder
318319 . WithApplication ( _defaultApplicationId )
319320 . WithEnvironment ( _defaultEnvironmentId )
320321 . WithConfigProfile ( _defaultConfigProfileId ) ;
321-
322+
322323 }
323324
324325 /// <summary>
@@ -353,29 +354,29 @@ protected override AppConfigProviderConfigurationBuilder NewConfigurationBuilder
353354 . GetAsync ( )
354355 . ConfigureAwait ( false ) ;
355356 }
356-
357+
357358 /// <summary>
358359 /// Get last AppConfig value and transform it to JSON value.
359360 /// </summary>
360361 /// <typeparam name="T">JSON value type.</typeparam>
361362 /// <returns>The AppConfig JSON value.</returns>
362- public T ? Get < T > ( ) where T : class
363+ public T ? Get < T > ( ) where T : class
363364 {
364365 return GetAsync < T > ( ) . GetAwaiter ( ) . GetResult ( ) ;
365366 }
366-
367+
367368 /// <summary>
368369 /// Get last AppConfig value and transform it to JSON value.
369370 /// </summary>
370371 /// <typeparam name="T">JSON value type.</typeparam>
371372 /// <returns>The AppConfig JSON value.</returns>
372- public async Task < T ? > GetAsync < T > ( ) where T : class
373+ public async Task < T ? > GetAsync < T > ( ) where T : class
373374 {
374375 return await NewConfigurationBuilder ( )
375376 . GetAsync < T > ( )
376377 . ConfigureAwait ( false ) ;
377378 }
378-
379+
379380 /// <summary>
380381 /// Get parameter value for the provided key.
381382 /// </summary>
@@ -384,7 +385,7 @@ protected override AppConfigProviderConfigurationBuilder NewConfigurationBuilder
384385 /// <returns>The parameter value.</returns>
385386 protected override async Task < string ? > GetAsync ( string key , ParameterProviderConfiguration ? config )
386387 {
387- if ( config is not AppConfigProviderConfiguration configuration )
388+ if ( config is not AppConfigProviderConfiguration configuration )
388389 throw new ArgumentNullException ( nameof ( config ) ) ;
389390
390391 var cacheKey = AppConfigProviderCacheHelper . GetCacheKey
@@ -395,7 +396,7 @@ protected override AppConfigProviderConfigurationBuilder NewConfigurationBuilder
395396 ) ;
396397
397398 var result = GetAppConfigResult ( cacheKey ) ;
398-
399+
399400 if ( _dateTimeWrapper . UtcNow < result . NextAllowedPollTime )
400401 {
401402 if ( ! config . ForceFetch )
@@ -427,7 +428,7 @@ await Client.GetLatestConfigurationAsync(request)
427428
428429 using ( var reader = new StreamReader ( response . Configuration ) )
429430 {
430- result . LastConfig =
431+ result . LastConfig =
431432 await reader . ReadToEndAsync ( )
432433 . ConfigureAwait ( false ) ;
433434 }
@@ -446,7 +447,7 @@ await reader.ReadToEndAsync()
446447 {
447448 throw new NotSupportedException ( "Impossible to get multiple values from AWS AppConfig" ) ;
448449 }
449-
450+
450451 /// <summary>
451452 /// Gets Or Adds AppConfigResult with provided key
452453 /// </summary>
@@ -462,7 +463,7 @@ private AppConfigResult GetAppConfigResult(string cacheKey)
462463
463464 return cachedResult ;
464465 }
465-
466+
466467 /// <summary>
467468 /// Starts a configuration session used to retrieve a deployed configuration.
468469 /// </summary>
@@ -479,4 +480,57 @@ private async Task<string> GetInitialConfigurationTokenAsync(AppConfigProviderCo
479480
480481 return ( await Client . StartConfigurationSessionAsync ( request ) . ConfigureAwait ( false ) ) . InitialConfigurationToken ;
481482 }
483+
484+ /// <summary>
485+ /// Check if the feature flag is enabled.
486+ /// </summary>
487+ /// <param name="key">The unique feature key for the feature flag</param>
488+ /// <param name="defaultValue">The default value of the flag</param>
489+ /// <returns>The feature flag value, or defaultValue if the flag cannot be evaluated</returns>
490+ public bool IsFeatureFlagEnabled ( string key , bool defaultValue = false )
491+ {
492+ return IsFeatureFlagEnabledAsync ( key , defaultValue ) . GetAwaiter ( ) . GetResult ( ) ;
493+ }
494+
495+ /// <summary>
496+ /// Check if the feature flag is enabled.
497+ /// </summary>
498+ /// <param name="key">The unique feature key for the feature flag</param>
499+ /// <param name="defaultValue">The default value of the flag</param>
500+ /// <returns>The feature flag value, or defaultValue if the flag cannot be evaluated</returns>
501+ public async Task < bool > IsFeatureFlagEnabledAsync ( string key , bool defaultValue = false )
502+ {
503+ return await GetFeatureFlagAttributeValueAsync ( key , AppConfigFeatureFlagHelper . EnabledAttributeKey ,
504+ defaultValue ) . ConfigureAwait ( false ) ;
505+ }
506+
507+ /// <summary>
508+ /// Get feature flag's attribute value.
509+ /// </summary>
510+ /// <param name="key">The unique feature key for the feature flag</param>
511+ /// <param name="attributeKey">The unique attribute key for the feature flag</param>
512+ /// <param name="defaultValue">The default value of the feature flag's attribute value</param>
513+ /// <typeparam name="T">The type of the value to obtain from feature flag's attribute.</typeparam>
514+ /// <returns>The feature flag's attribute value.</returns>
515+ public T ? GetFeatureFlagAttributeValue < T > ( string key , string attributeKey , T ? defaultValue = default )
516+ {
517+ return GetFeatureFlagAttributeValueAsync ( key , attributeKey , defaultValue ) . GetAwaiter ( ) . GetResult ( ) ;
518+ }
519+
520+ /// <summary>
521+ /// Get feature flag's attribute value.
522+ /// </summary>
523+ /// <param name="key">The unique feature key for the feature flag</param>
524+ /// <param name="attributeKey">The unique attribute key for the feature flag</param>
525+ /// <param name="defaultValue">The default value of the feature flag's attribute value</param>
526+ /// <typeparam name="T">The type of the value to obtain from feature flag's attribute.</typeparam>
527+ /// <returns>The feature flag's attribute value.</returns>
528+ public async Task < T ? > GetFeatureFlagAttributeValueAsync < T > ( string key , string attributeKey ,
529+ T ? defaultValue = default )
530+ {
531+ return string . IsNullOrWhiteSpace ( key )
532+ ? defaultValue
533+ : AppConfigFeatureFlagHelper . GetFeatureFlagAttributeValueAsync ( key , attributeKey , defaultValue ,
534+ await GetAsync < JsonObject > ( ) . ConfigureAwait ( false ) ) ;
535+ }
482536}
0 commit comments