Skip to content

Commit

Permalink
add base rule provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 15, 2024
1 parent 3ec24ca commit 4092d0a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 58 deletions.
39 changes: 39 additions & 0 deletions src/Nager.PublicSuffix/RuleProviders/BaseRuleProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Nager.PublicSuffix.RuleProviders
{
/// <summary>
/// BaseRuleProvider
/// </summary>
public abstract class BaseRuleProvider : IRuleProvider
{
protected DomainDataStructure _domainDataStructure;

Check warning on line 14 in src/Nager.PublicSuffix/RuleProviders/BaseRuleProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_domainDataStructure' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 14 in src/Nager.PublicSuffix/RuleProviders/BaseRuleProvider.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BaseRuleProvider._domainDataStructure'

Check warning on line 14 in src/Nager.PublicSuffix/RuleProviders/BaseRuleProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_domainDataStructure' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 14 in src/Nager.PublicSuffix/RuleProviders/BaseRuleProvider.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BaseRuleProvider._domainDataStructure'

Check warning on line 14 in src/Nager.PublicSuffix/RuleProviders/BaseRuleProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_domainDataStructure' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 14 in src/Nager.PublicSuffix/RuleProviders/BaseRuleProvider.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BaseRuleProvider._domainDataStructure'

/// <inheritdoc/>
public abstract Task<bool> BuildAsync(
bool ignoreCache = false,
CancellationToken cancellationToken = default);

/// <summary>
/// Create a DomainDataStructure
/// </summary>
/// <param name="rules"></param>
protected void CreareDomainDataStructure(IEnumerable<TldRule> rules)
{
var domainDataStructure = new DomainDataStructure("*", new TldRule("*"));
domainDataStructure.AddRules(rules);

this._domainDataStructure = domainDataStructure;
}

/// <inheritdoc/>
public DomainDataStructure? GetDomainDataStructure()
{
return this._domainDataStructure;
}
}
}
18 changes: 3 additions & 15 deletions src/Nager.PublicSuffix/RuleProviders/CachedHttpRuleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Nager.PublicSuffix.Exceptions;
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.RuleParsers;
using Nager.PublicSuffix.RuleProviders.CacheProviders;
using System;
Expand All @@ -15,13 +13,12 @@ namespace Nager.PublicSuffix.RuleProviders
/// <summary>
/// CachedHttp RuleProvider
/// </summary>
public class CachedHttpRuleProvider : IRuleProvider
public class CachedHttpRuleProvider : BaseRuleProvider
{
private readonly string _dataFileUrl;
private readonly ILogger<CachedHttpRuleProvider> _logger;
private readonly ICacheProvider _cacheProvider;
private readonly HttpClient _httpClient;
private DomainDataStructure? _domainDataStructure;

/// <summary>
/// Returns the cache provider
Expand Down Expand Up @@ -58,7 +55,7 @@ HttpClient httpClient
}

