云眼配置

云眼About 3 min

云眼配置

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

概述

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

扩展公共API以定义数据模型和访问方法,客户端可以使用这些模型和方法访问项目配置数据。

版本

1.0.0-beta 或更高版本

EyeofcloudConfig API

公共配置数据模型 (EyeofcloudConfig) 是静态 Eyeofcloud 功能实验项目数据的结构化格式。

获取云眼配置

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

Dart

Future<EyeofcloudConfigResponse> getEyeofcloudConfig();

getEyeofcloudConfig 返回一个包含实例eyeofcloudConfigEyeofcloudConfigResponse,其中eyeofcloudConfig包括:

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

📘 注意

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

获取数据文件

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

对象模型

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

Dart

// EyeofcloudConfig is an object describing the current project configuration data 
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;   
  String? datafile; 
}  

// EyeofcloudFeature is an object describing a feature flag 
class EyeofcloudFeature {   
  final String? id;   
  final String? key;   
  final List<EyeofcloudExperiment> deliveryRules;   
  final List<EyeofcloudExperiment> experimentRules; 
}  

// EyeofcloudExperiment is an object describing an experiment 
class EyeofcloudExperiment {   
  final String? id;   
  final String? key;   
  final String audiences;   
  final Map<String, EyeofcloudVariation> variationsMap; 
}  

// EyeofcloudVariation is an object describing a variation 
class EyeofcloudVariation {   
  final String? id;   
  final String? key;   
  final bool featureEnabled;   
  final Map<String, EyeofcloudVariable> variablesMap; 
}  

// EyeofcloudVariable is an object describing a Variable 
class EyeofcloudVariable {   
  final String? id;   
  final String? key;   
  final String? type;   
  final String? value; 
}  

// EyeofcloudAttribute is an object describing an Attribute 
class EyeofcloudAttribute {   
  final String? id;   
  final String? key; 
}  

// EyeofcloudAudience is an object describing an Audience 
class EyeofcloudAudience {   
  final String? id;   
  final String? name;   
  final String? conditions; 
}  

// EyeofcloudEvent is an object describing an Event 
class EyeofcloudEvent {   
  final String? id;   
  final String? key;   
  final List<String> experimentIds; 
}

例子

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

Dart

var configResponse = await flutterSDK.getEyeofcloudConfig();   
var eyeofcloudConfig = configResponse.eyeofcloudConfig!;    

print("Eyeofcloud [EyeofcloudConfig] revision = ${eyeofcloudConfig.revision}");   
print("Eyeofcloud [EyeofcloudConfig] sdkKey = ${eyeofcloudConfig.sdkKey}");   
print("Eyeofcloud [EyeofcloudConfig] environmentKey = ${eyeofcloudConfig.environmentKey}");    

print("Eyeofcloud [EyeofcloudConfig] attributes = ${eyeofcloudConfig.attributes}");   
var attributes = eyeofcloudConfig.attributes;   
for (var attribute in attributes) {     
  print("Eyeofcloud [EyeofcloudAttribute]  -- (id, key) = ${attribute.id}, ${attribute.key}");   
}    

print("Eyeofcloud [EyeofcloudConfig] audiences = ${eyeofcloudConfig.audiences}");   
var audiences = eyeofcloudConfig.audiences;   
for (var audience in audiences) {     
  print("Eyeofcloud [EyeofcloudAudience]  -- (id, key, conditions) = ${audience.id}, ${audience.name}, ${audience.conditions.toString()}");   
}    

print("Eyeofcloud [EyeofcloudConfig] events = ${eyeofcloudConfig.events}");   
var events = eyeofcloudConfig.events;   
for (var event in events) {     
  print("Eyeofcloud [EyeofcloudEvent]  -- (id, key, experimentIds) = ${event.id}, ${event.key}, ${event.experimentIds.toString()}");   
}  

// all features   
  var featuresMap = eyeofcloudConfig.featuresMap;   
  for (var flagKey in featuresMap.keys) {    
    var flag = featuresMap[flagKey];     
    var experimentRules = flag?.experimentRules;     
    for (var experiment in experimentRules!) {       
      print("Eyeofcloud [EyeofcloudExperiment]  -- Experiment Rule Key: ${experiment.key}");       
      print("Eyeofcloud [EyeofcloudExperiment]  -- Experiment Audiences: ${experiment.audiences}");        
      
      var variationsMap = experiment.variationsMap;       
      for (var variationKey in variationsMap.keys) {         
        var variation = variationsMap[variationKey];         
        print("Eyeofcloud [EyeofcloudVariation]    -- variation = { key: ${variationKey}, id: ${variation?.id}, featureEnabled: ${variation?.featureEnabled}");         
        // use variation data here...          
        
        var eyeofcloudVariableMap = variation?.variablesMap;         
        for (var variableKey in eyeofcloudVariableMap!.keys) {           
          var variable = eyeofcloudVariableMap[variableKey];           
          print("Eyeofcloud [EyeofcloudVariable]      -- variable = key: ${variableKey}, value: ${variable!.value}");           
          // use variable data here...

        }       
     }     
   }   
  var deliveryRules = flag!.deliveryRules;   
  for (var delivery in deliveryRules) {     
    print("Eyeofcloud [EyeofcloudExperiment]  -- Delivery Rule Key: ${delivery.key}");     
    print("Eyeofcloud [EyeofcloudExperiment]  -- Delivery Audiences: ${delivery.audiences.toString()}");   
  }  
  
// use experiments and other feature flag data here... 
}  

// listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data 
flutterSDK.addUpdateConfigNotificationListener((msg) {   
  var newConfig = flutterSDK.getEyeofcloudConfig(); 
});
Last update:
Contributors: zhangweixue,“zhangweixue”