云眼配置

云眼About 4 min

云眼配置

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

概述

全栈 SDK 打开一组定义明确的公共 API,隐藏所有实现详细信息。但是,某些客户端可能需要访问数据文件中的项目配置数据

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

EyeofcloudConfig API

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

获取云眼配置

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

C#

public EyeofcloudConfig GetEyeofcloudConfig()

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

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

📘 注意

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

获取数据文件

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

对象模型

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

C# - 对象模型

// EyeofcloudConfig is class describing the current project configuration data being used by this SDK instance.  
public class EyeofcloudConfig  
{    
    public string Revision { get; private set; }    
    public string SDKKey { get; private set; }    
    public string EnvironmentKey { get; private set; }    
    public EyeofcloudEvent[] Events { get; private set; }    
    public EyeofcloudAudience[] Audiences { get; private set; }    
    public EyeofcloudAttribute[] Attributes { get; private set; }        
    
    // 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.    
    public IDictionary<string, EyeofcloudExperiment> ExperimentsMap { get; private set; }        
    
    public IDictionary<string, EyeofcloudFeature> FeaturesMap { get; private set; }    
    public string GetDatafile { get; private set; }  
}  

// Entity.IdKeyEntity is an abstract class used for inheritance in EyeofcloudExperiment, EyeofcloudFeature, EyeofcloudVariation and EyeofcloudVariable classes. 
public abstract class IdKeyEntity : Entity, IEquatable<object> 
{   
    public string Id { get; set; }   
    public string Key { get; set; } 
}  

// EyeofcloudAttribute is a class describing attribute and inherited from Entity.IdKeyEntity. 
public class EyeofcloudAttribute : Entity.IdKeyEntity 
{     
}  

// EyeofcloudAudience is a class describing an audience. 
public class EyeofcloudAudience 
{   
    public string Id { get; set; }   
    public string Name { get; set; }   
    public object Conditions { get; set; } 
}  

// EyeofcloudEvent is a class describing an event and inherited from Entity.IdKeyEntity. 
public class EyeofcloudEvent : Entity.IdKeyEntity 
{   
    public string[] ExperimentIds { get; set; } 
}  

// EyeofcloudFeature is a class describing a feature flag and inherited from Entity.IdKeyEntity. 
public class EyeofcloudFeature : Entity.IdKeyEntity 
{      
    
    public List<EyeofcloudExperiment> ExperimentRules { get; private set; }   
    public List<EyeofcloudExperiment> DeliveryRules { get; private set; }   
    public IDictionary<string, EyeofcloudVariable> VariablesMap { get; private set; }      
    
    [Obsolete("Use experimentRules and deliveryRules.")]   
    public IDictionary<string, EyeofcloudExperiment> ExperimentsMap { get; private set; } 
}  

// EyeofcloudExperiment is a class describing an experiment and inherited from Entity.IdKeyEntity. 
public class EyeofcloudExperiment : Entity.IdKeyEntity 
{   
    public IDictionary<string, EyeofcloudVariation> VariationsMap { get; private set; }   
    public string Audiences { get; private set; } 
}  

// EyeofcloudVariation is a class describing a variation in an experiment and inherited from Entity.IdKeyEntity. 
public class EyeofcloudVariation : Entity.IdKeyEntity 
{   
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]   
    public bool? FeatureEnabled { get; private set; }   
    public IDictionary<string, EyeofcloudVariable> VariablesMap { get; private set; } 
}  

// EyeofcloudVariable is a class describing a feature flag variable and inherited from Entity.IdKeyEntity. 
public class EyeofcloudVariable : Entity.IdKeyEntity 
{   
    public string Type { get; private set; }   
    public string Value { get; private set; } 
}

例子

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

C#

var eyeofcloudConfig = eyeofcloud.GetEyeofcloudConfig();  

// get the revision 
Console.WriteLine("[EyeofcloudConfig] revision:" + eyeofcloudConfig.Revision);  

// get the SDK key 
Console.WriteLine("[EyeofcloudConfig] SDKKey:" + eyeofcloudConfig.SDKKey);  

// get the environment key 
Console.WriteLine("[EyeofcloudConfig] environmentKey:" + eyeofcloudConfig.EnvironmentKey);  

// all attributes 
Console.WriteLine("[EyeofcloudConfig] attributes:"); 
var attributes = eyeofcloudConfig.Attributes; 
foreach(var attribute in attributes) 
{   
    Console.WriteLine(
        "[EyeofcloudAttribute]   -- (id, key) = ((" + attribute.id +"), ("+ attribute.key + "))"); 
}  

// all audiences 
Console.WriteLine("[EyeofcloudConfig] audiences:"); 
var audiences = eyeofcloudConfig.Audiences; 
foreach(var audience in audiences) 
{   
    Console.WriteLine(
        "[EyeofcloudAudience]   -- (id, name, conditions) = ((" + audience.Id +"), ("+ audience.Name + "), (" + audience.Conditions + "))");   
        
        // use audience data here. 
}  

// all events 
Console.WriteLine("[EyeofcloudConfig] events:"); 
var events = eyeofcloudConfig.Events; 
foreach(var _event in events) 
{   
    Console.WriteLine(
        "[EyeofcloudEvent]   -- (id, key, experimentIds) = ((" + _event.Id + "), (" + _event.Key + "), (" + _event.ExperimentIds + "))");   
        
        // use event data here. 
}  

// all flags 

var flags = eyeofcloudConfig.FeaturesMap.Values; 
foreach (var flag in flags) 
{   
    // use experiment rules and delivery rules and other flag data here...    
    var experimentRules = flag.ExperimentRules;   
    foreach (var experimentRule in experimentRules)   
    {     
        Console.WriteLine("[EyeofcloudExperiment]   - experiment rule-key = " + experimentRule.Key);     
        Console.WriteLine("[EyeofcloudExperiment]   - experiment audiences = " + experimentRule.Audiences);     
        
        // all variations     
        var variations = experimentRule.VariationsMap.Values;     
        foreach (var variation in variations)     
        {       
            Console.WriteLine(
                "[EyeofcloudVariation]       -- variation = { key: " + variation.Key + ", id: " + variation.Id + ", featureEnabled: " + variation.FeatureEnabled + " }");       
                
            var variables = variation.VariablesMap.Values;       
            foreach (var variable in variables)       
            {         
                Console.WriteLine(
                    "[EyeofcloudVariable]           --- variable: " + variable.Key + ", " + variable);         
                    
                // use variable data here.       
            }       
            
            // use experimentRule data here.     
        }      
        
        var deliveryRules = flag.DeliveryRules;     
        foreach (var deliveryRule in deliveryRules)     
        {       
            Console.WriteLine("[EyeofcloudExperiment]   - delivery rule-key = " + deliveryRule.Key);       
            Console.WriteLine(
                "[EyeofcloudExperiment]   - delivery audiences = " + deliveryRule.Audiences);        
                
                // use delivery rule data here...     
        }  
    }  
    
    // listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data 
    NotificationCenter.EyeofcloudConfigUpdateCallback configUpdateListener = () => 
    {    
        var eyeofcloudConfig = eyeofcloud.GetEyeofcloudConfig();   
        Console.WriteLine("[EyeofcloudConfig] revision = "+ eyeofcloudConfig?.revision);  
    };  
    eyeofcloud.NotificationCenter.AddNotification(
        NotificationCenter.NotificationType.EyeofcloudConfigUpdate, configUpdateListener);
}
Last update:
Contributors: zhangweixue,“zhangweixue”