Exemplo n.º 1
0
        public async Task <JObject> GetResultsAsync(string datasetName,
                                                    string indexBy,
                                                    string timeframe)
        {
            if (string.IsNullOrWhiteSpace(datasetName))
            {
                throw new KeenException("A dataset name is required.");
            }

            if (string.IsNullOrWhiteSpace(indexBy))
            {
                throw new KeenException("A value to index by is required.");
            }

            if (string.IsNullOrWhiteSpace(timeframe))
            {
                throw new KeenException("A timeframe by is required.");
            }

            if (string.IsNullOrWhiteSpace(_readKey))
            {
                throw new KeenException("An API ReadKey is required to get dataset results.");
            }

            var datasetResultsUrl = $"{GetDatasetUrl(datasetName)}/results";

            // Absolute timeframes can have reserved characters like ':', and index_by can be
            // any valid JSON member name, which can have all sorts of stuff, so we escape here.
            var url = $"{datasetResultsUrl}?" +
                      $"index_by={Uri.EscapeDataString(indexBy)}" +
                      $"&timeframe={Uri.EscapeDataString(timeframe)}";

            var responseMsg = await _keenHttpClient
                              .GetAsync(url, _readKey)
                              .ConfigureAwait(continueOnCapturedContext: false);

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

            var response = JObject.Parse(responseString);

            KeenUtil.CheckApiErrorCode(response);

            if (!responseMsg.IsSuccessStatusCode)
            {
                throw new KeenException($"Request failed with status: {responseMsg.StatusCode}");
            }

            return(response);
        }
Exemplo n.º 2
0
        public async Task <JObject> GetResultsAsync(string datasetName,
                                                    string indexBy,
                                                    string timeframe)
        {
            if (string.IsNullOrWhiteSpace(datasetName))
            {
                throw new KeenException("A dataset name is required.");
            }

            if (string.IsNullOrWhiteSpace(indexBy))
            {
                throw new KeenException("A value to index by is required.");
            }

            if (string.IsNullOrWhiteSpace(timeframe))
            {
                throw new KeenException("A timeframe by is required.");
            }

            if (string.IsNullOrWhiteSpace(_masterKey))
            {
                throw new KeenException("An API masterkey is required to get dataset results.");
            }

            var datasetResultsUrl = $"{GetDatasetUrl(datasetName)}/results";

            var url = $"{datasetResultsUrl}?index_by={indexBy}&timeframe={timeframe}";

            var responseMsg = await _keenHttpClient
                              .GetAsync(url, _masterKey)
                              .ConfigureAwait(continueOnCapturedContext: false);

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

            var response = JObject.Parse(responseString);

            KeenUtil.CheckApiErrorCode(response);

            if (!responseMsg.IsSuccessStatusCode)
            {
                throw new KeenException($"Request failed with status: {responseMsg.StatusCode}");
            }

            return(response);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get details of all schemas in the project.
        /// </summary>
        /// <returns></returns>
        public async Task <JArray> GetSchemas()
        {
            if (string.IsNullOrWhiteSpace(_readKey))
            {
                throw new KeenException("An API ReadKey is required to get schemas.");
            }

            var responseMsg = await _keenHttpClient
                              .GetAsync(_eventsRelativeUrl, _readKey)
                              .ConfigureAwait(continueOnCapturedContext: false);

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

            var 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);
        }
Exemplo n.º 4
0
        private async Task <JObject> KeenWebApiRequest(string operation = "",
                                                       Dictionary <string, string> parms = null)
        {
            if (string.IsNullOrWhiteSpace(_key))
            {
                throw new KeenException("An API ReadKey or MasterKey is required.");
            }

            var parmVals = (parms == null) ?
                           "" : string.Join("&", from p in parms.Keys
                                            where !string.IsNullOrEmpty(parms[p])
                                            select string.Format("{0}={1}",
                                                                 p,
                                                                 Uri.EscapeDataString(parms[p])));

            var url = string.Format("{0}{1}{2}",
                                    _queryRelativeUrl,
                                    string.IsNullOrWhiteSpace(operation) ? "" : "/" + operation,
                                    string.IsNullOrWhiteSpace(parmVals) ? "" : "?" + parmVals);

            var responseMsg = await _keenHttpClient.GetAsync(url, _key).ConfigureAwait(false);

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

            var response = JObject.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("Request failed with status: " +
                                        responseMsg.StatusCode);
            }

            return(response);
        }
Exemplo n.º 5
0
        public async Task <JObject> GetSchema(string collection)
        {
            // TODO : So much of this code, both in the constructor and in the actual message
            // dispatch, response parsing and error checking is copy/paste across Queries, Event
            // and EventCollection everywhere we use KeenHttpClient. We could shove some of that
            // into shared factory functionality (for the ctor stuff) and some of it into the
            // KeenHttpClient (for the dispatch/response portions).


            if (string.IsNullOrWhiteSpace(_readKey))
            {
                throw new KeenException("An API ReadKey is required to get collection schema.");
            }

            var responseMsg = await _keenHttpClient
                              .GetAsync(GetCollectionUrl(collection), _readKey)
                              .ConfigureAwait(continueOnCapturedContext: false);

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

            dynamic response = JObject.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("GetSchema failed with status: " + responseMsg.StatusCode);
            }

            return(response);
        }