OSDN Git Service

HttpTwitter.ShowListMemberメソッドをTwitterApiクラスに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Thu, 5 May 2016 04:53:22 +0000 (13:53 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 5 May 2016 17:01:17 +0000 (02:01 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/HttpTwitter.cs
OpenTween/MyLists.cs
OpenTween/Twitter.cs

index ea4b04c..c1a597f 100644 (file)
@@ -472,6 +472,34 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task ListsMembersShow_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterUser>(
+                        new Uri("lists/members/show.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "list_id", "12345" },
+                            { "screen_name", "twitterapi" },
+                            { "include_entities", "true" },
+                            { "include_ext_alt_text", "true" },
+                        },
+                        "/lists/members/show")
+                )
+                .ReturnsAsync(new TwitterUser());
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.ListsMembersShow(12345L, "twitterapi")
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task DirectMessagesRecv_Test()
         {
             using (var twitterApi = new TwitterApi())
index c8d1058..5c1ed64 100644 (file)
@@ -275,6 +275,20 @@ namespace OpenTween.Api
             return this.apiConnection.GetAsync<TwitterUsers>(endpoint, param, "/lists/members");
         }
 
+        public Task<TwitterUser> ListsMembersShow(long listId, string screenName)
+        {
+            var endpoint = new Uri("lists/members/show.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["list_id"] = listId.ToString(),
+                ["screen_name"] = screenName,
+                ["include_entities"] = "true",
+                ["include_ext_alt_text"] = "true",
+            };
+
+            return this.apiConnection.GetAsync<TwitterUser>(endpoint, param, "/lists/members/show");
+        }
+
         public Task<TwitterDirectMessage[]> DirectMessagesRecv(int? count = null, long? maxId = null, long? sinceId = null)
         {
             var endpoint = new Uri("direct_messages.json", UriKind.Relative);
index ad754bd..dcb1476 100644 (file)
@@ -196,19 +196,6 @@ namespace OpenTween
                 null,
                 null);
         }
-
-        public HttpStatusCode ShowListMember(string list_id, string memberName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", memberName);
-            param.Add("list_id", list_id);
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/lists/members/show.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/lists/members/show"));
-        }
         #endregion
 
         public HttpStatusCode Statusid_retweeted_by_ids(long statusid, int? count, int? page, ref string content)
index b580e07..0c8eb37 100644 (file)
@@ -159,50 +159,36 @@ namespace OpenTween
             }
         }
 
-        private void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
+        private async void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
         {
-            ListElement list = (ListElement)this.ListsCheckedListBox.Items[e.Index];
-
-            switch (e.CurrentValue)
+            using (ControlTransaction.Disabled(this))
             {
-                case CheckState.Indeterminate:
-                    try
-                    {
-                        var ret = this._tw.ContainsUserAtList(list.Id.ToString(), contextUserName);
+                ListElement list = (ListElement)this.ListsCheckedListBox.Items[e.Index];
 
-                        if (ret)
-                            e.NewValue = CheckState.Checked;
-                        else
-                            e.NewValue = CheckState.Unchecked;
-                    }
-                    catch (WebApiException ex)
-                    {
-                        MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
-                        e.NewValue = CheckState.Indeterminate;
-                    }
-                    break;
-                case CheckState.Unchecked:
-                    try
-                    {
-                        this._tw.AddUserToList(list.Id.ToString(), this.contextUserName.ToString());
-                    }
-                    catch (WebApiException ex)
-                    {
-                        MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
-                        e.NewValue = CheckState.Indeterminate;
-                    }
-                    break;
-                case CheckState.Checked:
-                    try
-                    {
-                        this._tw.RemoveUserToList(list.Id.ToString(), this.contextUserName.ToString());
-                    }
-                    catch (WebApiException ex)
+                try
+                {
+                    switch (e.CurrentValue)
                     {
-                        MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
-                        e.NewValue = CheckState.Indeterminate;
+                        case CheckState.Indeterminate:
+                            var ret = await this._tw.ContainsUserAtList(list.Id, this.contextUserName);
+                            if (ret)
+                                e.NewValue = CheckState.Checked;
+                            else
+                                e.NewValue = CheckState.Unchecked;
+                            break;
+                        case CheckState.Unchecked:
+                            this._tw.AddUserToList(list.Id.ToString(), this.contextUserName.ToString());
+                            break;
+                        case CheckState.Checked:
+                            this._tw.RemoveUserToList(list.Id.ToString(), this.contextUserName.ToString());
+                            break;
                     }
-                    break;
+                }
+                catch (WebApiException ex)
+                {
+                    MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
+                    e.NewValue = CheckState.Indeterminate;
+                }
             }
         }
 
index 897b77b..3b5f795 100644 (file)
@@ -1718,35 +1718,19 @@ namespace OpenTween
             TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
         }
 
-        public bool ContainsUserAtList(string listId, string user)
+        public async Task<bool> ContainsUserAtList(long listId, string user)
         {
             this.CheckAccountState();
 
-            HttpStatusCode res;
-            var content = "";
-
             try
             {
-                res = this.twCon.ShowListMember(listId, user, ref content);
-            }
-            catch(Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
-            }
-
-            if (res == HttpStatusCode.NotFound)
-            {
-                return false;
-            }
-
-            this.CheckStatusCode(res, content);
+                await this.Api.ListsMembersShow(listId, user)
+                    .ConfigureAwait(false);
 
-            try
-            {
-                TwitterUser.ParseJson(content);
                 return true;
             }
-            catch(Exception)
+            catch (TwitterApiException ex)
+                when (ex.ErrorResponse.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
             {
                 return false;
             }