private KiiHttpClient GetHttpClient(KiiHttpClientFactory factory, string url, ACLOperation operation) { switch (operation) { case ACLOperation.GRANT: return(factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.PUT)); case ACLOperation.REVOKE: return(factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.DELETE)); default: return(null); } }
private void ExecIsSubscribed(KiiSubscribable target, KiiHttpClientFactory factory, KiiCheckSubscriptionCallback callback) { if (callback == null) { throw new ArgumentNullException("KiiCheckSubscriptionCallback must not be null"); } if (target == null) { callback(target, false, new ArgumentNullException("KiiSubscribable must not be null")); return; } Utils.CheckInitialize(true); KiiHttpClient client = factory.Create(ToUrl(target), Kii.AppId, Kii.AppKey, KiiHttpMethod.GET); KiiCloudEngine.SetAuthBearer(client); client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { if (e is NotFoundException) { callback(target, false, null); } else { callback(target, false, e); } } else { callback(target, response.IsSuccess(), e); } }); }
private void ExecDelete(KiiHttpClientFactory factory, KiiBucketCallback callback) { Utils.CheckInitialize(false); KiiHttpClient client = factory.Create(Url, Kii.AppId, Kii.AppKey, KiiHttpMethod.DELETE); KiiCloudEngine.SetAuthBearer(client); // send request client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(this, e); } return; } if (callback != null) { callback(this, null); } }); }
private static void ExecUpload(KiiHttpClientFactory httpClientFactory, KiiEventCallback callback, KiiEvent[] eventList) { JsonArray array = new JsonArray(); foreach (KiiEvent ev in eventList) { if (ev == null) { throw new ArgumentException(ErrorInfo.KIIANALYTICS_EVENT_NULL); } if (ev.Sent) { throw new ArgumentException(ErrorInfo.KIIANALYTICS_EVENT_ALREADY_SENT); } if (ev.GetType() == typeof(KiiEvent.NullKiiEvent)) { continue; } array.Put(ev.ConvertToJsonObject(INSTANCE.mDeviceID)); } string url = KiiAnalytics.BaseUrl + "/apps/" + KiiAnalytics.AppID + "/events"; string body = array.ToString(); KiiHttpClient client = httpClientFactory.Create(url, KiiAnalytics.AppID, KiiAnalytics.AppKey, KiiHttpMethod.POST); client.ContentType = "application/vnd.kii.EventRecordList+json"; client.Body = body; client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { if (!(e is CloudException)) { InvokeUploadCallback(callback, e); } else { CloudException exp = (CloudException)e; InvokeUploadCallback(callback, new EventUploadException(exp.Status, exp.Body, eventList)); } return; } if (response.Status == 200) { try { List <KiiEvent> errorEventList = ParsePartialSuccessResponse(response.Body, eventList); InvokeUploadCallback(callback, new EventUploadException(response.Status, response.Body, errorEventList)); } catch (Exception ex) { InvokeUploadCallback(callback, ex); } return; } foreach (KiiEvent ev in eventList) { ev.Sent = true; } InvokeUploadCallback(callback, null); }); }
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); } }); }
private void RemoveMembersFromCloud(IList <KiiUser> userList, int position, KiiHttpClientFactory factory, KiiGroupCallback callback) { if (position == userList.Count) { if (callback != null) { callback(this, null); } return; } KiiUser user = userList[position]; string url = Utils.Path(MembersUrl, user.ID); KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.DELETE); KiiCloudEngine.SetAuthBearer(client); // send request client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(this, new GroupOperationException(e.Message, e, new List <KiiUser>(addUsers), new List <KiiUser>(removeUsers))); } return; } removeUsers.Remove(user); RemoveMembersFromCloud(userList, position + 1, factory, callback); }); }
private void ExecCount(KiiQuery query, KiiHttpClientFactory factory, CountCallback callback) { Utils.CheckInitialize(false); if (query == null) { query = new KiiQuery(null); } KiiHttpClient client = factory.Create(QueryUrl, Kii.AppId, Kii.AppKey, KiiHttpMethod.POST); KiiCloudEngine.SetAuthBearer(client); client.ContentType = "application/vnd.kii.QueryRequest+json"; // send request string queryString = CountAggregationQuery(query); int count = 0; client.SendRequest(queryString, (ApiResponse response, Exception e) => { if (e != null) { callback(this, query, count, e); return; } // parse response try { JsonObject responseJson = new JsonObject(response.Body); JsonObject aggregations = responseJson.GetJsonObject("aggregations"); count = aggregations.GetInt("count_field"); callback(this, query, count, null); } catch (JsonException jse) { callback(this, query, count, new IllegalKiiBaseObjectFormatException(jse.Message)); } }); }
private void ExecInstall(string deviceID, DeviceType deviceType, KiiHttpClientFactory factory, KiiPushInstallationCallback callback) { if (callback == null) { throw new ArgumentNullException("KiiPushInstallationCallback must not be null"); } if (Utils.IsEmpty(deviceID)) { callback(new ArgumentException(ErrorInfo.KIIPUSHINSTALLATION_DEVICE_ID_NULL)); return; } Utils.CheckInitialize(true); string url = Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "installations"); JsonObject requestBody = new JsonObject(); requestBody.Put("installationRegistrationID", deviceID); requestBody.Put("deviceType", Enum.GetName(typeof(DeviceType), deviceType)); if (this.development) { requestBody.Put("development", true); } KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.POST); KiiCloudEngine.SetAuthBearer(client); client.ContentType = "application/vnd.kii.InstallationCreationRequest+json"; client.SendRequest(requestBody.ToString(), (ApiResponse response, Exception e) => { callback(e); }); }
private void ExecExecute(KiiServerCodeEntryArgument argument, KiiHttpClientFactory factory, KiiServerCodeEntryCallback callback) { if (argument == null) { argument = KiiServerCodeEntryArgument.NewArgument(new JsonObject()); } string url = Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "server-code", "versions", this.version, this.entryName); if (this.environmentVersion.HasValue) { url += "?environment-version=" + this.environmentVersion.Value.GetValue(); } KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.POST); KiiCloudEngine.SetAuthBearer(client); client.ContentType = "application/json"; client.SendRequest(argument.ToJson().ToString(), (ApiResponse response, Exception e) => { if (callback == null) { return; } if (e != null) { callback(this, argument, null, e); return; } // parse X-step-count int steps; try { steps = int.Parse(response.GetHeader("X-Step-count")); } catch { steps = 0; } // X-Environment-version KiiServerCodeEnvironmentVersion?environmentVersion = KiiServerCodeEnvironmentVersionExtensions.FromValue(response.GetHeader("X-Environment-version")); // parse body JsonObject resultBody; try { resultBody = new JsonObject(response.Body); } catch (JsonException e2) { callback(this, argument, null, new IllegalKiiBaseObjectFormatException(e2.Message)); return; } callback(this, argument, new KiiServerCodeExecResult(resultBody, steps, environmentVersion), null); }); }
private void ExecQuery(KiiQuery query, KiiHttpClientFactory factory, KiiQueryCallback <KiiObject> callback) { Utils.CheckInitialize(false); KiiHttpClient client = factory.Create(QueryUrl, Kii.AppId, Kii.AppKey, KiiHttpMethod.POST); KiiCloudEngine.SetAuthBearer(client); client.ContentType = "application/vnd.kii.QueryRequest+json"; if (query == null) { query = new KiiQuery(null); } // send request client.SendRequest(query.ToString(), (ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(null, e); } return; } // parse response KiiQueryResult <KiiObject> queryResult = null; try { JsonObject obj = new JsonObject(response.Body); JsonArray array = obj.GetJsonArray("results"); String nextPaginationKey = obj.OptString("nextPaginationKey"); queryResult = new KiiQueryResult <KiiObject>(query, nextPaginationKey, this, false); for (int i = 0; i < array.Length(); i++) { JsonObject entry = array.GetJsonObject(i); KiiObject kiiObject = new KiiObject(mParent, mBucketName, entry); queryResult.Add(kiiObject); } } catch (JsonException e2) { if (callback != null) { callback(null, new IllegalKiiBaseObjectFormatException(e2.Message)); } return; } if (callback != null) { callback(queryResult, null); } }); }
private void ExecListAclEntries(KiiHttpClientFactory factory, KiiACLListCallback <T, U> callback) { if (callback == null) { throw new ArgumentNullException("KiiACLListCallback must not be null"); } try { Utils.CheckInitialize(true); } catch (Exception e) { callback(null, e); return; } string id = ParentID; if (Utils.IsEmpty(id)) { callback(null, new InvalidOperationException("Topic does not exist in the cloud.")); return; } // Fetch ACL string aclUrl = Utils.Path(ParentUrl, "acl"); KiiHttpClient client = factory.Create(aclUrl, Kii.AppId, Kii.AppKey, KiiHttpMethod.GET); KiiCloudEngine.SetAuthBearer(client); // send request client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { callback(null, e); return; } // parse response IList <KiiACLEntry <T, U> > list = null; try { JsonObject respObj = new JsonObject(response.Body); list = ParseListResponse(respObj); } catch (JsonException) { callback(null, new IllegalKiiBaseObjectFormatException(response.Body)); return; } callback(list, null); }); }
private void ExecRefresh(KiiHttpClientFactory factory, KiiGroupCallback callback) { Utils.CheckInitialize(true); if (Utils.IsEmpty(ID)) { if (callback != null) { callback(this, new InvalidOperationException(ErrorInfo.KIIGROUP_NO_ID)); } return; } KiiHttpClient client = factory.Create(Url, Kii.AppId, Kii.AppKey, KiiHttpMethod.GET); KiiCloudEngine.SetAuthBearer(client); client.Accept = "application/vnd.kii.GroupRetrievalResponse+json"; // send request client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(this, e); } return; } // parse response try { JsonObject respObj = new JsonObject(response.Body); mId = respObj.GetString("groupID"); mOwnerId = respObj.OptString("owner", null); groupName = respObj.GetString("name"); } catch (JsonException) { if (callback != null) { callback(this, new IllegalKiiBaseObjectFormatException(response.Body)); } return; } if (callback != null) { callback(this, null); } }); }
private void ExecSave(KiiHttpClientFactory factory, KiiTopicCallback callback) { if (callback == null) { throw new ArgumentNullException("KiiTopicCallback must not be null"); } Utils.CheckInitialize(false); KiiHttpClient client = factory.Create(Url, Kii.AppId, Kii.AppKey, KiiHttpMethod.PUT); KiiCloudEngine.SetAuthBearer(client); client.SendRequest((ApiResponse response, Exception e) => { callback(this, e); }); }
private void ExecChangeName(string name, KiiHttpClientFactory factory, KiiGroupCallback callback) { Utils.CheckInitialize(true); if (Utils.IsEmpty(name)) { if (callback != null) { callback(this, new ArgumentException("provided name is null")); } return; } string id = ID; if (Utils.IsEmpty(id)) { if (callback != null) { callback(this, new InvalidOperationException(ErrorInfo.KIIGROUP_NO_ID)); } return; } string url = Utils.Path(Url, "name"); KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.PUT); KiiCloudEngine.SetAuthBearer(client); client.ContentType = "text/plain"; // send request client.SendRequest(name, (ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(this, e); } return; } this.groupName = name; if (callback != null) { callback(this, null); } }); }
private void ExecListTopics(KiiHttpClientFactory factory, string paginationKey, KiiGenericsCallback <KiiListResult <KiiTopic> > callback) { if (callback == null) { throw new ArgumentNullException("KiiGenericsCallback must not be null"); } try { Utils.CheckInitialize(true); } catch (Exception e) { callback(null, e); return; } if (Utils.IsEmpty(ID)) { callback(null, new InvalidOperationException(ErrorInfo.KIIGROUP_NO_ID)); return; } String url = Utils.Path(Url, "topics"); if (!String.IsNullOrEmpty(paginationKey)) { url = url + "?paginationKey=" + Uri.EscapeUriString(paginationKey); } KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.GET); KiiCloudEngine.SetAuthBearer(client); client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { callback(null, e); return; } JsonObject json = new JsonObject(response.Body); String newPaginationKey = json.OptString("paginationKey", null); JsonArray array = json.GetJsonArray("topics"); List <KiiTopic> topics = new List <KiiTopic>(); for (int i = 0; i < array.Length(); i++) { topics.Add(this.Topic(array.GetJsonObject(i).GetString("topicID"))); } callback(new KiiListResult <KiiTopic>(topics, newPaginationKey), null); }); }
private void ExecUnsubscribe(KiiSubscribable target, KiiHttpClientFactory factory, KiiSubscriptionCallback callback) { if (callback == null) { throw new ArgumentNullException("KiiSubscriptionCallback must not be null"); } if (target == null) { callback(target, new ArgumentNullException("KiiSubscribable must not be null")); return; } Utils.CheckInitialize(true); KiiHttpClient client = factory.Create(ToUrl(target), Kii.AppId, Kii.AppKey, KiiHttpMethod.DELETE); KiiCloudEngine.SetAuthBearer(client); client.SendRequest((ApiResponse response, Exception e) => { callback(target, e); }); }
private void ExecSendMessage(KiiPushMessage message, KiiHttpClientFactory factory, KiiPushMessageCallback callback) { if (callback == null) { throw new ArgumentNullException("KiiPushMessageCallback must not be null"); } if (message == null) { callback(message, new ArgumentNullException("KiiPushMessage must not be null")); return; } Utils.CheckInitialize(false); KiiHttpClient client = factory.Create(MessageUrl, Kii.AppId, Kii.AppKey, KiiHttpMethod.POST); KiiCloudEngine.SetAuthBearer(client); client.ContentType = "application/vnd.kii.SendPushMessageRequest+json"; client.SendRequest(message.ToJson().ToString(), (ApiResponse response, Exception e) => { callback(message, e); }); }
private void ExecDelete(KiiHttpClientFactory factory, KiiGroupCallback callback) { Utils.CheckInitialize(true); if (Utils.IsEmpty(ID)) { if (callback != null) { callback(this, new InvalidOperationException(ErrorInfo.KIIGROUP_NO_ID)); } return; } KiiHttpClient client = factory.Create(Url, Kii.AppId, Kii.AppKey, KiiHttpMethod.DELETE); KiiCloudEngine.SetAuthBearer(client); client.Accept = "application/vnd.kii.GroupRetrievalResponse+json"; // send request client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(this, e); } return; } // clear id and members mId = null; mOwnerId = null; addUsers.Clear(); removeUsers.Clear(); if (callback != null) { callback(this, null); } }); }
private void ExecUninstall(string deviceID, DeviceType deviceType, KiiHttpClientFactory factory, KiiPushInstallationCallback callback) { if (callback == null) { throw new ArgumentNullException("KiiPushInstallationCallback must not be null"); } if (Utils.IsEmpty(deviceID)) { callback(new ArgumentException(ErrorInfo.KIIPUSHINSTALLATION_DEVICE_ID_NULL)); return; } Utils.CheckInitialize(true); string url = Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "installations", Enum.GetName(typeof(DeviceType), deviceType) + ":" + deviceID); KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.DELETE); KiiCloudEngine.SetAuthBearer(client); client.SendRequest((ApiResponse response, Exception e) => { callback(e); }); }
private void ExecExists(KiiHttpClientFactory factory, KiiGenericsCallback <Boolean?> callback) { if (callback == null) { throw new ArgumentNullException("KiiGenericsCallback must not be null"); } try { Utils.CheckInitialize(true); } catch (Exception e) { callback(null, e); return; } KiiHttpClient client = factory.Create(Url, Kii.AppId, Kii.AppKey, KiiHttpMethod.HEAD); KiiCloudEngine.SetAuthBearer(client); client.SendRequest((ApiResponse response, Exception e) => { if (e == null) { callback(true, null); } else { if (e is NotFoundException) { callback(false, null); } else { callback(null, e); } } }); }
private static void ExecRegisterWithID(string id, string name, IList <KiiUser> members, KiiHttpClientFactory factory, KiiGenericsCallback <KiiGroup> callback) { try { Utils.CheckInitialize(true); } catch (Exception e) { callback(null, e); return; } if (String.IsNullOrEmpty(id)) { callback(null, new ArgumentException("id is null or empty.")); return; } if (!Utils.ValidateGroupID(id)) { callback(null, new ArgumentException("Invalid groupID format. " + id)); return; } if (String.IsNullOrEmpty(name)) { callback(null, new ArgumentException("name is null or empty.")); return; } String ownerId = Kii.CurrentUser.ID; string url = Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "groups", id); KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.PUT); KiiCloudEngine.SetAuthBearer(client); client.Accept = "application/vnd.kii.GroupCreationResponse+json"; JsonObject request = new JsonObject(); try { request.Put("name", name); request.Put("owner", ownerId); if (members != null && members.Count > 0) { JsonArray memberIDs = new JsonArray(); foreach (KiiUser member in members) { if (!Utils.IsEmpty(member.ID)) { memberIDs.Put(member.ID); } } request.Put("members", memberIDs); } } catch (JsonException e) { if (callback != null) { callback(null, new SystemException("unexpected error!", e)); } return; } client.ContentType = "application/vnd.kii.GroupCreationRequest+json"; // send Request client.SendRequest(request.ToString(), (ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(null, new GroupOperationException(e.Message, e, members, null)); } return; } // parse response try { JsonObject respObj = new JsonObject(response.Body); KiiGroup group = new KiiGroup(name, members); group.mId = respObj.GetString("groupID"); group.mOwnerId = ownerId; if (callback != null) { callback(group, null); } } catch (JsonException) { if (callback != null) { callback(null, new IllegalKiiBaseObjectFormatException(response.Body)); } return; } }); }
private void SaveToCloud(KiiHttpClientFactory factory, KiiGroupCallback callback) { Utils.CheckInitialize(true); mOwnerId = Kii.CurrentUser.ID; string url = Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "groups"); KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.POST); KiiCloudEngine.SetAuthBearer(client); client.Accept = "application/vnd.kii.GroupCreationResponse+json"; JsonObject obj = new JsonObject(); try { obj.Put("name", groupName); obj.Put("owner", mOwnerId); obj.Put("members", UserIds()); } catch (JsonException e1) { if (callback != null) { callback(this, new SystemException("unexpected error!", e1)); } return; } client.ContentType = "application/vnd.kii.GroupCreationRequest+json"; // send Request client.SendRequest(obj.ToString(), (ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(this, new GroupOperationException(e.Message, e, new List <KiiUser>(addUsers), null)); } return; } // parse response try { JsonObject respObj = new JsonObject(response.Body); mId = respObj.GetString("groupID"); // TODO: parse response and get list of failed user (CMO-557) } catch (JsonException) { if (callback != null) { callback(this, new IllegalKiiBaseObjectFormatException(response.Body)); } return; } addUsers.Clear(); if (callback != null) { callback(this, null); } }); }
private void ExecListMembers(KiiHttpClientFactory factory, KiiUserListCallback callback) { Utils.CheckInitialize(true); string groupId = ID; if (Utils.IsEmpty(groupId)) { if (callback != null) { callback(null, new InvalidOperationException(ErrorInfo.KIIGROUP_NO_ID)); } return; } string getUrl = Utils.Path(MembersUrl); KiiHttpClient client = factory.Create(getUrl, Kii.AppId, Kii.AppKey, KiiHttpMethod.GET); KiiCloudEngine.SetAuthBearer(client); client.Accept = "application/vnd.kii.MembersRetrievalResponse+json"; // send request client.SendRequest((ApiResponse response, Exception e) => { if (e != null) { if (callback != null) { callback(null, e); } return; } // parse response List <KiiUser> members = new List <KiiUser>(); try { JsonObject respObj = new JsonObject(response.Body); JsonArray array = respObj.GetJsonArray("members"); if (array == null || array.Length() == 0) { if (callback != null) { callback(members, null); } return; } for (int i = 0; i < array.Length(); i++) { JsonObject obj = array.GetJsonObject(i); string id = obj.GetString("userID"); if (Utils.IsEmpty(id)) { callback(null, new IllegalKiiBaseObjectFormatException(response.Body)); return; } KiiUser user = KiiUser.UserWithID(id); members.Add(user); } } catch (JsonException) { if (callback != null) { callback(null, new IllegalKiiBaseObjectFormatException(response.Body)); } return; } if (callback != null) { callback(members, null); } }); }