-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHttpProjectConfigManager.cs
457 lines (377 loc) · 15.2 KB
/
HttpProjectConfigManager.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Copyright 2019-2021, 2023 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
*
* https://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.
*/
#if !(NET35 || NET40 || NETSTANDARD1_6)
#define USE_ODP
#endif
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using OptimizelySDK.ErrorHandler;
using OptimizelySDK.Logger;
using OptimizelySDK.Notifications;
namespace OptimizelySDK.Config
{
public class HttpProjectConfigManager : PollingProjectConfigManager
{
private string Url;
internal string LastModifiedSince = string.Empty;
private string DatafileAccessToken = string.Empty;
private HttpProjectConfigManager(TimeSpan period, string url, TimeSpan blockingTimeout,
bool autoUpdate, ILogger logger, IErrorHandler errorHandler, string sdkKey
)
: base(period, blockingTimeout, autoUpdate, logger, errorHandler)
{
Url = url;
SdkKey = sdkKey;
}
private HttpProjectConfigManager(TimeSpan period, string url, TimeSpan blockingTimeout,
bool autoUpdate, ILogger logger, IErrorHandler errorHandler, string datafileAccessToken,
string sdkKey
)
: this(period, url, blockingTimeout, autoUpdate, logger, errorHandler, sdkKey)
{
DatafileAccessToken = datafileAccessToken;
}
/// <summary>
/// SDK key in use for this project
/// </summary>
public override string SdkKey { get; }
public Task OnReady()
{
return CompletableConfigManager.Task;
}
#if !NET40 && !NET35
// HttpClient wrapper class which can be used to mock HttpClient for unit testing.
public class HttpClient
{
private System.Net.Http.HttpClient Client;
public HttpClient()
{
Client = new System.Net.Http.HttpClient(GetHttpClientHandler());
}
public HttpClient(System.Net.Http.HttpClient httpClient) : this()
{
if (httpClient != null)
{
Client = httpClient;
}
}
public static System.Net.Http.HttpClientHandler GetHttpClientHandler()
{
var handler = new System.Net.Http.HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip |
System.Net.DecompressionMethods.Deflate,
};
return handler;
}
public virtual Task<System.Net.Http.HttpResponseMessage> SendAsync(
System.Net.Http.HttpRequestMessage httpRequestMessage
)
{
return Client.SendAsync(httpRequestMessage);
}
}
private static HttpClient Client;
static HttpProjectConfigManager()
{
Client = new HttpClient();
}
private string GetRemoteDatafileResponse()
{
Logger.Log(LogLevel.DEBUG, $"Making datafile request to url \"{Url}\"");
var request = new System.Net.Http.HttpRequestMessage
{
RequestUri = new Uri(Url),
Method = System.Net.Http.HttpMethod.Get,
};
// Send If-Modified-Since header if Last-Modified-Since header contains any value.
if (!string.IsNullOrEmpty(LastModifiedSince))
{
request.Headers.Add("If-Modified-Since", LastModifiedSince);
Logger.Log(LogLevel.DEBUG,
$"Set If-Modified-Since in request header: {LastModifiedSince}");
}
if (!string.IsNullOrEmpty(DatafileAccessToken))
{
request.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",
DatafileAccessToken);
}
var httpResponse = Client.SendAsync(request);
httpResponse.Wait();
// Return from here if datafile is not modified.
var result = httpResponse.Result;
if (result.StatusCode == HttpStatusCode.NotModified)
{
return null;
}
if (!result.IsSuccessStatusCode)
{
Logger.Log(LogLevel.ERROR, $"Error fetching datafile \"{result.StatusCode}\"");
return null;
}
// Update Last-Modified header if provided.
if (result.Content.Headers.LastModified.HasValue)
{
LastModifiedSince = result.Content.Headers.LastModified?.UtcDateTime.ToString("r");
Logger.Log(LogLevel.DEBUG,
$"Set LastModifiedSince from response header: {LastModifiedSince}");
}
var content = result.Content.ReadAsStringAsync();
content.Wait();
return content.Result;
}
#elif NET40
private string GetRemoteDatafileResponse()
{
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);
// Send If-Modified-Since header if Last-Modified-Since header contains any value.
if (!string.IsNullOrEmpty(LastModifiedSince))
{
request.Headers.Add("If-Modified-Since", LastModifiedSince);
}
var result = (System.Net.HttpWebResponse)request.GetResponse();
if (result.StatusCode != System.Net.HttpStatusCode.OK)
{
Logger.Log(LogLevel.ERROR, $"Error fetching datafile \"{result.StatusCode}\"");
}
var lastModified = result.Headers.GetValues("Last-Modified");
if (!string.IsNullOrEmpty(lastModified?.First()))
{
LastModifiedSince = lastModified.First();
}
var encoding = System.Text.Encoding.ASCII;
using (var reader = new System.IO.StreamReader(result.GetResponseStream(), encoding))
{
var responseText = reader.ReadToEnd();
return responseText;
}
}
#else
private string GetRemoteDatafileResponse()
{
return null;
}
#endif
protected override ProjectConfig Poll()
{
var datafile = GetRemoteDatafileResponse();
if (datafile == null)
{
return null;
}
return DatafileProjectConfig.Create(datafile, Logger, ErrorHandler);
}
public class Builder
{
private const long MAX_MILLISECONDS_LIMIT = 4294967294;
private readonly TimeSpan DEFAULT_PERIOD = TimeSpan.FromMinutes(5);
private readonly TimeSpan DEFAULT_BLOCKINGOUT_PERIOD = TimeSpan.FromSeconds(15);
private readonly string DEFAULT_FORMAT =
"https://cdn.optimizely.com/datafiles/{0}.json";
private readonly string DEFAULT_AUTHENTICATED_DATAFILE_FORMAT =
"https://config.optimizely.com/datafiles/auth/{0}.json";
private string Datafile;
private string DatafileAccessToken;
private string SdkKey;
private string Url;
private string Format;
private ILogger Logger;
private IErrorHandler ErrorHandler;
private TimeSpan Period;
private TimeSpan BlockingTimeoutSpan;
private bool AutoUpdate = true;
private bool StartByDefault = true;
private NotificationCenter NotificationCenter;
private bool IsBlockingTimeoutProvided = false;
private bool IsPollingIntervalProvided = false;
public Builder WithBlockingTimeoutPeriod(TimeSpan blockingTimeoutSpan)
{
IsBlockingTimeoutProvided = true;
BlockingTimeoutSpan = blockingTimeoutSpan;
return this;
}
public Builder WithDatafile(string datafile)
{
Datafile = datafile;
return this;
}
public Builder WithSdkKey(string sdkKey)
{
SdkKey = sdkKey;
return this;
}
#if !NET40 && !NET35
public Builder WithAccessToken(string accessToken)
{
DatafileAccessToken = accessToken;
return this;
}
#endif
public Builder WithUrl(string url)
{
Url = url;
return this;
}
public Builder WithPollingInterval(TimeSpan period)
{
IsPollingIntervalProvided = true;
Period = period;
return this;
}
public Builder WithFormat(string format)
{
Format = format;
return this;
}
public Builder WithLogger(ILogger logger)
{
Logger = logger;
return this;
}
public Builder WithErrorHandler(IErrorHandler errorHandler)
{
ErrorHandler = errorHandler;
return this;
}
public Builder WithAutoUpdate(bool autoUpdate)
{
AutoUpdate = autoUpdate;
return this;
}
public Builder WithStartByDefault(bool startByDefault = true)
{
StartByDefault = startByDefault;
return this;
}
public Builder WithNotificationCenter(NotificationCenter notificationCenter)
{
NotificationCenter = notificationCenter;
return this;
}
/// <summary>
/// HttpProjectConfigManager.Builder that builds and starts a HttpProjectConfigManager.
/// This is the default builder which will block until a config is available.
/// </summary>
/// <returns>HttpProjectConfigManager instance</returns>
public HttpProjectConfigManager Build()
{
return Build(false);
}
/// <summary>
/// HttpProjectConfigManager.Builder that builds and starts a HttpProjectConfigManager.
/// </summary>
/// <param name="defer">When true, we will not wait for the configuration to be available
/// before returning the HttpProjectConfigManager instance.</param>
/// <returns>HttpProjectConfigManager instance</returns>
public HttpProjectConfigManager Build(bool defer)
{
HttpProjectConfigManager configManager = null;
if (Logger == null)
{
Logger = new DefaultLogger();
}
if (ErrorHandler == null)
{
ErrorHandler = new DefaultErrorHandler(Logger, false);
}
if (string.IsNullOrEmpty(Format))
{
if (string.IsNullOrEmpty(DatafileAccessToken))
{
Format = DEFAULT_FORMAT;
}
else
{
Format = DEFAULT_AUTHENTICATED_DATAFILE_FORMAT;
}
}
if (string.IsNullOrEmpty(Url))
{
if (string.IsNullOrEmpty(SdkKey))
{
ErrorHandler.HandleError(new Exception("SdkKey cannot be null"));
}
Url = string.Format(Format, SdkKey);
}
if (IsPollingIntervalProvided && (Period.TotalMilliseconds <= 0 ||
Period.TotalMilliseconds >
MAX_MILLISECONDS_LIMIT))
{
Logger.Log(LogLevel.DEBUG,
$"Polling interval is not valid for periodic calls, using default period {DEFAULT_PERIOD.TotalMilliseconds}ms");
Period = DEFAULT_PERIOD;
}
else if (!IsPollingIntervalProvided)
{
Logger.Log(LogLevel.DEBUG,
$"No polling interval provided, using default period {DEFAULT_PERIOD.TotalMilliseconds}ms");
Period = DEFAULT_PERIOD;
}
if (IsBlockingTimeoutProvided && (BlockingTimeoutSpan.TotalMilliseconds <= 0 ||
BlockingTimeoutSpan.TotalMilliseconds >
MAX_MILLISECONDS_LIMIT))
{
Logger.Log(LogLevel.DEBUG,
$"Blocking timeout is not valid, using default blocking timeout {DEFAULT_BLOCKINGOUT_PERIOD.TotalMilliseconds}ms");
BlockingTimeoutSpan = DEFAULT_BLOCKINGOUT_PERIOD;
}
else if (!IsBlockingTimeoutProvided)
{
Logger.Log(LogLevel.DEBUG,
$"No Blocking timeout provided, using default blocking timeout {DEFAULT_BLOCKINGOUT_PERIOD.TotalMilliseconds}ms");
BlockingTimeoutSpan = DEFAULT_BLOCKINGOUT_PERIOD;
}
configManager = new HttpProjectConfigManager(Period, Url, BlockingTimeoutSpan,
AutoUpdate, Logger, ErrorHandler, DatafileAccessToken, SdkKey);
if (Datafile != null)
{
try
{
var config = DatafileProjectConfig.Create(Datafile, Logger, ErrorHandler);
configManager.SetConfig(config);
}
catch (Exception ex)
{
Logger.Log(LogLevel.WARN, "Error parsing fallback datafile." + ex.Message);
}
}
configManager.NotifyOnProjectConfigUpdate += () =>
{
#if USE_ODP
NotificationCenterRegistry.GetNotificationCenter(SdkKey)
.SendNotifications(
NotificationCenter.NotificationType.OptimizelyConfigUpdate);
#endif
NotificationCenter?.SendNotifications(NotificationCenter.NotificationType
.OptimizelyConfigUpdate);
};
if (StartByDefault)
{
configManager.Start();
}
// Optionally block until config is available.
if (!defer)
{
configManager.GetConfig();
}
return configManager;
}
}
}
}