Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit f91e068

Browse files
authored
Multiplatform support (#55)
* Add multi-platform support. (.Net Standard 1.3 and .Net Framework 3.5) * Change nuget package info * Add test project for .Net Framework 3.5 * Update reference to debug dll. (Temporary) It's for testing purpose. After fix all the errors in Analytics project, the reference will be back to Nuget packages. * HttpClient error in previous version was fixed. * Add test project for .Net Framework 3.5 * Upgrade version to 0.1.4. Tested it in Release mode. * Add test method as an executable. * Fix bugs in API. * Remove debug message from MakeRequest() function. * Update package version to 0.1.5 and add API testing code. * Prepare for official 3.1.0-alpha release
1 parent 3a6b1ce commit f91e068

16 files changed

+607
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,5 @@ UpgradeLog*
115115
**sublime**
116116

117117
.DS_STORE
118+
/.vs
119+
/.vscode

Analytics.NET.sln

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26430.13
4+
VisualStudioVersion = 15.0.26730.16
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Analytics", "Analytics\Analytics.csproj", "{A386CF02-D902-4B22-90A8-E828093E3AA4}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{6AE99FF7-EECF-4639-B6B1-3E7CF1CC3048}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", "{6AE99FF7-EECF-4639-B6B1-3E7CF1CC3048}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C316F1BB-6C70-4DEB-BC04-5664C39313F3}"
1111
ProjectSection(SolutionItems) = preProject
1212
.editorconfig = .editorconfig
1313
.gitignore = .gitignore
1414
EndProjectSection
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Net35", "Test.Net35\Test.Net35.csproj", "{14046A5D-168B-4320-B031-C72BEE19F206}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -27,10 +29,17 @@ Global
2729
{6AE99FF7-EECF-4639-B6B1-3E7CF1CC3048}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{6AE99FF7-EECF-4639-B6B1-3E7CF1CC3048}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{6AE99FF7-EECF-4639-B6B1-3E7CF1CC3048}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{14046A5D-168B-4320-B031-C72BEE19F206}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{14046A5D-168B-4320-B031-C72BEE19F206}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{14046A5D-168B-4320-B031-C72BEE19F206}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{14046A5D-168B-4320-B031-C72BEE19F206}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE
3339
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {424C3E96-6F35-4A0F-8626-D085289A3999}
42+
EndGlobalSection
3443
GlobalSection(MonoDevelopProperties) = preSolution
3544
StartupItem = Analytics-Test\Analytics-Test.csproj
3645
EndGlobalSection

Analytics.nuspec

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
33
<metadata>
44
<id>Analytics</id>
5-
<version>3.0.0</version>
5+
<version>3.1.0-alpha</version>
66
<title>Analytics</title>
77
<authors>Segment Team</authors>
88
<owners>Segment Team &lt;[email protected]&gt;</owners>
@@ -15,13 +15,8 @@
1515
<releaseNotes>Check out the changelog at: https://raw.githubusercontent.com/segmentio/Analytics.NET/master/Analytics/History.md</releaseNotes>
1616
<language>en-US</language>
1717
<tags>analytics, Segment</tags>
18-
<dependencies>
19-
<group targetFramework=".NETFramework3.5">
20-
<dependency id="Newtonsoft.Json" version="4.5.11" />
21-
</group>
22-
</dependencies>
2318
</metadata>
2419
<files>
25-
<file src="Analytics/bin/Debug/Analytics.NET.dll" target="lib" />
20+
<file src="Analytics/bin/Release/**/*.dll" target="lib" />
2621
</files>
2722
</package>

Analytics/Analytics.csproj

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.3</TargetFramework>
4+
<TargetFrameworks>netstandard1.3;net35</TargetFrameworks>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<PackageId>Analytics</PackageId>
7+
<Version>3.1.0-alpha</Version>
8+
<Authors>Segment Team</Authors>
9+
<Company />
10+
<Product>Analytics.NET</Product>
11+
<Description>The .NET API for analytics, by Segment.</Description>
12+
</PropertyGroup>
13+
14+
<PropertyGroup Condition="'$(TargetFramework)'=='net35'">
15+
<DefineConstants>NET35</DefineConstants>
16+
</PropertyGroup>
17+
18+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard1.3|AnyCPU'">
19+
<WarningLevel>2</WarningLevel>
520
</PropertyGroup>
621

