OSDN Git Service

DeleteRequestクラスを追加
authorKimura Youichi <kim.upsilon@bucyou.net>
Mon, 11 Dec 2023 16:50:26 +0000 (01:50 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Mon, 11 Dec 2023 17:09:54 +0000 (02:09 +0900)
OpenTween.Tests/Connection/DeleteRequestTest.cs [new file with mode: 0644]
OpenTween/Connection/DeleteRequest.cs [new file with mode: 0644]
OpenTween/Connection/TwitterApiConnection.cs

diff --git a/OpenTween.Tests/Connection/DeleteRequestTest.cs b/OpenTween.Tests/Connection/DeleteRequestTest.cs
new file mode 100644 (file)
index 0000000..3b72c5b
--- /dev/null
@@ -0,0 +1,50 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using System.Collections.Generic;
+using System.Net.Http;
+using Xunit;
+
+namespace OpenTween.Connection
+{
+    public class DeleteRequestTest
+    {
+        [Fact]
+        public void CreateMessage_Test()
+        {
+            var request = new DeleteRequest
+            {
+                RequestUri = new("hoge/aaa.json", UriKind.Relative),
+                Query = new Dictionary<string, string>
+                {
+                    ["id"] = "12345",
+                },
+            };
+
+            var baseUri = new Uri("https://example.com/v1/");
+            using var requestMessage = request.CreateMessage(baseUri);
+
+            Assert.Equal(HttpMethod.Delete, requestMessage.Method);
+            Assert.Equal(new("https://example.com/v1/hoge/aaa.json?id=12345"), requestMessage.RequestUri);
+        }
+    }
+}
diff --git a/OpenTween/Connection/DeleteRequest.cs b/OpenTween/Connection/DeleteRequest.cs
new file mode 100644 (file)
index 0000000..f155028
--- /dev/null
@@ -0,0 +1,45 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+#nullable enable
+
+using System;
+using System.Collections.Generic;
+using System.Net.Http;
+
+namespace OpenTween.Connection
+{
+    public class DeleteRequest : IHttpRequest
+    {
+        public required Uri RequestUri { get; set; }
+
+        public IDictionary<string, string>? Query { get; set; }
+
+        public string? EndpointName { get; set; }
+
+        public HttpRequestMessage CreateMessage(Uri baseUri)
+            => new()
+            {
+                Method = HttpMethod.Delete,
+                RequestUri = GetRequest.BuildUriWithQuery(new(baseUri, this.RequestUri), this.Query),
+            };
+    }
+}
index 2b3f651..93db05a 100644 (file)
@@ -353,25 +353,14 @@ namespace OpenTween.Connection
 
         public async Task DeleteAsync(Uri uri)
         {
-            var requestUri = new Uri(RestApiBase, uri);
-            using var request = new HttpRequestMessage(HttpMethod.Delete, requestUri);
-
-            try
+            var request = new DeleteRequest
             {
-                using var response = await this.Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
-                    .ConfigureAwait(false);
+                RequestUri = uri,
+            };
 
-                await TwitterApiConnection.CheckStatusCode(response)
-                    .ConfigureAwait(false);
-            }
-            catch (HttpRequestException ex)
-            {
-                throw TwitterApiException.CreateFromException(ex);
-            }
-            catch (OperationCanceledException ex)
-            {
-                throw TwitterApiException.CreateFromException(ex);
-            }
+            await this.SendAsync(request)
+                .IgnoreResponse()
+                .ConfigureAwait(false);
         }
 
         public static async Task<T> HandleTimeout<T>(Func<CancellationToken, Task<T>> func, TimeSpan timeout)