// SettingCommon.xml の TwitterUrl との互換性のために用意
public static string RestApiHost
{
- get { return RestApiBase.Host; }
- set { RestApiBase = new Uri($"https://{value}/1.1/"); }
+ get => RestApiBase.Host;
+ set => RestApiBase = new Uri($"https://{value}/1.1/");
}
public bool IsDisposed { get; private set; } = false;
try
{
- return await this.httpStreaming.GetStreamAsync(requestUri)
+ var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
+ var response = await this.httpStreaming.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
+ .ConfigureAwait(false);
+
+ await this.CheckStatusCode(response)
+ .ConfigureAwait(false);
+
+ return await response.Content.ReadAsStreamAsync()
.ConfigureAwait(false);
}
catch (HttpRequestException ex)
{
if (param != null)
{
- foreach (var kv in param)
- postContent.Add(new StringContent(kv.Value), kv.Key);
+ foreach (var (key, value) in param)
+ postContent.Add(new StringContent(value), key);
}
if (media != null)
{
- foreach (var kv in media)
- postContent.Add(new StreamContent(kv.Value.OpenRead()), kv.Key, kv.Value.Name);
+ foreach (var (key, value) in media)
+ postContent.Add(new StreamContent(value.OpenRead()), key, value.Name);
}
request.Content = postContent;
}
}
+ public async Task PostAsync(Uri uri, IDictionary<string, string> param, IDictionary<string, IMediaItem> media)
+ {
+ var requestUri = new Uri(RestApiBase, uri);
+ var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
+
+ using (var postContent = new MultipartFormDataContent())
+ {
+ if (param != null)
+ {
+ foreach (var (key, value) in param)
+ postContent.Add(new StringContent(value), key);
+ }
+ if (media != null)
+ {
+ foreach (var (key, value) in media)
+ postContent.Add(new StreamContent(value.OpenRead()), key, value.Name);
+ }
+
+ request.Content = postContent;
+
+ try
+ {
+ using (var response = await this.httpUpload.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
+ .ConfigureAwait(false))
+ {
+ await this.CheckStatusCode(response)
+ .ConfigureAwait(false);
+ }
+ }
+ catch (HttpRequestException ex)
+ {
+ throw TwitterApiException.CreateFromException(ex);
+ }
+ catch (OperationCanceledException ex)
+ {
+ throw TwitterApiException.CreateFromException(ex);
+ }
+ }
+ }
+
public async Task PostJsonAsync(Uri uri, string json)
{
var requestUri = new Uri(RestApiBase, uri);
}
}
+ public async Task DeleteAsync(Uri uri, IDictionary<string, string> param)
+ {
+ var requestUri = new Uri(RestApiBase, uri);
+ var request = new HttpRequestMessage(HttpMethod.Delete, requestUri);
+
+ using (var postContent = new FormUrlEncodedContent(param))
+ {
+ request.Content = postContent;
+
+ try
+ {
+ using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
+ .ConfigureAwait(false))
+ {
+ await this.CheckStatusCode(response)
+ .ConfigureAwait(false);
+ }
+ }
+ catch (HttpRequestException ex)
+ {
+ throw TwitterApiException.CreateFromException(ex);
+ }
+ catch (OperationCanceledException ex)
+ {
+ throw TwitterApiException.CreateFromException(ex);
+ }
+ }
+ }
+
protected async Task CheckStatusCode(HttpResponseMessage response)
{
var statusCode = response.StatusCode;
- if (statusCode == HttpStatusCode.OK)
+
+ if ((int)statusCode >= 200 && (int)statusCode <= 299)
{
Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
return;
Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
}
- throw new TwitterApiException(error, responseText);
+ throw new TwitterApiException(statusCode, error, responseText);
}
catch (SerializationException)
{
}
~TwitterApiConnection()
- {
- this.Dispose(false);
- }
+ => this.Dispose(false);
private void Networking_WebProxyChanged(object sender, EventArgs e)
- {
- this.InitializeHttpClients();
- }
+ => this.InitializeHttpClients();
public static async Task<Tuple<string, string>> GetRequestTokenAsync()
{