Ejemplo n.º 1
0
        public async Task <DatasetDefinition> GetDefinitionAsync(string datasetName)
        {
            if (string.IsNullOrWhiteSpace(datasetName))
            {
                throw new KeenException("A dataset name is required.");
            }

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

            var responseMsg = await _keenHttpClient
                              .GetAsync(GetDatasetUrl(datasetName), _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(JsonConvert.DeserializeObject <DatasetDefinition>(responseString,
                                                                     SerializerSettings));
        }
Ejemplo n.º 2
0
        public async Task DeleteDatasetAsync(string datasetName)
        {
            if (string.IsNullOrWhiteSpace(datasetName))
            {
                throw new KeenException("A dataset name is required.");
            }

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

            var responseMsg = await _keenHttpClient
                              .DeleteAsync(GetDatasetUrl(datasetName), _masterKey)
                              .ConfigureAwait(continueOnCapturedContext: false);

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

            if (HttpStatusCode.NoContent != responseMsg.StatusCode)
            {
                var response = JObject.Parse(responseString);

                KeenUtil.CheckApiErrorCode(response);

                throw new KeenException($"Request failed with status: {responseMsg.StatusCode}");
            }
        }
Ejemplo n.º 3
0
        private async Task <JObject> KeenWebApiRequest(string operation = "", Dictionary <string, string> parms = null)
        {
            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}",
                                    _serverUrl,
                                    string.IsNullOrWhiteSpace(operation) ? "" : "/" + operation,
                                    string.IsNullOrWhiteSpace(parmVals) ? "" : "?" + parmVals);

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", _prjSettings.MasterKey);

                var responseMsg = await client.GetAsync(url).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((dynamic)response);
                if (!responseMsg.IsSuccessStatusCode)
                {
                    throw new KeenException("Request failed with status: " + responseMsg.StatusCode);
                }

                return(response);
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public async Task <DatasetDefinitionCollection> ListDefinitionsAsync(
            int limit        = 10,
            string afterName = null)
        {
            if (string.IsNullOrWhiteSpace(_readKey))
            {
                throw new KeenException("An API ReadKey is required to get dataset results.");
            }

            // limit is just an int, so no need to encode here.
            var datasetResultsUrl = $"{_cachedDatasetRelativeUrl}?limit={limit}";

            if (!string.IsNullOrWhiteSpace(afterName))
            {
                // afterName should be a valid dataset name, which can only be
                // alphanumerics, '_' and '-', so we don't escape here.
                datasetResultsUrl += $"&after_name={afterName}";
            }

            var responseMsg = await _keenHttpClient
                              .GetAsync(datasetResultsUrl, _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(JsonConvert.DeserializeObject <DatasetDefinitionCollection>(responseString,
                                                                               SerializerSettings));
        }
Ejemplo n.º 8
0
        public async Task <DatasetDefinition> CreateDatasetAsync(DatasetDefinition dataset)
        {
            if (string.IsNullOrWhiteSpace(_masterKey))
            {
                throw new KeenException("An API MasterKey is required to get dataset results.");
            }

            // Validate
            if (null == dataset)
            {
                throw new KeenException("An instance of DatasetDefinition must be provided");
            }

            // This throws if dataset is not valid.
            dataset.Validate();

            var content = JsonConvert.SerializeObject(dataset, SerializerSettings);

            var responseMsg = await _keenHttpClient
                              .PutAsync(GetDatasetUrl(dataset.DatasetName), _masterKey, content)
                              .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(JsonConvert.DeserializeObject <DatasetDefinition>(responseString,
                                                                     SerializerSettings));
        }
Ejemplo n.º 9
0
        public async Task <DatasetDefinitionCollection> ListDefinitionsAsync(
            int limit        = 10,
            string afterName = null)
        {
            if (string.IsNullOrWhiteSpace(_masterKey))
            {
                throw new KeenException("An API masterkey is required to get dataset results.");
            }

            var datasetResultsUrl = $"{_cachedDatasetRelativeUrl}?limit={limit}";

            if (!string.IsNullOrWhiteSpace(afterName))
            {
                datasetResultsUrl += $"&after_name={afterName}";
            }

            var responseMsg = await _keenHttpClient
                              .GetAsync(datasetResultsUrl, _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(JsonConvert.DeserializeObject <DatasetDefinitionCollection>(responseString,
                                                                               SERIALIZER_SETTINGS));
        }