diff --git a/AmplitudeSharp/AmplitudeService.cs b/AmplitudeSharp/AmplitudeService.cs index 87c2207..7ed7743 100644 --- a/AmplitudeSharp/AmplitudeService.cs +++ b/AmplitudeSharp/AmplitudeService.cs @@ -32,6 +32,17 @@ public static AmplitudeService Instance private AmplitudeIdentify identification; private SemaphoreSlim eventsReady; private long sessionId; + private readonly JsonSerializerSettings apiJsonSerializerSettings = new JsonSerializerSettings + { + NullValueHandling = NullValueHandling.Ignore, + Formatting = Formatting.None, + }; + private readonly JsonSerializerSettings persistenceJsonSerializerSettings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Objects, + NullValueHandling = NullValueHandling.Ignore, + Formatting = Formatting.None, + }; /// /// Sets Offline mode, which means the events are never sent to actual amplitude service @@ -52,32 +63,26 @@ public bool OfflineMode /// /// Additional properties to send with every event /// - public Dictionary ExtraEventProperties { get; private set; } = new Dictionary(); + public Dictionary ExtraEventProperties { get; } = new Dictionary(); private AmplitudeService(string apiKey) { lockObject = new object(); - api = new AmplitudeApi(apiKey); + api = new AmplitudeApi(apiKey, apiJsonSerializerSettings); eventQueue = new List(); cancellationToken = new CancellationTokenSource(); eventsReady = new SemaphoreSlim(0); - - JsonConvert.DefaultSettings = () => new JsonSerializerSettings() - { - NullValueHandling = NullValueHandling.Ignore, - Formatting = Formatting.None - }; } public void Dispose() - { + { Uninitialize(); s_instance = null; } /// /// Initialize AmplitudeSharp - /// Takes an API key for the project and, optionally, + /// Takes an API key for the project and, optionally, /// a stream where offline/past events are stored /// /// api key for the project to stream data to @@ -233,7 +238,7 @@ private void SaveEvents(Stream persistenceStore) { try { - string persistedData = JsonConvert.SerializeObject(eventQueue, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects }); + string persistedData = JsonConvert.SerializeObject(eventQueue, persistenceJsonSerializerSettings); using (var writer = new StreamWriter(persistenceStore)) { writer.Write(persistedData); @@ -241,7 +246,7 @@ private void SaveEvents(Stream persistenceStore) } catch (Exception e) { - AmplitudeService.s_logger(LogLevel.Error, $"Failed to persist events: {e.ToString()}"); + AmplitudeService.s_logger(LogLevel.Error, $"Failed to persist events: {e}"); } } } @@ -253,7 +258,7 @@ private void LoadPastEvents(Stream persistenceStore) using (var reader = new StreamReader(persistenceStore)) { string persistedData = reader.ReadLine(); - var data = JsonConvert.DeserializeObject>(persistedData, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects }); + var data = JsonConvert.DeserializeObject>(persistedData, persistenceJsonSerializerSettings); eventQueue.InsertRange(0, data); eventsReady.Release(); @@ -261,7 +266,7 @@ private void LoadPastEvents(Stream persistenceStore) } catch (Exception e) { - AmplitudeService.s_logger(LogLevel.Error, $"Failed to load persisted events: {e.ToString()}"); + AmplitudeService.s_logger(LogLevel.Error, $"Failed to load persisted events: {e}"); } } diff --git a/AmplitudeSharp/AmplitudeSharp.csproj b/AmplitudeSharp/AmplitudeSharp.csproj index b0b6180..5fa5000 100644 --- a/AmplitudeSharp/AmplitudeSharp.csproj +++ b/AmplitudeSharp/AmplitudeSharp.csproj @@ -34,8 +34,9 @@ - - ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + ..\..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + True diff --git a/AmplitudeSharp/Api/AmplitudeApi.cs b/AmplitudeSharp/Api/AmplitudeApi.cs index ae5a6ff..6ae4109 100644 --- a/AmplitudeSharp/Api/AmplitudeApi.cs +++ b/AmplitudeSharp/Api/AmplitudeApi.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; namespace AmplitudeSharp.Api { @@ -29,16 +30,18 @@ class AmplitudeApi : IAmplitudeApi private string apiKey; private HttpClient httpClient; private HttpClientHandler httpHandler; + private readonly JsonSerializerSettings jsonSerializerSettings; - public AmplitudeApi(string apiKey) + public AmplitudeApi(string apiKey, JsonSerializerSettings jsonSerializerSettings) { this.apiKey = apiKey; + this.jsonSerializerSettings = jsonSerializerSettings; - httpHandler = new HttpClientHandler(); - httpHandler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; - httpHandler.Proxy = WebRequest.GetSystemWebProxy(); - httpHandler.UseProxy = true; - + httpHandler = new HttpClientHandler { + AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, + Proxy = WebRequest.GetSystemWebProxy(), + UseProxy = true, + }; httpClient = new HttpClient(httpHandler); } @@ -49,21 +52,21 @@ public void ConfigureProxy(string proxyUserName, string proxyPassword) httpHandler.Proxy = WebRequest.GetSystemWebProxy(); } else - { + { httpHandler.Proxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword); - } + } } public override Task Identify(AmplitudeIdentify identification) { - string data = Newtonsoft.Json.JsonConvert.SerializeObject(identification); + string data = JsonConvert.SerializeObject(identification, jsonSerializerSettings); return DoApiCall("identify", "identification", data); } public override Task SendEvents(List events) { - string data = Newtonsoft.Json.JsonConvert.SerializeObject(events); + string data = JsonConvert.SerializeObject(events, jsonSerializerSettings); return DoApiCall("httpapi", "event", data); } @@ -117,7 +120,7 @@ private async Task DoApiCall(string endPoint, string paramName, stri catch (Exception e) { result = SendResult.ServerError; - AmplitudeService.s_logger(LogLevel.Warning, $"Failed to get device make/model: {e.ToString()}"); + AmplitudeService.s_logger(LogLevel.Warning, $"Failed to get device make/model: {e}"); } }