From 243ae2ff906e44a7e06273e05f56593c39cb95b4 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sun, 16 Jul 2023 20:24:42 +0900 Subject: [PATCH] =?utf8?q?IApiConnection.PostJsonAsync=E3=81=8B=E3=82=89?= =?utf8?q?=E3=83=AC=E3=82=B9=E3=83=9D=E3=83=B3=E3=82=B9=E3=83=9C=E3=83=87?= =?utf8?q?=E3=82=A3=E3=82=92=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=82=8B?= =?utf8?q?=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- OpenTween.Tests/Api/TwitterApiTest.cs | 2 +- .../Connection/TwitterApiConnectionTest.cs | 8 ++++-- OpenTween/Connection/IApiConnection.cs | 2 +- OpenTween/Connection/TwitterApiConnection.cs | 32 +++++++++++++++++++--- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/OpenTween.Tests/Api/TwitterApiTest.cs b/OpenTween.Tests/Api/TwitterApiTest.cs index 202d529d..3144b0c9 100644 --- a/OpenTween.Tests/Api/TwitterApiTest.cs +++ b/OpenTween.Tests/Api/TwitterApiTest.cs @@ -1359,7 +1359,7 @@ namespace OpenTween.Api new Uri("https://upload.twitter.com/1.1/media/metadata/create.json", UriKind.Absolute), """{"media_id": "12345", "alt_text": {"text": "hogehoge"}}""") ) - .Returns(Task.CompletedTask); + .ReturnsAsync(""); using var twitterApi = new TwitterApi(ApiKey.Create("fake_consumer_key"), ApiKey.Create("fake_consumer_secret")); twitterApi.ApiConnection = mock.Object; diff --git a/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs b/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs index 05b1fa53..e9754819 100644 --- a/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs +++ b/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs @@ -454,14 +454,18 @@ namespace OpenTween.Connection Assert.Equal("""{"aaaa": 1111}""", body); - return new HttpResponseMessage(HttpStatusCode.NoContent); + return new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(@"{""ok"":true}"), + }; }); var endpoint = new Uri("hoge/tetete.json", UriKind.Relative); - await apiConnection.PostJsonAsync(endpoint, """{"aaaa": 1111}""") + var response = await apiConnection.PostJsonAsync(endpoint, """{"aaaa": 1111}""") .ConfigureAwait(false); + Assert.Equal(@"{""ok"":true}", response); Assert.Equal(0, mockHandler.QueueCount); } diff --git a/OpenTween/Connection/IApiConnection.cs b/OpenTween/Connection/IApiConnection.cs index c25229ea..e5412630 100644 --- a/OpenTween/Connection/IApiConnection.cs +++ b/OpenTween/Connection/IApiConnection.cs @@ -44,7 +44,7 @@ namespace OpenTween.Connection Task PostAsync(Uri uri, IDictionary? param, IDictionary? media); - Task PostJsonAsync(Uri uri, string json); + Task PostJsonAsync(Uri uri, string json); Task> PostJsonAsync(Uri uri, string json); diff --git a/OpenTween/Connection/TwitterApiConnection.cs b/OpenTween/Connection/TwitterApiConnection.cs index 34317795..d76e0882 100644 --- a/OpenTween/Connection/TwitterApiConnection.cs +++ b/OpenTween/Connection/TwitterApiConnection.cs @@ -338,10 +338,34 @@ namespace OpenTween.Connection } } - public async Task PostJsonAsync(Uri uri, string json) - => await this.PostJsonAsync(uri, json) - .IgnoreResponse() - .ConfigureAwait(false); + 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 + { + using var response = await this.Http.SendAsync(request) + .ConfigureAwait(false); + + await TwitterApiConnection.CheckStatusCode(response) + .ConfigureAwait(false); + + return await response.Content.ReadAsStringAsync() + .ConfigureAwait(false); + } + catch (HttpRequestException ex) + { + throw TwitterApiException.CreateFromException(ex); + } + catch (OperationCanceledException ex) + { + throw TwitterApiException.CreateFromException(ex); + } + } public async Task> PostJsonAsync(Uri uri, string json) { -- 2.11.0