diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-06 18:29:15 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-06 18:29:15 +0200 |
commit | e5591eef3850a9796cc87386128651a828b70697 (patch) | |
tree | 7e501ec749229a3d96838265289266bb6f6ce208 /LibMatrix/Homeservers | |
parent | Unit tests, small refactors (diff) | |
download | LibMatrix-bak-e5591eef3850a9796cc87386128651a828b70697.tar.xz |
Small refactors
Diffstat (limited to 'LibMatrix/Homeservers')
-rw-r--r-- | LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs | 42 | ||||
-rw-r--r-- | LibMatrix/Homeservers/RemoteHomeServer.cs | 16 |
2 files changed, 40 insertions, 18 deletions
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index f70dd39..d5b0a77 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -12,19 +12,35 @@ using LibMatrix.Services; namespace LibMatrix.Homeservers; -public class AuthenticatedHomeserverGeneric : RemoteHomeServer { - public AuthenticatedHomeserverGeneric(string baseUrl, string accessToken) : base(baseUrl) { - AccessToken = accessToken.Trim(); - SyncHelper = new SyncHelper(this); - - _httpClient.Timeout = TimeSpan.FromMinutes(15); - _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); +public class AuthenticatedHomeserverGeneric(string baseUrl, string accessToken) : RemoteHomeServer(baseUrl) { + public static async Task<T> Create<T>(string baseUrl, string accessToken) where T : AuthenticatedHomeserverGeneric { + var instance = Activator.CreateInstance(typeof(T), baseUrl, accessToken) as T + ?? throw new InvalidOperationException($"Failed to create instance of {typeof(T).Name}"); + instance._httpClient = new() { + BaseAddress = new Uri(await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl) + ?? throw new InvalidOperationException("Failed to resolve homeserver")), + Timeout = TimeSpan.FromMinutes(15), + DefaultRequestHeaders = { + Authorization = new AuthenticationHeaderValue("Bearer", accessToken) + } + }; + instance.WhoAmI = await instance._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"); + return instance; } - public virtual SyncHelper SyncHelper { get; init; } - private WhoAmIResponse? _whoAmI; + // Activator.CreateInstance(baseUrl, accessToken) { + // _httpClient = new() { + // BaseAddress = new Uri(await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl) + // ?? throw new InvalidOperationException("Failed to resolve homeserver")), + // Timeout = TimeSpan.FromMinutes(15), + // DefaultRequestHeaders = { + // Authorization = new AuthenticationHeaderValue("Bearer", accessToken) + // } + // } + // }; + - public WhoAmIResponse? WhoAmI => _whoAmI ??= _httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami").Result; + public WhoAmIResponse? WhoAmI { get; set; } public string UserId => WhoAmI.UserId; // public virtual async Task<WhoAmIResponse> WhoAmI() { @@ -33,9 +49,9 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer { // return _whoAmI; // } - public virtual string AccessToken { get; set; } + public string AccessToken { get; set; } = accessToken; - public virtual GenericRoom GetRoom(string roomId) { + public GenericRoom GetRoom(string roomId) { if (roomId is null || !roomId.StartsWith("!")) throw new ArgumentException("Room ID must start with !", nameof(roomId)); return new GenericRoom(this, roomId); } @@ -112,7 +128,7 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer { #region Account Data - public virtual async Task<T> GetAccountData<T>(string key) { + public virtual async Task<T> GetAccountDataAsync<T>(string key) { // var res = await _httpClient.GetAsync($"/_matrix/client/v3/user/{UserId}/account_data/{key}"); // if (!res.IsSuccessStatusCode) { // Console.WriteLine($"Failed to get account data: {await res.Content.ReadAsStringAsync()}"); diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index d10c837..798349a 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -1,3 +1,4 @@ +using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,13 +11,18 @@ using LibMatrix.Services; namespace LibMatrix.Homeservers; public class RemoteHomeServer(string baseUrl) { + public static async Task<RemoteHomeServer> Create(string baseUrl) => + new(baseUrl) { + _httpClient = new() { + BaseAddress = new Uri(await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl) + ?? throw new InvalidOperationException("Failed to resolve homeserver")), + Timeout = TimeSpan.FromSeconds(120) + } + }; private Dictionary<string, object> _profileCache { get; set; } = new(); - public string BaseUrl { get; } = baseUrl.Trim(); - public MatrixHttpClient _httpClient { get; set; } = new() { - BaseAddress = new Uri(new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl).Result ?? throw new InvalidOperationException("Failed to resolve homeserver")), - Timeout = TimeSpan.FromSeconds(120) - }; + public string BaseUrl { get; } = baseUrl; + public MatrixHttpClient _httpClient { get; set; } public async Task<ProfileResponseEventContent> GetProfileAsync(string mxid) { if (mxid is null) throw new ArgumentNullException(nameof(mxid)); |