Package: azure-appconfiguration-provider
Azure App Configuration Python Provider client library for PythonAzure App Configuration is a managed service that helps developers centralize their application configurations simply and securely. This provider adds additional functionality above the azure-sdk-for-python.Using the provider enables loading sets of configurations from an Azure App Configuration store in a managed way.Getting startedGet credentialsUse theAzure CLIsnippet below to get the connection string from the Configuration Store.azappconfigcredentiallist--name<config-store-name>Alternatively, get the connection string from the Azure Portal.Creating a providerYou can create a client with a connection string:fromazure.appconfiguration.providerimportloadconfig=load(connection_string="your-connection-string")or with AAD:fromazure.appconfiguration.providerimportloadconfig=load(endpoint="your-endpoint",credential=DefaultAzureCredential())these providers will by default load all configurations with(No Label)from your configuration store into a dictionary of key/values.FeaturesCurrently the Azure App Configuration Provider enables:Connecting to an App Configuration Store using a connection string or Azure Active Directory.Selecting multiple sets of configurations usingSettingSelector.Dynamic RefreshTrim prefixes off key names.Resolving Key Vault References, requires AAD.Secret Resolver, resolve Key Vault References locally without connecting to Key Vault.Json Content TypeFuture FeaturesList of features we are going to add to the Python Provider in the future.Geo-Replication supportFeature ManagementConfiguration PlaceholdersExamplesSelecting configurationsYou can refine or expand the configurations loaded from your store by usingSettingSelectors. Setting selectors provide a way to pass a key filter and label filter into the provider.fromazure.appconfiguration.providerimportload,SettingSelectorfromazure.identityimportDefaultAzureCredentialselects={SettingSelector(key_filter="*",label_filter="\0"),SettingSelector(key_filter="*",label_filter="dev")}config=load(endpoint=endpoint,credential=DefaultAzureCredential(),selects=selects)In this example all configuration with empty label and the dev label are loaded. Because the dev selector is listed last, any configurations from dev take priority over those with(No Label)when duplicates are found.Dynamic RefreshThe provider can be configured to refresh configurations from the store on a set interval. This is done by providing arefresh_onto the provider, which is a list of key(s) that will be watched for changes, and when they do change a refresh can happen.refresh_intervalis the period of time in seconds between refreshes.on_refresh_successis a callback that will be called only if a change is detected and no error happens.on_refresh_erroris a callback that will be called when a refresh fails.fromazure.appconfiguration.providerimportload,WatchKeyimportosconnection_string=os.environ.get("APPCONFIGURATION_CONNECTION_STRING")defmy_callback_on_success():# Do something on success...defmy_callback_on_fail(error):# Do something on fail...config=load(connection_string=connection_string,refresh_on=[WatchKey("Sentinel")],refresh_interval=60,on_refresh_success=my_callback_on_success,on_refresh_error=my_callback_on_fail,**kwargs,)In this example, the sentinel key will be checked for changes no sooner than every 60 seconds. In order to check for changes, the provider'srefreshmethod needs to be called.config.refresh()Once the provider is refreshed, the configurations can be accessed as normal. And if any changes have been made it will be updated with the latest values. If therefresh_intervalhasn't passed since the last refresh check, the provider will not check for changes.For additional info check outDynamic Refreshon MS Learn.Trimming KeysYou can trim the prefix off of keys by providing a list of trimmed key prefixes to the provider. For example, if you have the key(s) like/application/messagein your configuration store, you could trim/application/from them.fromazure.appconfiguration.providerimportloadfromazure.identityimportDefaultAzureCredentialtrim_prefixes={"/application/"}config=load(endpoint=endpoint,credential=DefaultAzureCredential(),trim_prefixes=trim_prefixes)print(config["message"])Resolving Key Vault ReferencesKey Vault References can be resolved by providing credentials to your key vault to the provider usingAzureAppConfigurationKeyVaultOptions.With CredentialsYou can provideAzureAppConfigurationKeyVaultOptionswith a credential and all key vault references will be resolved with it. The provider will attempt to connect to any key vault referenced with the credential provided.fromazure.appconfiguration.providerimportload,AzureAppConfigurationKeyVaultOptionsfromazure.identityimportDefaultAzureCredentialkey_vault_options=AzureAppConfigurationKeyVaultOptions(credential=DefaultAzureCredential())config=load(endpoint=endpoint,credential=DefaultAzureCredential(),key_vault_options=key_vault_options)With ClientsYou can provideAzureAppConfigurationKeyVaultOptionswith a list ofSecretClients.fromazure.appconfiguration.providerimportload,AzureAppConfigurationKeyVaultOptionsfromazure.identityimportDefaultAzureCredentialkey_vault_options=AzureAppConfigurationKeyVaultOptions(client_configs={key_vault_uri:{'credential':credential}})config=load(endpoint=endpoint,credential=DefaultAzureCredential(),key_vault_options=key_vault_options)Secret ResolverIf no Credentials or Clients are provided a secret resolver can be used. Secret resolver provides a way to return any value you want to a key vault reference.fromazure.appconfiguration.providerimportload,AzureAppConfigurationKeyVaultOptionsfromazure.identityimportDefaultAzureCredentialdefsecret_resolver(uri):return"From Secret Resolver"key_vault_options=AzureAppConfigurationKeyVaultOptions(secret_resolver=secret_resolver)config=load(endpoint=endpoint,credential=DefaultAzureCredential(),key_vault_options=key_vault_options)Key conceptsTroubleshootingNext stepsCheck out our Django and Flask examples to see how to use the provider in a web application.DjangoFlaskContributingThis project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution.
For details, visithttps://cla.microsoft.com.When you submit a pull request, a CLA-bot will automatically determine whether
you need to provide a CLA and decorate the PR appropriately (e.g., label,
comment). Simply follow the instructions provided by the bot. You will only
need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct. For more information,
see the Code of Conduct FAQ or contactopencode@microsoft.comwith any
additional questions or comments.Release History1.1.0 (2024-01-29)Features AddedNew API for Azure App Configuration Provider,refresh, which can be used to refresh the configuration from the Azure App Configuration service.refreshby default can check every 30 seconds for changes to specified sentinel keys. If a change is detected then all configurations are reloaded. Sentinel keys can be set by passing a list ofSentinelKey's torefresh_on.Added new optionson_refresh_successandon_refresh_failurecallbacks to the load method. These callbacks are called when the refresh method successfully refreshes the configuration or fails to refresh the configuration.Bugs FixedVerifies that therefresh_intervalis at least 1 second.1.1.0b3 (2023-12-19)Features AddedAdded on_refresh_success callback to load method. This callback is called when the refresh method successfully refreshes the configuration.Added minimum up time. This is the minimum amount of time the provider will try to be up before throwing an error. This is to prevent quick restart loops.Bugs FixedFixes issue where the refresh timer only reset after a change was found.Other ChangesRenamed the typeSentinelKeyto beWatchKey.1.1.0b2 (2023-09-29)Features AddedAdded support forkeyvault_credential,keyvault_client_configs, andsecret_resolveraskwargsinstead of usingAzureAppConfigurationKeyVaultOptions.Bugs FixedFixes issue whereuser_agentwas required to be set.Fixes issue where correlation context info is wrong on refresh.1.1.0b1 (2023-09-13)Features AddedNew API for Azure App Configuration Provider,refresh, which can be used to refresh the configuration from the Azure App Configuration service.refreshby default can check every 30 seconds for changes to specified sentinel keys. If a change is detected then all configurations are reloaded. Sentinel keys can be set by passing  a list ofSentinelKey's torefresh_on.Added support for customer provided user agent prefix.Other ChangesUpdated to use AZURE_APP_CONFIGURATION_TRACING_DISABLED environment variable to disable tracing.Changed the maximum number of retries to 2 from the default of 3 retries.Changed the maximum back off time between retries to 1 minute from the default of 2 minutes.Bumped minimum dependency onazure-coreto>=1.25.01.0.0 (2023-03-09)Breaking ChangesRenamedload_providertoloadAddedAzureAppConfigurationKeyVaultOptionsto take in aclient_configsa Mapping of endpoints to client kwargs instead of taking in the whole client.RemovedAzureAppConfigurationKeyVaultOptionssecret_clients,client_configsshould be used instead.Made key_filter and label_filter kwargs for Setting SelectorRenamedtrimmed_key_prefixestotrim_prefixesOther ChangesMade EMPTY_LABEL a constant. i.e. "\0"1.0.0b2 (2023-02-15)Features AddedAdded Async SupportAdded missing methods for Mapping APIMade load method properties unordered.Breaking ChangesChanges how load works. Moves if from AzureAppConfigurationProvider.load to load_provider.Removed custom Key Vault ErrorRemoved unneededreprand copy methods.All Feature Flags are added to there own key and have there prefix removedBugs FixedFixed Issue where Key Vault Clients couldn't be set in some situationsOther ChangesUpdated method docsFixed load doc that usedselectorinstead ofselects.Fixed CLI link in Readme.1.0.0b1 (2022-10-13)New Azure App Configuration ProviderProvides additional support above the Azure App Configuration SDK. Enables:Connecting to an Azure App Configuration storeSelecting multiple keys using Setting SelectorResolve Key Vault References when supplied AzureAppConfigurationKeyVaultOptionsThe Azure App Configuration Provider once loaded returns a dictionary of key/value pairs to use in configuration.endpoint="https://<your-store>.azconfig.io"default_credential=DefaultAzureCredential()config=AzureAppConfigurationProvider.load(endpoint=endpoint,credential=default_credential)print(config["message"])