Skip to content

Commit 8912789

Browse files
committed
Use http client for code quality
1 parent 71ab7a6 commit 8912789

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

Flow.Launcher/SettingPages/ViewModels/SettingsPaneProxyViewModel.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
24
using CommunityToolkit.Mvvm.Input;
35
using Flow.Launcher.Core;
46
using Flow.Launcher.Infrastructure.UserSettings;
@@ -8,49 +10,43 @@ namespace Flow.Launcher.SettingPages.ViewModels;
810

911
public partial class SettingsPaneProxyViewModel : BaseModel
1012
{
11-
private readonly Updater _updater;
1213
public Settings Settings { get; }
1314

15+
private readonly Updater _updater;
16+
1417
public SettingsPaneProxyViewModel(Settings settings, Updater updater)
1518
{
16-
_updater = updater;
1719
Settings = settings;
20+
_updater = updater;
1821
}
1922

2023
[RelayCommand]
21-
private void OnTestProxyClicked()
24+
private async Task OnTestProxyClickedAsync()
2225
{
23-
var message = TestProxy();
26+
var message = await TestProxyAsync();
2427
App.API.ShowMsgBox(App.API.GetTranslation(message));
2528
}
2629

27-
private string TestProxy()
30+
private async Task<string> TestProxyAsync()
2831
{
2932
if (string.IsNullOrEmpty(Settings.Proxy.Server)) return "serverCantBeEmpty";
3033
if (Settings.Proxy.Port <= 0) return "portCantBeEmpty";
3134

32-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_updater.GitHubRepository);
33-
34-
if (string.IsNullOrEmpty(Settings.Proxy.UserName) || string.IsNullOrEmpty(Settings.Proxy.Password))
35+
var handler = new HttpClientHandler
3536
{
36-
request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port);
37-
}
38-
else
37+
Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port)
38+
};
39+
40+
if (!string.IsNullOrEmpty(Settings.Proxy.UserName) && !string.IsNullOrEmpty(Settings.Proxy.Password))
3941
{
40-
request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port)
41-
{
42-
Credentials = new NetworkCredential(Settings.Proxy.UserName, Settings.Proxy.Password)
43-
};
42+
handler.Proxy.Credentials = new NetworkCredential(Settings.Proxy.UserName, Settings.Proxy.Password);
4443
}
4544

45+
using var client = new HttpClient(handler);
4646
try
4747
{
48-
var response = (HttpWebResponse)request.GetResponse();
49-
return response.StatusCode switch
50-
{
51-
HttpStatusCode.OK => "proxyIsCorrect",
52-
_ => "proxyConnectFailed"
53-
};
48+
var response = await client.GetAsync(_updater.GitHubRepository);
49+
return response.IsSuccessStatusCode ? "proxyIsCorrect" : "proxyConnectFailed";
5450
}
5551
catch
5652
{

0 commit comments

Comments
 (0)