OSDN Git Service

/direct_messages/events/destroy.json によるDMの削除に対応
authorKimura Youichi <kim.upsilon@bucyou.net>
Fri, 17 Aug 2018 03:56:30 +0000 (12:56 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 17 Aug 2018 03:56:30 +0000 (12:56 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween.Tests/Connection/TwitterApiConnectionTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/IApiConnection.cs
OpenTween/Connection/TwitterApiConnection.cs
OpenTween/Resources/ChangeLog.txt
OpenTween/Tween.cs

index 0cf0610..84aa1a2 100644 (file)
@@ -724,29 +724,6 @@ namespace OpenTween.Api
         }
 
         [Fact]
-        public async Task DirectMessagesDestroy_Test()
-        {
-            using (var twitterApi = new TwitterApi())
-            {
-                var mock = new Mock<IApiConnection>();
-                mock.Setup(x =>
-                    x.PostLazyAsync<TwitterDirectMessage>(
-                        new Uri("direct_messages/destroy.json", UriKind.Relative),
-                        new Dictionary<string, string> { { "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<IApiConnection>();
+                mock.Setup(x =>
+                    x.DeleteAsync(
+                        new Uri("direct_messages/events/destroy.json", UriKind.Relative),
+                        new Dictionary<string, string> { { "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())
index 0f20f7e..dd1cf8d 100644 (file)
@@ -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<string, string>
+                {
+                    ["aaaa"] = "1111",
+                    ["bbbb"] = "2222",
+                };
+
+                await apiConnection.DeleteAsync(endpoint, param)
+                    .ConfigureAwait(false);
+
+                Assert.Equal(0, mockHandler.QueueCount);
+            }
+        }
     }
 }
index 4704986..0b535ae 100644 (file)
@@ -421,17 +421,6 @@ namespace OpenTween.Api
             return this.apiConnection.GetAsync<TwitterDirectMessage[]>(endpoint, param, "/direct_messages/sent");
         }
 
-        public Task<LazyJson<TwitterDirectMessage>> DirectMessagesDestroy(long statusId)
-        {
-            var endpoint = new Uri("direct_messages/destroy.json", UriKind.Relative);
-            var param = new Dictionary<string, string>
-            {
-                ["id"] = statusId.ToString(),
-            };
-
-            return this.apiConnection.PostLazyAsync<TwitterDirectMessage>(endpoint, param);
-        }
-
         public Task<TwitterMessageEventList> 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<string, string>
+            {
+                ["id"] = eventId.ToString(),
+            };
+
+            return this.apiConnection.DeleteAsync(endpoint, param);
+        }
+
         public Task<TwitterUser> UsersShow(string screenName)
         {
             var endpoint = new Uri("users/show.json", UriKind.Relative);
index b53514c..0cca640 100644 (file)
@@ -43,5 +43,7 @@ namespace OpenTween.Connection
         Task PostAsync(Uri uri, IDictionary<string, string> param, IDictionary<string, IMediaItem> media);
 
         Task PostJsonAsync(Uri uri, string json);
+
+        Task DeleteAsync(Uri uri, IDictionary<string, string> param);
     }
 }
index 43b0b5e..b8b015f 100644 (file)
@@ -329,6 +329,35 @@ namespace OpenTween.Connection
             }
         }
 
+        public async Task DeleteAsync(Uri uri, IDictionary<string, string> 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;
index 589a259..33ba235 100644 (file)
@@ -1,7 +1,7 @@
 更新履歴
 
 ==== Ver 2.0.2-dev(2018/xx/xx)
- * NEW: DMの一覧取得について新APIに対応しました
+ * NEW: DMã\81®ä¸\80覧å\8f\96å¾\97ã\83»å\89\8aé\99¤ã\81«ã\81¤ã\81\84ã\81¦æ\96°APIã\81«å¯¾å¿\9cã\81\97ã\81¾ã\81\97ã\81\9f
  * CHG: UserStreams停止によるエラーが発生した場合の再接続の間隔を10分に変更
 
 ==== Ver 2.0.1(2018/06/13)
index 59987de..0e97f29 100644 (file)
@@ -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
                         {