OSDN Git Service

ThumbnailInfo.LoadThumbnailImageAsync メソッドにHttpClientインスタンスを受け付けるように変更
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 26 Apr 2014 05:11:09 +0000 (14:11 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 26 Apr 2014 09:44:47 +0000 (18:44 +0900)
コンストラクタの引数にある HttpClient は不要になったため削除

15 files changed:
OpenTween.Tests/TweetThumbnailTest.cs
OpenTween/Thumbnail/Services/FoursquareCheckin.cs
OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
OpenTween/Thumbnail/Services/MetaThumbnailService.cs
OpenTween/Thumbnail/Services/Nicovideo.cs
OpenTween/Thumbnail/Services/PhotoShareShortlink.cs
OpenTween/Thumbnail/Services/Pixiv.cs
OpenTween/Thumbnail/Services/SimpleThumbnailService.cs
OpenTween/Thumbnail/Services/Tinami.cs
OpenTween/Thumbnail/Services/TonTwitterCom.cs
OpenTween/Thumbnail/Services/Tumblr.cs
OpenTween/Thumbnail/Services/ViaMe.cs
OpenTween/Thumbnail/Services/Vimeo.cs
OpenTween/Thumbnail/Services/Youtube.cs
OpenTween/Thumbnail/ThumbnailInfo.cs

index 3db90f6..813d3da 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Net.Http;
 using System.Reflection;
 using System.Runtime.InteropServices;
 using System.Text.RegularExpressions;
@@ -71,7 +72,7 @@ namespace OpenTween
 
             class MockThumbnailInfo : ThumbnailInfo
             {
-                public override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+                public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
                 {
                     var image = MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl));
                     return Task.FromResult(image);
@@ -82,6 +83,7 @@ namespace OpenTween
         public TweetThumbnailTest()
         {
             this.ThumbnailGeneratorSetup();
+            this.MyCommonSetup();
         }
 
         public void ThumbnailGeneratorSetup()
@@ -94,6 +96,14 @@ namespace OpenTween
             });
         }
 
