Skip to main content

IBM Cloud App Configuration Python SDK

Project description

IBM Cloud App Configuration Python server SDK

IBM 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 Contents

Overview

IBM Cloud App Configuration is a centralized feature management and configuration service on IBM Cloud for 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.

Installation

To install, use pip or easy_install:

pip install --upgrade ibm-appconfiguration-python-sdk

or

 easy_install --upgrade ibm-appconfiguration-python-sdk

Import the SDK

from ibm_appconfiguration import AppConfiguration, Feature, Property, ConfigurationType

Initialize SDK

app_configuration_client = AppConfiguration.get_instance()
app_configuration_client.init(region='region',
               guid='guid',
               apikey='apikey')

## Initialize configurations 
app_configuration_client.set_context(collection_id='airlines-webapp',
                       environment_id='dev')
  • region : Region name where the service instance is created. Use
    • AppConfiguration.REGION_US_SOUTH for Dallas
    • AppConfiguration.REGION_EU_GB for London
    • AppConfiguration.REGION_AU_SYD for Sydney
  • guid : GUID of the App Configuration service. Get it from the service credentials section of the dashboard
  • apikey : ApiKey of the App Configuration service. Get it from the service credentials section of the dashboard
  • collection_id : Id of the collection created in App Configuration service instance.
  • environment_id : Id of the environment created in App Configuration service instance.

Work offline with local configuration file

SDK can work offline with a local configuration file and perform feature and property related operations.

app_configuration_client.set_context(collection_id='airlines-webapp',
                       environment_id='dev',
                       configuration_file='saflights/flights.json',
                       live_config_update_enabled=False)
  • configuration_file : Path to the JSON file which contains configuration details.
  • live_config_update_enabled : Set this value to false if the new configuration values shouldn't be fetched from the server. Make sure to provide a proper JSON file in the configuration_file path. By default, this value is enabled.

Permissions required by SDK

Add the permission for non-root users for writing permission to use the cache and logs in AppConfiguration SDK. AppConfiguration cache location will be ./ibm_appconfiguration/configurations/internal/utils/appconfiguration.json. The logs.txt file location will be the application root folder.

Get single feature

feature = app_configuration_client.get_feature('online-check-in') # feature can be null incase of an invalid feature id

if feature:
    print(f'Feature Name : {0}'.format(feature.get_feature_name()))
    print(f'Feature Id : {0}'.format(feature.get_feature_id()))
    print(f'Feature Type : {0}'.format(feature.get_feature_data_type()))
    print(f'Feature is enabled : {0}'.format(feature.is_enabled()))

Get all features

features_dictionary = app_configuration_client.get_features()

Evaluate a feature

Use the feature.get_current_value(entity_id=entity_id, entity_attributes=entity_attributes) method to evaluate the value of the feature flag. Pass an unique entityId as the parameter to perform the feature flag evaluation.

Usage

  • If the feature flag is configured with segments in the AppConfiguration service, provide a json object as entityAttributes parameter to this method.

    entity_id = "john_doe"
    entity_attributes = {
      'city': 'Bangalore',
      'country': 'India'
    }
    feature_value = feature.get_current_value(entity_id=entity_id,    entity_attributes=entity_attributes)
    
  • If the feature flag is not targeted to any segments and the feature flag is turned ON this method returns the feature enabled value. And when the feature flag is turned OFF this method returns the feature disabled value.

    entity_id = "john_doe"
    feature_value = feature.get_current_value(entity_id=entity_id)
    

Get single Property

property = app_configuration_client.get_property('check-in-charges') # property can be null incase of an invalid property id
if property:
    print(f'Property Name : {0}'.format(property.get_property_name()))
    print(f'Property Id : {0}'.format(property.get_property_id()))
    print(f'Property Type : {0}'.format(property.get_property_data_type()))

Get all Properties

properties_dictionary = app_configuration_client.get_properties()

Evaluate a property

Use the property.get_current_value(entity_id=entity_id, entity_attributes=entity_attributes) method to evaluate the value of the property. Pass an unique entityId as the parameter to perform the property evaluation.

Usage

  • If the property is configured with segments in the App Configuration service, provide a json object as entityAttributes parameter to this method.

    entity_id = "john_doe"
    entity_attributes = {
        'city': 'Bangalore',
        'country': 'India'
    }
    property_value = property.get_current_value(entity_id=entity_id, entity_attributes=entity_attributes)
    
  • If the property is not targeted to any segments this method returns the property value.

    entity_id = "john_doe"
    property_value = property.get_current_value(entity_id=entity_id)
    

Set listener for the feature and property data changes

To listen to the data changes add the following code in the application.

def configuration_update(self):
    print('Received updates on configurations')

app_configuration_client.register_configuration_update_listener(configuration_update)

Fetch latest data

Fetch the latest configuration data.

app_configuration_client.fetch_configurations()

Enable debugger (Optional)

Use this method to enable/disable the logging in SDK.

app_configuration_client.enable_debug(True)

Examples

The examples folder has the examples.

License

This project is released under the Apache 2.0 license. The license's full text can be found in LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ibm-appconfiguration-python-sdk-0.1.1.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file ibm-appconfiguration-python-sdk-0.1.1.tar.gz.

File metadata

  • Download URL: ibm-appconfiguration-python-sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for ibm-appconfiguration-python-sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9dc1817ca8ccccf5f12202c6618c10a38d9307d52d0f6c392aa586a8683952d0
MD5 df3d7af65a9dfdff872b5308db56b519
BLAKE2b-256 14d66581f00e9af4a29534f44444552ff1b2cd741bf192d5db2f1cb29f5fe71d

See more details on using hashes here.

File details

Details for the file ibm_appconfiguration_python_sdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ibm_appconfiguration_python_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 60.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for ibm_appconfiguration_python_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9393febe2de6ba3b62a6433ad3724ed0e01c9a14f5e99d07d98b235930540db6
MD5 355b7c983c599ac48325cba8e6eacde4
BLAKE2b-256 9661b2f220015469d82bee126ea880499eb0812f335f6e85de837d42009f5b7d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page