Skip to content

Commit

Permalink
fixup! Mastodon APIに対するリクエストを送信するMastodonApiConnectionを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed May 5, 2017
1 parent 4d34b53 commit 9cc4c44
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions OpenTween/Connection/MastodonApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Linq;
using System.Net.Cache;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.WebSockets;
using System.Runtime.Serialization;
using System.Threading;
Expand All @@ -41,7 +42,7 @@ public sealed class MastodonApiConnection : IMastodonApiConnection

public MastodonApiConnection(Uri instanceUri, string accessToken)
{
this.InstanceUri = InstanceUri;
this.InstanceUri = instanceUri;
this.AccessToken = accessToken;

var websocketUri = new UriBuilder(this.InstanceUri);
Expand All @@ -66,11 +67,12 @@ public async Task<WebSocket> GetWebSocketAsync(Uri uri, IEnumerable<KeyValuePair

Networking.SetWebSocketOptions(socket.Options);

var requestParams = this.GetAccessTokenParams();
if (param != null)
requestParams = requestParams.Concat(param);
socket.Options.SetRequestHeader("Authorization", "Bearer " + this.AccessToken);

var requestUri = new Uri(this.WebsocketUri, uri);

var requestUri = new Uri(new Uri(this.WebsocketUri, uri), "?" + MyCommon.BuildQueryString(requestParams));
if (param != null)
requestUri = new Uri(requestUri, "?" + MyCommon.BuildQueryString(param));

await socket.ConnectAsync(requestUri, CancellationToken.None);

Expand All @@ -85,32 +87,35 @@ public void Dispose()

private async Task<T> GetAsync<T>(Uri uri, IEnumerable<KeyValuePair<string, string>> param)
{
var requestParams = this.GetAccessTokenParams();
if (param != null)
requestParams = requestParams.Concat(param);
var requestUri = new Uri(this.InstanceUri, uri);

var requestUri = new Uri(new Uri(this.InstanceUri, uri), "?" + MyCommon.BuildQueryString(requestParams));
if (param != null)
requestUri = new Uri(requestUri, "?" + MyCommon.BuildQueryString(param));

try
{
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.AccessToken);

using (var content = response.Content)
using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false))
{
var responseText = await content.ReadAsStringAsync()
.ConfigureAwait(false);
response.EnsureSuccessStatusCode();

try
{
return MyCommon.CreateDataFromJson<T>(responseText);
}
catch (SerializationException ex)
using (var content = response.Content)
{
throw new WebApiException("Invalid Response", responseText, ex);
var responseText = await content.ReadAsStringAsync()
.ConfigureAwait(false);

try
{
return MyCommon.CreateDataFromJson<T>(responseText);
}
catch (SerializationException ex)
{
throw new WebApiException("Invalid Response", responseText, ex);
}
}
}
}
Expand All @@ -129,15 +134,12 @@ private async Task<T> PostAsync<T>(HttpMethod method, Uri uri, IEnumerable<KeyVa
{
var requestUri = new Uri(this.InstanceUri, uri);

var requestParams = this.GetAccessTokenParams();
if (param != null)
requestParams = requestParams.Concat(param);

try
{
using (var request = new HttpRequestMessage(method, requestUri))
using (var postContent = new FormUrlEncodedContent(requestParams))
using (var postContent = new FormUrlEncodedContent(param))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.AccessToken);
request.Content = postContent;

using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
Expand Down Expand Up @@ -172,14 +174,6 @@ private async Task<T> PostAsync<T>(HttpMethod method, Uri uri, IEnumerable<KeyVa
}
}

private IEnumerable<KeyValuePair<string, string>> GetAccessTokenParams()
{
return new[]
{
new KeyValuePair<string, string>("access_token", this.AccessToken),
};
}

private void InitializeHttpClient()
{
var innerHandler = Networking.CreateHttpClientHandler();
Expand Down

0 comments on commit 9cc4c44

Please sign in to comment.