OSDN Git Service

HttpTwitter.GetLists, GetListsSubscriptionsをTwitterApiクラスに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Wed, 4 May 2016 15:04:59 +0000 (00:04 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 4 May 2016 15:56:48 +0000 (00:56 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/HttpTwitter.cs
OpenTween/FilterDialog.cs
OpenTween/ListAvailable.cs
OpenTween/ListManage.cs
OpenTween/MyLists.cs
OpenTween/Tween.cs
OpenTween/Twitter.cs

index 273ff89..72b2718 100644 (file)
@@ -281,6 +281,58 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task ListsOwnerships_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterLists>(
+                        new Uri("lists/ownerships.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "screen_name", "twitterapi" },
+                            { "cursor", "-1" },
+                        },
+                        "/lists/ownerships")
+                )
+                .ReturnsAsync(new TwitterLists());
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.ListsOwnerships("twitterapi", cursor: -1L)
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
+        public async Task ListsSubscriptions_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterLists>(
+                        new Uri("lists/subscriptions.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "screen_name", "twitterapi" },
+                            { "cursor", "-1" },
+                        },
+                        "/lists/subscriptions")
+                )
+                .ReturnsAsync(new TwitterLists());
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.ListsSubscriptions("twitterapi", cursor: -1L)
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task ListsStatuses_Test()
         {
             using (var twitterApi = new TwitterApi())
index 52f7572..df87f6f 100644 (file)
@@ -164,6 +164,34 @@ namespace OpenTween.Api
             return this.apiConnection.PostLazyAsync<TwitterStatus>(endpoint, param);
         }
 
+        public Task<TwitterLists> ListsOwnerships(string screenName, long? cursor = null)
+        {
+            var endpoint = new Uri("lists/ownerships.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["screen_name"] = screenName,
+            };
+
+            if (cursor != null)
+                param["cursor"] = cursor.ToString();
+
+            return this.apiConnection.GetAsync<TwitterLists>(endpoint, param, "/lists/ownerships");
+        }
+
+        public Task<TwitterLists> ListsSubscriptions(string screenName, long? cursor = null)
+        {
+            var endpoint = new Uri("lists/subscriptions.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["screen_name"] = screenName,
+            };
+
+            if (cursor != null)
+                param["cursor"] = cursor.ToString();
+
+            return this.apiConnection.GetAsync<TwitterLists>(endpoint, param, "/lists/subscriptions");
+        }
+
         public Task<TwitterStatus[]> ListsStatuses(long listId, int? count = null, long? maxId = null, long? sinceId = null, bool? includeRTs = null)
         {
             var endpoint = new Uri("lists/statuses.json", UriKind.Relative);
index fc7b875..08a5be3 100644 (file)
@@ -201,19 +201,6 @@ namespace OpenTween
         }
 
         #region Lists
-        public HttpStatusCode GetLists(string user, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", user);
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/lists/list.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/lists/list"));
-        }
-
         public HttpStatusCode UpdateListID(string user, string list_id, string name, Boolean isPrivate, string description, ref string content)
         {
             string mode = "public";
@@ -249,19 +236,6 @@ namespace OpenTween
                 null);
         }
 
-        public HttpStatusCode GetListsSubscriptions(string user, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", user);
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/lists/subscriptions.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/lists/subscriptions"));
-        }
-
         public HttpStatusCode CreateLists(string listname, Boolean isPrivate, string description, ref string content)
         {
             string mode = "public";
index 4bedad6..fab1caf 100644 (file)
@@ -984,7 +984,7 @@ namespace OpenTween
                 ListFilters.Items.Clear();
         }
 
