Skip to content

Commit 88fa862

Browse files
committed
Use event triggered update method instead of checking Proxy every time doing Http request
1 parent 5ab8c4f commit 88fa862

File tree

2 files changed

+105
-40
lines changed

2 files changed

+105
-40
lines changed

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 30 additions & 34 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,60 +33,54 @@ 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-
private static WebProxy _proxy = new WebProxy();
51-
public static WebProxy WebProxy {
52-
get
53-
{
54-
UpdateProxy();
55-
return _proxy;
56-
}
50+
private static readonly WebProxy _proxy = new WebProxy();
51+
52+
public static WebProxy WebProxy
53+
{
54+
get { return _proxy; }
5755
}
5856

5957
/// <summary>
6058
/// Update the Address of the Proxy to modify the client Proxy
6159
/// </summary>
62-
public static void UpdateProxy()
63-
// TODO: need test with a proxy
60+
public static void UpdateProxy(ProxyProperty property)
6461
{
65-
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
62+
(_proxy.Address, _proxy.Credentials) = property switch
6663
{
67-
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
64+
ProxyProperty.Enabled => (Proxy.Enabled) switch
6865
{
69-
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}");
70-
WebProxy.Credentials = null;
71-
}
72-
else
73-
{
74-
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}");
75-
WebProxy.Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password);
76-
}
77-
}
78-
else
79-
{
80-
WebProxy.Address = new WebProxy().Address;
81-
WebProxy.Credentials = null;
82-
}
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+
};
8380
}
8481

8582
public static async Task Download([NotNull] string url, [NotNull] string filePath)
8683
{
87-
UpdateProxy();
8884
using var response = await client.GetAsync(url);
8985
if (response.StatusCode == HttpStatusCode.OK)
9086
{
@@ -99,7 +95,6 @@ public static async Task Download([NotNull] string url, [NotNull] string filePat
9995

10096
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
10197
{
102-
UpdateProxy();
10398
Log.Debug($"|Http.Get|Url <{url}>");
10499
var response = await client.GetAsync(url);
105100
await using var stream = await response.Content.ReadAsStreamAsync();
@@ -111,7 +106,8 @@ public static async Task<string> Get([NotNull] string url, string encoding = "UT
111106
}
112107
else
113108
{
114-
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}>");
115111
}
116112
}
117113
}
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
}

0 commit comments

Comments
 (0)