OSDN Git Service

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

index 4514677..6bc80d4 100644 (file)
@@ -143,6 +143,37 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task StatusesUserTimeline_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterStatus[]>(
+                        new Uri("statuses/user_timeline.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "screen_name", "twitterapi" },
+                            { "include_rts", "true" },
+                            { "include_entities", "true" },
+                            { "include_ext_alt_text", "true" },
+                            { "count", "200" },
+                            { "max_id", "900" },
+                            { "since_id", "100" },
+                        },
+                        "/statuses/user_timeline")
+                )
+                .ReturnsAsync(new TwitterStatus[0]);
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.StatusesUserTimeline("twitterapi", count: 200, maxId: 900L, sinceId: 100L)
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task StatusesShow_Test()
         {
             using (var twitterApi = new TwitterApi())
index 05d5852..179974c 100644 (file)
@@ -85,6 +85,27 @@ namespace OpenTween.Api
             return this.apiConnection.GetAsync<TwitterStatus[]>(endpoint, param, "/statuses/mentions_timeline");
         }
 
+        public Task<TwitterStatus[]> StatusesUserTimeline(string screenName, int? count = null, long? maxId = null, long? sinceId = null)
+        {
+            var endpoint = new Uri("statuses/user_timeline.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["screen_name"] = screenName,
+                ["include_rts"] = "true",
+                ["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/user_timeline");
+        }
+
         public Task<TwitterStatus> StatusesShow(long statusId)
         {
             var endpoint = new Uri("statuses/show.json", UriKind.Relative);
index 512f0ff..34f25dd 100644 (file)
@@ -152,36 +152,6 @@ namespace OpenTween
             this.Initialize("", "", "", 0);
         }
 
-        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>();
-
-            if ((user_id == null && string.IsNullOrEmpty(screen_name)) ||
-                (user_id != null && !string.IsNullOrEmpty(screen_name))) return HttpStatusCode.BadRequest;
-
-            if (user_id != null)
-                param.Add("user_id", user_id.ToString());
-            if (!string.IsNullOrEmpty(screen_name))
-                param.Add("screen_name", screen_name);
-            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_rts", "true");
-            param.Add("include_entities", "true");
-            param.Add("include_ext_alt_text", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/statuses/user_timeline.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/user_timeline"));
-        }
-
         public HttpStatusCode DirectMessages(int? count, long? max_id, long? since_id, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
index 8500f4e..a759bf4 100644 (file)
@@ -2626,7 +2626,7 @@ namespace OpenTween
             catch (WebApiException ex)
             {
                 this._myStatusError = true;
-                this.StatusLabel.Text = ex.Message;
+                this.StatusLabel.Text = $"Err:{ex.Message}(GetUserTimeline)";
             }
             finally
             {
@@ -2650,7 +2650,7 @@ namespace OpenTween
 
             p.Report("UserTimeline refreshing...");
 
-            await Task.Run(() =>
+            await Task.Run(async () =>
             {
                 WebApiException lastException = null;
 
@@ -2661,7 +2661,8 @@ namespace OpenTween
                         if (string.IsNullOrEmpty(tab.User))
                             continue;
 
-                        this.tw.GetUserTimelineApi(read, tab.User, tab, loadMore);
+                        await this.tw.GetUserTimelineApi(read, tab.User, tab, loadMore)
+                            .ConfigureAwait(false);
                     }
                     catch (WebApiException ex)
                     {
index 4a18fb9..cb44903 100644 (file)
@@ -910,49 +910,36 @@ namespace OpenTween
             }
         }
 
-        public void GetUserTimelineApi(bool read,
-                                         string userName,
-                                         TabClass tab,
-                                         bool more)
+        public async Task GetUserTimelineApi(bool read, string userName, TabClass tab, bool more)
         {
             this.CheckAccountState();
 
-            HttpStatusCode res;
-            var content = "";
             var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
 
-            try
+            TwitterStatus[] statuses;
+            if (string.IsNullOrEmpty(userName))
+            {
+                var target = tab.User;
+                if (string.IsNullOrEmpty(target)) return;
+                userName = target;
+                statuses = await this.Api.StatusesUserTimeline(userName, count)
+                    .ConfigureAwait(false);
+            }
+            else
             {
-                if (string.IsNullOrEmpty(userName))
+                if (more)
                 {
-                    var target = tab.User;
-                    if (string.IsNullOrEmpty(target)) return;
-                    userName = target;
-                    res = twCon.UserTimeline(null, target, count, null, null, ref content);
+                    statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
+                        .ConfigureAwait(false);
                 }
                 else
                 {
-                    if (more)
-                    {
-                        res = twCon.UserTimeline(null, userName, count, tab.OldestId, null, ref content);
-                    }
-                    else
-                    {
-                        res = twCon.UserTimeline(null, userName, count, null, null, ref content);
-                    }
+                    statuses = await this.Api.StatusesUserTimeline(userName, count)
+                        .ConfigureAwait(false);
                 }
             }
-            catch(Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message, ex);
-            }
-
-            if (res == HttpStatusCode.Unauthorized)
-                throw new WebApiException("Err:@" + userName + "'s Tweets are protected.");
-
-            this.CheckStatusCode(res, content);
 
-            var minimumId = CreatePostsFromJson(content, MyCommon.WORKERTYPE.UserTimeline, tab, read);
+            var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
 
             if (minimumId != null)
                 tab.OldestId = minimumId.Value;