+        public void MyCommonSetup()
+        {
+            var mockAssembly = Substitute.For<_Assembly>();
+            mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
+
+            MyCommon.EntryAssembly = mockAssembly;
+        }
+
         [Fact]
         public void CreatePictureBoxTest()
         {
index e025041..7c2310d 100644 (file)
@@ -93,7 +93,7 @@ namespace OpenTween.Thumbnail.Services
 
                     var map = MapThumb.GetDefaultInstance();
 
-                    return new ThumbnailInfo(this.http)
+                    return new ThumbnailInfo
                     {
                         ImageUrl = map.CreateMapLinkUrl(location.Latitude, location.Longitude),
                         ThumbnailUrl = map.CreateStaticMapUrl(location.Latitude, location.Longitude),
index 39ef490..c662835 100644 (file)
@@ -200,7 +200,7 @@ namespace OpenTween.Thumbnail.Services
                     {
                         if (regex.IsMatch(url))
                         {
-                            return new ThumbnailInfo(this.http)
+                            return new ThumbnailInfo
                             {
                                 ImageUrl = url,
                                 ThumbnailUrl = this.ApiBase + "redirect?size=large&uri=" + Uri.EscapeDataString(url),
index 91697f9..c62d25e 100644 (file)
@@ -61,7 +61,7 @@ namespace OpenTween.Thumbnail.Services
                 var thumbnailUrl = this.GetThumbnailUrl(content);
                 if (string.IsNullOrEmpty(thumbnailUrl)) return null;
 
-                return new ThumbnailInfo(this.http)
+                return new ThumbnailInfo
                 {
                     ImageUrl = url,
                     ThumbnailUrl = thumbnailUrl,
index 91602f6..7ffe97f 100644 (file)
@@ -178,7 +178,7 @@ namespace OpenTween.Thumbnail.Services
 
                     if (!string.IsNullOrEmpty(imgurl))
                     {
-                        return new ThumbnailInfo(this.http)
+                        return new ThumbnailInfo
                         {
                             ImageUrl = url,
                             ThumbnailUrl = imgurl,
index cd6d8e4..0398be4 100644 (file)
@@ -32,17 +32,10 @@ namespace OpenTween.Thumbnail.Services
 {
     class PhotoShareShortlink : IThumbnailService
     {
-        protected readonly HttpClient http;
         protected Regex regex;
 
         public PhotoShareShortlink(string pattern)
-            : this(null, pattern)
         {
-        }
-
-        public PhotoShareShortlink(HttpClient http, string pattern)
-        {
-            this.http = http ?? MyCommon.CreateHttpClient();
             this.regex = new Regex(pattern);
         }
 
@@ -54,7 +47,7 @@ namespace OpenTween.Thumbnail.Services
 
                 if (!match.Success) return null;
 
-                return new ThumbnailInfo(this.http)
+                return new ThumbnailInfo
                 {
                     ImageUrl = url,
                     ThumbnailUrl = "http://images.bcphotoshare.com/storages/" + RadixConvert.ToInt32(match.Result("${1}"), 36) + "/thumb180.jpg",
index b2e0210..46dce68 100644 (file)
@@ -45,7 +45,7 @@ namespace OpenTween.Thumbnail.Services
 
             if (thumb == null) return null;
 
-            return new Pixiv.Thumbnail(this.http)
+            return new Pixiv.Thumbnail
             {
                 ImageUrl = thumb.ImageUrl,
                 ThumbnailUrl = thumb.ThumbnailUrl,
@@ -56,19 +56,14 @@ namespace OpenTween.Thumbnail.Services
 
         public class Thumbnail : ThumbnailInfo
         {
-            public Thumbnail(HttpClient http)
-                : base(http)
-            {
-            }
-
-            public async override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+            public async override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
             {
                 var request = new HttpRequestMessage(HttpMethod.Get, this.ThumbnailUrl);
 
                 request.Headers.Add("User-Agent", MyCommon.GetUserAgentString(fakeMSIE: true));
                 request.Headers.Referrer = new Uri(this.ImageUrl);
 
-                using (var response = await this.http.SendAsync(request, token).ConfigureAwait(false))
+                using (var response = await http.SendAsync(request, cancellationToken).ConfigureAwait(false))
                 {
                     response.EnsureSuccessStatusCode();
 
index c7fb113..a1b608b 100644 (file)
@@ -71,7 +71,7 @@ namespace OpenTween.Thumbnail.Services
                 var thumbnailUrl = this.ReplaceUrl(url);
                 if (thumbnailUrl == null) return null;
 
-                return new ThumbnailInfo(this.http)
+                return new ThumbnailInfo
                 {
                     ImageUrl = url,
                     ThumbnailUrl = thumbnailUrl,
index bf0e73e..ea96c5a 100644 (file)
@@ -55,7 +55,7 @@ namespace OpenTween.Thumbnail.Services
                 {
                     var descElm = xdoc.XPathSelectElement("/rsp/content/description");
 
-                    return new ThumbnailInfo(this.http)
+                    return new ThumbnailInfo
                     {
                         ImageUrl = url,
                         ThumbnailUrl = thumbUrlElm.Attribute("url").Value,
index e7feace..3725706 100644 (file)
@@ -24,6 +24,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Net;
+using System.Net.Http;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
@@ -62,8 +63,9 @@ namespace OpenTween.Thumbnail.Services
 
         public class Thumbnail : ThumbnailInfo
         {
-            public override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+            public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
             {
+                // TODO: HttpClient を使用したコードに置き換えたい
                 return Task.Run(() =>
                 {
                     var oauth = new HttpOAuthApiProxy();
@@ -79,7 +81,7 @@ namespace OpenTween.Thumbnail.Services
                         else
                             throw new WebException(statusCode.ToString(), WebExceptionStatus.ProtocolError);
                     }
-                }, token);
+                }, cancellationToken);
             }
         }
     }
index 20de664..c278561 100644 (file)
@@ -75,7 +75,7 @@ namespace OpenTween.Thumbnail.Services
                         return null;
                     }
 
-                    return new ThumbnailInfo(this.http)
+                    return new ThumbnailInfo
                     {
                         ImageUrl = url,
                         ThumbnailUrl = imgurl,
index f834da3..4e705ac 100644 (file)
@@ -60,7 +60,7 @@ namespace OpenTween.Thumbnail.Services
 
                 var textElm = xElm.XPathSelectElement("/response/post/text");
 
-                return new ThumbnailInfo(this.http)
+                return new ThumbnailInfo
                 {
                     ImageUrl = url,
                     ThumbnailUrl = thumbUrlElm.Value,
index 3302ecb..48dc688 100644 (file)
@@ -65,7 +65,7 @@ namespace OpenTween.Thumbnail.Services
                     tooltipText = string.Format("{0} ({1:00}:{2:00})", titleElm.Value, minute, second);
                 }
 
-                return new ThumbnailInfo(this.http)
+                return new ThumbnailInfo
                 {
                     ImageUrl = url,
                     ThumbnailUrl = thumbUrlElm.Value,
index 13deea8..69d5e28 100644 (file)
@@ -171,7 +171,7 @@ namespace OpenTween.Thumbnail.Services
 
                         }
 
-                        return new ThumbnailInfo(this.http)
+                        return new ThumbnailInfo
                         {
                             ImageUrl = url,
                             ThumbnailUrl = imgUrl,
index b07d17a..b16ab98 100644 (file)
@@ -36,26 +36,23 @@ namespace OpenTween.Thumbnail
         public string TooltipText { get; set; }
         public string FullSizeImageUrl { get; set; }
 
-        protected readonly HttpClient http;
-
-        public ThumbnailInfo()
-            : this(null)
+        public Task<MemoryImage> LoadThumbnailImageAsync()
         {
+            return this.LoadThumbnailImageAsync(CancellationToken.None);
         }
 
-        public ThumbnailInfo(HttpClient http)
+        public async Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken cancellationToken)
         {
-            this.http = http ?? MyCommon.CreateHttpClient();
-        }
-
-        public Task<MemoryImage> LoadThumbnailImageAsync()
-        {
-            return this.LoadThumbnailImageAsync(CancellationToken.None);
+            using (var http = MyCommon.CreateHttpClient())
+            {
+                return await this.LoadThumbnailImageAsync(http, cancellationToken)
+                    .ConfigureAwait(false);
+            }
         }
 
-        public async virtual Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+        public async virtual Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
         {
-            using (var response = await this.http.GetAsync(this.ThumbnailUrl, token).ConfigureAwait(false))
+            using (var response = await http.GetAsync(this.ThumbnailUrl, cancellationToken).ConfigureAwait(false))
             {
                 response.EnsureSuccessStatusCode();