722
<ItemGroup>
8-
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
23+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
24+
</ItemGroup>
25+
26+
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.3'">
927
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
1028
</ItemGroup>
29+
<ItemGroup Condition="'$(TargetFramework)' == 'net35'">
30+
<PackageReference Include="AsyncBridge.Net35">
31+
<Version>0.2.0</Version>
32+
</PackageReference>
33+
<Reference Include="System.Net" />
34+
</ItemGroup>
1135

12-
</Project>
36+
</Project>

Analytics/Request/BlockingRequestHandler.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
using System;
1+
using System;
22
using System.Diagnostics;
33
using System.Net;
4+
#if NET35
5+
#else
46
using System.Net.Http;
57
using System.Net.Http.Headers;
8+
#endif
69
using System.Text;
710
using System.Threading.Tasks;
811
using Newtonsoft.Json;
@@ -12,6 +15,21 @@
1215

1316
namespace Segment.Request
1417
{
18+
#if NET35
19+
internal class HttpClient : WebClient
20+
{
21+
public TimeSpan Timeout { get; set; }
22+
23+
protected override WebRequest GetWebRequest(Uri address)
24+
{
25+
WebRequest w = base.GetWebRequest(address);
26+
if (Timeout.Milliseconds != 0)
27+
w.Timeout = Timeout.Milliseconds;
28+
return w;
29+
}
30+
}
31+
#endif
32+
1533
internal class BlockingRequestHandler : IRequestHandler
1634
{
1735
/// <summary>
@@ -50,7 +68,12 @@ public async Task MakeRequest(Batch batch)
5068

5169
// Basic Authentication
5270
// https://segment.io/docs/tracking-api/reference/#authentication
71+
#if NET35
72+
_httpClient.Headers.Add("Authorization", "Basic " + BasicAuthHeader(batch.WriteKey, string.Empty));
73+
_httpClient.Headers.Add("Content-Type", "application/json; charset=utf-8");
74+
#else
5375
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", BasicAuthHeader(batch.WriteKey, string.Empty));
76+
#endif
5477

5578
Logger.Info("Sending analytics request to Segment.io ..", new Dict
5679
{
@@ -59,14 +82,32 @@ public async Task MakeRequest(Batch batch)
5982
{ "batch size", batch.batch.Count }
6083
});
6184

85+
#if NET35
86+
watch.Start();
87+
88+
try
89+
{
90+
var response = Encoding.UTF8.GetString(_httpClient.UploadData(uri, "POST", Encoding.UTF8.GetBytes(json)));
91+
watch.Stop();
92+
93+
Succeed(batch, watch.ElapsedMilliseconds);
94+
}
95+
catch (WebException ex)
96+
{
97+
watch.Stop();
98+
99+
string responseStr = ex.Message;
100+
Fail(batch, new APIException("Unexpected Response", responseStr), watch.ElapsedMilliseconds);
101+
}
102+
#else
62103
watch.Start();
63104

64105
var response = await _httpClient.PostAsync(uri, new StringContent(json, Encoding.UTF8, "application/json")).ConfigureAwait(false);
65106

66107
watch.Stop();
67108

68109
if (response.StatusCode == HttpStatusCode.OK)
69-
{
110+
{
70111
Succeed(batch, watch.ElapsedMilliseconds);
71112
}
72113
else
@@ -75,6 +116,7 @@ public async Task MakeRequest(Batch batch)
75116
responseStr += await response.Content.ReadAsStringAsync().ConfigureAwait(false);
76117
Fail(batch, new APIException("Unexpected Status Code", responseStr), watch.ElapsedMilliseconds);
77118
}
119+
#endif
78120
}
79121
catch (System.Exception e)
80122
{

Test.Net35/ActionTests.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using NUnit.Framework;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace Segment.Test
7+
{
8+
[TestFixture()]
9+
public class ActionTests
10+
{
11+
12+
[SetUp]
13+
public void Init()
14+
{
15+
Analytics.Dispose();
16+
Logger.Handlers += LoggingHandler;
17+
Analytics.Initialize(Constants.WRITE_KEY, new Config().SetAsync(false));
18+
}
19+
20+
[Test ()]
21+
public void IdentifyTestNet35()
22+
{
23+
Actions.Identify(Analytics.Client);
24+
FlushAndCheck(1);
25+
}
26+
27+
[Test()]
28+
public void TrackTestNet35()
29+
{
30+
Actions.Track(Analytics.Client);
31+
FlushAndCheck(1);
32+
}
33+
34+
[Test()]
35+
public void AliasTestNet35()
36+
{
37+
Actions.Alias(Analytics.Client);
38+
FlushAndCheck(1);
39+
}
40+
41+
[Test()]
42+
public void GroupTestNet35()
43+
{
44+
Actions.Group(Analytics.Client);
45+
FlushAndCheck(1);
46+
}
47+
48+
[Test()]
49+
public void PageTestNet35()
50+
{
51+
Actions.Page(Analytics.Client);
52+
FlushAndCheck(1);
53+
}
54+
55+
[Test()]
56+
public void ScreenTestNet35()
57+
{
58+
Actions.Screen(Analytics.Client);
59+
FlushAndCheck(1);
60+
}
61+
62+
private void FlushAndCheck(int messages)
63+
{
64+
Analytics.Client.Flush();
65+
Assert.AreEqual(messages, Analytics.Client.Statistics.Submitted);
66+
Assert.AreEqual(messages, Analytics.Client.Statistics.Succeeded);
67+
Assert.AreEqual(0, Analytics.Client.Statistics.Failed);
68+
}
69+
70+
static void LoggingHandler(Logger.Level level, string message, IDictionary<string, object> args)
71+
{
72+
if (args != null)
73+
{
74+
foreach (string key in args.Keys)
75+
{
76+
message += String.Format(" {0}: {1},", "" + key, "" + args[key]);
77+
}
78+
}
79+
Console.WriteLine(String.Format("[ActionTests] [{0}] {1}", level, message));
80+
}
81+
}
82+
83+
84+
}
85+

Test.Net35/Actions.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Segment.Model;
4+
5+
namespace Segment.Test
6+
{
7+
public class Actions
8+
{
9+
private static Random random = new Random();
10+
11+
public static Properties Properties()
12+
{
13+
return new Properties() {
14+
{ "Success", true },
15+
{ "When", DateTime.Now }
16+
};
17+
}
18+
19+
public static Traits Traits()
20+
{
21+
return new Traits() {
22+
{ "Subscription Plan", "Free" },
23+
{ "Friends", 30 },
24+
{ "Joined", DateTime.Now },
25+
{ "Cool", true },
26+
{ "Company", new Dict () { { "name", "Initech, Inc " } } },
27+
{ "Revenue", 40.32 },
28+
{ "Don't Submit This, Kids", new UnauthorizedAccessException () }
29+
};
30+
}
31+
32+
public static Options Options()
33+
{
34+
return new Options()
35+
.SetTimestamp(DateTime.Now)
36+
.SetAnonymousId(Guid.NewGuid().ToString())
37+
.SetIntegration("all", false)
38+
.SetIntegration("Mixpanel", true)
39+
.SetIntegration("Salesforce", true)
40+
.SetContext(new Context()
41+
.Add("ip", "12.212.12.49")
42+
.Add("language", "en-us")
43+
);
44+
}
45+
46+
public static void Identify(Client client)
47+
{
48+
client.Identify("user", Traits(), Options());
49+
Analytics.Client.Flush();
50+
}
51+
52+
public static void Group(Client client)
53+
{
54+
client.Group("user", "group", Traits(), Options());
55+
Analytics.Client.Flush();
56+
}
57+
58+
public static void Track(Client client)
59+
{
60+
client.Track("user", "Ran .NET test", Properties(), Options());
61+
}
62+
63+
public static void Alias(Client client)
64+
{
65+
client.Alias("previousId", "to");
66+
}
67+
68+
public static void Page(Client client)
69+
{
70+
client.Page("user", "name", "category", Properties(), Options());
71+
}
72+
73+
public static void Screen(Client client)
74+
{
75+
client.Screen("user", "name", "category", Properties(), Options());
76+
}
77+
78+
public static void Random(Client client)
79+
{
80+
switch (random.Next(0, 6))
81+
{
82+
case 0:
83+
Identify(client);
84+
return;
85+
case 1:
86+
Track(client);
87+
return;
88+
case 2:
89+
Alias(client);
90+
return;
91+
case 3:
92+
Group(client);
93+
return;
94+
case 4:
95+
Page(client);
96+
return;
97+
case 5:
98+
Screen(client);
99+
return;
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)