From: Kimura Youichi Date: Sun, 10 Dec 2023 09:56:35 +0000 (+0900) Subject: PostJsonRequestクラスを追加 X-Git-Tag: OpenTween_v3.10.0^2~9^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=c522ba5957c74f9c83f85e5a83b52123ce09c125;p=opentween%2Fopen-tween.git PostJsonRequestクラスを追加 --- diff --git a/OpenTween.Tests/Connection/PostJsonRequestTest.cs b/OpenTween.Tests/Connection/PostJsonRequestTest.cs new file mode 100644 index 00000000..d8b5f283 --- /dev/null +++ b/OpenTween.Tests/Connection/PostJsonRequestTest.cs @@ -0,0 +1,48 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2023 kim_upsilon (@kim_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 , 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()); + } + } +} diff --git a/OpenTween/Connection/PostJsonRequest.cs b/OpenTween/Connection/PostJsonRequest.cs new file mode 100644 index 00000000..715d5418 --- /dev/null +++ b/OpenTween/Connection/PostJsonRequest.cs @@ -0,0 +1,46 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2023 kim_upsilon (@kim_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 , 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"), + }; + } +} diff --git a/OpenTween/Connection/TwitterApiConnection.cs b/OpenTween/Connection/TwitterApiConnection.cs index 091a3203..35a8e0b9 100644 --- a/OpenTween/Connection/TwitterApiConnection.cs +++ b/OpenTween/Connection/TwitterApiConnection.cs @@ -368,67 +368,31 @@ namespace OpenTween.Connection public async Task 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> PostJsonAsync(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(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(); } public async Task DeleteAsync(Uri uri)