Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: STA warnings #229

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/IbanNet.CodeGen/Wikipedia/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@ namespace IbanNet.CodeGen.Wikipedia;

public static class Loader
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };

public static WikiResult GetWikiData()
{
#pragma warning disable S1075
var uri = new Uri("https://en.wikipedia.org/w/api.php?format=json&action=parse&page=International_Bank_Account_Number&section=16", UriKind.Absolute);
#pragma warning restore S1075
HttpWebRequest req = WebRequest.CreateHttp(uri);
using WebResponse response = req.GetResponse();
using var ms = new MemoryStream();
response.GetResponseStream().CopyTo(ms);
response.GetResponseStream()!.CopyTo(ms);
byte[] buffer = ms.ToArray();

var jsonOpts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
WikiResponse? wikiResponse = JsonSerializer.Deserialize<WikiResponse>(buffer, jsonOpts);
WikiResponse? wikiResponse = JsonSerializer.Deserialize<WikiResponse>(buffer, JsonSerializerOptions);
if (wikiResponse is null)
{
throw new InvalidOperationException("Unexpected response.");
}

var tableRegex = new Regex("(<table[^\\>]*?>.*<\\/table>)", RegexOptions.Singleline);
Match tableMatch = tableRegex.Match(wikiResponse.Parse.Text["*"]);

var doc = new HtmlDocument();
doc.LoadHtml($"<div>${tableMatch.Value}</div>");

IEnumerable<WikiRecord>? records = doc.DocumentNode.SelectNodes("//tr")
IEnumerable<WikiRecord> records = doc.DocumentNode.SelectNodes("//tr")
.GroupBy(e => e.ParentNode)
.Select(g => g.Where(e => e.Element("td") != null))
.Select(rows => rows.Select(r =>
Expand All @@ -38,7 +45,7 @@ public static WikiResult GetWikiData()
Pattern = cells[2].InnerText.Trim().Replace(" ", "")
};
}))
.SelectMany(_ => _);
.SelectMany(x => x);

return new WikiResult(records, wikiResponse.Parse);
}
Expand Down
8 changes: 4 additions & 4 deletions src/IbanNet.CodeGen/Wikipedia/ParseResult.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace IbanNet.CodeGen.Wikipedia;

public class ParseResult
public sealed record ParseResult
{
public int PageId { get; set; }
public int RevId { get; set; }
public Dictionary<string, string> Text { get; set; }
public int PageId { get; init; }
public int RevId { get; init; }
public Dictionary<string, string> Text { get; init; } = [];
}
4 changes: 2 additions & 2 deletions src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace IbanNet.CodeGen.Wikipedia;

public class WikiResponse
public sealed record WikiResponse
{
public ParseResult Parse { get; set; }
public ParseResult Parse { get; init; } = default!;
}
Loading