Ejemplo n.º 1
0
        public async Task DefaultHeaders_Success()
        {
            object responseData = new[] { new { result = 2 } };

            var handler = new FuncHandler()
            {
                PreProcess = (req, ct) =>
                {
                    // Make sure the default headers are in place
                    Assert.IsTrue(req.Headers.Contains("Keen-Sdk"));
                    Assert.AreEqual(KeenUtil.GetSdkVersion(), req.Headers.GetValues("Keen-Sdk").Single());

                    Assert.IsTrue(req.Headers.Contains("Authorization"));

                    var key = req.Headers.GetValues("Authorization").Single();
                    Assert.IsTrue(SettingsEnv.ReadKey == key ||
                                  SettingsEnv.WriteKey == key ||
                                  SettingsEnv.MasterKey == key);
                },
                ProduceResultAsync = (req, ct) =>
                {
                    return(CreateJsonStringResponseAsync(responseData));
                },
                DeferToDefault = false
            };

            var client = new KeenClient(SettingsEnv, new TestKeenHttpClientProvider()
            {
                ProvideKeenHttpClient =
                    (url) => KeenHttpClientFactory.Create(url,
                                                          new HttpClientCache(),
                                                          null,
                                                          new DelegatingHandlerMock(handler))
            });

            // Try out all the endpoints
            Assert.DoesNotThrow(() => client.GetSchemas());

            // Remaining operations expect an object, not an array of objects
            responseData = new { result = 2 };

            var @event = new { AProperty = "AValue" };

            Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", @event));
            Assert.DoesNotThrow(() => client.AddEvents("AddEventTest", new[] { @event }));

            Assert.DoesNotThrow(() => client.DeleteCollection("DeleteColTest"));
            Assert.IsNotNull(client.GetSchema("AddEventTest"));

            // Currently all the queries/extraction go through the same KeenWebApiRequest() call.
            var count = await client.QueryAsync(
                QueryType.Count(),
                "testCollection",
                "",
                QueryRelativeTimeframe.ThisMonth());

            Assert.IsNotNull(count);
            Assert.AreEqual("2", count);
        }
Ejemplo n.º 2
0
        public void GetSdkVersion_Success()
        {
            string sdkVersion = KeenUtil.GetSdkVersion();

            Assert.IsNotNull(sdkVersion);
            Assert.IsNotEmpty(sdkVersion);
            Assert.IsTrue(sdkVersion.StartsWith(".net"));
        }
Ejemplo n.º 3
0
        public void GetSdkVersion_Success()
        {
            // TODO : Decide on a better place for this when we break out tests and do a CC push.
            string sdkVersion = KeenUtil.GetSdkVersion();

            Assert.IsNotNull(sdkVersion);
            Assert.IsNotEmpty(sdkVersion);
            Assert.IsTrue(sdkVersion.StartsWith(".net"));
        }
Ejemplo n.º 4
0
        private async Task <JObject> KeenWebApiRequest(string operation = "", Dictionary <string, string> parms = null)
        {
            // Either an API read key or a master key is required
            var key = string.IsNullOrWhiteSpace(_prjSettings.MasterKey) ? _prjSettings.ReadKey : _prjSettings.MasterKey;

            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}",
                                    _serverUrl,
                                    string.IsNullOrWhiteSpace(operation) ? "" : "/" + operation,
                                    string.IsNullOrWhiteSpace(parmVals) ? "" : "?" + parmVals);

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", key);
                client.DefaultRequestHeaders.Add("Keen-Sdk", KeenUtil.GetSdkVersion());

                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);
            }
        }