OSDN Git Service

IApiConnection.PostJsonAsyncからレスポンスボディを取得できるように変更
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 16 Jul 2023 11:24:42 +0000 (20:24 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sun, 16 Jul 2023 11:24:42 +0000 (20:24 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween.Tests/Connection/TwitterApiConnectionTest.cs
OpenTween/Connection/IApiConnection.cs
OpenTween/Connection/TwitterApiConnection.cs

index 202d529..3144b0c 100644 (file)
@@ -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;
index 05b1fa5..e975481 100644 (file)
@@ -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);
         }
 
index c25229e..e541263 100644 (file)
@@ -44,7 +44,7 @@ namespace OpenTween.Connection
 
         Task PostAsync(Uri uri, IDictionary<string, string>? param, IDictionary<string, IMediaItem>? media);
 
-        Task PostJsonAsync(Uri uri, string json);
+        Task<string> PostJsonAsync(Uri uri, string json);
 
         Task<LazyJson<T>> PostJsonAsync<T>(Uri uri, string json);
 
index 3431779..d76e088 100644 (file)
@@ -338,10 +338,34 @@ namespace OpenTween.Connection
             }
         }
 
-        public async Task PostJsonAsync(Uri uri, string json)
-            => await this.PostJsonAsync<object>(uri, json)
-                         .IgnoreResponse()
-                         .ConfigureAwait(false);
+        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
+            {
+                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<LazyJson<T>> PostJsonAsync<T>(Uri uri, string json)
         {