Ejemplo n.º 1
0
        /// <summary>
        /// Get details of all schemas in the project.
        /// </summary>
        /// <returns></returns>
        public async Task <JArray> GetSchemas()
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", _prjSettings.MasterKey);
                client.DefaultRequestHeaders.Add("Keen-Sdk", KeenUtil.GetSdkVersion());

                var responseMsg = await client.GetAsync(_serverUrl)
                                  .ConfigureAwait(continueOnCapturedContext: false);

                var responseString = await responseMsg.Content.ReadAsStringAsync()
                                     .ConfigureAwait(continueOnCapturedContext: false);

                dynamic response = JArray.Parse(responseString);

                // error checking, throw an exception with information from the json
                // response if available, then check the HTTP response.
                KeenUtil.CheckApiErrorCode(response);
                if (!responseMsg.IsSuccessStatusCode)
                {
                    throw new KeenException("GetSchemas failed with status: " + responseMsg.StatusCode);
                }

                return(response);
            }
        }
Ejemplo n.º 2
0
        /// <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);
                    client.DefaultRequestHeaders.Add("Keen-Sdk", KeenUtil.GetSdkVersion());

                    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);
                }
        }
Ejemplo n.º 3
0
        public async System.Threading.Tasks.Task DeleteCollection(string collection)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", _prjSettings.MasterKey);
                client.DefaultRequestHeaders.Add("Keen-Sdk", KeenUtil.GetSdkVersion());

                var responseMsg = await client.DeleteAsync(_serverUrl + collection)
                                  .ConfigureAwait(continueOnCapturedContext: false);

                if (!responseMsg.IsSuccessStatusCode)
                {
                    throw new KeenException("DeleteCollection failed with status: " + responseMsg.StatusCode);
                }
            }
        }
Ejemplo n.º 4
0
        public async System.Threading.Tasks.Task AddEvent(string collection, JObject anEvent)
        {
            var content = anEvent.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);
                    client.DefaultRequestHeaders.Add("Keen-Sdk", KeenUtil.GetSdkVersion());

                    var httpResponse = await client.PostAsync(_serverUrl + collection, 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)
                    { }

                    // error checking, throw an exception with information from the
                    // json response if available, then check the HTTP response.
                    KeenUtil.CheckApiErrorCode(jsonResponse);
                    if (!httpResponse.IsSuccessStatusCode)
                    {
                        throw new KeenException("AddEvent failed with status: " + httpResponse);
                    }
                }
        }