summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HyperBooru.cs77
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 {