Skip to content

Commit de828b8

Browse files
committed
Resolve Issue With Caching API Client Configuration
1 parent 18be01e commit de828b8

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/Aydsko.iRacingData.IntegrationTests/CachingIntegrationFixture.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ internal abstract class CachingIntegrationFixture
1313
protected FakeTimeProvider FakeTimeProvider { get; private set; } = new FakeTimeProvider(DateTimeOffset.UtcNow);
1414

1515
private LegacyUsernamePasswordApiClient? _legacyApiClient;
16-
private ApiClient? _apiClientBase;
1716
private CachingApiClient? _cachingApiClientBase;
1817

1918
[SetUp]
@@ -24,8 +23,12 @@ public void SetUp()
2423
MemoryCache = new MemoryCache(new MemoryCacheOptions() { TrackStatistics = true });
2524

2625
_legacyApiClient = new(HttpClient, options, CookieContainer, new TestLogger<LegacyUsernamePasswordApiClient>());
27-
_apiClientBase = new(_legacyApiClient, options, new TestLogger<ApiClient>());
28-
_cachingApiClientBase = new(_apiClientBase, MemoryCache, new TestLogger<CachingApiClient>(), FakeTimeProvider);
26+
_cachingApiClientBase = new(_legacyApiClient,
27+
options,
28+
MemoryCache,
29+
new TestLogger<CachingApiClient>(),
30+
new TestLogger<ApiClient>(),
31+
FakeTimeProvider);
2932

3033
Client = new DataClient(_cachingApiClientBase, options, new TestLogger<DataClient>(), FakeTimeProvider);
3134
}
@@ -35,7 +38,7 @@ protected override void Dispose(bool disposing)
3538
if (disposing)
3639
{
3740
_legacyApiClient?.Dispose();
38-
_apiClientBase?.Dispose();
41+
_cachingApiClientBase?.Dispose();
3942
}
4043

4144
base.Dispose(disposing);

src/Aydsko.iRacingData/CachingApiClient.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
namespace Aydsko.iRacingData;
55

6-
internal sealed class CachingApiClient(ApiClient apiClient,
6+
internal sealed class CachingApiClient(IAuthenticatingHttpClient httpClient,
7+
iRacingDataClientOptions options,
78
IMemoryCache memoryCache,
89
ILogger<CachingApiClient> logger,
10+
ILogger<ApiClient> apiClientLogger,
911
TimeProvider timeProvider)
10-
: IApiClient
12+
: IApiClient, IDisposable
1113
{
14+
private readonly ApiClient apiClient = new(httpClient, options, apiClientLogger);
15+
private bool disposedValue;
16+
1217
public async Task<DataResponse<(THeader, TChunkData[])>> CreateResponseFromChunksAsync<THeader, TChunkData>(Uri uri,
1318
bool isViaInfoLink,
1419
JsonTypeInfo<THeader> jsonTypeInfo,
@@ -161,4 +166,34 @@ public void UseUsernameAndPassword(string username, string password, bool passwo
161166
{
162167
apiClient.UseUsernameAndPassword(username, password, passwordIsEncoded);
163168
}
169+
170+
private void Dispose(bool disposing)
171+
{
172+
if (!disposedValue)
173+
{
174+
if (disposing)
175+
{
176+
// TODO: dispose managed state (managed objects)
177+
apiClient?.Dispose();
178+
}
179+
180+
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
181+
// TODO: set large fields to null
182+
disposedValue = true;
183+
}
184+
}
185+
186+
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
187+
// ~CachingApiClient()
188+
// {
189+
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
190+
// Dispose(disposing: false);
191+
// }
192+
193+
public void Dispose()
194+
{
195+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
196+
Dispose(disposing: true);
197+
GC.SuppressFinalize(this);
198+
}
164199
}

0 commit comments

Comments
 (0)