Skip to content

Commit

Permalink
#26 Fixed NetHttpClient to initialize HttpClient in constructor. Adde…
Browse files Browse the repository at this point in the history
…d CancellationToken logic for AZLyricsProvider
  • Loading branch information
skuill committed Dec 10, 2024
1 parent f145c5f commit a754487
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
32 changes: 17 additions & 15 deletions LyricsScraperNET/Network/NetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,58 @@ namespace LyricsScraperNET.Network
internal sealed class NetHttpClient : IWebClient
{
private readonly ILogger<NetHttpClient> _logger;
private readonly HttpClient _httpClient;

public NetHttpClient()

Check warning on line 15 in LyricsScraperNET/Network/NetHttpClient.cs

View workflow job for this annotation

GitHub Actions / lyrics_scraper_net-cicd

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 15 in LyricsScraperNET/Network/NetHttpClient.cs

View workflow job for this annotation

GitHub Actions / lyrics_scraper_net-cicd

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
_httpClient = new HttpClient();
}

public NetHttpClient(ILogger<NetHttpClient> logger)
public NetHttpClient(ILogger<NetHttpClient> logger) : this()
{
_logger = logger;
}

public string Load(Uri uri, CancellationToken cancellationToken)
{
var httpClient = new HttpClient();
string htmlPageBody;

try
{
#if NETSTANDARD
htmlPageBody = httpClient.GetStringAsync(uri).GetAwaiter().GetResult();
#else
htmlPageBody = httpClient.GetStringAsync(uri, cancellationToken).GetAwaiter().GetResult();
#endif
return LoadAsync(uri, cancellationToken).GetAwaiter().GetResult();
}
catch (HttpRequestException ex)
{
_logger?.LogWarning($"HttpClient GetStringAsync throw exception for uri: {uri}. Exception: {ex}");
return string.Empty;
}

CheckResult(htmlPageBody, uri);

return htmlPageBody;
}

public async Task<string> LoadAsync(Uri uri, CancellationToken cancellationToken)
{
var httpClient = new HttpClient();
string htmlPageBody;

try
{
#if NETSTANDARD
htmlPageBody = await httpClient.GetStringAsync(uri);
htmlPageBody = await _httpClient.GetStringAsync(uri);
#else
htmlPageBody = await httpClient.GetStringAsync(uri, cancellationToken);
htmlPageBody = await _httpClient.GetStringAsync(uri, cancellationToken);
#endif
}
catch (HttpRequestException ex)
{
_logger?.LogWarning($"HttpClient GetStringAsync throw exception for uri: {uri}. Exception: {ex}");
_logger?.LogWarning($"HttpClient GetStringAsync threw exception for URI: {uri}. Exception: {ex}");
return string.Empty;
}
catch (OperationCanceledException ex)
{
_logger?.LogInformation($"Load request for URI: {uri} was canceled. Exception: {ex}");
throw;
}
catch (Exception ex)
{
_logger?.LogError($"An unexpected error occurred while loading URI: {uri}. Exception: {ex}");
return string.Empty;
}

Expand Down
17 changes: 15 additions & 2 deletions LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public AZLyricsProvider(IOptionsSnapshot<AZLyricsOptions> options)

protected override SearchResult SearchLyric(string artist, string song, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested(); // Ensure cancellation is handled early
return SearchLyric(_uriConverter.GetLyricUri(artist, song), cancellationToken);
}

Expand All @@ -71,7 +72,13 @@ protected override SearchResult SearchLyric(Uri uri, CancellationToken cancellat
_logger?.LogWarning($"AZLyrics. Please set up WebClient and Parser first");
return new SearchResult(Models.ExternalProviderType.AZLyrics);
}

cancellationToken.ThrowIfCancellationRequested();

var text = WebClient.Load(uri, cancellationToken);

cancellationToken.ThrowIfCancellationRequested();

return PostProcessLyric(uri, text);
}

Expand All @@ -81,6 +88,7 @@ protected override SearchResult SearchLyric(Uri uri, CancellationToken cancellat

protected override async Task<SearchResult> SearchLyricAsync(string artist, string song, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested(); // Ensure cancellation is handled early
return await SearchLyricAsync(_uriConverter.GetLyricUri(artist, song), cancellationToken);
}

Expand All @@ -91,7 +99,13 @@ protected override async Task<SearchResult> SearchLyricAsync(Uri uri, Cancellati
_logger?.LogWarning($"AZLyrics. Please set up WebClient and Parser first");
return new SearchResult(Models.ExternalProviderType.AZLyrics);
}

cancellationToken.ThrowIfCancellationRequested();

var text = await WebClient.LoadAsync(uri, cancellationToken);

cancellationToken.ThrowIfCancellationRequested();

return PostProcessLyric(uri, text);
}

Expand All @@ -117,10 +131,9 @@ private SearchResult PostProcessLyric(Uri uri, string text)
_logger?.LogWarning($"AZLyrics. Can't find lyrics for Uri: [{uri}]");
return new SearchResult(Models.ExternalProviderType.AZLyrics);
}

string result = Parser.Parse(text.Substring(startIndex, endIndex - startIndex));

return new SearchResult(result, Models.ExternalProviderType.AZLyrics);
}
}
}
}

0 comments on commit a754487

Please sign in to comment.