public static GetBulkApiError ( Newtonsoft.Json.Linq.JObject apiResponse ) : |
||
apiResponse | Newtonsoft.Json.Linq.JObject | Deserialized json response from a Keen API call. |
return |
/// <summary> /// Add all events in a single request. /// </summary> /// <param name="events"></param> /// <returns></returns> public async Task <IEnumerable <CachedEvent> > AddEvents(JObject events) { if (string.IsNullOrWhiteSpace(_writeKey)) { throw new KeenException("An API WriteKey is required to add events."); } var content = events.ToString(); var responseMsg = await _keenHttpClient .PostAsync(_eventsRelativeUrl, _writeKey, content) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await responseMsg .Content .ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); 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); // TODO : Why do we not call KeenUtil.CheckApiErrorCode(jsonResponse); ?? } catch (Exception) { } if (!responseMsg.IsSuccessStatusCode) { throw new KeenException("AddEvents failed with status: " + responseMsg.StatusCode); } if (null == jsonResponse) { throw new KeenException("AddEvents failed with empty response from server."); } // error checking, return failed events in the list, // or if the HTTP response is a failure, throw. var failedItems = from respCols in jsonResponse.Properties() from eventsCols in events.Properties() where respCols.Name == eventsCols.Name let collection = respCols.Name let combined = eventsCols.Children().Children() .Zip(respCols.Children().Children(), (e, r) => new { eventObj = (JObject)e, result = (JObject)r }) from e in combined where !(bool)(e.result.Property("success").Value) select new CachedEvent(collection, e.eventObj, KeenUtil.GetBulkApiError(e.result)); return(failedItems); }
/// <summary> /// Add all events in a single request. /// </summary> /// <param name="events"></param> /// <returns></returns> public async Task <IEnumerable <CachedEvent> > AddEvents(JObject events) { var content = events.ToString(); using (var client = new HttpClient()) using (var contentStream = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(content)))) { contentStream.Headers.Add("content-type", "application/json"); client.DefaultRequestHeaders.Add("Authorization", _prjSettings.WriteKey); var httpResponse = await client.PostAsync(_serverUrl, contentStream) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await httpResponse.Content.ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); 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); } catch (Exception) { } if (!httpResponse.IsSuccessStatusCode) { throw new KeenException("AddEvents failed with status: " + httpResponse); } if (null == jsonResponse) { throw new KeenException("AddEvents failed with empty response from server."); } // error checking, return failed events in the list, // or if the HTTP response is a failure, throw. var failedItems = from respCols in jsonResponse.Properties() from eventsCols in events.Properties() where respCols.Name == eventsCols.Name let collection = respCols.Name let combined = eventsCols.Children().Children() .Zip(respCols.Children().Children(), (e, r) => new { eventObj = (JObject)e, result = (JObject)r }) from e in combined where !(bool)(e.result.Property("success").Value) select new CachedEvent(collection, e.eventObj, KeenUtil.GetBulkApiError(e.result)); return(failedItems); } }