Nager.PublicSuffix icon indicating copy to clipboard operation
Nager.PublicSuffix copied to clipboard

Replacement for WebTldRuleProvider

Open mayerwin opened this issue 1 year ago • 8 comments

After upgrading the package to v3, WebTldRuleProvider is no longer recognized. It seems there is no equivalent provider we can use as a replacement. CachedHttpRuleProvider requires a number of arguments. Can you re-add this provider with default suitable arguments?

mayerwin avatar Apr 30 '24 10:04 mayerwin

SimpleHttpRuleProvider?

tinohager avatar Apr 30 '24 10:04 tinohager

There is no caching with this RuleProvider :/.

mayerwin avatar Apr 30 '24 10:04 mayerwin

can you briefly explain to me where you use the library, console application, website...?

tinohager avatar Apr 30 '24 10:04 tinohager

This is with a console application, the goal is to extract the root domain from a URI, just like in the first example shown in the v2 Readme:

var domainParser = new DomainParser(new WebTldRuleProvider());

var domainInfo = domainParser.Parse("sub.test.co.uk");
//domainInfo.Domain = "test";
//domainInfo.Hostname = "sub.test.co.uk";
//domainInfo.RegistrableDomain = "test.co.uk";
//domainInfo.SubDomain = "sub";
//domainInfo.TLD = "co.uk";

mayerwin avatar Apr 30 '24 11:04 mayerwin

Required NuGet Packages

  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Console
using var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
});

var ruleProviderLogger = loggerFactory.CreateLogger<CachedHttpRuleProvider>();

IConfiguration configuration = new ConfigurationBuilder()
    .AddInMemoryCollection(new List<KeyValuePair<string, string?>>
    {
        new("Nager:PublicSuffix:DataUrl", "https://publicsuffix.org/list/public_suffix_list.dat")
    })
    .Build();

var httpClient = new HttpClient();
var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider();
var ruleProvider = new CachedHttpRuleProvider(ruleProviderLogger, configuration, cacheProvider, httpClient);

await ruleProvider.BuildAsync();

var domainParser = new DomainParser(ruleProvider);

tinohager avatar Apr 30 '24 13:04 tinohager

With the new version i have optimize this issue

var httpClient = new HttpClient();
var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider();
var ruleProvider = new CachedHttpRuleProvider(cacheProvider, httpClient);

await ruleProvider.BuildAsync();

var domainParser = new DomainParser(ruleProvider);

tinohager avatar May 10 '24 19:05 tinohager

Thanks, since it was use case prominently shown before, assuming indeed a lot of users just took advantage of the earlier one-liner, wouldn't it make sense to still expose a WebTldRuleProvider class (at least for retrocompatibility), or maybe add a SimpleCachedHttpRuleProvider?

mayerwin avatar May 10 '24 19:05 mayerwin

I will include the example in the documentation.

tinohager avatar May 10 '24 20:05 tinohager