private static void ExecGetResult(string ruleId, ResultCondition condition, KiiHttpClientFactory factory, KiiResultCallback callback) { string url = KiiAnalytics.BaseUrl + "/apps/" + KiiAnalytics.AppID + "/analytics/" + ruleId + "/data"; if (condition != null) { url += "?" + condition.ToQueryString(); } KiiHttpClient client = factory.Create(url, KiiAnalytics.AppID, KiiAnalytics.AppKey, KiiHttpMethod.GET); client.Accept = "application/vnd.kii.GroupedAnalyticResult+json"; client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { invokeResultCallback(callback, ruleId, condition, null, e); return; } try { JsonObject obj = new JsonObject(response.Body); JsonArray snapshots = obj.GetJsonArray("snapshots"); GroupedResult result = GroupedResult.Parse(snapshots); invokeResultCallback(callback, ruleId, condition, result, null); } catch (JsonException) { Exception ex = new IllegalKiiBaseObjectFormatException("Server response is broken."); invokeResultCallback(callback, ruleId, condition, null, ex); } }); }
public void Test_1_3_GetResult_Fail() { // set mock MockHttpClientFactory factory = new MockHttpClientFactory(); KiiAnalytics.AsyncHttpClientFactory = factory; MockHttpClient client = factory.Client; client.AddResponse(new CloudException(400, "")); GroupedResult result = null; string ruleId = null; ResultCondition condition = null; Exception exp = null; CountDownLatch cd = new CountDownLatch(1); KiiAnalytics.GetResult("a", null, (string id, ResultCondition c, GroupedResult r, Exception e) => { Interlocked.Exchange(ref result, r); Interlocked.Exchange(ref ruleId, id); Interlocked.Exchange(ref condition, c); Interlocked.Exchange(ref exp, e); cd.Signal(); }); if (!cd.Wait(new TimeSpan(0, 0, 0, 3))) { Assert.Fail("Callback not fired."); } Assert.AreEqual(ruleId, "a"); Assert.IsNull(condition); Assert.IsNull(result); Assert.IsNotNull(exp); Assert.True(exp is CloudException); }
public void Test_1_2_GetResult_RuleIdNotExists() { GroupedResult result = null; string ruleId = null; ResultCondition condition = null; Exception exp = null; CountDownLatch cd = new CountDownLatch(1); KiiAnalytics.GetResult("0001", null, (string id, ResultCondition c, GroupedResult r, Exception e) => { Interlocked.Exchange(ref result, r); Interlocked.Exchange(ref ruleId, id); Interlocked.Exchange(ref condition, c); Interlocked.Exchange(ref exp, e); cd.Signal(); }); if (!cd.Wait(new TimeSpan(0, 0, 0, 3))) { Assert.Fail("Callback not fired."); } Assert.AreEqual(ruleId, "0001"); Assert.IsNull(condition); Assert.IsNull(result); Assert.IsNotNull(exp); Assert.That(exp, Is.InstanceOf <CloudException> ()); CloudException ex = (CloudException)exp; Assert.AreEqual(404, ex.Status); JsonObject json = new JsonObject(ex.Body); Assert.AreEqual("NOT_FOUND", json.GetString("errorCode")); }
/// <summary> /// Asynchronously retrieve analytics result. /// </summary> /// <remarks> /// This API will send a request to Kii Cloud. /// </remarks> /// <param name='ruleId'> /// Rule identifier. Must not be null. /// </param> /// <param name='condition'> /// Condition. /// </param> /// <param name='callback'> /// KiiResultCallback. /// </param> /// <exception cref='ArgumentException'> /// Is thrown when ruleId is null or empty. /// </exception> public static void GetResult(string ruleId, ResultCondition condition, KiiResultCallback callback) { if (Utils.IsEmpty(ruleId)) { throw new ArgumentException(ErrorInfo.KIIANALYTICS_RULE_ID_NULL); } ExecGetResult(ruleId, condition, KiiAnalytics.AsyncHttpClientFactory, callback); }
/// <summary> /// Gets the result of analytics. /// </summary> /// <remarks> /// This API will send a request to Kii Cloud. /// </remarks> /// <returns> /// The grouped result. /// </returns> /// <param name='ruleId'> /// Rule identifier. Must not be null. /// </param> /// <param name='condition'> /// Condition. /// </param> /// <exception cref='ArgumentException'> /// Is thrown when ruleId is null or empty. /// </exception> /// <exception cref="CloudException"> /// Is thrown when KiiCloud sends error response. /// </exception> public static GroupedResult GetResult(string ruleId, ResultCondition condition) { if (Utils.IsEmpty(ruleId)) { throw new ArgumentException(ErrorInfo.KIIANALYTICS_RULE_ID_NULL); } GroupedResult result = null; ExecGetResult(ruleId, condition, KiiAnalytics.HttpClientFactory, (string id, ResultCondition c, GroupedResult r, Exception e) => { if (e != null) { throw e; } result = r; }); return(result); }
public void Test_1_1_GetResult() { GroupedResult result = null; string ruleId = null; ResultCondition condition = null; Exception exp = null; CountDownLatch cd = new CountDownLatch(1); KiiAnalytics.GetResult(RULE_ID, null, (string id, ResultCondition c, GroupedResult r, Exception e) => { Interlocked.Exchange(ref result, r); Interlocked.Exchange(ref ruleId, id); Interlocked.Exchange(ref condition, c); Interlocked.Exchange(ref exp, e); cd.Signal(); }); if (!cd.Wait(new TimeSpan(0, 0, 0, 3))) { Assert.Fail("Callback not fired."); } Assert.AreEqual(ruleId, RULE_ID); Assert.IsNull(condition); Assert.IsNotNull(result); Assert.IsNull(exp); Assert.IsNotNull(result.SnapShots); IList <GroupedSnapShot> snapshots = result.SnapShots; Assert.AreEqual(1, snapshots.Count); GroupedSnapShot snapShot1 = snapshots[0]; Assert.AreEqual("DEFAULT", snapShot1.Name); Assert.IsTrue(snapShot1.PointStart > 0); Assert.IsTrue(snapShot1.PointInterval > 0); JsonArray data = snapShot1.Data; Assert.AreEqual(1, data.Length()); }
public void Test_1_2_GetResult_ConditionNull() { // set mock MockHttpClientFactory factory = new MockHttpClientFactory(); KiiAnalytics.AsyncHttpClientFactory = factory; MockHttpClient client = factory.Client; client.AddResponse(200, "{" + "\"snapshots\" : [ {" + "\"name\" : \"Male\"," + "\"data\" : [ 546.775207 ]," + "\"pointStart\" : 1338044400000," + "\"pointInterval\" : 86400000}, " + "{" + "\"name\" : \"Female\"," + "\"data\" : [ 325.216381 ]," + "\"pointStart\" : 1338044400000," + "\"pointInterval\" : 86400000}" + "]}"); GroupedResult result = null; string ruleId = null; ResultCondition condition = null; Exception exp = null; CountDownLatch cd = new CountDownLatch(1); KiiAnalytics.GetResult("a", null, (string id, ResultCondition c, GroupedResult r, Exception e) => { Interlocked.Exchange(ref result, r); Interlocked.Exchange(ref ruleId, id); Interlocked.Exchange(ref condition, c); Interlocked.Exchange(ref exp, e); cd.Signal(); }); if (!cd.Wait(new TimeSpan(0, 0, 0, 3))) { Assert.Fail("Callback not fired."); } Assert.AreEqual(ruleId, "a"); Assert.IsNull(condition); Assert.IsNotNull(result); Assert.IsNull(exp); Assert.IsNotNull(result.SnapShots); IList <GroupedSnapShot> snapshots = result.SnapShots; Assert.AreEqual(2, snapshots.Count); GroupedSnapShot snapShot1 = snapshots[0]; Assert.AreEqual("Male", snapShot1.Name); Assert.AreEqual(1338044400000, snapShot1.PointStart); Assert.AreEqual(86400000, snapShot1.PointInterval); JsonArray data = snapShot1.Data; Assert.AreEqual(1, data.Length()); GroupedSnapShot snapShot2 = snapshots[1]; Assert.AreEqual("Female", snapShot2.Name); Assert.AreEqual(1338044400000, snapShot2.PointStart); Assert.AreEqual(86400000, snapShot2.PointInterval); data = snapShot2.Data; Assert.AreEqual(1, data.Length()); }
private static void invokeResultCallback(KiiResultCallback callback, string ruleId, ResultCondition condition, GroupedResult result, Exception e) { if (callback != null) { callback(ruleId, condition, result, e); } }