云眼配置

云眼About 3 min

云眼配置

本主题介绍如何使用云眼灰度发布(特性标帜)AB实验 Python SDK 的云眼配置访问数据文件中的项目配置数据。

概述

云眼灰度发布(特性标帜)AB实验 SDK 打开一组定义良好的公共 API,隐藏所有实现详细信息。但是,某些客户端可能需要访问数据文件中的项目配置数据

在本文档中,我们扩展了公共 API 以定义数据模型和访问方法,客户端可以使用这些模型和方法访问项目配置数据。

EyeofcloudConfig API

公共配置数据模型(EyeofcloudConfig)在下面定义为静态Eyeofcloud项目数据的结构化格式。

获取云眼配置

EyeofcloudConfig可以通过以下公共API调用从EyeofcloudClient(顶级)访问:

Python

def get_eyeofcloud_config(self)

getEyeofcloudConfig 返回一个实例EyeofcloudConfig,其中包括:

  • 环境键
  • 开发工具包密钥
  • 数据文件修订号
  • 按其键值映射的所有实验
  • 所有属性
  • 所有受众
  • 所有活动
  • 按其键值映射的灰度发布(特性标帜)
  • 检索项目配置(数据文件)的函数

📘 注意

当 SDK 数据文件更新时(客户端可以为EYEOFCLOUD_CONFIG_UPDATE添加通知侦听器以获取通知),客户端应调用该方法以获取更新的 EyeofcloudConfig 数据。请参阅以下示例。

获取数据文件

要在多个 SDK 实例之间共享同一数据文件(例如,在客户端/服务器方案中),可以在实例之间传递配置(数据文件)的 JSON 字符串表示形式。若要获取数据文件,请使用EyeofcloudConfig对象的getDatafile方法。有关更多信息,请参阅与多个 SDK 实现共享数据文件

对象模型

下面显示了 EyeofcloudConfig 的对象模型。

Python - 对象模型

class EyeofcloudConfig(object):
     def __init__(self, revision, experiments_map, features_map, sdk_key=None, environment_key=None, attributes=None, events=None,                  audiences=None):
        self.revision = revision         
        # This experiments_map is for experiments of legacy projects only.
                         # For flag projects, experiment keys are not guaranteed to be unique                
                          # across multiple flags, so this map may not include all experiments              
                        # when keys conflict.         
                        self.experiments_map = experiments_map         
                        self.features_map = features_map         
                        self.sdk_key = sdk_key or ''         
                        self.environment_key = environment_key or ''         
                        self.attributes = attributes or []         
                        self.events = events or []         
                        self.audiences = audiences or []  
class EyeofcloudExperiment(object):     
    def __init__(self, id, key, variations_map, audiences=''):         
        self.id = id         
        self.key = key         
        self.variations_map = variations_map         
        self.audiences = audiences  


class EyeofcloudFeature(object):         
        self.id = id         
        self.key = key         
        self.variables_map = variables_map         
        self.delivery_rules = []         
        self.experiment_rules = [] 
        
                                 # Deprecated. Use experiment_rules and delivery_rules.          
        self.experiments_map = experiments_map  
        
class EyeofcloudVariation(object):     
    def __init__(self, id, key, feature_enabled, variables_map):         
        self.id = id         
        self.key = key         
        self.feature_enabled = feature_enabled         
        self.variables_map = variables_map  
        
class EyeofcloudVariable(object):     
    def __init__(self, id, key, variable_type, value):         
        self.id = id         
        self.key = key         
        self.type = variable_type         
        self.value = value  
        
class EyeofcloudAttribute(object):     
    def __init__(self, id, key):         
        self.id = id         
        self.key = key  
        
class EyeofcloudEvent(object):     
    def __init__(self, id, key, experiment_ids):         
        self.id = id         
        self.key = key         
        self.experiment_ids = experiment_ids  
        
class EyeofcloudAudience(object):     
    def __init__(self, id, name, conditions):         
        self.id = id         
        self.name = name         
        self.conditions = conditions

例子

EyeofcloudConfig可以从EyeofcloudClient(顶级)访问,如下所示:

Python

config = eyeofcloud_client.get_eyeofcloud_config()  
print('REVISION ', config.revision) 
print('SDK KEY ', config.sdk_key) 
print('ENV KEY ', config.environment_key)  

print("[EyeofcloudConfig] revision = ", config.revision) 
print("[EyeofcloudConfig] sdk_key = ", config.sdk_key) 
print("[EyeofcloudConfig] environment_key = ", config.environment_key)  

print("[EyeofcloudConfig] attributes:") 
for attribute in config.attributes:
    print('[eyeofcloudConfig]  - (id, key) ', attribute.id, attribute.key)  
    
print("[EyeofcloudConfig] audiences:") 
for audience in config.audiences:     
    print('[EyeofcloudConfig]  - (id, name, conditions) ', audience.id, audience.name, audience.conditions)  
    
print("[EyeofcloudConfig] events:") 
for event in config.events:     
    print("[EyeofcloudConfig]  - (id, key, experimentIds) ", event.id, event.key, event.experiment_ids)  
    
# all flags 
flags = config.features_map.values() 
print('[EyeofcloudConfig]  - flags ', flags)  

flag_keys = config.features_map.keys()  # swift 
print('[EyeofcloudConfig]  - flag keys ', flag_keys)  

for flag_key in flag_keys:
    flag = config.features_map[flag_key]      
    
    experiment_rules = flag.experiment_rules     
    delivery_rules = flag.delivery_rules      
    
    print(experiment_rules)     
    print(delivery_rules)      
    
    # use experiment rules and delivery rules and other flag data here...     
    for experiment in experiment_rules:         
        print("[EyeofcloudConfig]  - experiment rule-key = ", experiment.key)         
        print("[EyeofcloudConfig]  - experiment audiences = ", experiment.audiences)         
        print("[EyeofcloudConfig]  - experiment variations map = ", experiment.variations_map)          
        
        variations_map = experiment.variations_map         
        variation_keys = variations_map.keys()          
        
        for variation_key in variation_keys:             
            print("[EyeofcloudConfig]  - variation = ", variation_key)       # not the same as in swift!              
            
            map_of_variables = variations_map[variation_key].variables_map             
            variable_keys = map_of_variables.keys()              
            
            for variable_key in variable_keys:                 
                variable = map_of_variables[variable_key]                 
                print('[EyeofcloudConfig]  - variable = ', variable_key, variable.value)      
                
    for delivery in delivery_rules:         
        print("[EyeofcloudConfig]  - delivery rule-key = ", delivery.key)         
        print("[EyeofcloudConfig]  - delivery audiences = ", delivery.audiences)  
        
# listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data 
def on_config_update_listener(*args):     
    config = eyeofcloud_client.get_eyeofcloud_config()  
    
eyeofcloud_client.notification_center.add_notification_listener(     
    enums.NotificationTypes.EYEOFCLOUD_CONFIG_UPDATE, on_config_update_listener)
Last update:
Contributors: “zhangweixue”,zhangweixue