-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathOptimizelyUserContext.cs
193 lines (176 loc) · 7.43 KB
/
OptimizelyUserContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright 2020-2021, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using OptimizelySDK.Logger;
using System.Collections.Generic;
using OptimizelySDK.ErrorHandler;
using OptimizelySDK.Entity;
using OptimizelySDK.OptimizelyDecisions;
namespace OptimizelySDK
{
/// <summary>
/// OptimizelyUserContext defines user contexts that the SDK will use to make decisions for
/// </summary>
public class OptimizelyUserContext
{
private ILogger Logger;
private IErrorHandler ErrorHandler;
private object mutex = new object();
// userID for Optimizely user context
private string UserId;
// user attributes for Optimizely user context.
private UserAttributes Attributes;
// Optimizely object to be used.
private Optimizely Optimizely;
public OptimizelyUserContext(Optimizely optimizely, string userId, UserAttributes userAttributes, IErrorHandler errorHandler, ILogger logger)
{
ErrorHandler = errorHandler;
Logger = logger;
Optimizely = optimizely;
Attributes = userAttributes ?? new UserAttributes();
UserId = userId;
}
private OptimizelyUserContext Copy() => new OptimizelyUserContext(Optimizely, UserId, GetAttributes(), ErrorHandler, Logger);
/// <summary>
/// Returns Optimizely instance associated with the UserContext.
/// </summary>
/// <returns> Optimizely instance.</returns>
public Optimizely GetOptimizely()
{
return Optimizely;
}
/// <summary>
/// Returns UserId associated with the UserContext
/// </summary>
/// <returns>UserId of this instance.</returns>
public string GetUserId()
{
return UserId;
}
/// <summary>
/// Returns copy of UserAttributes associated with UserContext.
/// </summary>
/// <returns>copy of UserAttributes.</returns>
public UserAttributes GetAttributes()
{
UserAttributes copiedAttributes = null;
lock(mutex) {
copiedAttributes = new UserAttributes(Attributes);
}
return copiedAttributes;
}
/// <summary>
/// Set an attribute for a given key.
/// </summary>
/// <param name="key">An attribute key</param>
/// <param name="value">value An attribute value</param>
public void SetAttribute(string key, object value)
{
if (key == null)
{
Logger.Log(LogLevel.WARN, "Null attribute key.");
}
else
{
lock (mutex)
{
Attributes[key] = value;
}
}
}
/// <summary>
/// Returns a decision result ({@link OptimizelyDecision}) for a given flag key and a user context, which contains all data required to deliver the flag.
/// <ul>
/// <li>If the SDK finds an error, it’ll return a decision with <b>null</b> for <b>variationKey</b>. The decision will include an error message in <b>reasons</b>.
/// </ul>
/// </summary>
/// <param name="key">A flag key for which a decision will be made.</param>
/// <returns>A decision result.</returns>
public OptimizelyDecision Decide(string key)
{
return Decide(key, new OptimizelyDecideOption[] { });
}
/// <summary>
/// Returns a decision result ({@link OptimizelyDecision}) for a given flag key and a user context, which contains all data required to deliver the flag.
/// <ul>
/// <li>If the SDK finds an error, it’ll return a decision with <b>null</b> for <b>variationKey</b>. The decision will include an error message in <b>reasons</b>.
/// </ul>
/// </summary>
/// <param name="key">A flag key for which a decision will be made.</param>
/// <param name="options">A list of options for decision-making.</param>
/// <returns>A decision result.</returns>
public OptimizelyDecision Decide(string key,
OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.Decide(optimizelyUserContext, key, options);
}
/// <summary>
/// Returns a key-map of decision results for multiple flag keys and a user context.
/// </summary>
/// <param name="keys">list of flag keys for which a decision will be made.</param>
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys, OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.DecideForKeys(optimizelyUserContext, keys, options);
}
/// <summary>
/// Returns a key-map of decision results for multiple flag keys and a user context.
/// </summary>
/// <param name="keys">list of flag keys for which a decision will be made.</param>
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys)
{
return DecideForKeys(keys, new OptimizelyDecideOption[] { });
}
/// <summary>
/// Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys.
/// </summary>
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideAll()
{
return DecideAll(new OptimizelyDecideOption[] { });
}
/// <summary>
/// Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys.
/// </summary>
/// <param name="options">A list of options for decision-making.</param>
/// <returns>All decision results mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.DecideAll(optimizelyUserContext, options);
}
/// <summary>
/// Track an event.
/// </summary>
/// <param name="eventName">The event name.</param>
public void TrackEvent(string eventName)
{
TrackEvent(eventName, new EventTags());
}
/// <summary>
/// Track an event.
/// </summary>
/// <param name="eventName">The event name.</param>
/// <param name="eventTags">A map of event tag names to event tag values.</param>
public void TrackEvent(string eventName,
EventTags eventTags)
{
Optimizely.Track(eventName, UserId, Attributes, eventTags);
}
}
}