-        private void ButtonAddTab_Click(object sender, EventArgs e)
+        private async void ButtonAddTab_Click(object sender, EventArgs e)
         {
             string tabName = null;
             MyCommon.TabUsageType tabType;
@@ -1005,8 +1005,17 @@ namespace OpenTween
                 {
                     try
                     {
-                        ((TweenMain)this.Owner).TwitterInstance.GetListsApi();
+                        using (var dialog = new WaitingDialog(Properties.Resources.ListsGetting))
+                        {
+                            var cancellationToken = dialog.EnableCancellation();
+
+                            var task = ((TweenMain)this.Owner).TwitterInstance.GetListsApi();
+                            await dialog.WaitForAsync(this, task);
+
+                            cancellationToken.ThrowIfCancellationRequested();
+                        }
                     }
+                    catch (OperationCanceledException) { return; }
                     catch (WebApiException ex)
                     {
                         MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
index fc56042..a016395 100644 (file)
@@ -154,7 +154,7 @@ namespace OpenTween
                 var cancellationToken = dialog.EnableCancellation();
 
                 var tw = ((TweenMain)this.Owner).TwitterInstance;
-                var task = Task.Run(() => tw.GetListsApi());
+                var task = tw.GetListsApi();
                 await dialog.WaitForAsync(this, task);
 
                 cancellationToken.ThrowIfCancellationRequested();
index 653bad5..7bd8033 100644 (file)
@@ -240,35 +240,39 @@ namespace OpenTween
             }
         }
 
-        private void DeleteListButton_Click(object sender, EventArgs e)
+        private async void DeleteListButton_Click(object sender, EventArgs e)
         {
             if (this.ListsList.SelectedItem == null) return;
-            ListElement list = (ListElement) this.ListsList.SelectedItem;
 
-            if (MessageBox.Show(Properties.Resources.ListManageDeleteLists1, Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.OK)
+            using (ControlTransaction.Disabled(this))
             {
-                try
-                {
-                    this.tw.DeleteList(list.Id.ToString());
-                }
-                catch (WebApiException ex)
-                {
-                    MessageBox.Show(Properties.Resources.ListManageOKButton2, ex.Message);
-                    return;
-                }
+                ListElement list = (ListElement)this.ListsList.SelectedItem;
 
-                try
-                {
-                    this.tw.GetListsApi();
-                }
-                catch (WebApiException ex)
+                if (MessageBox.Show(Properties.Resources.ListManageDeleteLists1, Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.OK)
                 {
-                    MessageBox.Show(Properties.Resources.ListsDeleteFailed, ex.Message);
-                    return;
+                    try
+                    {
+                        this.tw.DeleteList(list.Id.ToString());
+                    }
+                    catch (WebApiException ex)
+                    {
+                        MessageBox.Show(Properties.Resources.ListManageOKButton2, ex.Message);
+                        return;
+                    }
+
+                    try
+                    {
+                        await this.tw.GetListsApi();
+                    }
+                    catch (WebApiException ex)
+                    {
+                        MessageBox.Show(Properties.Resources.ListsDeleteFailed, ex.Message);
+                        return;
+                    }
+
+                    this.ListsList.Items.Clear();
+                    this.ListManage_Load(this, EventArgs.Empty);
                 }
-
-                this.ListsList.Items.Clear();
-                this.ListManage_Load(this, EventArgs.Empty);
             }
         }
 
@@ -354,7 +358,7 @@ namespace OpenTween
             {
                 var cancellationToken = dialog.EnableCancellation();
 
-                var task = Task.Run(() => tw.GetListsApi());
+                var task = this.tw.GetListsApi();
                 await dialog.WaitForAsync(this, task);
 
                 cancellationToken.ThrowIfCancellationRequested();
index 7d1e0fe..b580e07 100644 (file)
@@ -140,20 +140,23 @@ namespace OpenTween
             this.ListsCheckedListBox.ItemCheck += this.ListsCheckedListBox_ItemCheck;
         }
 
-        private void ListRefreshButton_Click(object sender, EventArgs e)
+        private async void ListRefreshButton_Click(object sender, EventArgs e)
         {
-            try
+            using (ControlTransaction.Disabled(this))
             {
-                this._tw.GetListsApi();
-            }
-            catch (WebApiException ex)
-            {
-                MessageBox.Show(string.Format(Properties.Resources.ListsDeleteFailed, ex.Message));
-                return;
-            }
+                try
+                {
+                    await this._tw.GetListsApi();
+                }
+                catch (WebApiException ex)
+                {
+                    MessageBox.Show(string.Format(Properties.Resources.ListsDeleteFailed, ex.Message));
+                    return;
+                }
 
-            this.ListsCheckedListBox.Items.Clear();
-            this.MyLists_Load(this, EventArgs.Empty);
+                this.ListsCheckedListBox.Items.Clear();
+                this.MyLists_Load(this, EventArgs.Empty);
+            }
         }
 
         private void ListsCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
index 54adcba..1f315be 100644 (file)
@@ -11961,7 +11961,7 @@ namespace OpenTween
             }
         }
 
-        private void ListManageUserContextToolStripMenuItem_Click(object sender, EventArgs e)
+        private async void ListManageUserContextToolStripMenuItem_Click(object sender, EventArgs e)
         {
             string user;
 
@@ -11985,8 +11985,17 @@ namespace OpenTween
             {
                 try
                 {
-                    this.tw.GetListsApi();
+                    using (var dialog = new WaitingDialog(Properties.Resources.ListsGetting))
+                    {
+                        var cancellationToken = dialog.EnableCancellation();
+
+                        var task = this.tw.GetListsApi();
+                        await dialog.WaitForAsync(this, task);
+
+                        cancellationToken.ThrowIfCancellationRequested();
+                    }
                 }
+                catch (OperationCanceledException) { return; }
                 catch (WebApiException ex)
                 {
                     MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
index 9e65c0d..ca54490 100644 (file)
@@ -1714,69 +1714,19 @@ namespace OpenTween
                 .ConfigureAwait(false);
         }
 
-        public void GetListsApi()
+        public async Task GetListsApi()
         {
             this.CheckAccountState();
 
-            HttpStatusCode res;
-            IEnumerable<ListElement> lists;
-            var content = "";
-
-            try
-            {
-                res = twCon.GetLists(this.Username, ref content);
-            }
-            catch (Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
-            }
-
-            this.CheckStatusCode(res, content);
-
-            try
-            {
-                lists = TwitterList.ParseJsonArray(content)
-                    .Select(x => new ListElement(x, this));
-            }
-            catch (SerializationException ex)
-            {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
-            }
-            catch (Exception ex)
-            {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                throw new WebApiException("Err:Invalid Json!", content, ex);
-            }
-
-            try
-            {
-                res = twCon.GetListsSubscriptions(this.Username, ref content);
-            }
-            catch (Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
-            }
-
-            this.CheckStatusCode(res, content);
+            var ownedLists = await TwitterLists.GetAllItemsAsync(x => this.Api.ListsOwnerships(this.Username, cursor: x))
+                .ConfigureAwait(false);
 
-            try
-            {
-                lists = lists.Concat(TwitterList.ParseJsonArray(content)
-                    .Select(x => new ListElement(x, this)));
-            }
-            catch (SerializationException ex)
-            {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
-            }
-            catch (Exception ex)
-            {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                throw new WebApiException("Err:Invalid Json!", content, ex);
-            }
+            var subscribedLists = await TwitterLists.GetAllItemsAsync(x => this.Api.ListsSubscriptions(this.Username, cursor: x))
+                .ConfigureAwait(false);
 
-            TabInformations.GetInstance().SubscribableLists = lists.ToList();
+            TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
+                .Select(x => new ListElement(x, this))
+                .ToList();
         }
 
         public void DeleteList(string list_id)