-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathClientSoftwareVersionSettingsLoader.cs
More file actions
62 lines (50 loc) · 2.16 KB
/
ClientSoftwareVersionSettingsLoader.cs
File metadata and controls
62 lines (50 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using ByteSync.Common.Business.Versions;
using ByteSync.Common.Controls.Json;
using ByteSync.ServerCommon.Business.Settings;
using ByteSync.ServerCommon.Interfaces.Loaders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Polly;
namespace ByteSync.ServerCommon.Loaders;
public class ClientSoftwareVersionSettingsLoader : IClientSoftwareVersionSettingsLoader
{
private readonly AppSettings _appSettings;
private readonly ILogger<ClientSoftwareVersionSettingsLoader> _logger;
private readonly HttpClient _httpClient;
public ClientSoftwareVersionSettingsLoader(
IOptions<AppSettings> appSettings,
ILogger<ClientSoftwareVersionSettingsLoader> logger,
HttpClient httpClient)
{
_appSettings = appSettings.Value;
_logger = logger;
_httpClient = httpClient;
}
public async Task<ClientSoftwareVersionSettings> Load()
{
SoftwareVersion? newMinimalVersionCandidate = null;
var policy = Policy
.Handle<Exception>()
.WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(3 * (retryAttempt + 1)));
await policy.Execute(async () =>
{
_logger.LogInformation("Loading minimal version from {url}", _appSettings.UpdatesDefinitionUrl);
var contents = await _httpClient.GetStringAsync(_appSettings.UpdatesDefinitionUrl);
var softwareUpdates = JsonHelper.Deserialize<List<SoftwareVersion>>(contents)!;
if (softwareUpdates != null)
{
newMinimalVersionCandidate = softwareUpdates.FirstOrDefault(u => u.Level == PriorityLevel.Minimal);
}
});
if (newMinimalVersionCandidate == null)
{
throw new Exception("Failed to load minimal version");
}
_logger.LogInformation("Minimal version is now: {version}", newMinimalVersionCandidate!.Version);
ClientSoftwareVersionSettings clientSoftwareVersionSettings = new ClientSoftwareVersionSettings
{
MinimalVersion = newMinimalVersionCandidate
};
return clientSoftwareVersionSettings;
}
}