@@ -14,45 +14,44 @@ internal sealed class BaseRestService
1414 public BaseRestService ( HttpClient httpClient , ScryfallApiClientConfig clientConfig , IMemoryCache cache )
1515 {
1616 _httpClient = httpClient ?? throw new ArgumentNullException ( nameof ( httpClient ) ) ;
17- if ( _httpClient . BaseAddress is null )
18- _httpClient . BaseAddress = clientConfig . ScryfallApiBaseAddress ;
17+ _httpClient . BaseAddress ??= clientConfig . ScryfallApiBaseAddress ;
1918 _clientConfig = clientConfig ;
2019 _cache = cache ;
21-
22- if ( clientConfig . EnableCaching )
20+ _cacheOptions = new MemoryCacheEntryOptions
2321 {
24- _cacheOptions = new MemoryCacheEntryOptions
25- {
26- AbsoluteExpirationRelativeToNow = _clientConfig . UseSlidingCacheExpiration ? null : _clientConfig . CacheDuration ,
27- SlidingExpiration = _clientConfig . UseSlidingCacheExpiration ? _clientConfig . CacheDuration : null ,
28- } ;
29- }
22+ AbsoluteExpirationRelativeToNow = _clientConfig . UseSlidingCacheExpiration ? null : _clientConfig . CacheDuration ,
23+ SlidingExpiration = _clientConfig . UseSlidingCacheExpiration ? _clientConfig . CacheDuration : null ,
24+ } ;
3025 }
3126
32- public async Task < T > GetAsync < T > ( string resourceUrl , bool useCache = true ) where T : BaseItem
27+ public async Task < T ? > GetAsync < T > ( string resourceUrl , bool useCache = true ) where T : BaseItem
3328 {
3429 if ( string . IsNullOrWhiteSpace ( resourceUrl ) )
3530 throw new ArgumentNullException ( nameof ( resourceUrl ) ) ;
3631
37- var cacheKey = _httpClient . BaseAddress . AbsoluteUri + resourceUrl ;
32+ var baseAddress = _httpClient . BaseAddress ? . AbsoluteUri ?? ScryfallApiClientConfig . ScryfallApiAddress ;
33+ var cacheKey = baseAddress + resourceUrl ;
3834
39- if ( useCache && _cache != null && _cache . TryGetValue ( cacheKey , out T cached ) )
35+ if ( useCache && _cache . TryGetValue ( cacheKey , out T ? cached ) && cached is not null )
4036 return cached ;
4137
42- var response = await _httpClient . GetAsync ( resourceUrl ) . ConfigureAwait ( false ) ;
38+ var request = new HttpRequestMessage ( HttpMethod . Get , resourceUrl ) ;
39+ var response = await _httpClient . SendAsync ( request ) . ConfigureAwait ( false ) ;
40+ response . EnsureSuccessStatusCode ( ) ;
41+
4342 var jsonStream = await response . Content . ReadAsStreamAsync ( ) ;
4443 var obj = await JsonSerializer . DeserializeAsync < T > ( jsonStream ) ;
4544
46- if ( obj . ObjectType . Equals ( "error" , StringComparison . OrdinalIgnoreCase ) )
45+ if ( obj ? . ObjectType . Equals ( "error" , StringComparison . OrdinalIgnoreCase ) ?? false )
4746 {
4847 jsonStream . Position = 0 ;
4948 var error = await JsonSerializer . DeserializeAsync < Error > ( jsonStream ) ;
50- throw new ScryfallApiException ( error . Details )
49+ throw new ScryfallApiException ( error ? . Details ?? "An unknown response was returned from the API." )
5150 {
5251 ResponseStatusCode = response . StatusCode ,
53- RequestUri = response . RequestMessage . RequestUri ,
54- RequestMethod = response . RequestMessage . Method ,
55- ScryfallError = error
52+ RequestUri = request . RequestUri ?? new ( ScryfallApiClientConfig . ScryfallApiAddress ) ,
53+ RequestMethod = request . Method ,
54+ ScryfallError = error ?? new ( )
5655 } ;
5756 }
5857
0 commit comments