diff options
author | Emma [it/its]@Rory& <root@rory.gay> | 2024-04-05 18:58:32 +0200 |
---|---|---|
committer | Emma [it/its]@Rory& <root@rory.gay> | 2024-04-05 18:58:32 +0200 |
commit | 37b97d65c0a5262539a5de560e911048166b8bba (patch) | |
tree | f704a9c703b0ec47122a460576e151e0cb06fdc6 /LibMatrix/Homeservers/FederationClient.cs | |
parent | Fix merge conficts between machines (diff) | |
download | LibMatrix-37b97d65c0a5262539a5de560e911048166b8bba.tar.xz |
Fix homeserver resolution, rewrite homeserver initialisation, HSE work
Diffstat (limited to 'LibMatrix/Homeservers/FederationClient.cs')
-rw-r--r-- | LibMatrix/Homeservers/FederationClient.cs | 39 |
1 files changed, 8 insertions, 31 deletions
diff --git a/LibMatrix/Homeservers/FederationClient.cs b/LibMatrix/Homeservers/FederationClient.cs index 3926b29..dc0d1f6 100644 --- a/LibMatrix/Homeservers/FederationClient.cs +++ b/LibMatrix/Homeservers/FederationClient.cs @@ -1,41 +1,19 @@ using System.Text.Json.Serialization; using LibMatrix.Extensions; using LibMatrix.Services; +using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Homeservers; -public class FederationClient(string baseUrl) { - public static async Task<FederationClient?> TryCreate(string baseUrl, string? proxy = null) { - try { - return await Create(baseUrl, proxy); - } - catch (Exception e) { - Console.WriteLine($"Failed to create homeserver {baseUrl}: {e.Message}"); - return null; - } +public class FederationClient { + public FederationClient(string federationEndpoint, string? proxy = null) { + HttpClient = new MatrixHttpClient { + BaseAddress = new Uri(proxy?.TrimEnd('/') ?? federationEndpoint.TrimEnd('/')), + Timeout = TimeSpan.FromSeconds(120) + }; + if (proxy is not null) HttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", federationEndpoint); } - public static async Task<FederationClient> Create(string baseUrl, string? proxy = null) { - var homeserver = new FederationClient(baseUrl); - homeserver.WellKnownUris = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl); - if (string.IsNullOrWhiteSpace(proxy) && string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Client)) - Console.WriteLine($"Failed to resolve homeserver client URI for {baseUrl}"); - if (string.IsNullOrWhiteSpace(proxy) && string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Server)) - Console.WriteLine($"Failed to resolve homeserver server URI for {baseUrl}"); - - if (!string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Server)) - homeserver.HttpClient = new MatrixHttpClient { - BaseAddress = new Uri(proxy ?? homeserver.WellKnownUris.Server ?? throw new InvalidOperationException($"Failed to resolve homeserver server URI for {baseUrl}")), - Timeout = TimeSpan.FromSeconds(120) - }; - - if (proxy is not null) homeserver.HttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl); - - return homeserver; - } - - public string BaseUrl { get; } = baseUrl; - public MatrixHttpClient HttpClient { get; set; } = null!; public HomeserverResolverService.WellKnownUris WellKnownUris { get; set; } = null!; @@ -46,7 +24,6 @@ public class ServerVersionResponse { [JsonPropertyName("server")] public required ServerInfo Server { get; set; } - // ReSharper disable once ClassNeverInstantiated.Global public class ServerInfo { [JsonPropertyName("name")] public string Name { get; set; } |