OSDN Git Service

HttpTwitter.HomeTimeline, MentionsメソッドをTwitterApiクラスに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 30 Apr 2016 13:02:49 +0000 (22:02 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 30 Apr 2016 18:11:33 +0000 (03:11 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/HttpTwitter.cs
OpenTween/Tween.cs
OpenTween/Twitter.cs

index 84210be..4514677 100644 (file)
@@ -85,6 +85,64 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task StatusesHomeTimeline_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterStatus[]>(
+                        new Uri("statuses/home_timeline.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "include_entities", "true" },
+                            { "include_ext_alt_text", "true" },
+                            { "count", "200" },
+                            { "max_id", "900" },
+                            { "since_id", "100" },
+                        },
+                        "/statuses/home_timeline")
+                )
+                .ReturnsAsync(new TwitterStatus[0]);
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.StatusesHomeTimeline(200, maxId: 900L, sinceId: 100L)
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
+        public async Task StatusesMentionsTimeline_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterStatus[]>(
+                        new Uri("statuses/mentions_timeline.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "include_entities", "true" },
+                            { "include_ext_alt_text", "true" },
+                            { "count", "200" },
+                            { "max_id", "900" },
+                            { "since_id", "100" },
+                        },
+                        "/statuses/mentions_timeline")
+                )
+                .ReturnsAsync(new TwitterStatus[0]);
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.StatusesMentionsTimeline(200, maxId: 900L, sinceId: 100L)
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task StatusesShow_Test()
         {
             using (var twitterApi = new TwitterApi())
index 9968929..05d5852 100644 (file)
@@ -47,6 +47,44 @@ namespace OpenTween.Api
             this.CurrentScreenName = screenName;
         }
 
+        public Task<TwitterStatus[]> StatusesHomeTimeline(int? count = null, long? maxId = null, long? sinceId = null)
+        {
+            var endpoint = new Uri("statuses/home_timeline.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["include_entities"] = "true",
+                ["include_ext_alt_text"] = "true",
+            };
+
+            if (count != null)
+                param["count"] = count.ToString();
+            if (maxId != null)
+                param["max_id"] = maxId.ToString();
+            if (sinceId != null)
+                param["since_id"] = sinceId.ToString();
+
+            return this.apiConnection.GetAsync<TwitterStatus[]>(endpoint, param, "/statuses/home_timeline");
+        }
+
+        public Task<TwitterStatus[]> StatusesMentionsTimeline(int? count = null, long? maxId = null, long? sinceId = null)
+        {
+            var endpoint = new Uri("statuses/mentions_timeline.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["include_entities"] = "true",
+                ["include_ext_alt_text"] = "true",
+            };
+
+            if (count != null)
+                param["count"] = count.ToString();
+            if (maxId != null)
+                param["max_id"] = maxId.ToString();
+            if (sinceId != null)
+                param["since_id"] = sinceId.ToString();
+
+            return this.apiConnection.GetAsync<TwitterStatus[]>(endpoint, param, "/statuses/mentions_timeline");
+        }
+
         public Task<TwitterStatus> StatusesShow(long statusId)
         {
             var endpoint = new Uri("statuses/show.json", UriKind.Relative);
index 358f735..512f0ff 100644 (file)
@@ -152,27 +152,6 @@ namespace OpenTween
             this.Initialize("", "", "", 0);
         }
 
