Skip to content

Commit 68cf706

Browse files
committed
Update RemoteConfigurationProvider.cs
1 parent 5bcf624 commit 68cf706

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/Shiny.Extensions.Configuration.Remote/Infrastructure/RemoteConfigurationProvider.cs

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
using System.Text.Json;
2+
13
namespace Shiny.Extensions.Configuration.Remote.Infrastructure;
24

35

4-
public class RemoteConfigurationProvider(RemoteConfig config) : ConfigurationProvider, IRemoteConfigurationProvider
6+
public class RemoteConfigurationProvider(RemoteConfig config, Func<CancellationToken, Task<object>>? getData) : ConfigurationProvider, IRemoteConfigurationProvider
57
{
68
const string LAST_LOAD_KEY = "__LastLoaded";
9+
710
public override void Load()
811
{
912
base.Load();
@@ -38,6 +41,7 @@ public DateTimeOffset? LastLoaded
3841
}
3942

4043

44+
HttpClient? httpClient;
4145
readonly SemaphoreSlim semaphore = new(1);
4246
public async Task LoadAsync(CancellationToken cancellationToken = default)
4347
{
@@ -57,18 +61,34 @@ public async Task LoadAsync(CancellationToken cancellationToken = default)
5761
// if (wasWaiting)
5862
// return;
5963

60-
var httpClient = new HttpClient();
61-
var content = await httpClient.GetStringAsync(config.Uri, cancellationToken);
62-
63-
await File
64-
.WriteAllTextAsync(config.ConfigurationFilePath, content, cancellationToken)
65-
.ConfigureAwait(false);
64+
if (getData == null)
65+
{
66+
this.httpClient ??= new();
67+
var content = await this.httpClient
68+
.GetStringAsync(config.Uri, cancellationToken)
69+
.ConfigureAwait(false);
70+
}
71+
else
72+
{
73+
var obj = await getData.Invoke(cancellationToken).ConfigureAwait(false);
74+
var json = JsonSerializer.Serialize(obj);
75+
await this.WriteJson(json, cancellationToken);
76+
}
6677

67-
this.LastLoaded = DateTimeOffset.UtcNow;
6878
}
6979
finally
7080
{
7181
this.semaphore.Release();
7282
}
7383
}
84+
85+
86+
async Task WriteJson(string json, CancellationToken cancellationToken)
87+
{
88+
await File
89+
.WriteAllTextAsync(config.ConfigurationFilePath, json, cancellationToken)
90+
.ConfigureAwait(false);
91+
92+
this.LastLoaded = DateTimeOffset.UtcNow;
93+
}
7494
}

0 commit comments

Comments
 (0)