Skip to content

Commit 8ec781d

Browse files
committed
Merge branch 'UpdateHttpMaster' of github.com:taooceros/Flow.Launcher into UpdateHttpMaster
2 parents d015fce + 88fa862 commit 8ec781d

File tree

3 files changed

+115
-44
lines changed

3 files changed

+115
-44
lines changed

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Flow.Launcher.Infrastructure.Logger;
88
using Flow.Launcher.Infrastructure.UserSettings;
99
using System;
10+
using System.ComponentModel;
1011

1112
namespace Flow.Launcher.Infrastructure.Http
1213
{
@@ -15,6 +16,7 @@ public static class Http
1516
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
1617

1718
private static HttpClient client;
19+
1820
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
1921
{
2022
UseProxy = true,
@@ -31,56 +33,58 @@ static Http()
3133

3234
client = new HttpClient(socketsHttpHandler, false);
3335
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
34-
3536
}
37+
3638
private static HttpProxy proxy;
39+
3740
public static HttpProxy Proxy
3841
{
39-
private get
40-
{
41-
return proxy;
42-
}
42+
private get { return proxy; }
4343
set
4444
{
4545
proxy = value;
46-
UpdateProxy();
46+
proxy.PropertyChanged += UpdateProxy;
4747
}
4848
}
4949

50-
public static WebProxy WebProxy { get; private set; } = new WebProxy();
50+
private static readonly WebProxy _proxy = new WebProxy();
51+
52+
public static WebProxy WebProxy
53+
{
54+
get { return _proxy; }
55+
}
5156

5257
/// <summary>
5358
/// Update the Address of the Proxy to modify the client Proxy
5459
/// </summary>
55-
public static void UpdateProxy()
56-
// TODO: need test with a proxy
60+
public static void UpdateProxy(ProxyProperty property)
5761
{
58-
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
62+
(_proxy.Address, _proxy.Credentials) = property switch
5963
{
60-
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
64+
ProxyProperty.Enabled => (Proxy.Enabled) switch
6165
{
62-
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}");
63-
WebProxy.Credentials = null;
64-
}
65-
else
66-
{
67-
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}");
68-
WebProxy.Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password);
69-
}
70-
}
71-
else
72-
{
73-
WebProxy.Address = new WebProxy().Address;
74-
WebProxy.Credentials = null;
75-
}
66+
true => Proxy.UserName switch
67+
{
68+
var userName when !string.IsNullOrEmpty(userName) =>
69+
(new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null),
70+
_ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"),
71+
new NetworkCredential(Proxy.UserName, Proxy.Password))
72+
},
73+
false => (null, null)
74+
},
75+
ProxyProperty.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), _proxy.Credentials),
76+
ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), _proxy.Credentials),
77+
ProxyProperty.UserName => (_proxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
78+
ProxyProperty.Password => (_proxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password))
79+
};
7680
}
7781

78-
public async static Task Download([NotNull] string url, [NotNull] string filePath)
82+
public static async Task Download([NotNull] string url, [NotNull] string filePath)
7983
{
8084
using var response = await client.GetAsync(url);
8185
if (response.StatusCode == HttpStatusCode.OK)
8286
{
83-
using var fileStream = new FileStream(filePath, FileMode.CreateNew);
87+
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
8488
await response.Content.CopyToAsync(fileStream);
8589
}
8690
else
@@ -93,7 +97,7 @@ public static async Task<string> Get([NotNull] string url, string encoding = "UT
9397
{
9498
Log.Debug($"|Http.Get|Url <{url}>");
9599
var response = await client.GetAsync(url);
96-
using var stream = await response.Content.ReadAsStreamAsync();
100+
await using var stream = await response.Content.ReadAsStreamAsync();
97101
using var reader = new StreamReader(stream, Encoding.GetEncoding(encoding));
98102
var content = await reader.ReadToEndAsync();
99103
if (response.StatusCode == HttpStatusCode.OK)
@@ -102,7 +106,8 @@ public static async Task<string> Get([NotNull] string url, string encoding = "UT
102106
}
103107
else
104108
{
105-
throw new HttpRequestException($"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
109+
throw new HttpRequestException(
110+
$"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
106111
}
107112
}
108113
}
Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,80 @@
1-
namespace Flow.Launcher.Infrastructure.UserSettings
1+
using System.ComponentModel;
2+
3+
namespace Flow.Launcher.Infrastructure.UserSettings
24
{
5+
public enum ProxyProperty
6+
{
7+
Enabled,
8+
Server,
9+
Port,
10+
UserName,
11+
Password
12+
}
13+
314
public class HttpProxy
415
{
5-
public bool Enabled { get; set; } = false;
6-
public string Server { get; set; }
7-
public int Port { get; set; }
8-
public string UserName { get; set; }
9-
public string Password { get; set; }
16+
private bool _enabled = false;
17+
private string _server;
18+
private int _port;
19+
private string _userName;
20+
private string _password;
21+
22+
public bool Enabled
23+
{
24+
get => _enabled;
25+
set
26+
{
27+
_enabled = value;
28+
OnPropertyChanged(ProxyProperty.Enabled);
29+
}
30+
}
31+
32+
public string Server
33+
{
34+
get => _server;
35+
set
36+
{
37+
_server = value;
38+
OnPropertyChanged(ProxyProperty.Server);
39+
}
40+
}
41+
42+
public int Port
43+
{
44+
get => _port;
45+
set
46+
{
47+
_port = value;
48+
OnPropertyChanged(ProxyProperty.Port);
49+
}
50+
}
51+
52+
public string UserName
53+
{
54+
get => _userName;
55+
set
56+
{
57+
_userName = value;
58+
OnPropertyChanged(ProxyProperty.UserName);
59+
}
60+
}
61+
62+
public string Password
63+
{
64+
get => _password;
65+
set
66+
{
67+
_password = value;
68+
OnPropertyChanged(ProxyProperty.Password);
69+
}
70+
}
71+
72+
public delegate void ProxyPropertyChangedHandler(ProxyProperty property);
73+
public event ProxyPropertyChangedHandler PropertyChanged;
74+
75+
private void OnPropertyChanged(ProxyProperty property)
76+
{
77+
PropertyChanged?.Invoke(property);
78+
}
1079
}
1180
}

Plugins/Flow.Launcher.Plugin.PluginsManager/Models/PluginsManifest.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@ namespace Flow.Launcher.Plugin.PluginsManager.Models
1010
internal class PluginsManifest
1111
{
1212
internal List<UserPlugin> UserPlugins { get; private set; }
13+
1314
internal PluginsManifest()
1415
{
15-
DownloadManifest();
16+
DownloadManifest().Wait();
1617
}
1718

18-
private void DownloadManifest()
19+
private async Task DownloadManifest()
1920
{
2021
var json = string.Empty;
2122
try
2223
{
23-
var t = Task.Run(
24-
async () =>
25-
json = await Http.Get("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json"));
26-
27-
t.Wait();
24+
json = await Http.Get(
25+
"https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json");
2826

2927
UserPlugins = JsonConvert.DeserializeObject<List<UserPlugin>>(json);
3028
}
@@ -34,7 +32,6 @@ private void DownloadManifest()
3432

3533
UserPlugins = new List<UserPlugin>();
3634
}
37-
3835
}
3936
}
40-
}
37+
}

0 commit comments

Comments
 (0)