OSDN Git Service

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

index 3ab22f1..ea4b04c 100644 (file)
@@ -444,6 +444,34 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task ListsMembers_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterUsers>(
+                        new Uri("lists/members.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "list_id", "12345" },
+                            { "include_entities", "true" },
+                            { "include_ext_alt_text", "true" },
+                            { "cursor", "-1" },
+                        },
+                        "/lists/members")
+                )
+                .ReturnsAsync(new TwitterUsers());
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.ListsMembers(12345L, cursor: -1)
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task DirectMessagesRecv_Test()
         {
             using (var twitterApi = new TwitterApi())
index 3104e1e..c8d1058 100644 (file)
@@ -259,6 +259,22 @@ namespace OpenTween.Api
             return this.apiConnection.GetAsync<TwitterStatus[]>(endpoint, param, "/lists/statuses");
         }
 
+        public Task<TwitterUsers> ListsMembers(long listId, long? cursor = null)
+        {
+            var endpoint = new Uri("lists/members.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["list_id"] = listId.ToString(),
+                ["include_entities"] = "true",
+                ["include_ext_alt_text"] = "true",
+            };
+
+            if (cursor != null)
+                param["cursor"] = cursor.ToString();
+
+            return this.apiConnection.GetAsync<TwitterUsers>(endpoint, param, "/lists/members");
+        }
+
         public Task<TwitterDirectMessage[]> DirectMessagesRecv(int? count = null, long? maxId = null, long? sinceId = null)
         {
             var endpoint = new Uri("direct_messages.json", UriKind.Relative);
index cd2bf7e..ad754bd 100644 (file)
@@ -171,20 +171,6 @@ namespace OpenTween
         }
 
         #region Lists
-        public HttpStatusCode GetListMembers(string user, string list_id, long cursor, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", user);
-            param.Add("list_id", list_id);
-            param.Add("cursor", cursor.ToString());
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/lists/members.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/lists/members"));
-        }
-
         public HttpStatusCode CreateListMembers(string list_id, string memberName, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
index 09bdeec..4467c93 100644 (file)
@@ -105,16 +105,18 @@ namespace OpenTween
             }
         }
 
-        public void RefreshMembers()
+        public async Task RefreshMembers()
         {
             var users = new List<UserInfo>();
-            this._cursor = this._tw.GetListMembers(this.Id.ToString(), users, cursor: -1);
+            this._cursor = await this._tw.GetListMembers(this.Id, users, cursor: -1)
+                .ConfigureAwait(false);
             this._members = users;
         }
 
-        public void GetMoreMembers()
+        public async Task GetMoreMembers()
         {
-            this._cursor = this._tw.GetListMembers(this.Id.ToString(), this._members, this._cursor);
+            this._cursor = await this._tw.GetListMembers(this.Id, this._members, this._cursor)
+                .ConfigureAwait(false);
         }
 
         public override string ToString()
index 6820b41..84d7c14 100644 (file)
@@ -182,7 +182,7 @@ namespace OpenTween
                 var list = (ListElement)this.ListsList.SelectedItem;
                 try
                 {
-                    await Task.Run(() => list.RefreshMembers());
+                    await list.RefreshMembers();
                 }
                 catch (WebApiException ex)
                 {
@@ -205,7 +205,7 @@ namespace OpenTween
                 var list = (ListElement)this.ListsList.SelectedItem;
                 try
                 {
-                    await Task.Run(() => list.GetMoreMembers());
+                    await list.GetMoreMembers();
                 }
                 catch (WebApiException ex)
                 {
index 36da220..897b77b 100644 (file)
@@ -1693,42 +1693,16 @@ namespace OpenTween
             return new ListElement(list, this);
         }
 
-        public long GetListMembers(string list_id, List<UserInfo> lists, long cursor)
+        public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
         {
             this.CheckAccountState();
 
-            HttpStatusCode res;
-            var content = "";
-            try
-            {
-                res = twCon.GetListMembers(this.Username, list_id, cursor, ref content);
-            }
-            catch(Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message);
-            }
+            var users = await this.Api.ListsMembers(listId, cursor)
+                .ConfigureAwait(false);
 
-            this.CheckStatusCode(res, content);
+            Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
 
-            try
-            {
-                var users = TwitterUsers.ParseJson(content);
-                Array.ForEach<TwitterUser>(
-                    users.Users,
-                    u => lists.Add(new UserInfo(u)));
-
-                return users.NextCursor;
-            }
-            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);
-            }
+            return users.NextCursor;
         }
 
         public async Task CreateListApi(string listName, bool isPrivate, string description)