云眼配置 EyeofcloudConfig

云眼About 4 min

云眼配置 EyeofcloudConfig

本主题介绍如何使用 EyeofcloudConfig 访问数据文件中的项目配置数据。

概述

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

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

EyeofcloudConfig API

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

获取云眼配置

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

Java

public EyeofcloudConfig getEyeofcloudConfig();

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

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

📘 注意

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

获取数据文件

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

对象模型

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

Java

// EyeofcloudConfig is an object describing the current project configuration data
public class EyeofcloudConfig {        
    
    // This experimentsMap 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.     
    Map<String, EyeofcloudExperiment> experimentsMap;        
    
    Map<String, EyeofcloudFeature> featuresMap;     
    List<EyeofcloudAttribute> attributes;     
    List<EyeofcloudEvent> events;     
    List<EyeofcloudAudience> audiences;     
    String revision;     
    String sdkKey;     
    String environmentKey; 
}  

// EyeofcloudFeature is an object describing a feature flag 
public class EyeofcloudFeature {     
    String id;     
    String key;        
    
    /**      
    * @deprecated use {@link #experimentRules} and {@link #deliveryRules} instead      
    */     
    @Deprecated     
    Map<String, EyeofcloudExperiment> experimentsMap;        
    
    Map<String, EyeofcloudVariable> variablesMap;     
    List<EyeofcloudExperiment> experimentRules;     
    List<EyeofcloudExperiment> deliveryRules; 
}  
    // EyeofcloudExperiment is an object describing an experiment 
    public class EyeofcloudExperiment {     
        String id;     
        String key;     
        String audiences;     
        Map<String, EyeofcloudVariation> variationsMap; 
    }  
        
        // EyeofcloudVariation is an object describing a variation 
        public class EyeofcloudVariation {     
            String id;     
            String key;     
            Boolean featureEnabled;     
            Map<String, EyeofcloudVariable> variablesMap; 
        }  
        
        // EyeofcloudVariable is an object describing a Variable 
        public class EyeofcloudVariable {     
            String id;     
            String key;     
            String type;     
            String value; 
        }  
        
        // EyeofcloudAttribute is an object describing an Attribute 
        public class EyeofcloudAttribute {   
            String id;   
            String key; 
        }  
        
        // EyeofcloudAudience is an object describing an Audience 
        public class EyeofcloudAudience {   
            String id;   
            String name;   
            String conditions; 
        }  
        
        // EyeofcloudEvent is an object describing an Event 
        public class EyeofcloudEvent {   
            String id;   
            String key;   
            List<String> experimentIds; 
        }

例子

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

Kotlin

val config = eyeofcloudClient.eyeofcloudConfig              
Log.d("Eyeofcloud", "[EyeofcloudConfig] revision = " + config!!.revision) 
Log.d("Eyeofcloud", "[EyeofcloudConfig] sdkKey = " + config.sdkKey) 
Log.d("Eyeofcloud", "[EyeofcloudConfig] environmentKey = " + config.environmentKey)  

Log.d("Eyeofcloud", "[EyeofcloudConfig] attributes:") 
for (attribute in config.attributes) {     
    Log.d("Eyeofcloud", "[EyeofcloudAttribute]  -- (id, key) = " + attribute.id + ", " + attribute.key) 
    }              
    
Log.d("Eyeofcloud", "[EyeofcloudConfig] audiences:") 
for (audience in config.audiences) {     
    Log.d("Eyeofcloud", "[EyeofcloudAudience]  -- (id, name, conditions) = " + audience.id + ", " + audience.name + ", " + audience.conditions) 
    }              
    
Log.d("Eyeofcloud", "[EyeofcloudConfig] events:") 
for (event in config.events) {     
    Log.d("Eyeofcloud", "[EyeofcloudEvent]  -- (id, key, experimentIds) = " + event.id + ", " + event.key + ", " + Arrays.toString(event.experimentIds.toTypedArray())) 
    }  
    
