Skip to content

Commit 7a67e94

Browse files
mnoman09Michael Ng
authored andcommitted
Feat: Add Config support for initializing EP & HttpProjectConfigManager (#197)
1 parent 2340ab3 commit 7a67e94

File tree

11 files changed

+324
-10
lines changed

11 files changed

+324
-10
lines changed

OptimizelySDK.Net40/OptimizelySDK.Net40.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<Reference Include="System.Core" />
4040
<Reference Include="System.Xml.Linq" />
4141
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="System.configuration" />
4243
<Reference Include="Microsoft.CSharp" />
4344
<Reference Include="System.Data" />
4445
<Reference Include="System.Xml" />
@@ -230,6 +231,9 @@
230231
</Compile>
231232
<Compile Include="..\OptimizelySDK\Config\PollingProjectConfigManager.cs">
232233
<Link>Config\PollingProjectConfigManager</Link>
234+
</Compile>
235+
<Compile Include="..\OptimizelySDK\ClientConfigHandler.cs">
236+
<Link>ClientConfigHandler</Link>
233237
</Compile>
234238
<Compile Include="..\OptimizelySDK\Config\HttpProjectConfigManager.cs">
235239
<Link>Config\HttpProjectConfigManager</Link>

OptimizelySDK.NetStandard20/OptimizelySDK.NetStandard20.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
@@ -72,6 +72,9 @@
7272
</Compile>
7373
<Compile Include="..\OptimizelySDK\Config\ProjectConfigManager.cs">
7474
<Link>Config\ProjectConfigManager.cs</Link>
75+
</Compile>
76+
<Compile Include="..\OptimizelySDK\ClientConfigHandler.cs">
77+
<Link>Config\ClientConfigHandler.cs</Link>
7578
</Compile>
7679
<Compile Include="..\OptimizelySDK\Entity\Attribute.cs">
7780
<Link>Entity\Attribute.cs</Link>
@@ -257,6 +260,7 @@
257260
<PackageReference Include="murmurhash-signed" Version="1.0.2" />
258261
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
259262
<PackageReference Include="NJsonSchema" Version="8.30.6304.31883" />
263+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
260264
</ItemGroup>
261265
<ItemGroup>
262266
<EmbeddedResource Include="..\OptimizelySDK\Utils\schema.json">

OptimizelySDK.Tests/App.config

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,31 @@
44
that apply only to the Test project.
55
-->
66
<configuration>
7-
<appSettings>
87

9-
</appSettings>
8+
<configSections>
9+
<section name="optlySDKConfigSection"
10+
type="OptimizelySDK.OptimizelySDKConfigSection, OptimizelySDK, Version=3.2.0.0, Culture=neutral, PublicKeyToken=null" />
11+
</configSections>
12+
13+
<optlySDKConfigSection>
14+
15+
<HttpProjectConfig sdkKey="43214321"
16+
url="www.testurl.com"
17+
format="https://cdn.optimizely.com/data/{0}.json"
18+
pollingInterval="2000"
19+
blockingTimeOutPeriod="10000"
20+
autoUpdate="true"
21+
defaultStart="true">
22+
</HttpProjectConfig>
1023

24+
<BatchEventProcessor batchSize="10"
25+
flushInterval="2000"
26+
timeoutInterval="10000"
27+
defaultStart="true">
28+
</BatchEventProcessor>
29+
30+
</optlySDKConfigSection>
31+
1132
<connectionStrings>
1233

1334
</connectionStrings>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if !NETSTANDARD1_6 && !NET35
18+
19+
using NUnit.Framework;
20+
using System.Configuration;
21+
22+
namespace OptimizelySDK.Tests
23+
{
24+
[TestFixture]
25+
public class ClientConfigHandlerTest
26+
{
27+
28+
[Test]
29+
public void TestHTTPAppConfigSection()
30+
{
31+
var configSection = ConfigurationManager.GetSection("optlySDKConfigSection") as OptimizelySDKConfigSection;
32+
var httpSetting = configSection.HttpProjectConfig;
33+
Assert.IsNotNull(httpSetting);
34+
Assert.IsTrue(httpSetting.AutoUpdate);
35+
Assert.AreEqual(httpSetting.BlockingTimeOutPeriod, 10000);
36+
Assert.AreEqual(httpSetting.Format, "https://cdn.optimizely.com/data/{0}.json");
37+
Assert.IsTrue(httpSetting.DefaultStart);
38+
Assert.AreEqual(httpSetting.PollingInterval, 2000);
39+
Assert.AreEqual(httpSetting.SDKKey, "43214321");
40+
Assert.AreEqual(httpSetting.Url, "www.testurl.com");
41+
}
42+
43+
[Test]
44+
public void TestBatchEventAppConfigSection()
45+
{
46+
var configSection = ConfigurationManager.GetSection("optlySDKConfigSection") as OptimizelySDKConfigSection;
47+
var batchSetting = configSection.BatchEventProcessor;
48+
Assert.IsNotNull(batchSetting);
49+
Assert.AreEqual(batchSetting.BatchSize, 10);
50+
Assert.AreEqual(batchSetting.FlushInterval, 2000);
51+
Assert.AreEqual(batchSetting.TimeoutInterval, 10000);
52+
Assert.IsTrue(batchSetting.DefaultStart);
53+
}
54+
55+
}
56+
}
57+
#endif

OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ public void TestHttpConfigManagerRetreiveProjectConfigByURL()
5555
Assert.NotNull(httpManager.GetConfig());
5656
}
5757