-        public HttpStatusCode HomeTimeline(int? count, long? max_id, long? since_id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            if (count != null)
-                param.Add("count", count.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-
-            param.Add("include_entities", "true");
-            param.Add("include_ext_alt_text", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/statuses/home_timeline.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/home_timeline"));
-        }
-
         public HttpStatusCode UserTimeline(long? user_id, string screen_name, int? count, long? max_id, long? since_id, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
@@ -203,27 +182,6 @@ namespace OpenTween
                 this.CreateApiCalllback("/statuses/user_timeline"));
         }
 
-        public HttpStatusCode Mentions(int? count, long? max_id, long? since_id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            if (count != null)
-                param.Add("count", count.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-
-            param.Add("include_entities", "true");
-            param.Add("include_ext_alt_text", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/statuses/mentions_timeline.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/mentions_timeline"));
-        }
-
         public HttpStatusCode DirectMessages(int? count, long? max_id, long? since_id, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
index 95dca89..8500f4e 100644 (file)
@@ -2260,7 +2260,7 @@ namespace OpenTween
             catch (WebApiException ex)
             {
                 this._myStatusError = true;
-                this.StatusLabel.Text = ex.Message;
+                this.StatusLabel.Text = $"Err:{ex.Message}(GetTimeline)";
             }
             finally
             {
@@ -2284,9 +2284,10 @@ namespace OpenTween
 
             p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, loadMore ? -1 : 1));
 
-            await Task.Run(() =>
+            await Task.Run(async () =>
             {
-                this.tw.GetTimelineApi(read, MyCommon.WORKERTYPE.Timeline, loadMore, this._initial);
+                await this.tw.GetTimelineApi(read, MyCommon.WORKERTYPE.Timeline, loadMore, this._initial)
+                    .ConfigureAwait(false);
 
                 // 新着時未読クリア
                 if (this._cfgCommon.ReadOldPosts)
@@ -2350,7 +2351,7 @@ namespace OpenTween
             catch (WebApiException ex)
             {
                 this._myStatusError = true;
-                this.StatusLabel.Text = ex.Message;
+                this.StatusLabel.Text = $"Err:{ex.Message}(GetTimeline)";
             }
             finally
             {
@@ -2374,9 +2375,10 @@ namespace OpenTween
 
             p.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText4, loadMore ? -1 : 1));
 
-            await Task.Run(() =>
+            await Task.Run(async () =>
             {
-                this.tw.GetTimelineApi(read, MyCommon.WORKERTYPE.Reply, loadMore, this._initial);
+                await this.tw.GetTimelineApi(read, MyCommon.WORKERTYPE.Reply, loadMore, this._initial)
+                    .ConfigureAwait(false);
 
                 this._statuses.DistributePosts();
             });
index b8055d2..4a18fb9 100644 (file)
@@ -865,50 +865,41 @@ namespace OpenTween
             return Math.Min(count, GetMaxApiResultCount(type));
         }
 
-        public void GetTimelineApi(bool read,
-                                MyCommon.WORKERTYPE gType,
-                                bool more,
-                                bool startup)
+        public async Task GetTimelineApi(bool read, MyCommon.WORKERTYPE gType, bool more, bool startup)
         {
             this.CheckAccountState();
 
-            HttpStatusCode res;
-            var content = "";
             var count = GetApiResultCount(gType, more, startup);
 
-            try
+            TwitterStatus[] statuses;
+            if (gType == MyCommon.WORKERTYPE.Timeline)
             {
-                if (gType == MyCommon.WORKERTYPE.Timeline)
+                if (more)
                 {
-                    if (more)
-                    {
-                        res = twCon.HomeTimeline(count, this.minHomeTimeline, null, ref content);
-                    }
-                    else
-                    {
-                        res = twCon.HomeTimeline(count, null, null, ref content);
-                    }
+                    statuses = await this.Api.StatusesHomeTimeline(count, maxId: this.minHomeTimeline)
+                        .ConfigureAwait(false);
                 }
                 else
                 {
-                    if (more)
-                    {
-                        res = twCon.Mentions(count, this.minMentions, null, ref content);
-                    }
-                    else
-                    {
-                        res = twCon.Mentions(count, null, null, ref content);
-                    }
+                    statuses = await this.Api.StatusesHomeTimeline(count)
+                        .ConfigureAwait(false);
                 }
             }
-            catch(Exception ex)
+            else
             {
-                throw new WebApiException("Err:" + ex.Message, ex);
+                if (more)
+                {
+                    statuses = await this.Api.StatusesMentionsTimeline(count, maxId: this.minMentions)
+                        .ConfigureAwait(false);
+                }
+                else
+                {
+                    statuses = await this.Api.StatusesMentionsTimeline(count)
+                        .ConfigureAwait(false);
+                }
             }
 
-            this.CheckStatusCode(res, content);
-
-            var minimumId = CreatePostsFromJson(content, gType, null, read);
+            var minimumId = CreatePostsFromJson(statuses, gType, null, read);
 
             if (minimumId != null)
             {
@@ -1171,6 +1162,11 @@ namespace OpenTween
                 throw new WebApiException("Invalid Json!", content, ex);
             }
 
+            return this.CreatePostsFromJson(items, gType, tab, read);
+        }
+
+        private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabClass tab, bool read)
+        {
             long? minimumId = null;
 
             foreach (var status in items)