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"] ?? CookieContainer.GetAllCookies()["__Secure-3PAPISID"]; 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() { if (ClientState == null || !ClientState.LoggedIn) { var state = await NetworkService.GetClientStateAsync(this); if (!state.IsSuccess) { return; } ClientState = state.Value; } var accountInfo = await NetworkService.GetCurrentAccountInfoAsync(this); } public void Dispose() { _httpClient?.Dispose(); } }