--- /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.Net.Http;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace OpenTween.Connection
+{
+ public class PostJsonRequestTest
+ {
+ [Fact]
+ public async Task CreateMessage_Test()
+ {
+ var request = new PostJsonRequest
+ {
+ RequestUri = new("aaa/bbb.json", UriKind.Relative),
+ JsonString = """{"foo":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/aaa/bbb.json"), requestMessage.RequestUri);
+ Assert.Equal("""{"foo":12345}""", await requestMessage.Content.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.Net.Http;
+using System.Text;
+
+namespace OpenTween.Connection
+{
+ public class PostJsonRequest : IHttpRequest
+ {
+ public required Uri RequestUri { get; set; }
+
+ public required string JsonString { get; set; }
+
+ public string? EndpointName { get; set; }
+
+ public HttpRequestMessage CreateMessage(Uri baseUri)
+ => new()
+ {
+ Method = HttpMethod.Post,
+ RequestUri = new(baseUri, this.RequestUri),
+ Content = new StringContent(this.JsonString, Encoding.UTF8, "application/json"),
+ };
+ }
+}
public async Task<string> PostJsonAsync(Uri uri, string json)
{
- var requestUri = new Uri(RestApiBase, uri);
- using var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
-
- using var postContent = new StringContent(json, Encoding.UTF8, "application/json");
- request.Content = postContent;
-
- try
+ var request = new PostJsonRequest
{
- using var response = await this.Http.SendAsync(request)
- .ConfigureAwait(false);
+ RequestUri = uri,
+ JsonString = json,
+ };
- await TwitterApiConnection.CheckStatusCode(response)
- .ConfigureAwait(false);
+ using var response = await this.SendAsync(request)
+ .ConfigureAwait(false);
- return await response.Content.ReadAsStringAsync()
- .ConfigureAwait(false);
- }
- catch (HttpRequestException ex)
- {
- throw TwitterApiException.CreateFromException(ex);
- }
- catch (OperationCanceledException ex)
- {
- throw TwitterApiException.CreateFromException(ex);
- }
+ return await response.ReadAsString()
+ .ConfigureAwait(false);
}
public async Task<LazyJson<T>> PostJsonAsync<T>(Uri uri, string json)
{
- var requestUri = new Uri(RestApiBase, uri);
- var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
-
- using var postContent = new StringContent(json, Encoding.UTF8, "application/json");
- request.Content = postContent;
-
- HttpResponseMessage? response = null;
- try
+ var request = new PostJsonRequest
{
- response = await this.Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
- .ConfigureAwait(false);
-
- await TwitterApiConnection.CheckStatusCode(response)
- .ConfigureAwait(false);
+ RequestUri = uri,
+ JsonString = json,
+ };
- 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 DeleteAsync(Uri uri)