OSDN Git Service

HttpTwitter.CreateListsメソッドをTwitterApiクラスに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Wed, 4 May 2016 15:19:49 +0000 (00:19 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 4 May 2016 15:56:50 +0000 (00:56 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/HttpTwitter.cs
OpenTween/ListElement.cs
OpenTween/ListManage.cs
OpenTween/Twitter.cs

index 72b2718..71094c0 100644 (file)
@@ -333,6 +333,33 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task ListsCreate_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.PostLazyAsync<TwitterList>(
+                        new Uri("lists/create.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "name", "hogehoge" },
+                            { "description", "aaaa" },
+                            { "mode", "private" },
+                        })
+                )
+                .ReturnsAsync(LazyJson.Create(new TwitterList()));
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.ListsCreate("hogehoge", description: "aaaa", @private: true)
+                    .IgnoreResponse()
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task ListsStatuses_Test()
         {
             using (var twitterApi = new TwitterApi())
index df87f6f..4fa1997 100644 (file)
@@ -192,6 +192,22 @@ namespace OpenTween.Api
             return this.apiConnection.GetAsync<TwitterLists>(endpoint, param, "/lists/subscriptions");
         }
 
+        public Task<LazyJson<TwitterList>> ListsCreate(string name, string description = null, bool? @private = null)
+        {
+            var endpoint = new Uri("lists/create.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["name"] = name,
+            };
+
+            if (description != null)
+                param["description"] = description;
+            if (@private != null)
+                param["mode"] = @private.Value ? "private" : "public";
+
+            return this.apiConnection.PostLazyAsync<TwitterList>(endpoint, param);
+        }
+
         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 08a5be3..fc3cbb6 100644 (file)
@@ -236,26 +236,6 @@ namespace OpenTween
                 null);
         }
 
-        public HttpStatusCode CreateLists(string listname, Boolean isPrivate, string description, ref string content)
-        {
-            string mode = "public";
-            if (isPrivate)
-                mode = "private";
-
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("name", listname);
-            param.Add("mode", mode);
-            if (!string.IsNullOrEmpty(description))
-                param.Add("description", description);
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/lists/create.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
         public HttpStatusCode GetListMembers(string user, string list_id, long cursor, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
index d48de45..35421e1 100644 (file)
@@ -25,6 +25,7 @@
 // Boston, MA 02110-1301, USA.
 
 using System.Collections.Generic;
+using System.Threading.Tasks;
 using System.Xml.Serialization;
 using OpenTween.Api.DataModel;
 
@@ -68,7 +69,7 @@ namespace OpenTween
             this._tw = tw;
         }
 
-        public virtual void Refresh()
+        public virtual async Task Refresh()
         {
             var newList = _tw.EditList(this.Id.ToString(), Name, !this.IsPublic, this.Description);
 
index 7bd8033..7633466 100644 (file)
@@ -124,36 +124,40 @@ namespace OpenTween
             if (this.EditCheckBox.Checked == true) this.NameTextBox.Focus();
         }
 
-        private void OKEditButton_Click(object sender, EventArgs e)
+        private async void OKEditButton_Click(object sender, EventArgs e)
         {
             if (this.ListsList.SelectedItem == null) return;
-            ListElement listItem = (ListElement) this.ListsList.SelectedItem;
 
-            if (string.IsNullOrEmpty(this.NameTextBox.Text))
+            using (ControlTransaction.Disabled(this))
             {
-                MessageBox.Show(Properties.Resources.ListManageOKButton1);
-                return;
-            }
+                ListElement listItem = (ListElement)this.ListsList.SelectedItem;
+
+                if (string.IsNullOrEmpty(this.NameTextBox.Text))
+                {
+                    MessageBox.Show(Properties.Resources.ListManageOKButton1);
+                    return;
+                }
 
-            listItem.Name = this.NameTextBox.Text;
-            listItem.IsPublic = this.PublicRadioButton.Checked;
-            listItem.Description = this.DescriptionText.Text;
+                listItem.Name = this.NameTextBox.Text;
+                listItem.IsPublic = this.PublicRadioButton.Checked;
+                listItem.Description = this.DescriptionText.Text;
 
-            try
-            {
-                listItem.Refresh();
-            }
-            catch (WebApiException ex)
-            {
-                MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
-                return;
-            }
+                try
+                {
+                    await listItem.Refresh();
+                }
+                catch (WebApiException ex)
+                {
+                    MessageBox.Show(string.Format(Properties.Resources.ListManageOKButton2, ex.Message));
+                    return;
+                }
 
-            this.ListsList.Items.Clear();
-            this.ListManage_Load(null, EventArgs.Empty);
+                this.ListsList.Items.Clear();
+                this.ListManage_Load(null, EventArgs.Empty);
 
-            this.EditCheckBox.AutoCheck = true;
-            this.EditCheckBox.Checked = false;
+                this.EditCheckBox.AutoCheck = true;
+                this.EditCheckBox.Checked = false;
+            }
         }
 
         private void CancelEditButton_Click(object sender, EventArgs e)
@@ -397,15 +401,17 @@ namespace OpenTween
                 this._tw = tw;
             }
 
-            public override void Refresh()
+            public override async Task Refresh()
             {
                 if (this.IsCreated)
                 {
-                    base.Refresh();
+                    await base.Refresh().ConfigureAwait(false);
                 }
                 else
                 {
-                    this._tw.CreateListApi(this.Name, !this.IsPublic, this.Description);
+                    await this._tw.CreateListApi(this.Name, !this.IsPublic, this.Description)
+                        .ConfigureAwait(false);
+
                     this._isCreated = true;
                 }
             }
index ca54490..46cc306 100644 (file)
@@ -1817,38 +1817,17 @@ namespace OpenTween
             }
         }
 
-        public void CreateListApi(string listName, bool isPrivate, string description)
+        public async Task CreateListApi(string listName, bool isPrivate, string description)
         {
             this.CheckAccountState();
 
-            HttpStatusCode res;
-            var content = "";
-            try
-            {
-                res = twCon.CreateLists(listName, isPrivate, description, ref content);
-            }
-            catch(Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
-            }
+            var response = await this.Api.ListsCreate(listName, description, isPrivate)
+                .ConfigureAwait(false);
 
-            this.CheckStatusCode(res, content);
+            var list = await response.LoadJsonAsync()
+                .ConfigureAwait(false);
 
-            try
-            {
-                var le = TwitterList.ParseJson(content);
-                TabInformations.GetInstance().SubscribableLists.Add(new ListElement(le, 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);
-            }
+            TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
         }
 
         public bool ContainsUserAtList(string listId, string user)