-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mastodon APIに対するリクエストを送信するMastodonApiConnectionを実装
- Loading branch information
Showing
4 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// OpenTween - Client of Twitter | ||
// Copyright (c) 2017 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/> | ||
// All rights reserved. | ||
// | ||
// This file is part of OpenTween. | ||
// | ||
// This program is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by the Free | ||
// Software Foundation; either version 3 of the License, or (at your option) | ||
// any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
// for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to | ||
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, | ||
// Boston, MA 02110-1301, USA. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.WebSockets; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenTween.Connection | ||
{ | ||
public interface IMastodonApiConnection : IDisposable | ||
{ | ||
Task<T> SendAsync<T>(HttpMethod method, Uri uri, IEnumerable<KeyValuePair<string, string>> param); | ||
Task<WebSocket> GetWebSocketAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> param); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// OpenTween - Client of Twitter | ||
// Copyright (c) 2017 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/> | ||
// All rights reserved. | ||
// | ||
// This file is part of OpenTween. | ||
// | ||
// This program is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by the Free | ||
// Software Foundation; either version 3 of the License, or (at your option) | ||
// any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
// for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to | ||
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, | ||
// Boston, MA 02110-1301, USA. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Cache; | ||
using System.Net.Http; | ||
using System.Net.WebSockets; | ||
using System.Runtime.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenTween.Connection | ||
{ | ||
public sealed class MastodonApiConnection : IMastodonApiConnection | ||
{ | ||
public Uri InstanceUri { get; } | ||
public Uri WebsocketUri { get; } | ||
public string AccessToken { get; } | ||
|
||
internal HttpClient http; | ||
|
||
public MastodonApiConnection(Uri instanceUri, string accessToken) | ||
{ | ||
this.InstanceUri = InstanceUri; | ||
this.AccessToken = accessToken; | ||
|
||
var websocketUri = new UriBuilder(this.InstanceUri); | ||
websocketUri.Scheme = websocketUri.Scheme == "https" ? "wss" : "ws"; | ||
this.WebsocketUri = websocketUri.Uri; | ||
|
||
this.InitializeHttpClient(); | ||
Networking.WebProxyChanged += this.Networking_WebProxyChanged; | ||
} | ||
|
||
public Task<T> SendAsync<T>(HttpMethod method, Uri uri, IEnumerable<KeyValuePair<string, string>> param) | ||
{ | ||
if (method == HttpMethod.Get) | ||
return this.GetAsync<T>(uri, param); | ||
|
||
return this.PostAsync<T>(method, uri, param); | ||
} | ||
|
||
public async Task<WebSocket> GetWebSocketAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> param) | ||
{ | ||
var socket = new ClientWebSocket(); | ||
|
||
Networking.SetWebSocketOptions(socket.Options); | ||
|
||
var requestParams = this.GetAccessTokenParams(); | ||
if (param != null) | ||
requestParams = requestParams.Concat(param); | ||
|
||
var requestUri = new Uri(new Uri(this.WebsocketUri, uri), "?" + MyCommon.BuildQueryString(requestParams)); | ||
|
||
await socket.ConnectAsync(requestUri, CancellationToken.None); | ||
|
||
return socket; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Networking.WebProxyChanged -= this.Networking_WebProxyChanged; | ||
this.http.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(new Uri(this.InstanceUri, uri), "?" + MyCommon.BuildQueryString(requestParams)); | ||
|
||
try | ||
{ | ||
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) | ||
using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead) | ||
.ConfigureAwait(false)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
|
||
using (var content = response.Content) | ||
{ | ||
var responseText = await content.ReadAsStringAsync() | ||
.ConfigureAwait(false); | ||
|
||
try | ||
{ | ||
return MyCommon.CreateDataFromJson<T>(responseText); | ||
} | ||
catch (SerializationException ex) | ||
{ | ||
throw new WebApiException("Invalid Response", responseText, ex); | ||
} | ||
} | ||
} | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
throw new WebApiException(ex.InnerException?.Message ?? ex.Message, ex); | ||
} | ||
catch (OperationCanceledException ex) | ||
{ | ||
throw new WebApiException("Timeout", ex); | ||
} | ||
} | ||
|
||
private async Task<T> PostAsync<T>(HttpMethod method, Uri uri, IEnumerable<KeyValuePair<string, string>> param) | ||
{ | ||
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)) | ||
{ | ||
request.Content = postContent; | ||
|
||
using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead) | ||
.ConfigureAwait(false)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
|
||
using (var content = response.Content) | ||
{ | ||
var responseText = await content.ReadAsStringAsync() | ||
.ConfigureAwait(false); | ||
|
||
try | ||
{ | ||
return MyCommon.CreateDataFromJson<T>(responseText); | ||
} | ||
catch (SerializationException ex) | ||
{ | ||
throw new WebApiException("Invalid Response", responseText, ex); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
throw new WebApiException(ex.InnerException?.Message ?? ex.Message, ex); | ||
} | ||
catch (OperationCanceledException ex) | ||
{ | ||
throw new WebApiException("Timeout", ex); | ||
} | ||
} | ||
|
||
private IEnumerable<KeyValuePair<string, string>> GetAccessTokenParams() | ||
{ | ||
return new[] | ||
{ | ||
new KeyValuePair<string, string>("access_token", this.AccessToken), | ||
}; | ||
} | ||
|
||
private void InitializeHttpClient() | ||
{ | ||
var innerHandler = Networking.CreateHttpClientHandler(); | ||
innerHandler.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache); | ||
|
||
this.http = Networking.CreateHttpClient(innerHandler); | ||
} | ||
|
||
private void Networking_WebProxyChanged(object sender, EventArgs e) | ||
=> this.InitializeHttpClient(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters