From: Kimura Youichi Date: Sat, 30 Apr 2016 13:02:49 +0000 (+0900) Subject: HttpTwitter.HomeTimeline, MentionsメソッドをTwitterApiクラスに置き換え X-Git-Tag: OpenTween_v1.3.3~87^2~40 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=220bd8befffb6a994a4fac3fadd58d5b8c2edb3e;p=opentween%2Fopen-tween.git HttpTwitter.HomeTimeline, MentionsメソッドをTwitterApiクラスに置き換え --- diff --git a/OpenTween.Tests/Api/TwitterApiTest.cs b/OpenTween.Tests/Api/TwitterApiTest.cs index 84210be1..4514677e 100644 --- a/OpenTween.Tests/Api/TwitterApiTest.cs +++ b/OpenTween.Tests/Api/TwitterApiTest.cs @@ -85,6 +85,64 @@ namespace OpenTween.Api } [Fact] + public async Task StatusesHomeTimeline_Test() + { + using (var twitterApi = new TwitterApi()) + { + var mock = new Mock(); + mock.Setup(x => + x.GetAsync( + new Uri("statuses/home_timeline.json", UriKind.Relative), + new Dictionary { + { "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(); + mock.Setup(x => + x.GetAsync( + new Uri("statuses/mentions_timeline.json", UriKind.Relative), + new Dictionary { + { "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()) diff --git a/OpenTween/Api/TwitterApi.cs b/OpenTween/Api/TwitterApi.cs index 99689291..05d5852f 100644 --- a/OpenTween/Api/TwitterApi.cs +++ b/OpenTween/Api/TwitterApi.cs @@ -47,6 +47,44 @@ namespace OpenTween.Api this.CurrentScreenName = screenName; } + public Task StatusesHomeTimeline(int? count = null, long? maxId = null, long? sinceId = null) + { + var endpoint = new Uri("statuses/home_timeline.json", UriKind.Relative); + var param = new Dictionary + { + ["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(endpoint, param, "/statuses/home_timeline"); + } + + public Task StatusesMentionsTimeline(int? count = null, long? maxId = null, long? sinceId = null) + { + var endpoint = new Uri("statuses/mentions_timeline.json", UriKind.Relative); + var param = new Dictionary + { + ["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(endpoint, param, "/statuses/mentions_timeline"); + } + public Task StatusesShow(long statusId) { var endpoint = new Uri("statuses/show.json", UriKind.Relative); diff --git a/OpenTween/Connection/HttpTwitter.cs b/OpenTween/Connection/HttpTwitter.cs index 358f7359..512f0fff 100644 --- a/OpenTween/Connection/HttpTwitter.cs +++ b/OpenTween/Connection/HttpTwitter.cs @@ -152,27 +152,6 @@ namespace OpenTween this.Initialize("", "", "", 0); } - public HttpStatusCode HomeTimeline(int? count, long? max_id, long? since_id, ref string content) - { - Dictionary param = new Dictionary(); - 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 param = new Dictionary(); @@ -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 param = new Dictionary(); - 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 param = new Dictionary(); diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index 95dca897..8500f4ed 100644 --- a/OpenTween/Tween.cs +++ b/OpenTween/Tween.cs @@ -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(); }); diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index b8055d27..4a18fb9a 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -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)