/// <summary>
        /// Assign contacts to the specified list.
        /// </summary>
        /// <param name="list">ContactList object</param>
        /// <param name="contacts">Contacts</param>
        /// <returns></returns>
        public LinkResult AddContactsToList(ContactList list, List<Contact> contacts)
        {
            List<int> contactIds = new List<int>();
            foreach (var contact in contacts)
            {
                contactIds.Add(contact.Id);
            }

            return AddContactsToList(list.Id, contactIds.ToArray());
        }
        public void ShouldAddContactsToList()
        {
            var contactId1 = 12345;
            var contactId2 = 23456;
            var contactId3 = 34567;

            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<LinkResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new LinkResult());
            var client = mockClient.Object;

            var contact1 = new Contact() { Id = contactId1 };
            var contact2 = new Contact() { Id = contactId2 };
            var contact3 = new Contact() { Id = contactId3 };
            int[] contactIds = { contactId1, contactId2, contactId3 };
            var contacts = new System.Collections.Generic.List<Contact> { contact1, contact2, contact3 };
            var list = new ContactList() { Id = listId };
            client.AddContactsToList(list, contacts);

            mockClient.Verify(trc => trc.Execute<LinkResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("lists/{id}/contacts", savedRequest.Resource);
            Assert.AreEqual(Method.PUT, savedRequest.Method);
            Assert.AreEqual(2, savedRequest.Parameters.Count);
            Assert.AreEqual(listId.ToString(), savedRequest.Parameters.Find(x => x.Name == "id").Value);
            Assert.AreEqual(string.Join(",", contactIds), savedRequest.Parameters.Find(x => x.Name == "contacts").Value);

            var content = "{ \"id\": \"31337\", \"href\": \"/api/v2/lists/31337/contacts\"}";

            var testClient = Common.CreateClient<LinkResult>(content, null, null);
            client = new Client(testClient);

            var link = client.AddContactsToList(list, contacts);

            Assert.IsTrue(link.Success);
            Assert.AreEqual(31337, link.Id);
            Assert.AreEqual("/api/v2/lists/31337/contacts", link.Href);
        }
        /// <summary>
        /// Update existing list.
        /// </summary>
        /// <param name="list">ContactList object</param>
        /// <returns></returns>
        public LinkResult UpdateList(ContactList list)
        {
            var request = new RestRequest(Method.PUT);
            request.Resource = "lists/{id}";
            request.AddUrlSegment("id", list.Id.ToString());
            request.AddParameter("name", list.Name);
            request.AddParameter("shared", list.Shared? "1" : "0");

            return Execute<LinkResult>(request);
        }
 /// <summary>
 /// Fetch user contacts by given list id.
 /// </summary>
 /// <param name="list">ContactList object</param>
 /// <param name="page">Fetch specified results page</param>
 /// <param name="limit">How many results to return</param>
 /// <returns></returns>
 public ContactsResult GetListContacts(ContactList list, int? page, int? limit)
 {
     return GetListContacts(list.Id, page, limit);
 }
 /// <summary>
 /// Delete a single list.
 /// </summary>
 /// <param name="id">List ID</param>
 /// <returns></returns>
 public DeleteResult DeleteList(ContactList list)
 {
     return DeleteList(list.Id);
 }
        /// <summary>
        /// Unassign contacts from the specified list.
        /// </summary>
        /// <param name="list">ContactList object</param>
        /// <param name="contacts">Contacts</param>
        /// <returns></returns>
        public DeleteResult DeleteContactsFromList(ContactList list, List<Contact> contacts)
        {
            List<int> contactIds = new List<int>();
            foreach (var contact in contacts)
            {
                contactIds.Add(contact.Id);
            }

            return DeleteContactsFromList(list.Id, contactIds.ToArray());
        }
        public void ShouldUpdateList()
        {
            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<LinkResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new LinkResult());
            var client = mockClient.Object;

            var list = new ContactList()
            {
                Id = listId,
                Name = listName,
                Description = listDescription,
                MembersCount = listMembersCount,
                Shared = listIsShared,
            };
            client.UpdateList(list);

            mockClient.Verify(trc => trc.Execute<LinkResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("lists/{id}", savedRequest.Resource);
            Assert.AreEqual(Method.PUT, savedRequest.Method);
            Assert.AreEqual(3, savedRequest.Parameters.Count);
            Assert.AreEqual(listId.ToString(), savedRequest.Parameters.Find(x => x.Name == "id").Value);
            Assert.AreEqual(listName, savedRequest.Parameters.Find(x => x.Name == "name").Value);
            Assert.AreEqual("0", savedRequest.Parameters.Find(x => x.Name == "shared").Value);

            var content = "{ \"id\": \"31337\", \"href\": \"/api/v2/lists/31337\"}";

            var testClient = Common.CreateClient<LinkResult>(content, null, null);
            client = new Client(testClient);

            var link = client.UpdateList(list);

            Assert.IsTrue(link.Success);
            Assert.AreEqual(31337, link.Id);
            Assert.AreEqual("/api/v2/lists/31337", link.Href);
        }
        public void ShouldGetContactsInList()
        {
            var page = 2;
            var limit = 3;
            var list = new ContactList() { Id = listId };

            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<ContactsResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new ContactsResult());
            var client = mockClient.Object;

            client.GetListContacts(list.Id, page, limit);

            mockClient.Verify(trc => trc.Execute<ContactsResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("lists/{id}/contacts", savedRequest.Resource);
            Assert.AreEqual(Method.GET, savedRequest.Method);
            Assert.AreEqual(3, savedRequest.Parameters.Count);
            Assert.AreEqual(list.Id.ToString(), savedRequest.Parameters.Find(x => x.Name == "id").Value);
            Assert.AreEqual(page.ToString(), savedRequest.Parameters.Find(x => x.Name == "page").Value);
            Assert.AreEqual(limit.ToString(), savedRequest.Parameters.Find(x => x.Name == "limit").Value);

            var content = "{ \"page\": 2,  \"limit\": 3, \"pageCount\": 3, \"resources\": ["
                + "{ \"id\": \"31337\", \"firstName\": \"John\", \"lastName\": \"Doe\", \"companyName\": null,"
                + "\"phone\": \"999123456\", \"email\": \"[email protected]\", \"country\": { \"id\": \"GB\", \"name\": \"United Kingdom\" },"
                + "\"customFields\": [ { \"id\": 73, \"name\": \"Secure ID\", \"value\": \"ABC\", \"createdAt\": \"2007-12-27T13:04:20+0000\" } ] }"

                + "{ \"id\": \"31338\", \"first_name\": \"Jack\", \"lastName\": \"Doe\", \"companyName\": null,"
                + "\"phone\": \"999123457\", \"email\": \"[email protected]\", \"country\": { \"id\": \"GB\", \"name\": \"United Kingdom\" },"
                + "\"customFields\": [ ] }"
                + "] }";

            var testClient = Common.CreateClient<ContactsResult>(content, null, null);
            client = new Client(testClient);

            var contacts = client.GetListContacts(list.Id, page, limit);

            Assert.IsTrue(contacts.Success);
            Assert.NotNull(contacts.Contacts);
            Assert.AreEqual(2, contacts.Contacts.Count);
            Assert.AreEqual(page, contacts.Page);
            Assert.AreEqual(limit, contacts.Limit);
            Assert.AreEqual(3, contacts.PageCount);
            Assert.IsNotNull(contacts.Contacts[0].CustomFields);
            Assert.AreEqual(1, contacts.Contacts[0].CustomFields.Count);
            Assert.IsEmpty(contacts.Contacts[1].CustomFields);
        }
        public void ShouldDeleteList()
        {
            IRestRequest savedRequest = null;
            mockClient.Setup(trc => trc.Execute<DeleteResult>(It.IsAny<IRestRequest>()))
                .Callback<IRestRequest>((request) => savedRequest = request)
                .Returns(new DeleteResult());
            var client = mockClient.Object;

            var list = new ContactList() { Id = listId };

            client.DeleteList(list);

            mockClient.Verify(trc => trc.Execute<DeleteResult>(It.IsAny<IRestRequest>()), Times.Once);
            Assert.IsNotNull(savedRequest);
            Assert.AreEqual("lists/{id}", savedRequest.Resource);
            Assert.AreEqual(Method.DELETE, savedRequest.Method);
            Assert.AreEqual(1, savedRequest.Parameters.Count);
            Assert.AreEqual(listId.ToString(), savedRequest.Parameters.Find(x => x.Name == "id").Value);

            var content = "{}";

            var testClient = Common.CreateClient<DeleteResult>(content, null, null);
            client = new Client(testClient);

            var link = client.DeleteList(list);

            Assert.IsTrue(link.Success);
        }