From: Kimura Youichi Date: Sat, 26 Apr 2014 05:11:09 +0000 (+0900) Subject: ThumbnailInfo.LoadThumbnailImageAsync メソッドにHttpClientインスタンスを受け付けるように変更 X-Git-Tag: OpenTween_v1.2.0~37 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=1d391a1cf4969f7c3ba196bdce71789d8ae8684b;p=opentween%2Fopen-tween.git ThumbnailInfo.LoadThumbnailImageAsync メソッドにHttpClientインスタンスを受け付けるように変更 コンストラクタの引数にある HttpClient は不要になったため削除 --- diff --git a/OpenTween.Tests/TweetThumbnailTest.cs b/OpenTween.Tests/TweetThumbnailTest.cs index 3db90f69..813d3da3 100644 --- a/OpenTween.Tests/TweetThumbnailTest.cs +++ b/OpenTween.Tests/TweetThumbnailTest.cs @@ -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 LoadThumbnailImageAsync(CancellationToken token) + public override Task 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() { diff --git a/OpenTween/Thumbnail/Services/FoursquareCheckin.cs b/OpenTween/Thumbnail/Services/FoursquareCheckin.cs index e0250410..7c2310df 100644 --- a/OpenTween/Thumbnail/Services/FoursquareCheckin.cs +++ b/OpenTween/Thumbnail/Services/FoursquareCheckin.cs @@ -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), diff --git a/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs b/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs index 39ef490f..c662835f 100644 --- a/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs +++ b/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs @@ -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), diff --git a/OpenTween/Thumbnail/Services/MetaThumbnailService.cs b/OpenTween/Thumbnail/Services/MetaThumbnailService.cs index 91697f98..c62d25e8 100644 --- a/OpenTween/Thumbnail/Services/MetaThumbnailService.cs +++ b/OpenTween/Thumbnail/Services/MetaThumbnailService.cs @@ -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, diff --git a/OpenTween/Thumbnail/Services/Nicovideo.cs b/OpenTween/Thumbnail/Services/Nicovideo.cs index 91602f6e..7ffe97fa 100644 --- a/OpenTween/Thumbnail/Services/Nicovideo.cs +++ b/OpenTween/Thumbnail/Services/Nicovideo.cs @@ -178,7 +178,7 @@ namespace OpenTween.Thumbnail.Services if (!string.IsNullOrEmpty(imgurl)) { - return new ThumbnailInfo(this.http) + return new ThumbnailInfo { ImageUrl = url, ThumbnailUrl = imgurl, diff --git a/OpenTween/Thumbnail/Services/PhotoShareShortlink.cs b/OpenTween/Thumbnail/Services/PhotoShareShortlink.cs index cd6d8e47..0398be45 100644 --- a/OpenTween/Thumbnail/Services/PhotoShareShortlink.cs +++ b/OpenTween/Thumbnail/Services/PhotoShareShortlink.cs @@ -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", diff --git a/OpenTween/Thumbnail/Services/Pixiv.cs b/OpenTween/Thumbnail/Services/Pixiv.cs index b2e02109..46dce68d 100644 --- a/OpenTween/Thumbnail/Services/Pixiv.cs +++ b/OpenTween/Thumbnail/Services/Pixiv.cs @@ -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 LoadThumbnailImageAsync(CancellationToken token) + public async override Task 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(); diff --git a/OpenTween/Thumbnail/Services/SimpleThumbnailService.cs b/OpenTween/Thumbnail/Services/SimpleThumbnailService.cs index c7fb1135..a1b608b3 100644 --- a/OpenTween/Thumbnail/Services/SimpleThumbnailService.cs +++ b/OpenTween/Thumbnail/Services/SimpleThumbnailService.cs @@ -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, diff --git a/OpenTween/Thumbnail/Services/Tinami.cs b/OpenTween/Thumbnail/Services/Tinami.cs index bf0e73e6..ea96c5a4 100644 --- a/OpenTween/Thumbnail/Services/Tinami.cs +++ b/OpenTween/Thumbnail/Services/Tinami.cs @@ -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, diff --git a/OpenTween/Thumbnail/Services/TonTwitterCom.cs b/OpenTween/Thumbnail/Services/TonTwitterCom.cs index e7feaced..37257062 100644 --- a/OpenTween/Thumbnail/Services/TonTwitterCom.cs +++ b/OpenTween/Thumbnail/Services/TonTwitterCom.cs @@ -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 LoadThumbnailImageAsync(CancellationToken token) + public override Task 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); } } } diff --git a/OpenTween/Thumbnail/Services/Tumblr.cs b/OpenTween/Thumbnail/Services/Tumblr.cs index 20de6645..c2785611 100644 --- a/OpenTween/Thumbnail/Services/Tumblr.cs +++ b/OpenTween/Thumbnail/Services/Tumblr.cs @@ -75,7 +75,7 @@ namespace OpenTween.Thumbnail.Services return null; } - return new ThumbnailInfo(this.http) + return new ThumbnailInfo { ImageUrl = url, ThumbnailUrl = imgurl, diff --git a/OpenTween/Thumbnail/Services/ViaMe.cs b/OpenTween/Thumbnail/Services/ViaMe.cs index f834da3b..4e705ac3 100644 --- a/OpenTween/Thumbnail/Services/ViaMe.cs +++ b/OpenTween/Thumbnail/Services/ViaMe.cs @@ -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, diff --git a/OpenTween/Thumbnail/Services/Vimeo.cs b/OpenTween/Thumbnail/Services/Vimeo.cs index 3302ecb8..48dc688f 100644 --- a/OpenTween/Thumbnail/Services/Vimeo.cs +++ b/OpenTween/Thumbnail/Services/Vimeo.cs @@ -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, diff --git a/OpenTween/Thumbnail/Services/Youtube.cs b/OpenTween/Thumbnail/Services/Youtube.cs index 13deea8a..69d5e28e 100644 --- a/OpenTween/Thumbnail/Services/Youtube.cs +++ b/OpenTween/Thumbnail/Services/Youtube.cs @@ -171,7 +171,7 @@ namespace OpenTween.Thumbnail.Services } - return new ThumbnailInfo(this.http) + return new ThumbnailInfo { ImageUrl = url, ThumbnailUrl = imgUrl, diff --git a/OpenTween/Thumbnail/ThumbnailInfo.cs b/OpenTween/Thumbnail/ThumbnailInfo.cs index b07d17ae..b16ab98a 100644 --- a/OpenTween/Thumbnail/ThumbnailInfo.cs +++ b/OpenTween/Thumbnail/ThumbnailInfo.cs @@ -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 LoadThumbnailImageAsync() { + return this.LoadThumbnailImageAsync(CancellationToken.None); } - public ThumbnailInfo(HttpClient http) + public async Task LoadThumbnailImageAsync(CancellationToken cancellationToken) { - this.http = http ?? MyCommon.CreateHttpClient(); - } - - public Task LoadThumbnailImageAsync() - { - return this.LoadThumbnailImageAsync(CancellationToken.None); + using (var http = MyCommon.CreateHttpClient()) + { + return await this.LoadThumbnailImageAsync(http, cancellationToken) + .ConfigureAwait(false); + } } - public async virtual Task LoadThumbnailImageAsync(CancellationToken token) + public async virtual Task 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();