Ejemplo n.º 1
0
        /// <summary>
        /// Removes the specified member from their group in GroupMe
        /// </summary>
        /// <param name="group">The <see cref="GroupMeGroup"/> to remove them from</param>
        /// <param name="groupMember">The <see cref="GroupMeGroupMember"/> to remove</param>
        public bool RemoveGroupMember(GroupMeGroup group, GroupMeGroupMember groupMember)
        {
            var request  = new RestRequest($"groups/{ group.Id }/members/{ groupMember.Id }/remove", Method.POST);
            var response = _restClient.Execute(request);

            if (response.ErrorException != null)
            {
                throw response.ErrorException;
            }

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add the specified person to a group in GroupMe
        /// </summary>
        /// <param name="group">The <see cref="GroupMeGroup"/> to add them to</param>
        /// <param name="member">The <see cref="GroupMeGroupMemberAdd"/> to add to the group</param>
        /// <param name="memberId">The Id of the added member, if they are successfully added</param>
        public bool AddGroupMember(GroupMeGroup group, GroupMeGroupMemberAdd member, out int memberId)
        {
            memberId = 0;
            var memberList = new List <GroupMeGroupMemberAdd>();

            memberList.Add(member);
            var memberWrapper = new { members = memberList };
            var addRequest    = new RestRequest($"groups/{ group.Id }/members/add", Method.POST);

            addRequest.RequestFormat = DataFormat.Json;
            addRequest.AddParameter("application/json", JObject.FromObject(memberWrapper), ParameterType.RequestBody);
            var addResponse = _restClient.Execute(addRequest);

            if (addResponse.ErrorException != null)
            {
                throw addResponse.ErrorException;
            }

            if (addResponse.StatusCode == HttpStatusCode.Accepted)
            {
                var addResponseObject = (dynamic)JObject.Parse(addResponse.Content);
                var resultsId         = addResponseObject.response.results_id;

                var resultRequest  = new RestRequest($"groups/{ group.Id }/members/results/{ resultsId }", Method.GET);
                var resultResponse = _restClient.Execute(resultRequest);

                // According to GroupMe's API docs, 503 = try later
                while (resultResponse.StatusCode == HttpStatusCode.ServiceUnavailable)
                {
                    Task.Delay(500).Wait();
                    resultResponse = _restClient.Execute(resultRequest);
                }

                if (resultResponse.StatusCode == HttpStatusCode.OK)
                {
                    var resultResponseObject = JObject.Parse(resultResponse.Content);
                    var addedGroupMember     = resultResponseObject.SelectToken("response.members[0]").ToObject <GroupMeGroupMember>();
                    memberId = addedGroupMember.Id.AsInteger();

                    return(true);
                }
            }

            return(false);
        }