From 1d87bc669bd95cd938c09be7431f4de0b6bf8540 Mon Sep 17 00:00:00 2001 From: Vadim Tsedrik Date: Wed, 22 Jul 2015 11:49:42 +0300 Subject: [PATCH] Issue #19. KeenClient.AddEventAsync crashes app when internet connection is lost. --- Keen/Event.cs | 2 +- Keen/EventCollection.cs | 28 ++++++++++++++++++++-------- Keen/KeenClient.cs | 4 ++-- Keen/KeenUtil.cs | 3 ++- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Keen/Event.cs b/Keen/Event.cs index 45672c4..cfb9fe2 100644 --- a/Keen/Event.cs +++ b/Keen/Event.cs @@ -32,7 +32,7 @@ public async Task GetSchemas() .ConfigureAwait(continueOnCapturedContext: false); var responseString = await responseMsg.Content.ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); - dynamic response = JArray.Parse(responseString); + var response = JArray.Parse(responseString); // error checking, throw an exception with information from the json // response if available, then check the HTTP response. diff --git a/Keen/EventCollection.cs b/Keen/EventCollection.cs index 0e13e29..75f6765 100644 --- a/Keen/EventCollection.cs +++ b/Keen/EventCollection.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json.Linq; +using System.Net; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics; @@ -26,7 +27,7 @@ public async System.Threading.Tasks.Task GetSchema(string collection) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await responseMsg.Content.ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); - dynamic response = JObject.Parse(responseString); + var response = JObject.Parse(responseString); // error checking, throw an exception with information from the json // response if available, then check the HTTP response. @@ -60,17 +61,28 @@ public async System.Threading.Tasks.Task AddEvent(string collection, JObject anE contentStream.Headers.Add("content-type", "application/json"); client.DefaultRequestHeaders.Add("Authorization", _prjSettings.WriteKey); - var httpResponse = await client.PostAsync(_serverUrl + collection, contentStream) - .ConfigureAwait(continueOnCapturedContext: false); - var responseString = await httpResponse.Content.ReadAsStringAsync() - .ConfigureAwait(continueOnCapturedContext: false); + string responseString= null; + HttpResponseMessage httpResponse = null; + try + { + httpResponse = await client.PostAsync(_serverUrl + collection, contentStream) + .ConfigureAwait(continueOnCapturedContext: false); + responseString = await httpResponse.Content.ReadAsStringAsync() + .ConfigureAwait(continueOnCapturedContext: false); + } + catch (Exception) + { + //throw new KeenException("AddEvent failed. Api is not available now."); + } + JObject jsonResponse = null; try { // Normally the response content should be parsable JSON, // but if the server returned a 404 error page or something // like that, this will throw. - jsonResponse = JObject.Parse(responseString); + if (!string.IsNullOrWhiteSpace(responseString)) + jsonResponse = JObject.Parse(responseString); } catch (Exception) { } @@ -78,7 +90,7 @@ public async System.Threading.Tasks.Task AddEvent(string collection, JObject anE // error checking, throw an exception with information from the // json response if available, then check the HTTP response. KeenUtil.CheckApiErrorCode(jsonResponse); - if (!httpResponse.IsSuccessStatusCode) + if (httpResponse != null && httpResponse.StatusCode != HttpStatusCode.NotFound && !httpResponse.IsSuccessStatusCode) throw new KeenException("AddEvent failed with status: " + httpResponse); } } diff --git a/Keen/KeenClient.cs b/Keen/KeenClient.cs index 1ba9557..4896ade 100644 --- a/Keen/KeenClient.cs +++ b/Keen/KeenClient.cs @@ -189,7 +189,7 @@ public JArray GetSchemas() /// a value for the project settings Master API key. /// /// - public async Task GetSchemaAsync(string collection) + public async Task GetSchemaAsync(string collection) { // Preconditions KeenUtil.ValidateEventCollectionName(collection); @@ -205,7 +205,7 @@ public async Task GetSchemaAsync(string collection) /// a value for the project settings Master API key. /// /// - public dynamic GetSchema(string collection) + public JObject GetSchema(string collection) { try { diff --git a/Keen/KeenUtil.cs b/Keen/KeenUtil.cs index dd9255e..7d30db0 100644 --- a/Keen/KeenUtil.cs +++ b/Keen/KeenUtil.cs @@ -134,8 +134,9 @@ public static Exception GetBulkApiError(JObject apiResponse) /// Check the 'error_code' field and throw the appropriate exception if non-null. /// /// Deserialized json response from a Keen API call. - public static void CheckApiErrorCode(dynamic apiResponse) + public static void CheckApiErrorCode(JContainer apiResponse) { + if (apiResponse == null) return; if (apiResponse is JArray) return; var errorCode = (string) apiResponse.SelectToken("$.error_code");