From a75448730066d2bc1d7d9cacedaeeedffc35e34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=9A=D0=B2?= =?UTF-8?q?=D0=B0=D1=88=D0=B8=D0=BD?= Date: Tue, 10 Dec 2024 13:01:52 +0400 Subject: [PATCH] #26 Fixed NetHttpClient to initialize HttpClient in constructor. Added CancellationToken logic for AZLyricsProvider --- LyricsScraperNET/Network/NetHttpClient.cs | 32 ++++++++++--------- .../Providers/AZLyrics/AZLyricsProvider.cs | 17 ++++++++-- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/LyricsScraperNET/Network/NetHttpClient.cs b/LyricsScraperNET/Network/NetHttpClient.cs index 299c997..b0925fc 100644 --- a/LyricsScraperNET/Network/NetHttpClient.cs +++ b/LyricsScraperNET/Network/NetHttpClient.cs @@ -10,56 +10,58 @@ namespace LyricsScraperNET.Network internal sealed class NetHttpClient : IWebClient { private readonly ILogger _logger; + private readonly HttpClient _httpClient; public NetHttpClient() { + _httpClient = new HttpClient(); } - public NetHttpClient(ILogger logger) + public NetHttpClient(ILogger 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 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; } diff --git a/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs b/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs index 44a232c..e7b5513 100644 --- a/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs +++ b/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs @@ -61,6 +61,7 @@ public AZLyricsProvider(IOptionsSnapshot 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); } @@ -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); } @@ -81,6 +88,7 @@ protected override SearchResult SearchLyric(Uri uri, CancellationToken cancellat protected override async Task SearchLyricAsync(string artist, string song, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); // Ensure cancellation is handled early return await SearchLyricAsync(_uriConverter.GetLyricUri(artist, song), cancellationToken); } @@ -91,7 +99,13 @@ protected override async Task 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); } @@ -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); } } -} +} \ No newline at end of file