OSDN Git Service

bit.ly の短縮URLの生成処理をBitlyApiクラスに移動
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 23 Apr 2017 11:55:10 +0000 (20:55 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 28 Apr 2017 18:27:02 +0000 (03:27 +0900)
OpenTween/Api/BitlyApi.cs
OpenTween/ShortUrl.cs

index 0599e73..b8993e4 100644 (file)
@@ -24,6 +24,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Net.Http;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using OpenTween.Connection;
 
@@ -33,6 +34,9 @@ namespace OpenTween.Api
     {
         public static readonly Uri ApiBase = new Uri("https://api-ssl.bitly.com/");
 
+        public string EndUserLoginName { get; set; }
+        public string EndUserApiKey { get; set; }
+
         private HttpClient http => this.localHttpClient ?? Networking.Http;
         private readonly HttpClient localHttpClient;
 
@@ -46,6 +50,34 @@ namespace OpenTween.Api
             this.localHttpClient = http;
         }
 
+        public async Task<Uri> ShortenAsync(Uri srcUri, string domain = null)
+        {
+            var query = new Dictionary<string, string>
+            {
+                ["login"] = this.EndUserLoginName,
+                ["apiKey"] = this.EndUserApiKey,
+                ["format"] = "txt",
+                ["longUrl"] = srcUri.OriginalString,
+            };
+
+            if (!string.IsNullOrEmpty(domain))
+                query["domain"] = domain;
+
+            var uri = new Uri(ApiBase, "/v3/shorten?" + MyCommon.BuildQueryString(query));
+            using (var response = await this.http.GetAsync(uri).ConfigureAwait(false))
+            {
+                response.EnsureSuccessStatusCode();
+
+                var result = await response.Content.ReadAsStringAsync()
+                    .ConfigureAwait(false);
+
+                if (!Regex.IsMatch(result, @"^https?://"))
+                    throw new WebApiException("Failed to create URL.", result);
+
+                return new Uri(result.TrimEnd());
+            }
+        }
+
         public bool ValidateApiKey(string login, string apiKey)
             => this.ValidateApiKeyAsync(login, apiKey).Result;
 
index a08527e..b3ee02d 100644 (file)
@@ -35,6 +35,7 @@ using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Web;
+using OpenTween.Api;
 using OpenTween.Connection;
 
 namespace OpenTween
@@ -449,28 +450,14 @@ namespace OpenTween
             if (string.IsNullOrEmpty(this.BitlyId) || string.IsNullOrEmpty(this.BitlyKey))
                 return srcUri;
 
-            var query = new Dictionary<string, string>
+            var bitly = new BitlyApi
             {
-                ["login"] = this.BitlyId,
-                ["apiKey"] = this.BitlyKey,
-                ["format"] = "txt",
-                ["domain"] = domain,
-                ["longUrl"] = srcUri.OriginalString,
+                EndUserLoginName = this.BitlyId,
+                EndUserApiKey = this.BitlyKey,
             };
 
-            var uri = new Uri("https://api-ssl.bitly.com/v3/shorten?" + MyCommon.BuildQueryString(query));
-            using (var response = await this.http.GetAsync(uri).ConfigureAwait(false))
-            {
-                response.EnsureSuccessStatusCode();
-
-                var result = await response.Content.ReadAsStringAsync()
-                    .ConfigureAwait(false);
-
-                if (!Regex.IsMatch(result, @"^https?://"))
-                    throw new WebApiException("Failed to create URL.", result);
-
-                return new Uri(result.TrimEnd());
-            }
+            return await bitly.ShortenAsync(srcUri, domain)
+                .ConfigureAwait(false);
         }
 
         private async Task<Uri> ShortenByUxnuAsync(Uri srcUri)