Skip to content

Commit

Permalink
Offline ready for config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Sep 21, 2024
1 parent f92113b commit 3601887
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 45 deletions.
47 changes: 10 additions & 37 deletions samples/Sample/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
{
"Mediator": {
"Http": {
"ContractNamespace" : "https://localhost"
"My.Namespace.*" : "https://localhost"
},
"Offline": {
"*": {
"AvailableAcrossSessions": true
}
},
"TimerRefresh": {
"My.Contact": {
"*": {
"Interval": 30000
}
},
"Resilient": {
"Resilience": {
"*": {
"RetryCount": 3,
"RetryDelay": 2000,
"CircuitBreakerCount": 5,
"CircuitBreakerDelay": 5000
},
"Namespace.Contact": {
"RetryCount": 3,
"RetryDelay": 2000,
"CircuitBreakerCount": 5,
"CircuitBreakerDelay": 5000
},
"Namespace.*": {
"RetryCount": 3,
"RetryDelay": 2000,
"CircuitBreakerCount": 5,
"CircuitBreakerDelay": 5000
}
},
"Caching": {
"*": {
"Cache": {
"*": { // TODO: needs a key name though for the cache
"Priority": "High",
"AbsoluteExpirationSeconds": 300,
"SlidingExpirationSeconds": 60
Expand All @@ -45,26 +38,6 @@
"Title": "ERREUR",
"ErrorMessage" : "Échec de faire quelque chose"
}
},
"Namespace.Contract": {
"en-US": {
"Title": "ERROR",
"ErrorMessage" : "Failed to do something"
},
"fr-CA": {
"Title": "ERREUR",
"ErrorMessage" : "Échec de faire quelque chose"
}
},
"Namespace.*": {
"en-US": {
"Title": "ERROR",
"ErrorMessage" : "Failed to do something"
},
"fr-CA": {
"Title": "ERREUR",
"ErrorMessage" : "Échec de faire quelque chose"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Shiny.Mediator.Middleware;
public class OfflineAvailableRequestMiddleware<TRequest, TResult>(
IInternetService connectivity,
IStorageService storage,
IConfiguration config
IConfiguration configuration
) : IRequestMiddleware<TRequest, TResult>
{
public async Task<TResult> Process(
Expand All @@ -21,21 +21,32 @@ CancellationToken cancellationToken
if (typeof(TResult) == typeof(Unit))
return await next().ConfigureAwait(false);

var cfg = requestHandler.GetHandlerHandleMethodAttribute<TRequest, OfflineAvailableAttribute>();
cfg ??= request!.GetType().GetCustomAttribute<OfflineAvailableAttribute>();
if (cfg == null)
return await next().ConfigureAwait(false);

var acrossSessions = true;
var section = configuration.GetHandlerSection("Offline", request!, this);
if (section == null)
{
var cfg = requestHandler.GetHandlerHandleMethodAttribute<TRequest, OfflineAvailableAttribute>();
cfg ??= request!.GetType().GetCustomAttribute<OfflineAvailableAttribute>();
if (cfg == null)
return await next().ConfigureAwait(false);

acrossSessions = cfg.AvailableAcrossSessions;
}
else
{
acrossSessions = section.GetValue("AvailableAcrossSessions", acrossSessions);
}

var result = default(TResult);
if (connectivity.IsAvailable)
{
result = await next().ConfigureAwait(false);
if (result != null)
await storage.Store(request!, result, cfg.AvailableAcrossSessions);
await storage.Store(request!, result, acrossSessions);
}
else
{
result = await storage.Get<TResult>(request, cfg.AvailableAcrossSessions);
result = await storage.Get<TResult>(request!, acrossSessions);
}
return result;
}
Expand Down

0 comments on commit 3601887

Please sign in to comment.