about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/RemoteHomeServer.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-10-26 13:09:05 +0000
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-10-26 13:09:05 +0000
commitb75135d8cdb702423d693558ffaec3f025264b98 (patch)
tree1e7c0d746ac387a0d6b648d4e452169db17debc6 /LibMatrix/Homeservers/RemoteHomeServer.cs
parentMinor cleanup (diff)
downloadLibMatrix-b75135d8cdb702423d693558ffaec3f025264b98.tar.xz
split client and server http client for homeservers
Diffstat (limited to '')
-rw-r--r--LibMatrix/Homeservers/RemoteHomeServer.cs48
1 files changed, 36 insertions, 12 deletions
diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs
index a8d0326..0757f6e 100644
--- a/LibMatrix/Homeservers/RemoteHomeServer.cs
+++ b/LibMatrix/Homeservers/RemoteHomeServer.cs
@@ -9,19 +9,25 @@ 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")),
+public class RemoteHomeserver(string baseUrl) {
+    public static async Task<RemoteHomeserver> Create(string baseUrl) {
+        var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl);
+        return new RemoteHomeserver(baseUrl) {
+            ClientHttpClient = new() {
+                BaseAddress = new Uri(urls.client ?? throw new InvalidOperationException("Failed to resolve homeserver")),
+                Timeout = TimeSpan.FromSeconds(120)
+            },
+            ServerHttpClient = new() {
+                BaseAddress = new Uri(urls.server ?? throw new InvalidOperationException("Failed to resolve homeserver")),
                 Timeout = TimeSpan.FromSeconds(120)
             }
         };
+    }
 
     private Dictionary<string, object> _profileCache { get; set; } = new();
     public string BaseUrl { get; } = baseUrl;
-    public MatrixHttpClient _httpClient { get; set; }
+    public MatrixHttpClient ClientHttpClient { get; set; }
+    public MatrixHttpClient ServerHttpClient { get; set; }
 
     public async Task<UserProfileResponse> GetProfileAsync(string mxid) {
         if (mxid is null) throw new ArgumentNullException(nameof(mxid));
@@ -32,7 +38,7 @@ public class RemoteHomeServer(string baseUrl) {
 
         _profileCache[mxid] = new SemaphoreSlim(1);
 
-        var resp = await _httpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}");
+        var resp = await ClientHttpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}");
         var data = await resp.Content.ReadFromJsonAsync<UserProfileResponse>();
         if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data);
         _profileCache[mxid] = data;
@@ -41,14 +47,14 @@ public class RemoteHomeServer(string baseUrl) {
     }
 
     public async Task<ClientVersionsResponse> GetClientVersionsAsync() {
-        var resp = await _httpClient.GetAsync($"/_matrix/client/versions");
+        var resp = await ClientHttpClient.GetAsync($"/_matrix/client/versions");
         var data = await resp.Content.ReadFromJsonAsync<ClientVersionsResponse>();
         if (!resp.IsSuccessStatusCode) Console.WriteLine("ClientVersions: " + data);
         return data;
     }
 
     public async Task<AliasResult> ResolveRoomAliasAsync(string alias) {
-        var resp = await _httpClient.GetAsync($"/_matrix/client/v3/directory/room/{alias.Replace("#", "%23")}");
+        var resp = await ClientHttpClient.GetAsync($"/_matrix/client/v3/directory/room/{alias.Replace("#", "%23")}");
         var data = await resp.Content.ReadFromJsonAsync<AliasResult>();
         var text = await resp.Content.ReadAsStringAsync();
         if (!resp.IsSuccessStatusCode) Console.WriteLine("ResolveAlias: " + data.ToJson());
@@ -58,7 +64,7 @@ public class RemoteHomeServer(string baseUrl) {
 #region Authentication
 
     public async Task<LoginResponse> LoginAsync(string username, string password, string? deviceName = null) {
-        var resp = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/login", new {
+        var resp = await ClientHttpClient.PostAsJsonAsync("/_matrix/client/r0/login", new {
             type = "m.login.password",
             identifier = new {
                 type = "m.id.user",
@@ -73,7 +79,7 @@ public class RemoteHomeServer(string baseUrl) {
     }
 
     public async Task<LoginResponse> RegisterAsync(string username, string password, string? deviceName = null) {
-        var resp = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/register", new {
+        var resp = await ClientHttpClient.PostAsJsonAsync("/_matrix/client/r0/register", new {
             kind = "user",
             auth = new {
                 type = "m.login.dummy"
@@ -90,6 +96,24 @@ public class RemoteHomeServer(string baseUrl) {
     }
 
 #endregion
+
+    public async Task<ServerVersionResponse> GetServerVersionAsync() {
+        return await ServerHttpClient.GetFromJsonAsync<ServerVersionResponse>("/_matrix/federation/v1/version");
+    }
+}
+
+public class ServerVersionResponse {
+
+    [JsonPropertyName("server")]
+    public ServerInfo Server { get; set; }
+    
+    public class ServerInfo {
+        [JsonPropertyName("name")]
+        public string Name { get; set; }
+        
+        [JsonPropertyName("version")]
+        public string Version { get; set; }
+    }
 }
 
 public class AliasResult {