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;
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);
}
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);
}
}
- 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)
{