--- /dev/null
+// 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());
+ }
+ }
+}
--- /dev/null
+// 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,
+ };
+ }
+}
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)