use unified http method for plugin installation + add more exceptions

#573
#610
This commit is contained in:
bao-qian
2016-05-10 20:26:47 +01:00
parent 8325083402
commit 6359826fd9
7 changed files with 111 additions and 108 deletions

View File

@@ -1,34 +0,0 @@
using System;
using System.Net;
namespace Wox.Plugin.PluginManagement
{
public class HttpRequest
{
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
public static HttpWebResponse CreateGetHttpResponse(string url,IHttpProxy proxy)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server))
{
if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password))
{
request.Proxy = new WebProxy(proxy.Server, proxy.Port);
}
else
{
request.Proxy = new WebProxy(proxy.Server, proxy.Port);
request.Proxy.Credentials = new NetworkCredential(proxy.UserName, proxy.Password);
}
}
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
return request.GetResponse() as HttpWebResponse;
}
}
}

View File

@@ -8,6 +8,8 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Newtonsoft.Json;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.PluginManagement
{
@@ -108,68 +110,71 @@ namespace Wox.Plugin.PluginManagement
List<Result> results = new List<Result>();
string pluginName = query.SecondSearch;
if (string.IsNullOrEmpty(pluginName)) return results;
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + pluginName, context.Proxy);
Stream s = response.GetResponseStream();
if (s != null)
string json;
try
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
string json = reader.ReadToEnd();
List<WoxPluginResult> searchedPlugins = null;
try
{
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
}
catch
{
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
return results;
}
var task = Http.Get(pluginSearchUrl + pluginName, context.Proxy);
task.RunSynchronously();
json = task.Result;
}
catch (WebException e)
{
Log.Warn("Can't connect to Wox plugin website, check your conenction");
Log.Error(e);
return new List<Result>();
}
List<WoxPluginResult> searchedPlugins;
try
{
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
}
catch(JsonSerializationException e)
{
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
Log.Error(e);
return results;
}
foreach (WoxPluginResult r in searchedPlugins)
foreach (WoxPluginResult r in searchedPlugins)
{
WoxPluginResult r1 = r;
results.Add(new Result
{
WoxPluginResult r1 = r;
results.Add(new Result
Title = r.name,
SubTitle = r.description,
IcoPath = "Images\\plugin.png",
Action = c =>
{
Title = r.name,
SubTitle = r.description,
IcoPath = "Images\\plugin.png",
Action = e =>
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
"Install plugin", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
"Install plugin", MessageBoxButton.YesNo);
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
if (result == MessageBoxResult.Yes)
context.API.StartLoadingBar();
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
try
{
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
context.API.StartLoadingBar();
Task.Run(() =>
{
using (WebClient Client = new WebClient())
{
try
{
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
Client.DownloadFile(pluginUrl, filePath);
context.API.InstallPlugin(filePath);
}
catch (Exception exception)
{
MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message);
}
finally
{
context.API.StopLoadingBar();
}
}
});
Http.Download(pluginUrl, filePath, context.Proxy);
}
return false;
catch (WebException e)
{
var info = "download plugin " + r.name + "failed.";
MessageBox.Show(info);
Log.Warn(info);
Log.Error(e);
return false;
}
context.API.InstallPlugin(filePath);
context.API.StopLoadingBar();
}
});
}
return false;
}
});
}
return results;
}

View File

@@ -53,7 +53,6 @@
<Compile Include="..\..\SolutionAssemblyInfo.cs">
<Link>Properties\SolutionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="HttpRequest.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WoxPluginResult.cs" />

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
@@ -18,7 +19,20 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
public override async Task<List<string>> GetSuggestions(string query)
{
var result = await HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), Proxy, "GB2312");
string result;
try
{
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
result = await Http.Get(api + Uri.EscapeUriString(query), Proxy, "GB2312");
}
catch (WebException e)
{
Log.Warn("Can't get suggestion from baidu");
Log.Error(e);
return new List<string>(); ;
}
if (string.IsNullOrEmpty(result)) return new List<string>();
Match match = reg.Match(result);

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -14,7 +15,18 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
public override string Domain { get; set; } = "www.google.com";
public override async Task<List<string>> GetSuggestions(string query)
{
var result = await HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), Proxy);
string result;
try
{
const string api = "https://www.google.com/complete/search?output=chrome&q=";
result = await Http.Get(api + Uri.EscapeUriString(query), Proxy);
}
catch (WebException e)
{
Log.Warn("Can't get suggestion from google");
Log.Error(e);
return new List<string>(); ;
}
if (string.IsNullOrEmpty(result)) return new List<string>();
JContainer json;
try