From: Kimura Youichi Date: Fri, 17 Aug 2018 03:56:30 +0000 (+0900) Subject: /direct_messages/events/destroy.json によるDMの削除に対応 X-Git-Tag: OpenTween_v2.1.0~3 X-Git-Url: http://git.osdn.net/view?p=opentween%2Fopen-tween.git;a=commitdiff_plain;h=1a38ae79089ae6949ff75bc8e446f0723e95f7f5;ds=sidebyside /direct_messages/events/destroy.json によるDMの削除に対応 --- diff --git a/OpenTween.Tests/Api/TwitterApiTest.cs b/OpenTween.Tests/Api/TwitterApiTest.cs index 0cf06106..84aa1a25 100644 --- a/OpenTween.Tests/Api/TwitterApiTest.cs +++ b/OpenTween.Tests/Api/TwitterApiTest.cs @@ -724,29 +724,6 @@ namespace OpenTween.Api } [Fact] - public async Task DirectMessagesDestroy_Test() - { - using (var twitterApi = new TwitterApi()) - { - var mock = new Mock(); - mock.Setup(x => - x.PostLazyAsync( - new Uri("direct_messages/destroy.json", UriKind.Relative), - new Dictionary { { "id", "100" } }) - ) - .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage { Id = 100L })); - - twitterApi.apiConnection = mock.Object; - - await twitterApi.DirectMessagesDestroy(statusId: 100L) - .IgnoreResponse() - .ConfigureAwait(false); - - mock.VerifyAll(); - } - } - - [Fact] public async Task DirectMessagesEventsList_Test() { using (var twitterApi = new TwitterApi()) @@ -813,6 +790,28 @@ namespace OpenTween.Api } [Fact] + public async Task DirectMessagesEventsDestroy_Test() + { + using (var twitterApi = new TwitterApi()) + { + var mock = new Mock(); + mock.Setup(x => + x.DeleteAsync( + new Uri("direct_messages/events/destroy.json", UriKind.Relative), + new Dictionary { { "id", "100" } }) + ) + .Returns(Task.CompletedTask); + + twitterApi.apiConnection = mock.Object; + + await twitterApi.DirectMessagesEventsDestroy(eventId: "100") + .ConfigureAwait(false); + + mock.VerifyAll(); + } + } + + [Fact] public async Task UsersShow_Test() { using (var twitterApi = new TwitterApi()) diff --git a/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs b/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs index 0f20f7e9..dd1cf8dc 100644 --- a/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs +++ b/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs @@ -493,5 +493,47 @@ namespace OpenTween.Connection Assert.Equal(0, mockHandler.QueueCount); } } + + [Fact] + public async Task DeleteAsync_Test() + { + using (var mockHandler = new HttpMessageHandlerMock()) + using (var http = new HttpClient(mockHandler)) + using (var apiConnection = new TwitterApiConnection("", "")) + { + apiConnection.http = http; + + mockHandler.Enqueue(async x => + { + Assert.Equal(HttpMethod.Delete, x.Method); + Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json", + x.RequestUri.AbsoluteUri); + + var body = await x.Content.ReadAsStringAsync() + .ConfigureAwait(false); + var query = HttpUtility.ParseQueryString(body); + + Assert.Equal("1111", query["aaaa"]); + Assert.Equal("2222", query["bbbb"]); + + return new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent("\"hogehoge\""), + }; + }); + + var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); + var param = new Dictionary + { + ["aaaa"] = "1111", + ["bbbb"] = "2222", + }; + + await apiConnection.DeleteAsync(endpoint, param) + .ConfigureAwait(false); + + Assert.Equal(0, mockHandler.QueueCount); + } + } } } diff --git a/OpenTween/Api/TwitterApi.cs b/OpenTween/Api/TwitterApi.cs index 47049862..0b535ae5 100644 --- a/OpenTween/Api/TwitterApi.cs +++ b/OpenTween/Api/TwitterApi.cs @@ -421,17 +421,6 @@ namespace OpenTween.Api return this.apiConnection.GetAsync(endpoint, param, "/direct_messages/sent"); } - public Task> DirectMessagesDestroy(long statusId) - { - var endpoint = new Uri("direct_messages/destroy.json", UriKind.Relative); - var param = new Dictionary - { - ["id"] = statusId.ToString(), - }; - - return this.apiConnection.PostLazyAsync(endpoint, param); - } - public Task DirectMessagesEventsList(int? count = null, string cursor = null) { var endpoint = new Uri("direct_messages/events/list.json", UriKind.Relative); @@ -478,6 +467,17 @@ namespace OpenTween.Api return this.apiConnection.PostJsonAsync(endpoint, json); } + public Task DirectMessagesEventsDestroy(string eventId) + { + var endpoint = new Uri("direct_messages/events/destroy.json", UriKind.Relative); + var param = new Dictionary + { + ["id"] = eventId.ToString(), + }; + + return this.apiConnection.DeleteAsync(endpoint, param); + } + public Task UsersShow(string screenName) { var endpoint = new Uri("users/show.json", UriKind.Relative); diff --git a/OpenTween/Connection/IApiConnection.cs b/OpenTween/Connection/IApiConnection.cs index b53514c3..0cca6401 100644 --- a/OpenTween/Connection/IApiConnection.cs +++ b/OpenTween/Connection/IApiConnection.cs @@ -43,5 +43,7 @@ namespace OpenTween.Connection Task PostAsync(Uri uri, IDictionary param, IDictionary media); Task PostJsonAsync(Uri uri, string json); + + Task DeleteAsync(Uri uri, IDictionary param); } } diff --git a/OpenTween/Connection/TwitterApiConnection.cs b/OpenTween/Connection/TwitterApiConnection.cs index 43b0b5e5..b8b015f8 100644 --- a/OpenTween/Connection/TwitterApiConnection.cs +++ b/OpenTween/Connection/TwitterApiConnection.cs @@ -329,6 +329,35 @@ namespace OpenTween.Connection } } + public async Task DeleteAsync(Uri uri, IDictionary param) + { + var requestUri = new Uri(RestApiBase, uri); + var request = new HttpRequestMessage(HttpMethod.Delete, requestUri); + + using (var postContent = new FormUrlEncodedContent(param)) + { + request.Content = postContent; + + try + { + using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead) + .ConfigureAwait(false)) + { + await this.CheckStatusCode(response) + .ConfigureAwait(false); + } + } + catch (HttpRequestException ex) + { + throw TwitterApiException.CreateFromException(ex); + } + catch (OperationCanceledException ex) + { + throw TwitterApiException.CreateFromException(ex); + } + } + } + protected async Task CheckStatusCode(HttpResponseMessage response) { var statusCode = response.StatusCode; diff --git a/OpenTween/Resources/ChangeLog.txt b/OpenTween/Resources/ChangeLog.txt index 589a2591..33ba2356 100644 --- a/OpenTween/Resources/ChangeLog.txt +++ b/OpenTween/Resources/ChangeLog.txt @@ -1,7 +1,7 @@ 更新履歴 ==== Ver 2.0.2-dev(2018/xx/xx) - * NEW: DMの一覧取得について新APIに対応しました + * NEW: DMの一覧取得・削除について新APIに対応しました * CHG: UserStreams停止によるエラーが発生した場合の再接続の間隔を10分に変更 ==== Ver 2.0.1(2018/06/13) diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index 59987de7..0e97f294 100644 --- a/OpenTween/Tween.cs +++ b/OpenTween/Tween.cs @@ -3314,8 +3314,7 @@ namespace OpenTween { if (post.IsDm) { - await this.twitterApi.DirectMessagesDestroy(post.StatusId) - .IgnoreResponse(); + await this.twitterApi.DirectMessagesEventsDestroy(post.StatusId.ToString(CultureInfo.InvariantCulture)); } else {