OSDN Git Service

PostRequestクラスを追加
authorKimura Youichi <kim.upsilon@bucyou.net>
Mon, 11 Dec 2023 17:39:48 +0000 (02:39 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Tue, 12 Dec 2023 17:51:17 +0000 (02:51 +0900)
OpenTween.Tests/Connection/PostRequestTest.cs [new file with mode: 0644]
OpenTween/Connection/PostRequest.cs [new file with mode: 0644]
OpenTween/Connection/TwitterApiConnection.cs

diff --git a/OpenTween.Tests/Connection/PostRequestTest.cs b/OpenTween.Tests/Connection/PostRequestTest.cs
new file mode 100644 (file)
index 0000000..fc23f3a
--- /dev/null
@@ -0,0 +1,70 @@
+// 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 System.Threading.Tasks;
+using Xunit;
+
+namespace OpenTween.Connection
+{
+    public class PostRequestTest
+    {
+        [Fact]
+        public void CreateMessage_Test()
+        {
+            var request = new PostRequest
+            {
+                RequestUri = new("hoge/aaa.json", UriKind.Relative),
+            };
+
+            var baseUri = new Uri("https://example.com/v1/");
+            using var requestMessage = request.CreateMessage(baseUri);
+
+            Assert.Equal(HttpMethod.Post, requestMessage.Method);
+            Assert.Equal(new("https://example.com/v1/hoge/aaa.json"), requestMessage.RequestUri);
+            Assert.Null(requestMessage.Content);
+        }
+
+        [Fact]
+        public async Task CreateMessage_WithQueryTest()
+        {
+            var request = new PostRequest
+            {
+                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.Post, requestMessage.Method);
+            Assert.Equal(new("https://example.com/v1/hoge/aaa.json"), requestMessage.RequestUri);
+
+            using var requestContent = Assert.IsType<FormUrlEncodedContent>(requestMessage.Content);
+            Assert.Equal("id=12345", await requestContent.ReadAsStringAsync());
+        }
+    }
+}
diff --git a/OpenTween/Connection/PostRequest.cs b/OpenTween/Connection/PostRequest.cs
new file mode 100644 (file)
index 0000000..122a90d
--- /dev/null
@@ -0,0 +1,48 @@
+// 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 PostRequest : IHttpRequest
+    {
+        public required Uri RequestUri { get; set; }
+
+        public IDictionary<string, string>? Query { get; set; }
+
+        public string? EndpointName { get; set; }
+
+        public TimeSpan Timeout { get; set; } = Networking.DefaultTimeout;
+
+        public HttpRequestMessage CreateMessage(Uri baseUri)
+            => new()
+            {
+                Method = HttpMethod.Post,
+                RequestUri = new(baseUri, this.RequestUri),
+                Content = this.Query != null ? new FormUrlEncodedContent(this.Query) : null,
+            };
+    }
+}
index db00455..d17da21 100644 (file)
@@ -165,38 +165,16 @@ namespace OpenTween.Connection
 
         public async Task<LazyJson<T>> PostLazyAsync<T>(Uri uri, IDictionary<string, string>? param)
         {
-            var requestUri = new Uri(RestApiBase, uri);
-            var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
-
-            using var postContent = new FormUrlEncodedContent(param);
-            request.Content = postContent;
-
-            HttpResponseMessage? response = null;
-            try
+            var request = new PostRequest
             {
-                response = await this.Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
-                    .ConfigureAwait(false);
-
-                await TwitterApiConnection.CheckStatusCode(response)
-                    .ConfigureAwait(false);
+                RequestUri = uri,
+                Query = param,
+            };
 
-                var result = new LazyJson<T>(response);
-                response = null;
+            using var response = await this.SendAsync(request)
+                .ConfigureAwait(false);
 
-                return result;
-            }
-            catch (HttpRequestException ex)
-            {
-                throw TwitterApiException.CreateFromException(ex);
-            }
-            catch (OperationCanceledException ex)
-            {
-                throw TwitterApiException.CreateFromException(ex);
-            }
-            finally
-            {
-                response?.Dispose();
-            }
+            return response.ReadAsLazyJson<T>();
         }
 
         public async Task<LazyJson<T>> PostLazyAsync<T>(Uri uri, IDictionary<string, string>? param, IDictionary<string, IMediaItem>? media)