OSDN Git Service

bit.ly API を使用する処理をBitlyApiクラスに分離
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 23 Apr 2017 11:38:54 +0000 (20:38 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 28 Apr 2017 18:26:44 +0000 (03:26 +0900)
OpenTween/Api/BitlyApi.cs [new file with mode: 0644]
OpenTween/AppendSettingDialog.cs
OpenTween/OpenTween.csproj

diff --git a/OpenTween/Api/BitlyApi.cs b/OpenTween/Api/BitlyApi.cs
new file mode 100644 (file)
index 0000000..3b4a429
--- /dev/null
@@ -0,0 +1,81 @@
+// 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.Http;
+using System.Text;
+using System.Threading.Tasks;
+using OpenTween.Connection;
+
+namespace OpenTween.Api
+{
+    public class BitlyApi
+    {
+        public static readonly Uri ApiBase = new Uri("http://api.bit.ly/");
+
+        private HttpClient http => this.localHttpClient ?? Networking.Http;
+        private readonly HttpClient localHttpClient;
+
+        public BitlyApi()
+            : this(null)
+        {
+        }
+
+        public BitlyApi(HttpClient http)
+        {
+            this.localHttpClient = http;
+        }
+
+        public bool ValidateApiKey(string login, string apiKey)
+            => this.ValidateApiKeyAsync(login, apiKey).Result;
+
+        public async Task<bool> ValidateApiKeyAsync(string login, string apikey)
+        {
+            try
+            {
+                var requestUri = new Uri(ApiBase, "/v3/validate");
+                var param = new Dictionary<string, string>
+                {
+                    ["login"] = ApplicationSettings.BitlyLoginId,
+                    ["apiKey"] = ApplicationSettings.BitlyApiKey,
+                    ["x_login"] = login,
+                    ["x_apiKey"] = apikey,
+                    ["format"] = "txt",
+                };
+
+                using (var postContent = new FormUrlEncodedContent(param))
+                using (var response = await this.http.PostAsync(requestUri, postContent).ConfigureAwait(false))
+                {
+                    var responseText = await response.Content.ReadAsStringAsync()
+                        .ConfigureAwait(false);
+
+                    return responseText.TrimEnd() == "1";
+                }
+            }
+            catch (OperationCanceledException) { }
+            catch (HttpRequestException) { }
+
+            return false;
+        }
+    }
+}
index 2523f82..3b26776 100644 (file)
@@ -169,7 +169,8 @@ namespace OpenTween
                     return;
                 }
 
-                if (!BitlyValidation(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text).Result)
+                var bitly = new BitlyApi();
+                if (!bitly.ValidateApiKey(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text))
                 {
                     MessageBox.Show(Properties.Resources.SettingSave_ClickText1);
                     _ValidationError = true;
@@ -358,40 +359,6 @@ namespace OpenTween
             this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamActive;
         }
 
-        private async Task<bool> BitlyValidation(string id, string apikey)
-        {
-            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(apikey))
-            {
-                return false;
-            }
-
-            try
-            {
-                var requestUri = new Uri("http://api.bit.ly/v3/validate");
-                var param = new Dictionary<string, string>
-                {
-                    ["login"] = ApplicationSettings.BitlyLoginId,
-                    ["apiKey"] = ApplicationSettings.BitlyApiKey,
-                    ["x_login"] = id,
-                    ["x_apiKey"] = apikey,
-                    ["format"] = "txt",
-                };
-
-                using (var postContent = new FormUrlEncodedContent(param))
-                using (var response = await Networking.Http.PostAsync(requestUri, postContent).ConfigureAwait(false))
-                {
-                    var responseText = await response.Content.ReadAsStringAsync()
-                        .ConfigureAwait(false);
-
-                    return responseText.TrimEnd() == "1";
-                }
-            }
-            catch (OperationCanceledException) { }
-            catch (HttpRequestException) { }
-
-            return false;
-        }
-
         private void Cancel_Click(object sender, EventArgs e)
         {
             _ValidationError = false;
index b971f20..5972612 100644 (file)
@@ -69,6 +69,7 @@
       <DependentUpon>ApiInfoDialog.cs</DependentUpon>
     </Compile>
     <Compile Include="Api\ApiLimit.cs" />
+    <Compile Include="Api\BitlyApi.cs" />
     <Compile Include="Api\DataModel\GeoJson.cs" />
     <Compile Include="Api\DataModel\TwitterConfiguration.cs" />
     <Compile Include="Api\DataModel\TwitterEntity.cs" />