@@ -14,45 +14,44 @@ internal sealed class BaseRestService
14
14
public BaseRestService ( HttpClient httpClient , ScryfallApiClientConfig clientConfig , IMemoryCache cache )
15
15
{
16
16
_httpClient = httpClient ?? throw new ArgumentNullException ( nameof ( httpClient ) ) ;
17
- if ( _httpClient . BaseAddress is null )
18
- _httpClient . BaseAddress = clientConfig . ScryfallApiBaseAddress ;
17
+ _httpClient . BaseAddress ??= clientConfig . ScryfallApiBaseAddress ;
19
18
_clientConfig = clientConfig ;
20
19
_cache = cache ;
21
-
22
- if ( clientConfig . EnableCaching )
20
+ _cacheOptions = new MemoryCacheEntryOptions
23
21
{
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
+ } ;
30
25
}
31
26
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
33
28
{
34
29
if ( string . IsNullOrWhiteSpace ( resourceUrl ) )
35
30
throw new ArgumentNullException ( nameof ( resourceUrl ) ) ;
36
31
37
- var cacheKey = _httpClient . BaseAddress . AbsoluteUri + resourceUrl ;
32
+ var baseAddress = _httpClient . BaseAddress ? . AbsoluteUri ?? ScryfallApiClientConfig . ScryfallApiAddress ;
33
+ var cacheKey = baseAddress + resourceUrl ;
38
34
39
- if ( useCache && _cache != null && _cache . TryGetValue ( cacheKey , out T cached ) )
35
+ if ( useCache && _cache . TryGetValue ( cacheKey , out T ? cached ) && cached is not null )
40
36
return cached ;
41
37
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
+
43
42
var jsonStream = await response . Content . ReadAsStreamAsync ( ) ;
44
43
var obj = await JsonSerializer . DeserializeAsync < T > ( jsonStream ) ;
45
44
46
- if ( obj . ObjectType . Equals ( "error" , StringComparison . OrdinalIgnoreCase ) )
45
+ if ( obj ? . ObjectType . Equals ( "error" , StringComparison . OrdinalIgnoreCase ) ?? false )
47
46
{
48
47
jsonStream . Position = 0 ;
49
48
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." )
51
50
{
52
51
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 ( )
56
55
} ;
57
56
}
58
57
0 commit comments