diff options
| author | Jake Mannens <jake@asger.xyz> | 2026-05-22 12:29:05 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2026-06-13 03:40:09 +1000 |
| commit | e35cc9de62b051a02abc8583d0f2dcb067d250d6 (patch) | |
| tree | 76d562bad432d1313f49e6817dfcea97964b3c0f /HyperBooru.cs | |
| parent | a1df04a7ba2f0c810c67081642c1afb841d4f6c4 (diff) | |
Initial commitwasm
Diffstat (limited to 'HyperBooru.cs')
| -rw-r--r-- | HyperBooru.cs | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/HyperBooru.cs b/HyperBooru.cs index 4ee1256..f4d7567 100644 --- a/HyperBooru.cs +++ b/HyperBooru.cs @@ -4,37 +4,48 @@ using System.Net.Http.Json; namespace HyperBooru.ApiClient; -public sealed class HBSession : IDisposable { +public class HBSession : IDisposable { public Media Media { get; private init; } public Tag Tag { get; private init; } public Feed Feed { get; private init; } public User User { get; private init; } public Statistics Statistics { get; private init; } - public Uri BaseUri { get; private init; } + + public bool HasNsfwClaim => false; + + public Uri BaseUri { + get => HttpClient.BaseAddress!; + set => HttpClient.BaseAddress = new($"{value.Scheme}://{value.Host}:{value.Port}/"); + } + + public bool SkipCertificateCheck { + get => httpClientHandler.ClientCertificateOptions == ClientCertificateOption.Manual; + set { + if(value) { + httpClientHandler.ClientCertificateOptions = ClientCertificateOption.Manual; + httpClientHandler.ServerCertificateCustomValidationCallback = + (request, cert, certChain, policyErrors) => true; + } else { + httpClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; + httpClientHandler.ServerCertificateCustomValidationCallback = null; + } + } + } internal HttpClient HttpClient { get; private init; } - public HBSession ( - Uri baseUri, - bool skipCertificateCheck) { + private HttpClientHandler httpClientHandler; - BaseUri = new($"{baseUri.Scheme}://{baseUri.Host}:{baseUri.Port}/"); + private bool toDispose = true; - var handler = new HttpClientHandler() { + public HBSession() { + httpClientHandler = new() { AllowAutoRedirect = true, UseCookies = true, CookieContainer = new CookieContainer() }; - if(skipCertificateCheck) { - handler.ClientCertificateOptions = ClientCertificateOption.Manual; - handler.ServerCertificateCustomValidationCallback = - (request, cert, certChain, policyErrors) => true; - } - - HttpClient = new(new ErrorHandler(handler)) { - BaseAddress = BaseUri - }; + HttpClient = new(new ErrorHandler(httpClientHandler)); Media = new(this); Tag = new(this); @@ -43,27 +54,27 @@ public sealed class HBSession : IDisposable { Statistics = new(this); } - public async Task LoginAsync(string username, string password) { - var formData = new FormUrlEncodedContent(new[] { - new KeyValuePair<string, string>("username", username), - new KeyValuePair<string, string>("password", password) - }); + public HBSession(HttpClient httpClient) { + HttpClient = httpClient; + toDispose = false; - var resp = await HttpClient.PostAsync($"/Login", formData); - - if(resp.StatusCode == HttpStatusCode.Forbidden) { - throw new Exception("Invalid username or password"); - } else if(!resp.IsSuccessStatusCode) { - var content = await resp.Content.ReadAsStringAsync(); - throw new Exception($"Unknown error while logging in: {content}"); - } + Media = new(this); + Tag = new(this); + Feed = new(this); + User = new(this); + Statistics = new(this); } - public async Task LogoutAsync() => - await HttpClient.PostAsync("/Logout", null); + public Task LoginAsync(string username, string password) => + Task.CompletedTask; - public void Dispose() => - HttpClient.Dispose(); + public Task LogoutAsync() => + Task.CompletedTask; + + public void Dispose() { + if(toDispose) + HttpClient.Dispose(); + } } public class ErrorHandler : DelegatingHandler { |
