云眼配置 EyeofcloudConfig
About 3 min
云眼配置 EyeofcloudConfig
本主题介绍如何使用 EyeofcloudConfig 访问 Eyeofcloud 功能实验 Swift SDK 的数据文件中的项目配置数据。
概述
云眼灰度发布(特性标帜)AB实验 SDK 打开一组定义良好的公共 API,隐藏所有实现详细信息。但是,某些客户端可能需要访问数据文件中的项目配置数据。
在本文档中,我们扩展了公共 API 以定义数据模型和访问方法,客户端可以使用这些模型和方法访问项目配置数据。
EyeofcloudConfig API
公共配置数据模型(EyeofcloudConfig)在下面定义为静态Eyeofcloud项目数据的结构化格式。
获取云眼配置
EyeofcloudConfig可以通过以下公共API调用从EyeofcloudClient(顶级)访问:
Swift
public func getEyeofcloudConfig() throws -> EyeofcloudConfig
getEyeofcloudConfig
返回一个EyeofcloudConfig
实例,其中包括
- 环境键
- 开发工具包密钥
- 数据文件修订号
- 按其键值映射的所有实验
- 所有属性
- 所有受众
- 所有活动
- 按其键值映射的灰度发布(特性标帜)
📘 注意
当 SDK 数据文件更新时(客户端可以为
EYEOFCLOUD_CONFIG_UPDATE
添加通知侦听器以获取通知),客户端应调用该方法以获取更新的 EyeofcloudConfig 数据。请参阅以下示例。
获取数据文件
要在多个 SDK 实例之间共享同一数据文件(例如,在客户端/服务器方案中),可以在实例之间传递配置(数据文件)的 JSON 字符串表示形式。若要获取数据文件,请使用EyeofcloudConfig
对象的getDatafile
方法。有关更多信息,请参阅与多个 SDK 实现共享数据文件。
对象模型
下面显示了 EyeofcloudConfig 的对象模型。
Swift - 对象模型
public protocol EyeofcloudConfig {
var environmentKey: String { get }
var sdkKey: String { get }
var revision: String { get }
// 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.
var experimentsMap: [String: EyeofcloudExperiment] { get }
var featuresMap: [String: EyeofcloudFeature] { get }
var attributes: [EyeofcloudAttribute] { get }
var audiences: [EyeofcloudAudience] { get }
var events: [EyeofcloudEvent] { get }
}
public protocol EyeofcloudExperiment {
var id: String { get }
var key: String { get }
var audiences: String { get }
var variationsMap: [String: EyeofcloudVariation] { get }
}
public protocol EyeofcloudFeature {
var id: String { get }
var key: String { get }
var experimentRules: [EyeofcloudExperiment] { get }
var deliveryRules: [EyeofcloudExperiment] { get }
var variablesMap: [String: EyeofcloudVariable] { get }
@available(*, deprecated, message: "Use experimentRules and deliveryRules instead")
var experimentsMap: [String: EyeofcloudExperiment] { get }
}
public protocol EyeofcloudVariation {
var id: String { get }
var key: String { get }
var featureEnabled: Bool? { get }
var variablesMap: [String: EyeofcloudVariable] { get }
}
public protocol EyeofcloudVariable {
var id: String { get }
var key: String { get }
var type: String { get }
var value: String { get }
}
public protocol EyeofcloudAttribute {
var id: String { get }
var key: String { get }
}
public protocol EyeofcloudAudience {
var id: String { get }
var name: String { get }
var conditions: String { get }
}
public protocol EyeofcloudEvent {
var id: String { get }
var key: String { get }
var experimentIds: [String] { get }
}
例子
可以从 EyeofcloudClient(顶级)访问 EyeofcloudConfig,如下所示:
Swift
let config = try! eyeofcloudClient.getEyeofcloudConfig()
print("[EyeofcloudConfig] revision = \(config.revision)")
print("[EyeofcloudConfig] sdkKey = \(config.sdkKey)")
print("[EyeofcloudConfig] environmentKey = \(config.environmentKey)")
print("[EyeofcloudConfig] attributes:")
config.attributes.forEach { attribute in
print("[EyeofcloudAttribute] -- (id, key) = (\(attribute.id), \(attribute.key))")
}
print("[EyeofcloudConfig] audiences:")
config.audiences.forEach { audience in
print("[EyeofcloudAudience] -- (id, name, conditions) = (\(audience.id), \(audience.name), \(audience.conditions))")
}
print("[EyeofcloudConfig] events:")
config.events.forEach { event in
print("[EyeofcloudEvent] -- (id, key, experimentIds) = (\(event.id), \(event.key), \(event.experimentIds))")
}
// all flags
let flags = config.featuresMap.values
let flagKeys = config.featuresMap.keys
for flagKey in flagKeys {
let flag = config.featuresMap[flagKey]!
let experimentRules = flag.experimentRules
let deliveryRules = flag.deliveryRules
// use experiment rules and delivery rules and other flag data here...
experimentRules.forEach { experiment in
print("[EyeofcloudExperiment] - experiment rule-key = \(experiment.key)")
print("[EyeofcloudExperiment] - experiment audiences = \(experiment.audiences)")
let variationsMap = experiment.variationsMap
let variationKeys = variationsMap.keys
variationKeys.forEach { varKey in
let variation = variationsMap[varKey]!
print("[EyeofcloudVariation] -- variation = { key: \(varKey), id: \(variation.id), featureEnabled: \(String(describing: variation.featureEnabled)) }")
let variablesMap = variationsMap[varKey]!.variablesMap
let variableKeys = variablesMap.keys
variableKeys.forEach { variableKey in
let variable = variablesMap[variableKey]!
print("[EyeofcloudVariable] --- variable: \(variableKey), \(variable)")
}
}
}
deliveryRules.forEach { delivery in
print("[EyeofcloudExperiment] - delivery rule-key = \(delivery.key)")
print("[EyeofcloudExperiment] - delivery audiences = \(delivery.audiences)")
// use delivery rule data here...
}
}
// listen to NotificationType.datafileChange to get updated data
_ = eyeofcloudClient.notificationCenter?.addDatafileChangeNotificationListener { _ in
if let newOptConfig = try? self.eyeofcloud.getEyeofcloudConfig() {
print("[EyeofcloudConfig] revision = \(newOptConfig.revision)")
}
}