[CHANGE] Added logger to client

This commit is contained in:
max
2025-10-23 18:15:05 +02:00
parent ed9cb7eff1
commit 9fdde5e756

View File

@@ -3,6 +3,7 @@ using System.Net.Mime;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using DotBased.Logging;
using DotBased.Monads; using DotBased.Monads;
using Manager.YouTube.Models; using Manager.YouTube.Models;
using Manager.YouTube.Models.Innertube; using Manager.YouTube.Models.Innertube;
@@ -22,12 +23,15 @@ public sealed class YouTubeClient : IDisposable
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"]; public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
public HttpClient HttpClient { get; } public HttpClient HttpClient { get; }
private YouTubeClient(CookieCollection? cookies, string userAgent) private readonly ILogger _logger;
private YouTubeClient(CookieCollection? cookies, string userAgent, ILogger logger)
{ {
if (string.IsNullOrWhiteSpace(userAgent)) if (string.IsNullOrWhiteSpace(userAgent))
{ {
throw new ArgumentNullException(nameof(userAgent)); throw new ArgumentNullException(nameof(userAgent));
} }
_logger = logger;
UserAgent = userAgent; UserAgent = userAgent;
if (cookies == null || cookies.Count == 0) if (cookies == null || cookies.Count == 0)
{ {
@@ -47,10 +51,13 @@ public sealed class YouTubeClient : IDisposable
/// </summary> /// </summary>
/// <param name="cookies">The cookies to use for making requests. Empty collection or null for anonymous requests.</param> /// <param name="cookies">The cookies to use for making requests. Empty collection or null for anonymous requests.</param>
/// <param name="userAgent">The user agent to use for the requests. Only WEB client is supported.</param> /// <param name="userAgent">The user agent to use for the requests. Only WEB client is supported.</param>
/// <param name="logger">The logger that the client is going to use, if null will create a new logger.</param>
/// <returns></returns> /// <returns></returns>
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent) public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent, ILogger? logger = null)
{ {
var client = new YouTubeClient(cookies, userAgent); logger ??= LogService.RegisterLogger<YouTubeClient>();
var client = new YouTubeClient(cookies, userAgent, logger);
var clientInitializeResult = await client.FetchClientDataAsync(); var clientInitializeResult = await client.FetchClientDataAsync();
if (!clientInitializeResult.IsSuccess) if (!clientInitializeResult.IsSuccess)
{ {
@@ -329,7 +336,7 @@ public sealed class YouTubeClient : IDisposable
var stateResult = SetClientStateFromHtml(html); var stateResult = SetClientStateFromHtml(html);
if (!stateResult.IsSuccess) if (!stateResult.IsSuccess)
{ {
//TODO: Log warning: failed to update client state! _logger.Warning("Failed to update client state.");
} }
var htmlParseResult = HtmlParser.GetVideoDataFromHtml(html); var htmlParseResult = HtmlParser.GetVideoDataFromHtml(html);