using System.Net; using Manager.YouTube.Models.Innertube; namespace Manager.YouTube; public sealed class YouTubeClient : IDisposable { public string Id { get; private set; } = ""; public string AccountName => ClientState?.UserAccountName ?? ""; public string? UserAgent { get; set; } public CookieContainer CookieContainer { get; } = new(); public ClientState? ClientState { get; private set; } public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"]; public HttpClient? GetHttpClient() => _httpClient; private HttpClient? _httpClient; public YouTubeClient() { SetupClient(); } private void SetupClient() { _httpClient?.Dispose(); var clientHandler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, UseCookies = true, CookieContainer = CookieContainer }; _httpClient = new HttpClient(clientHandler); } public async Task GetStateAsync() { var state = await NetworkService.GetClientStateAsync(this); if (!state.IsSuccess) { return; } ClientState = state.Value; } public void Dispose() { _httpClient?.Dispose(); } }