Skip to content

Commit 825bd2d

Browse files
committed
Clean up errors and warnings.
1 parent 137bd7f commit 825bd2d

36 files changed

+359
-266
lines changed

samples/ScryfallApi.WebSample/Pages/Cards.cshtml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace ScryfallApi.WebSample.Pages
99
public class CardsModel : PageModel
1010
{
1111
private readonly ScryfallApiClient _scryfallApi;
12-
public List<SelectListItem> SetList { get; set; }
13-
public IReadOnlyCollection<Card> CardList { get; set; }
12+
public List<SelectListItem> SetList { get; set; } = [];
13+
public IReadOnlyCollection<Card> CardList { get; set; } = [];
1414

1515
public CardsModel(ScryfallApiClient scryfallApi)
1616
{
@@ -20,13 +20,13 @@ public CardsModel(ScryfallApiClient scryfallApi)
2020
public async Task<IActionResult> OnGet([FromQuery] string set)
2121
{
2222
var sets = await _scryfallApi.Sets.Get();
23-
SetList = sets.Data.OrderBy(s => s.Name).Select(s => new SelectListItem(s.Name, s.Code)).ToList();
23+
SetList = sets?.Data.OrderBy(s => s.Name).Select(s => new SelectListItem(s.Name, s.Code)).ToList() ?? [];
2424

2525
var selectedItem = SetList.FirstOrDefault(li => li.Value.Equals(set));
2626
if (selectedItem is not null)
2727
{
2828
selectedItem.Selected = true;
29-
CardList = (await _scryfallApi.Cards.Search($"e:{selectedItem.Value}", 1, SearchOptions.CardSort.Name)).Data;
29+
CardList = (await _scryfallApi.Cards.Search($"e:{selectedItem.Value}", 1, SearchOptions.CardSort.Name))?.Data ?? [];
3030
}
3131
else
3232
CardList = [];

samples/ScryfallApi.WebSample/Pages/Error.cshtml.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.AspNetCore.Mvc.RazorPages;
3-
using Microsoft.Extensions.Logging;
4-
using System;
5-
using System.Collections.Generic;
63
using System.Diagnostics;
7-
using System.Linq;
8-
using System.Threading.Tasks;
94

105
namespace ScryfallApi.WebSample.Pages
116
{
127
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
138
[IgnoreAntiforgeryToken]
149
public class ErrorModel : PageModel
1510
{
16-
public string RequestId { get; set; }
11+
public string RequestId { get; set; } = string.Empty;
1712

1813
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
1914

samples/ScryfallApi.WebSample/Pages/Index.cshtml.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Threading.Tasks;
3-
using Microsoft.AspNetCore.Mvc.RazorPages;
1+
using Microsoft.AspNetCore.Mvc.RazorPages;
42
using ScryfallApi.Client;
53
using ScryfallApi.Client.Models;
64

@@ -17,9 +15,9 @@ public IndexModel(ScryfallApiClient scryfallApi)
1715

1816
public async Task OnGet()
1917
{
20-
RandomCard = await _scryfallApi.Cards.GetRandom();
18+
RandomCard = await _scryfallApi.Cards.GetRandom() ?? new Card();
2119
}
2220

23-
public Card RandomCard { get; set; }
21+
public Card RandomCard { get; set; } = new Card();
2422
}
2523
}

samples/ScryfallApi.WebSample/Pages/Search.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
</div>
2121
@if (@Model?.Results?.Data != null && Model.Results.Data.Count > 0)
2222
{
23-
@foreach (var card in @Model.Results?.Data)
23+
@foreach (var card in @Model.Results?.Data ?? [])
2424
{
2525
<h3>Result: <a href="@card.ScryfallUri">@card.Name</a></h3>
2626
<div class="row">
2727
<div class="col-md-12">
2828
<a href="@card.ScryfallUri">
2929
@if (card.Layout.Equals("transform", StringComparison.OrdinalIgnoreCase))
3030
{
31-
foreach (var face in card.CardFaces)
31+
foreach (var face in card.CardFaces.Where(f => f is not null))
3232
{
3333
<img src="@face.ImageUris["normal"]" style="max-width: 320px;" />
3434
}

samples/ScryfallApi.WebSample/Pages/Search.cshtml.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Threading.Tasks;
31
using Microsoft.AspNetCore.Mvc;
42
using Microsoft.AspNetCore.Mvc.RazorPages;
53
using ScryfallApi.Client;
@@ -12,10 +10,10 @@ public class SearchModel : PageModel
1210
ScryfallApiClient _scryfallApi { get; }
1311

1412
[BindProperty]
15-
public ResultList<Card> Results { get; set; }
13+
public ResultList<Card>? Results { get; set; }
1614

1715
[BindProperty]
18-
public string Query { get; set; }
16+
public string Query { get; set; } = string.Empty;
1917

2018
public SearchModel(ScryfallApiClient scryfallApi)
2119
{

samples/ScryfallApi.WebSample/Pages/Sets.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
{
88
<li>@block.Key
99
<ul>
10-
@foreach (var set in block.OrderByDescending(s => s.ReleaseDate))
10+
@foreach (var set in block.Where(s => s is not null).OrderByDescending(s => s.ReleaseDate))
1111
{
12-
<li>@set.Name (@set.Code) - @set.ReleaseDate.Value.ToShortDateString()</li>
12+
<li>@set.Name (@set.Code) - @set.ReleaseDate.ToString()</li>
1313
}
1414
</ul>
1515
</li>
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
using Microsoft.AspNetCore.Mvc.RazorPages;
22
using ScryfallApi.Client;
33
using ScryfallApi.Client.Models;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Threading.Tasks;
84

95
namespace ScryfallApi.WebSample.Pages
106
{
117
public class SetsModel : PageModel
128
{
139
private readonly ScryfallApiClient _scryfallApi;
14-
public List<Set> MtgExpansions { get; set; }
10+
public List<Set> MtgExpansions { get; set; } = [];
1511

1612
public SetsModel(ScryfallApiClient scryfallApi)
1713
{
@@ -21,8 +17,7 @@ public SetsModel(ScryfallApiClient scryfallApi)
2117
public async Task OnGet()
2218
{
2319
var setsResult = await _scryfallApi.Sets.Get();
24-
MtgExpansions = new List<Set>(setsResult.Data.
25-
Where(s => s.SetType.Equals("expansion") && !s.IsDigital));
20+
MtgExpansions = [.. setsResult?.Data.Where(s => s.SetType.Equals("expansion") && !s.IsDigital) ?? []];
2621
}
2722
}
2823
}

samples/ScryfallApi.WebSample/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
*/
1818

1919
// Example of customizing settings with code
20-
builder.Services.AddScryfallApiClient(config =>
20+
builder.Services.AddScryfallApiClient(new ScryfallApiClientConfig
2121
{
22-
config.CacheDuration = TimeSpan.FromMinutes(30);
23-
config.UseSlidingCacheExpiration = true;
22+
CacheDuration = TimeSpan.FromMinutes(30),
23+
UseSlidingCacheExpiration = true
2424
});
2525

2626
var app = builder.Build();

src/ScryfallApi.Client/Apis/BaseRestService.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/ScryfallApi.Client/Apis/BulkData.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,50 @@ internal BulkData(BaseRestService restService)
1212
_restService = restService;
1313
}
1414

15-
public Task<ResultList<BulkDataItem>> Get() => _restService.GetAsync<ResultList<BulkDataItem>>("/bulk-data", false);
15+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
16+
public Task<ResultList<BulkDataItem>?> Get() => _restService.GetAsync<ResultList<BulkDataItem>>("/bulk-data", false);
1617

1718
public async Task<ICollection<BulkDataItem>> Get(DateTimeOffset updatedSince)
1819
{
1920
var bulkData = await Get();
20-
return bulkData.Data.Where(d => d.LastUpdated >= updatedSince).ToList();
21+
return bulkData?.Data.Where(d => d.LastUpdated >= updatedSince).ToList() ?? [];
2122
}
2223

23-
public async Task<BulkDataItem> Get(DateTimeOffset updatedSince, string bulkDataType) =>
24-
(await Get(updatedSince)).FirstOrDefault(d => d.Type.Equals(bulkDataType, StringComparison.OrdinalIgnoreCase));
24+
public async Task<BulkDataItem?> Get(DateTimeOffset updatedSince, string bulkDataType) =>
25+
(await Get(updatedSince))
26+
.FirstOrDefault(d => d.Type.Equals(bulkDataType, StringComparison.OrdinalIgnoreCase)
27+
);
2528

29+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
30+
31+
/// <summary>
32+
/// Lookup 'codes' of bulk data types
33+
/// </summary>
2634
public static class Types
2735
{
2836
/// <summary>
29-
/// A JSON file containing one Scryfall card object for each Oracle ID on Scryfall. The
30-
/// chosen sets for the cards are an attempt to return the most up-to-date recognizable
37+
/// A JSON file containing one Scryfall card object for each Oracle ID on Scryfall. The
38+
/// chosen sets for the cards are an attempt to return the most up-to-date recognizable
3139
/// version of the card.
3240
/// </summary>
33-
public static string OracleCards => "oracle_cards";
41+
public static string OracleCards => "oracle_cards";
3442

3543
/// <summary>
36-
/// A JSON file of Scryfall card objects that together contain all unique artworks. The
44+
/// A JSON file of Scryfall card objects that together contain all unique artworks. The
3745
/// chosen cards promote the best image scans.
3846
/// </summary>
39-
public static string UniqueArtwork => "unique_artwork";
47+
public static string UniqueArtwork => "unique_artwork";
4048

4149
/// <summary>
42-
/// A JSON file containing every card object on Scryfall in English or the printed
50+
/// A JSON file containing every card object on Scryfall in English or the printed
4351
/// language if the card is only available in one language.
4452
/// </summary>
4553
public static string DefaultCards => "default_cards";
4654

4755
/// <summary>
4856
/// A JSON file containing every card object on Scryfall in every language.
4957
/// </summary>
50-
public static string AllCards => "all_cards";
58+
public static string AllCards => "all_cards";
5159

5260
/// <summary>
5361
/// A JSON file containing all Rulings on Scryfall. Each ruling refers to cards via an `oracle_id`.

0 commit comments

Comments
 (0)