Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #19. KeenClient.AddEventAsync crashes app when internet connection is lost #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Keen/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<JArray> 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.
Expand Down
28 changes: 20 additions & 8 deletions Keen/EventCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using System.Net;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -26,7 +27,7 @@ public async System.Threading.Tasks.Task<JObject> 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.
Expand Down Expand Up @@ -60,25 +61,36 @@ 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)
{ }

// 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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Keen/KeenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public JArray GetSchemas()
/// a value for the project settings Master API key.
/// </summary>
/// <param name="collection"></param>
public async Task<dynamic> GetSchemaAsync(string collection)
public async Task<JObject> GetSchemaAsync(string collection)
{
// Preconditions
KeenUtil.ValidateEventCollectionName(collection);
Expand All @@ -205,7 +205,7 @@ public async Task<dynamic> GetSchemaAsync(string collection)
/// a value for the project settings Master API key.
/// </summary>
/// <param name="collection"></param>
public dynamic GetSchema(string collection)
public JObject GetSchema(string collection)
{
try
{
Expand Down
3 changes: 2 additions & 1 deletion Keen/KeenUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ public static Exception GetBulkApiError(JObject apiResponse)
/// Check the 'error_code' field and throw the appropriate exception if non-null.
/// </summary>
/// <param name="apiResponse">Deserialized json response from a Keen API call.</param>
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");
Expand Down