public static void Validate(this DatasetDefinition dataset)
        {
            if (string.IsNullOrWhiteSpace(dataset.DatasetName))
            {
                throw new KeenException("DatasetDefinition must have a name.");
            }

            if (string.IsNullOrWhiteSpace(dataset.DisplayName))
            {
                throw new KeenException("DatasetDefinition must have a display name.");
            }

            if (null == dataset.IndexBy ||
                string.IsNullOrWhiteSpace(dataset.IndexBy.FirstOrDefault()))
            {
                throw new KeenException("DatasetDefinition must specify a property by which to " +
                                        "index.");
            }

            if (null == dataset.Query)
            {
                throw new KeenException("DatasetDefinition must contain a query to be cached.");
            }

            dataset.Query.ValidateForCachedDataset();
        }
Example #2
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));
        }