From: Kimura Youichi Date: Sun, 16 Jul 2023 11:24:42 +0000 (+0900) Subject: IApiConnection.PostJsonAsyncからレスポンスボディを取得できるように変更 X-Git-Tag: OpenTween_v3.7.0^2~9^2~2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=243ae2ff906e44a7e06273e05f56593c39cb95b4;p=opentween%2Fopen-tween.git IApiConnection.PostJsonAsyncからレスポンスボディを取得できるように変更 --- 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) {