/// <inheritdoc/>
public async Task<bool> BuildAsync(
public override async Task<bool> BuildAsync(
bool ignoreCache = false,
CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -93,20 +90,11 @@ public async Task<bool> BuildAsync(
var ruleParser = new TldRuleParser();
var rules = ruleParser.ParseRules(ruleData);

var domainDataStructure = new DomainDataStructure("*", new TldRule("*"));
domainDataStructure.AddRules(rules);

this._domainDataStructure = domainDataStructure;
base.CreareDomainDataStructure(rules);

return true;
}

/// <inheritdoc/>
public DomainDataStructure? GetDomainDataStructure()
{
return this._domainDataStructure;
}

/// <summary>
/// Load the public suffix data from the given url
/// </summary>
Expand Down
13 changes: 3 additions & 10 deletions src/Nager.PublicSuffix/RuleProviders/LocalFileRuleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ namespace Nager.PublicSuffix.RuleProviders
/// <summary>
/// LocalFile RuleProvider
/// </summary>
public class LocalFileRuleProvider : IRuleProvider
public class LocalFileRuleProvider : BaseRuleProvider
{
private readonly string _filePath;
private DomainDataStructure? _domainDataStructure;

/// <summary>
/// LocalFile RuleProvider
Expand All @@ -25,7 +24,7 @@ public LocalFileRuleProvider(string filePath)
}

/// <inheritdoc/>
public async Task<bool> BuildAsync(
public override async Task<bool> BuildAsync(
bool ignoreCache = false,
CancellationToken cancellationToken = default)
{
Expand All @@ -37,17 +36,11 @@ public async Task<bool> BuildAsync(
var domainDataStructure = new DomainDataStructure("*", new TldRule("*"));
domainDataStructure.AddRules(rules);

this._domainDataStructure = domainDataStructure;
base.CreareDomainDataStructure(rules);

return true;
}

/// <inheritdoc/>
public DomainDataStructure? GetDomainDataStructure()
{
return this._domainDataStructure;
}

private async Task<string> LoadFromFile()
{
if (!File.Exists(this._filePath))
Expand Down
18 changes: 3 additions & 15 deletions src/Nager.PublicSuffix/RuleProviders/SimpleHttpRuleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Nager.PublicSuffix.Exceptions;
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.RuleParsers;
using System;
using System.Net.Http;
Expand All @@ -15,14 +13,13 @@ namespace Nager.PublicSuffix.RuleProviders
/// <summary>
/// Simple Http RuleProvider
/// </summary>
public class SimpleHttpRuleProvider : IRuleProvider, IDisposable
public class SimpleHttpRuleProvider : BaseRuleProvider, IDisposable
{
private bool _disposed;
private readonly string _dataFileUrl;
private readonly ILogger<SimpleHttpRuleProvider> _logger;
private readonly HttpClient _httpClient;
private readonly bool _disposeHttpClient;
private DomainDataStructure? _domainDataStructure;

/// <summary>
/// Simple Http RuleProvider<br/>
Expand Down Expand Up @@ -78,7 +75,7 @@ protected virtual void Dispose(bool disposing)
}

/// <inheritdoc/>
public async Task<bool> BuildAsync(
public override async Task<bool> BuildAsync(
bool ignoreCache = false,
CancellationToken cancellationToken = default)
{
Expand All @@ -105,20 +102,11 @@ public async Task<bool> BuildAsync(
var ruleParser = new TldRuleParser();
var rules = ruleParser.ParseRules(ruleData);

var domainDataStructure = new DomainDataStructure("*", new TldRule("*"));
domainDataStructure.AddRules(rules);

this._domainDataStructure = domainDataStructure;
base.CreareDomainDataStructure(rules);

return true;
}

/// <inheritdoc/>
public DomainDataStructure? GetDomainDataStructure()
{
return this._domainDataStructure;
}

/// <summary>
/// Load the public suffix data from the given url
/// </summary>
Expand Down
24 changes: 6 additions & 18 deletions src/Nager.PublicSuffix/RuleProviders/StaticRuleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using Nager.PublicSuffix.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -9,10 +8,8 @@ namespace Nager.PublicSuffix.RuleProviders
/// <summary>
/// Static RuleProvider
/// </summary>
public class StaticRuleProvider : IRuleProvider
public class StaticRuleProvider : BaseRuleProvider
{
private readonly DomainDataStructure _domainDataStructure;

/// <summary>
/// Static RuleProvider
/// </summary>
Expand All @@ -25,27 +22,18 @@ public StaticRuleProvider(DomainDataStructure domainDataStructure)
/// <summary>
/// Static RuleProvider
/// </summary>
/// <param name="tldRules"></param>
public StaticRuleProvider(IEnumerable<TldRule> tldRules)
/// <param name="rules"></param>
public StaticRuleProvider(IEnumerable<TldRule> rules)
{
var domainDataStructure = new DomainDataStructure("*", new TldRule("*"));
domainDataStructure.AddRules(tldRules);

this._domainDataStructure = domainDataStructure;
base.CreareDomainDataStructure(rules);
}

/// <inheritdoc/>
public Task<bool> BuildAsync(
public override Task<bool> BuildAsync(
bool ignoreCache = false,
CancellationToken cancellationToken = default)
{
return Task.FromResult(true);
}

/// <inheritdoc/>
public DomainDataStructure GetDomainDataStructure()
{
return this._domainDataStructure;
}
}
}

0 comments on commit 4092d0a

Please sign in to comment.