private IEnumerator TestStep()
        {
            string  username = StringUtils.RandomAlphabetic(20);
            string  password = StringUtils.RandomAlphabetic(20);
            KiiUser user     = KiiUser.BuilderWithName(username).Build();
            var     task     = RegisterUser(user, password, (KiiUser u, Exception e) => {
                if (e != null)
                {
                    throw new TestFailException();
                }
                Debug.Log("Register user.");
            });

            yield return(StartCoroutine(task));

            // create group
            string   groupname = StringUtils.RandomAlphabetic(20);
            KiiGroup group     = Kii.Group(groupname);

            Exception        exp      = null;
            bool             endFlag  = false;
            KiiGroupCallback callback = (KiiGroup grp, Exception e) => {
                exp     = e;
                endFlag = true;
            };

            group.Save(callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }

            if (exp != null)
            {
                throw new TestFailException();
            }

            // refresh
            KiiGroup groupWithId = KiiGroup.GroupWithID(group.ID);

            exp     = null;
            endFlag = false;
            groupWithId.Refresh(callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }
            if (exp != null)
            {
                throw new TestFailException();
            }
            if (!groupname.Equals(groupWithId.Name))
            {
                throw new TestFailException();
            }
            resultFlag = true;
        }
Esempio n. 2
0
        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);
                }
            });
        }
Esempio n. 3
0
        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);
                }
            });
        }
Esempio n. 4
0
        private void RemoveMembersFromCloud(KiiHttpClientFactory factory, KiiGroupCallback callback)
        {
            Utils.CheckInitialize(true);
            string id = ID;

            if (Utils.IsEmpty(id))
            {
                if (callback != null)
                {
                    callback(this, new InvalidOperationException(ErrorInfo.KIIGROUP_NO_ID));
                }
                return;
            }
            List <KiiUser> userList = new List <KiiUser>(removeUsers);

            RemoveMembersFromCloud(userList, 0, factory, callback);
        }
Esempio n. 5
0
        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);
                }
            });
        }
Esempio n. 6
0
 private void ExecSave(KiiHttpClientFactory factory, KiiGroupCallback callback)
 {
     Utils.CheckInitialize(true);
     if (ID == null)
     {
         SaveToCloud(factory, callback);
     }
     else
     {
         AddMembersToCloud(factory, (KiiGroup group, Exception e) =>
         {
             if (e != null)
             {
                 if (callback != null)
                 {
                     callback(this, e);
                 }
                 return;
             }
             RemoveMembersFromCloud(factory, callback);
         });
     }
 }
Esempio n. 7
0
        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);
            });
        }
Esempio n. 8
0
        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);
                }
            });
        }
Esempio n. 9
0
 /// <summary>
 /// Changes the name of this group.
 /// </summary>
 /// <remarks>
 /// New group name must no be empty.
 /// </remarks>
 /// <param name='name'>
 /// New group name.
 /// </param>
 /// <param name='callback'>
 /// Callback.
 /// </param>
 public void ChangeName(string name, KiiGroupCallback callback)
 {
     ExecChangeName(name, Kii.AsyncHttpClientFactory, callback);
 }
Esempio n. 10
0
 /// <summary>
 /// Delete this group from KiiCloud.
 /// </summary>
 /// <remarks>
 /// This API sends a request to KiiCloud.
 /// </remarks>
 /// <param name='callback'>
 /// Callback.
 /// </param>
 public void Delete(KiiGroupCallback callback)
 {
     ExecDelete(Kii.AsyncHttpClientFactory, callback);
 }
Esempio n. 11
0
 /// <summary>
 /// Gets the latest information of this group.
 /// </summary>
 /// <remarks>
 /// This api sends a request to server.
 /// </remarks>
 /// <param name='callback'>
 /// Callback.
 /// </param>
 public void Refresh(KiiGroupCallback callback)
 {
     ExecRefresh(Kii.AsyncHttpClientFactory, callback);
 }
Esempio n. 12
0
 /// <summary>
 /// Save this KiiGroup on KiiCloud
 /// </summary>
 /// <remarks>
 /// This API sends some requests to KiiCloud.
 /// </remarks>
 /// <param name='callback'>
 /// Callback.
 /// </param>
 public void Save(KiiGroupCallback callback)
 {
     ExecSave(Kii.AsyncHttpClientFactory, callback);
 }
        private IEnumerator TestStep()
        {
            string  username = StringUtils.RandomAlphabetic(20);
            string  password = StringUtils.RandomAlphabetic(20);
            KiiUser user     = KiiUser.BuilderWithName(username).Build();
            var     task     = RegisterUser(user, password, (KiiUser u, Exception e) => {
                if (e != null)
                {
                    throw new TestFailException();
                }
                Debug.Log("Register user.");
            });

            yield return(StartCoroutine(task));

            // create group
            string   groupname = StringUtils.RandomAlphabetic(20);
            KiiGroup group     = Kii.Group(groupname);

            Exception        exp      = null;
            bool             endFlag  = false;
            KiiGroupCallback callback = (KiiGroup grp, Exception e) => {
                exp     = e;
                endFlag = true;
            };

            group.Save(callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }

            if (exp != null)
            {
                throw new TestFailException();
            }

            // refresh
            KiiGroup groupWithId = KiiGroup.GroupWithID(group.ID);

            exp     = null;
            endFlag = false;
            groupWithId.Refresh(callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }
            if (exp != null)
            {
                throw new TestFailException();
            }
            if (!groupname.Equals(groupWithId.Name))
            {
                throw new TestFailException();
            }

            // change name
            string newGroupName = StringUtils.RandomAlphabetic(20);

            exp     = null;
            endFlag = false;
            groupWithId.ChangeName(newGroupName, callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }
            if (!newGroupName.Equals(groupWithId.Name))
            {
                throw new TestFailException();
            }

            //check groupname changed in server.
            string     respString  = ApiHelper.get(SDKUtils.GetGroupUFEUri(groupWithId), Kii.AppId, Kii.AppKey, KiiUser.AccessToken);
            JsonObject groupJson   = new JsonObject(respString);
            string     updatedName = groupJson.GetString("name");

            if (!newGroupName.Equals(updatedName))
            {
                throw new TestFailException();
            }
            resultFlag = true;
        }