Package: ibm-appconfiguration-python-sdk
IBM Cloud App Configuration Python server SDKIBM Cloud App Configuration SDK is used to perform feature flag and property evaluation based on the configuration on IBM Cloud App Configuration service.Table of ContentsOverviewInstallationImport the SDKInitialize SDKLicenseOverviewIBM Cloud App Configuration is a centralized feature management and configuration service onIBM Cloudfor use with web and mobile applications, microservices, and distributed environments.Instrument your applications with App Configuration Python SDK, and use the App Configuration dashboard, CLI or API to define feature flags or properties, organized into collections and targeted to segments. Toggle feature flag states in the cloud to activate or deactivate features in your application or environment, when required. You can also manage the properties for distributed applications centrally.InstallationTo install, usepiporeasy_install:pipinstall--upgradeibm-appconfiguration-python-sdkoreasy_install--upgradeibm-appconfiguration-python-sdkImport the SDKfromibm_appconfigurationimportAppConfiguration,Feature,Property,ConfigurationTypeInitialize SDKappconfig_client=AppConfiguration.get_instance()appconfig_client.init(region='region',guid='guid',apikey='apikey')appconfig_client.set_context(collection_id='airlines-webapp',environment_id='dev'):red_circle:Important:red_circle:Theinit()andset_context()are the initialisation methods and should be invokedonly onceusing
appconfig_client. The appconfig_client, once initialised, can be obtained across modules
usingAppConfiguration.get_instance().See this example below.region : Region name where the App Configuration service instance is created. UseAppConfiguration.REGION_US_SOUTHfor DallasAppConfiguration.REGION_EU_GBfor LondonAppConfiguration.REGION_AU_SYDfor SydneyAppConfiguration.REGION_US_EASTfor Washington DCguid : GUID of the App Configuration service. Obtain it from the service credentials section of the dashboardapikey : ApiKey of the App Configuration service. Obtain it from the service credentials section of the dashboardcollection_id : Id of the collection created in App Configuration service instance under theCollectionssection.environment_id : Id of the environment created in App Configuration service instance under theEnvironmentssection.Connect using private network connection (optional)Set the SDK to connect to App Configuration service by using a private endpoint that is accessible only through the IBM
Cloud private network.appconfig_client.use_private_endpoint(True);This must be done before calling theinitfunction on the SDK.(Optional)In order for your application and SDK to continue its operations even during the unlikely scenario of App Configuration
service across your application restarts, you can configure the SDK to work using a persistent cache. The SDK uses the
persistent cache to store the App Configuration data that will be available across your application restarts.# 1. default (without persistent cache)appconfig_client.set_context(collection_id='airlines-webapp',environment_id='dev')# 2. optional (with persistent cache)appconfig_client.set_context(collection_id='airlines-webapp',environment_id='dev',options={'persistent_cache_dir':'/var/lib/docker/volumes/'})persistent_cache_dir: Absolute path to a directory which has read & write permission for the user. The SDK will create
a file -appconfiguration.jsonin the specified directory, and it will be used as the persistent cache to store the
App Configuration service information.When persistent cache is enabled, the SDK will keep the last known good configuration at the persistent cache. In the
case of App Configuration server being unreachable, the latest configurations at the persistent cache is loaded to the
application to continue working.Please ensure that the cache file created in the given directory is not lost or deleted in any case. For example,
consider the case when a kubernetes pod is restarted and the cache file (appconfiguration.json) was stored in ephemeral
volume of the pod. As pod gets restarted, kubernetes destroys the ephermal volume in the pod, as a result the cache file
gets deleted. So, make sure that the cache file created by the SDK is always stored in persistent volume by providing
the correct absolute path of the persistent directory.(Optional)The SDK is also designed to serve configurations, perform feature flag & property evaluations without being connected to
App Configuration service.appconfig_client.set_context(collection_id='airlines-webapp',environment_id='dev',options={'bootstrap_file':'saflights/flights.json','live_config_update_enabled':False})bootstrap_file: Absolute path of the JSON file which contains configuration details. Make sure to provide a proper
JSON file. You can generate this file usingibmcloud ac configcommand of the IBM Cloud App Configuration CLI.live_config_update_enabled: Live configuration update from the server. Set this value toFalseif the new
configuration values shouldn't be fetched from the server. By default, this parameter (live_config_update_enabled)
is set to True.Get single featurefeature=appconfig_client.get_feature('online-check-in')# feature can be None incase of an invalid feature idiffeatureisnotNone:print(f'Feature Name :{0}'.format(feature.get_feature_name()))print(f'Feature Id :{0}'.format(feature.get_feature_id()))print(f'Feature Data Type :{0}'.format(feature.get_feature_data_type()))iffeature.is_enabled():# feature flag is enabledelse:# feature flag is disabledGet all featuresfeatures_dictionary=appconfig_client.get_features()Evaluate a featureUse thefeature.get_current_value(entity_id=entity_id, entity_attributes=entity_attributes)method to evaluate the
value of the feature flag. This method returns one of the Enabled/Disabled/Overridden value based on the evaluation. The
data type of returned value matches that of feature flag.```py
entity_id = "john_doe"
entity_attributes = {
  'city': 'Bangalore',
  'country': 'India'
}
feature_value = feature.get_current_value(entity_id=entity_id, entity_attributes=entity_attributes)
```entity_id: Id of the Entity. This will be a string identifier related to the Entity against which the feature is
evaluated. For example, an entity might be an instance of an app that runs on a mobile device, a microservice that
runs on the cloud, or a component of infrastructure that runs that microservice. For any entity to interact with App
Configuration, it must provide a unique entity ID.entity_attributes: A dictionary consisting of the attribute name and their values that defines the specified entity.
This is an optional parameter if the feature flag is not configured with any targeting definition. If the targeting is
configured, then entity_attributes should be provided for the rule evaluation. An attribute is a parameter that is
used to define a segment. The SDK uses the attribute values to determine if the specified entity satisfies the
targeting rules, and returns the appropriate feature flag value.Get single Propertyproperty=appconfig_client.get_property('check-in-charges')# property can be None incase of an invalid property idifpropertyisnotNone:print(f'Property Name :{0}'.format(property.get_property_name()))print(f'Property Id :{0}'.format(property.get_property_id()))print(f'Property Data Type :{0}'.format(property.get_property_data_type()))Get all Propertiesproperties_dictionary=appconfig_client.get_properties()Evaluate a propertyUse theproperty.get_current_value(entity_id=entity_id, entity_attributes=entity_attributes)method to evaluate the
value of the property. This method returns the default property value or its overridden value based on the evaluation.
The data type of returned value matches that of property.```py
entity_id = "john_doe"
entity_attributes = {
'city': 'Bangalore',
'country': 'India'
}
property_value = property.get_current_value(entity_id=entity_id, entity_attributes=entity_attributes)
```entity_id: Id of the Entity. This will be a string identifier related to the Entity against which the property is
evaluated. For example, an entity might be an instance of an app that runs on a mobile device, a microservice that
runs on the cloud, or a component of infrastructure that runs that microservice. For any entity to interact with App
Configuration, it must provide a unique entity ID.entity_attributes: A dictionary consisting of the attribute name and their values that defines the specified entity.
This is an optional parameter if the property is not configured with any targeting definition. If the targeting is
configured, then entity_attributes should be provided for the rule evaluation. An attribute is a parameter that is
used to define a segment. The SDK uses the attribute values to determine if the specified entity satisfies the
targeting rules, and returns the appropriate property value.Fetching the appconfig_client across other modulesOnce the SDK is initialized, the appconfig_client can be obtained across other modules as shown below:# **other modules**fromibm_appconfigurationimportAppConfigurationappconfig_client=AppConfiguration.get_instance()feature=appconfig_client.get_feature('online-check-in')enabled=feature.is_enabled()feature_value=feature.get_current_value(entity_id,entity_attributes)Supported Data typesApp Configuration service allows to configure the feature flag and properties in the following data types : Boolean,
Numeric, String. The String data type can be of the format of a TEXT string , JSON or YAML. The SDK processes each
format accordingly as shown in the below table.View TableFeature or Property valueDataTypeDataFormatType of data returnedbyGetCurrentValue()Example outputtrueBOOLEANnot applicablebooltrue25NUMERICnot applicableint25"a string text"STRINGTEXTstringa string text{"firefox": {"name": "Firefox","pref_url": "about:config"}}STRINGJSONDictionary or List of Dictionary{'firefox': {'name': 'Firefox', 'pref_url': 'about:config'}}men:- John Smith- Bill Joneswomen:- Mary Smith- Susan WilliamsSTRINGYAMLDictionary{'men': ['John Smith', 'Bill Jones'], 'women': ['Mary Smith', 'Susan Williams']}Feature flagfeature=appconfig_client.get_feature('json-feature')feature.get_feature_data_type()//STRINGfeature.get_feature_data_format()//JSONfeature.get_current_value(entityId,entityAttributes)//returnssingledictionaryobjectorlistofdictionaryobject//ExampleBelow//inputjson:-[{"role":"developer","description":"do coding"},{"role":"tester","description":"do testing"}]//expectedoutput:-"do coding"tar_val=feature.get_current_value(entityId,entityAttributes)expected_output=tar_val[0]['description']//inputjson:-{"role":"tester","description":"do testing"}//expectedoutput:-"tester"tar_val=feature.get_current_value(entityId,entityAttributes)expected_output=tar_val['role']feature=appconfig_client.getFeature('yaml-feature')feature.get_feature_data_type()//STRINGfeature.get_feature_data_format()//YAMLfeature.get_current_value(entityId,entityAttributes)//returnsdictionaryobject//ExampleBelow//inputyamlstring:-"---\nrole: tester\ndescription: do_testing"//expectedoutput:-"do_testing"tar_val=feature.get_current_value(entityId,entityAttributes)expected_output=tar_val['description']Propertyproperty=appconfig_client.get_property('json-property')property.get_property_data_type()//STRINGproperty.get_property_data_format()//JSONproperty.get_current_value(entityId,entityAttributes)//returnssingledictionaryobjectorlistofdictionaryobject//ExampleBelow//inputjson:-[{"role":"developer","description":"do coding"},{"role":"tester","description":"do testing"}]//expectedoutput:-"do coding"tar_val=property.get_current_value(entityId,entityAttributes)expected_output=tar_val[0]['description']//inputjson:-{"role":"tester","description":"do testing"}//expectedoutput:-"tester"tar_val=property.get_current_value(entityId,entityAttributes)expected_output=tar_val['role']property=appconfig_client.get_property('yaml-property')property.get_property_data_type()//STRINGproperty.get_property_data_format()//YAMLproperty.get_current_value(entityId,entityAttributes)//returnsdictionaryobject//ExampleBelow//inputyamlstring:-"---\nrole: tester\ndescription: do_testing"//expectedoutput:-"do_testing"tar_val=property.get_current_value(entityId,entityAttributes)expected_output=tar_val['description']Set listener for the feature and property data changesThe SDK provides mechanism to notify you in real-time when feature flag's or property's configuration changes. You can
subscribe to configuration changes using the same appconfig_client.defconfiguration_update(self):print('Received updates on configurations')# **add your code**# To find the effect of any configuration changes, you can call the feature or property related methods# feature = appconfig_client.getFeature('online-check-in')# new_value = feature.get_current_value(entity_id, entity_attributes)appconfig_client.register_configuration_update_listener(configuration_update)Fetch latest dataFetch the latest configuration data.appconfig_client.fetch_configurations()Enable debugger (Optional)Use this method to enable/disable the logging in SDK.appconfig_client.enable_debug(True)ExamplesTheexamplesfolder has the examples.LicenseThis project is released under the Apache 2.0 license. The license's full text can be found
inLICENSE