using System;
using System.Collections.Generic;
using System.Linq;
-using System.Net;
+using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
: base(autoupdate: false)
{
this.ApiHosts = apiHosts;
- this.LoadRegex();
+ this.LoadRegexAsync().Wait();
}
public string GetApiBase()
return this.ApiBase;
}
- protected override byte[] FetchRegex(string apiBase)
+ protected override Task<byte[]> FetchRegexAsync(string apiBase)
{
- if (apiBase == "http://down.example.com/api/")
- throw new WebException();
+ return Task.Run(() =>
+ {
+ if (apiBase == "http://down.example.com/api/")
+ throw new HttpRequestException();
- if (apiBase == "http://error.example.com/api/")
- return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
+ if (apiBase == "http://error.example.com/api/")
+ return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
- if (apiBase == "http://invalid.example.com/api/")
- return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
+ if (apiBase == "http://invalid.example.com/api/")
+ return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
- return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
+ return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
+ });
}
}
[Fact]
- public void HostFallbackTest()
+ public async Task HostFallbackTest()
{
var service = new TestImgAzyobuziNet(new[] { "http://avail1.example.com/api/", "http://avail2.example.com/api/" });
- service.LoadRegex();
+ await service.LoadRegexAsync();
Assert.Equal("http://avail1.example.com/api/", service.GetApiBase());
service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
- service.LoadRegex();
+ await service.LoadRegexAsync();
Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" });
- service.LoadRegex();
+ await service.LoadRegexAsync();
Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
service = new TestImgAzyobuziNet(new[] { "http://invalid.example.com/api/", "http://avail.example.com/api/" });
- service.LoadRegex();
+ await service.LoadRegexAsync();
Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
- service.LoadRegex();
+ await service.LoadRegexAsync();
Assert.Null(service.GetApiBase());
}
{
var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
- service.LoadRegex();
+ await service.LoadRegexAsync();
Assert.Null(service.GetApiBase());
var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", null, CancellationToken.None);
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Net;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.Xml;
public ImgAzyobuziNet(HttpClient http, bool autoupdate)
{
- this.UpdateTimer = new Timer(_ => this.LoadRegex());
+ this.UpdateTimer = new Timer(async _ => await this.LoadRegexAsync());
this.AutoUpdate = autoupdate;
this.Enabled = true;
this.UpdateTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
- public void LoadRegex()
+ public async Task LoadRegexAsync()
{
foreach (var host in this.ApiHosts)
{
try
{
- var result = this.LoadRegex(host);
+ var result = await this.LoadRegexAsync(host)
+ .ConfigureAwait(false);
+
if (result) return;
}
catch (Exception)
}
}
- public bool LoadRegex(string apiBase)
+ public async Task<bool> LoadRegexAsync(string apiBase)
{
try
{
- using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(this.FetchRegex(apiBase), XmlDictionaryReaderQuotas.Max))
+ var jsonBytes = await this.FetchRegexAsync(apiBase)
+ .ConfigureAwait(false);
+
+ using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max))
{
var xElm = XElement.Load(jsonReader);
return true;
}
- catch (WebException) { } // サーバーが2xx以外のステータスコードを返した場合
+ catch (HttpRequestException) { } // サーバーが2xx以外のステータスコードを返した場合
+ catch (OperationCanceledException) { } // リクエストがタイムアウトした場合
catch (XmlException) { } // サーバーが不正なJSONを返した場合
return false;
}
- protected virtual byte[] FetchRegex(string apiBase)
+ protected virtual async Task<byte[]> FetchRegexAsync(string apiBase)
{
- using (var client = new OTWebClient() { Timeout = 1000 })
+ using (var cts = new CancellationTokenSource(millisecondsDelay: 1000))
+ using (var response = await this.http.GetAsync(apiBase + "regex.json", cts.Token)
+ .ConfigureAwait(false))
{
- return client.DownloadData(apiBase + "regex.json");
+ response.EnsureSuccessStatusCode();
+
+ return await response.Content.ReadAsByteArrayAsync()
+ .ConfigureAwait(false);
}
}
////設定読み出し
LoadConfig();
+ ThumbnailGenerator.InitializeGenerator();
+
var imgazyobizinet = ThumbnailGenerator.ImgAzyobuziNetInstance.Value;
imgazyobizinet.Enabled = this._cfgCommon.EnableImgAzyobuziNet;
imgazyobizinet.DisabledInDM = this._cfgCommon.ImgAzyobuziNetDisabledInDM;
x.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret,
this.tw.AccessToken, this.tw.AccessTokenSecret, "", "");
- ThumbnailGenerator.InitializeGenerator();
-
//新着バルーン通知のチェック状態設定
NewPostPopMenuItem.Checked = _cfgCommon.NewAllPop;
this.NotifyFileMenuItem.Checked = NewPostPopMenuItem.Checked;