// all features 
for (flagKey in config.featuresMap.keys) {     
    val flag = config.featuresMap.get(flagKey)!!                      
    
    for (experiment in flag.experimentRules) {         
        Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Experiment Rule Key: " + experiment.key)         
        Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Experiment Audiences: " + experiment.audiences)           
        
        val variationsMap = experiment.variationsMap        
        for (variationKey in variationsMap.keys) {             
            val variation = variationsMap.get(variationKey)!!             
            Log.d("Eyeofcloud", "[EyeofcloudVariation]    -- variation = { key: " + variationKey + ", id: " + variation.id + ", featureEnabled: " + variation.featureEnabled + " }")             
            // use variation data here...                                      
            
            val eyeofcloudVariableMap = variation.variablesMap             
            for (variableKey in eyeofcloudVariableMap.keys) {                 
                val variable = eyeofcloudVariableMap.get(variableKey)!!                 
                Log.d("Eyeofcloud", "[EyeofcloudVariable]      -- variable = key: " + variableKey + ", value: " + variable.value)                 
                // use variable data here...                                          
                
                
            }         
        }     
    }      
    
    for (delivery in flag.deliveryRules) {         
        Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Delivery Rule Key: " + delivery.key)         
        Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Delivery Audiences: " + delivery.audiences)     
        }      
        
        // use experiments and other feature flag data here...  
        
}  

// listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data 
eyeofcloudClient.notificationCenter?.addNotificationHandler(     
    UpdateConfigNotification::class.java,     
    NotificationHandler { handler: UpdateConfigNotification? ->         
        val newConfig = eyeofcloudClient.eyeofcloudConfig     
    } 
)

Java

EyeofcloudConfig config = eyeofcloudClient.getEyeofcloudConfig();  

Log.d("Eyeofcloud", "[EyeofcloudConfig] revision = " + config.getRevision()); 
Log.d("Eyeofcloud", "[EyeofcloudConfig] sdkKey = " + config.getSdkKey()); 
Log.d("Eyeofcloud", "[EyeofcloudConfig] environmentKey = " + config.getEnvironmentKey());  

Log.d("Eyeofcloud", "[EyeofcloudConfig] attributes:"); 
for (EyeofcloudAttribute attribute: config.getAttributes()) {   
    Log.d("Eyeofcloud", "[EyeofcloudAttribute]  -- (id, key) = " + attribute.getId() + ", " + attribute.getKey()); 
}  

Log.d("Eyeofcloud", "[EyeofcloudConfig] audiences:"); 
for (EyeofcloudAudience audience: config.getAudiences()) {   
    Log.d("Eyeofcloud", "[EyeofcloudAudience]  -- (id, name, conditions) = " + audience.getId() + ", " +  audience.getName() + ", " + audience.getConditions()); 
}  

Log.d("Eyeofcloud", "[EyeofcloudConfig] events:"); 
for (EyeofcloudEvent event: config.getEvents()) {   
    Log.d("Eyeofcloud", "[EyeofcloudEvent]  -- (id, key, experimentIds) = " + event.getId() + ", "  + event.getKey() + ", "  + Arrays.toString(event.getExperimentIds().toArray())); 
}  

// all features 
for (String flagKey: config.getFeaturesMap().keySet()) {   
    EyeofcloudFeature flag = config.getFeaturesMap().get(flagKey);    
    
    for (EyeofcloudExperiment experiment: flag.getExperimentRules()) {     
        Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Experiment Rule Key: " + experiment.getKey());     
        Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Experiment Audiences: " + experiment.getAudiences());      
        
        Map<String, EyeofcloudVariation> variationsMap = experiment.getVariationsMap();     
        for (String variationKey: variationsMap.keySet()) {       
            EyeofcloudVariation variation = variationsMap.get(variationKey);       
            Log.d("Eyeofcloud", "[EyeofcloudVariation]    -- variation = { key: " + variationKey + ", id: " + variation.getId() + ", featureEnabled: " + variation.getFeatureEnabled() + " }");       
            // use variation data here...        
            
            Map<String, EyeofcloudVariable> eyeofcloudVariableMap = variation.getVariablesMap();
            for (String variableKey: eyeofcloudVariableMap.keySet()) {         
                EyeofcloudVariable variable = eyeofcloudVariableMap.get(variableKey);         
                Log.d("Eyeofcloud", "[EyeofcloudVariable]      -- variable = key: " + variableKey + ", value: " + variable.getValue());         
                // use variable data here...        
                }     
            }    
        }    
        
        for (EyeofcloudExperiment delivery: flag.getDeliveryRules()) {     
            Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Delivery Rule Key: " + delivery.getKey());     
            Log.d("Eyeofcloud", "[EyeofcloudExperiment]  -- Delivery Audiences: " + delivery.getAudiences());   
        }    
    // use experiments and other feature flag data here...  
}  

// listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data 
eyeofcloudClient.getNotificationCenter().addNotificationHandler(UpdateConfigNotification.class, handler -> {   
    EyeofcloudConfig newConfig = eyeofcloudClient.getEyeofcloudConfig(); 
});
Last update:
Contributors: zhangweixue,“zhangweixue”