58+
[Test]
59+
public void TestHttpConfigManagerRetreiveProjectConfigGivenEmptyFormatUseDefaultFormat()
60+
{
61+
HttpProjectConfigManager httpManager = new HttpProjectConfigManager.Builder()
62+
.WithSdkKey("QBw9gFM8oTn7ogY9ANCC1z")
63+
.WithFormat("")
64+
.WithLogger(LoggerMock.Object)
65+
.WithPollingInterval(TimeSpan.FromMilliseconds(1000))
66+
.WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(500))
67+
.WithStartByDefault()
68+
.Build();
69+
70+
httpManager.OnReady().Wait(System.Threading.Timeout.Infinite);
71+
Assert.NotNull(httpManager.GetConfig());
72+
}
73+
5874
[Test]
5975
public void TestHttpConfigManagerRetreiveProjectConfigBySDKKey()
6076
{

OptimizelySDK.Tests/OptimizelySDK.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<Compile Include="EventTests\TestForwardingEventDispatcher.cs" />
8787
<Compile Include="InvalidEventDispatcher.cs" />
8888
<Compile Include="NotificationTests\NotificationCenterTests.cs" />
89+
<Compile Include="ClientConfigHandlerTest.cs" />
8990
<Compile Include="OptimizelyTest.cs" />
9091
<Compile Include="Properties\AssemblyInfo.cs" />
9192
<Compile Include="TestBucketer.cs" />

OptimizelySDK/ClientConfigHandler.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Configuration;
18+
19+
namespace OptimizelySDK
20+
{
21+
public class HttpProjectConfigElement : ConfigurationElement
22+
{
23+
[ConfigurationProperty("sdkKey", IsRequired = true, IsKey = true)]
24+
public string SDKKey
25+
{
26+
get { return (string)base["sdkKey"]; }
27+
}
28+
29+
[ConfigurationProperty("url")]
30+
public string Url
31+
{
32+
get { return (string)base["url"]; }
33+
}
34+
35+
[ConfigurationProperty("format")]
36+
public string Format
37+
{
38+
get { return (string)base["format"]; }
39+
}
40+
41+
[ConfigurationProperty("pollingInterval")]
42+
public int PollingInterval
43+
{
44+
get { return base["pollingInterval"] is int ? (int)base["pollingInterval"] : 0; }
45+
}
46+
47+
[ConfigurationProperty("blockingTimeOutPeriod")]
48+
public int BlockingTimeOutPeriod
49+
{
50+
get { return base["blockingTimeOutPeriod"] is int ? (int)base["blockingTimeOutPeriod"] : 0; }
51+
}
52+
53+
[ConfigurationProperty("autoUpdate")]
54+
public bool AutoUpdate
55+
{
56+
get { return (bool)base["autoUpdate"]; }
57+
}
58+
59+
[ConfigurationProperty("defaultStart")]
60+
public bool DefaultStart
61+
{
62+
get { return (bool)base["defaultStart"]; }
63+
}
64+
}
65+
66+
public class BatchEventProcessorElement : ConfigurationElement
67+
{
68+
[ConfigurationProperty("batchSize")]
69+
public int BatchSize
70+
{
71+
get { return (int)base["batchSize"]; }
72+
}
73+
74+
[ConfigurationProperty("flushInterval")]
75+
public int FlushInterval
76+
{
77+
get { return base["flushInterval"] is int ? (int)base["flushInterval"] : 0; }
78+
}
79+
80+
[ConfigurationProperty("timeoutInterval")]
81+
public int TimeoutInterval
82+
{
83+
get { return base["timeoutInterval"] is int ? (int)base["timeoutInterval"] : 0; }
84+
}
85+
86+
[ConfigurationProperty("defaultStart")]
87+
public bool DefaultStart
88+
{
89+
get { return (bool)base["defaultStart"]; }
90+
}
91+
}
92+
93+
public class OptimizelySDKConfigSection : ConfigurationSection
94+
{
95+
[ConfigurationProperty("HttpProjectConfig")]
96+
public HttpProjectConfigElement HttpProjectConfig
97+
{
98+
get { return (HttpProjectConfigElement)base["HttpProjectConfig"]; }
99+
set { base["HttpProjectConfig"] = value; }
100+
}
101+
102+
[ConfigurationProperty("BatchEventProcessor")]
103+
public BatchEventProcessorElement BatchEventProcessor {
104+
get { return (BatchEventProcessorElement)(base["BatchEventProcessor"]); }
105+
set { base["BatchEventProcessor"] = value; }
106+
}
107+
}
108+
}

OptimizelySDK/Config/HttpProjectConfigManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ public class Builder
124124
private const long MAX_MILLISECONDS_LIMIT = 4294967294;
125125
private readonly TimeSpan DEFAULT_PERIOD = TimeSpan.FromMinutes(5);
126126
private readonly TimeSpan DEFAULT_BLOCKINGOUT_PERIOD = TimeSpan.FromSeconds(15);
127+
private readonly string DEFAULT_FORMAT = "https://cdn.optimizely.com/datafiles/{0}.json";
127128

128129
private string Datafile;
129130
private string SdkKey;
130131
private string Url;
131-
private string Format = "https://cdn.optimizely.com/datafiles/{0}.json";
132+
private string Format;
132133
private ILogger Logger;
133134
private IErrorHandler ErrorHandler;
134135
private TimeSpan Period;
@@ -247,6 +248,10 @@ public HttpProjectConfigManager Build(bool defer)
247248
if (ErrorHandler == null)
248249
ErrorHandler = new DefaultErrorHandler();
249250

251+
if (string.IsNullOrEmpty(Format)) {
252+
Format = DEFAULT_FORMAT;
253+
}
254+
250255
if (string.IsNullOrEmpty(Url) && string.IsNullOrEmpty(SdkKey))
251256
{
252257
ErrorHandler.HandleError(new Exception("SdkKey cannot be null"));

0 commit comments

Comments
 (0)