public void DeleteCollection(string collection) { JObject jsonResponse = null; try { var client = new RestClient(); var request = new RestRequest(_serverUrl + collection, Method.DELETE); request.AddHeader("Authorization", _prjSettings.MasterKey); var serverResponse = client.Execute(request); if (serverResponse == null) { throw new KeenException("No response from host"); } if (!serverResponse.ErrorMessage.IsNullOrWhiteSpace()) { throw new KeenException("DeleteCollection failed with error: " + serverResponse.ErrorMessage); } if (!serverResponse.Content.IsNullOrWhiteSpace()) { jsonResponse = JObject.Parse(serverResponse.Content); } } catch (Exception ex) { throw new KeenException("DeleteCollection failed " + ex.Message, ex); } // throw an exception with information from the json response KeenUtil.CheckApiErrorCode(jsonResponse); }
public void AddEvent(string collection, JObject anEvent) { JObject jsonResponse = null; try { var client = new RestClient(); var request = new RestRequest(_serverUrl + collection, Method.POST); request.AddHeader("Authorization", _prjSettings.WriteKey); request.AddParameter("application/json", anEvent.ToString(), ParameterType.RequestBody); var serverResponse = client.Execute(request); if (serverResponse == null) { throw new KeenException("No response from host"); } if (!serverResponse.ErrorMessage.IsNullOrWhiteSpace()) { throw new KeenException("AddEvent failed with status: " + serverResponse.ErrorMessage); } if (!serverResponse.Content.IsNullOrWhiteSpace()) { jsonResponse = JObject.Parse(serverResponse.Content); } } catch (Exception ex) { throw new KeenException("AddEvent failed", ex); } KeenUtil.CheckApiErrorCode(jsonResponse); }
public JObject GetSchema(string collection) { try { var client = new RestClient(); var request = new RestRequest(_serverUrl + collection, Method.GET); request.AddHeader("Authorization", _prjSettings.MasterKey); var serverResponse = client.Execute(request); if (serverResponse == null) { throw new KeenException("No response from host"); } if (!serverResponse.ErrorMessage.IsNullOrWhiteSpace()) { throw new KeenException("GetSchema failed with status: " + serverResponse.ErrorMessage); } var response = JObject.Parse(serverResponse.Content); KeenUtil.CheckApiErrorCode(response); return(response); } catch (Exception ex) { throw new KeenException("GetSchema failed", ex); } }
/// <summary> /// Add all events in a single request. /// </summary> /// <param name="events"></param> /// <returns></returns> public IEnumerable <CachedEvent> AddEvents(JObject events) { JObject jsonResponse = null; try { var client = new RestClient(_serverUrl); var request = new RestRequest("", Method.POST); request.AddHeader("Authorization", _prjSettings.WriteKey); request.AddHeader("Keen-Sdk", KeenUtil.GetSdkVersion()); request.AddParameter("application/json", events.ToString(), ParameterType.RequestBody); var serverResponse = client.Execute(request); if (serverResponse == null) { throw new KeenException("No response from host"); } if (!serverResponse.ErrorMessage.IsNullOrWhiteSpace()) { throw new KeenException("AddEvents failed with status: " + serverResponse.ErrorMessage); } if (!serverResponse.Content.IsNullOrWhiteSpace()) { jsonResponse = JObject.Parse(serverResponse.Content); } } catch (Exception ex) { throw new KeenException("AddEvents failed", ex); } KeenUtil.CheckApiErrorCode(jsonResponse); try { // 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); } catch (Exception ex) { throw new KeenException("AddEvents failed", ex); } }
/// <summary> /// Get details of all schemas in the project. /// </summary> /// <returns></returns> public JArray GetSchemas() { try { var client = new RestClient(_serverUrl); var request = new RestRequest("", Method.GET); request.AddHeader("Authorization", _prjSettings.MasterKey); request.AddHeader("Keen-Sdk", KeenUtil.GetSdkVersion()); var serverResponse = client.Execute(request); if (serverResponse == null) { throw new KeenException("No response from host"); } if (!serverResponse.ErrorMessage.IsNullOrWhiteSpace()) { throw new KeenException("GetSchemas failed with status: " + serverResponse.ErrorMessage); } JArray jsonResponse = null; try { // The response should be an array. An error will cause a parse failure. jsonResponse = JArray.Parse(serverResponse.Content); } catch (Exception) { var obj = JObject.Parse(serverResponse.Content); KeenUtil.CheckApiErrorCode(obj); } return(jsonResponse); } catch (Exception ex) { throw new KeenException("GetSchemas failed", ex); } }