-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAnnouncementsLoader.cs
More file actions
50 lines (41 loc) · 1.51 KB
/
AnnouncementsLoader.cs
File metadata and controls
50 lines (41 loc) · 1.51 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
using ByteSync.Common.Controls.Json;
using ByteSync.Common.Business.Announcements;
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 AnnouncementsLoader : IAnnouncementsLoader
{
private readonly AppSettings _appSettings;
private readonly ILogger<AnnouncementsLoader> _logger;
private readonly HttpClient _httpClient;
public AnnouncementsLoader(
IOptions<AppSettings> appSettings,
ILogger<AnnouncementsLoader> logger,
HttpClient httpClient)
{
_appSettings = appSettings.Value;
_logger = logger;
_httpClient = httpClient;
}
public async Task<List<Announcement>> Load()
{
List<Announcement>? announcements = null;
var policy = Policy
.Handle<Exception>()
.WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(3 * (retryAttempt + 1)));
await policy.Execute(async () =>
{
_logger.LogInformation("Loading announcements from {url}", _appSettings.AnnouncementsUrl);
var contents = await _httpClient.GetStringAsync(_appSettings.AnnouncementsUrl);
announcements = JsonHelper.Deserialize<List<Announcement>>(contents);
});
if (announcements == null)
{
throw new Exception("Failed to load announcements");
}
return announcements!;
}
}