From 3d3edeae16252a311704b390cfad6faa435a8b84 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Wed, 3 May 2023 18:40:53 +0200 Subject: Refactor --- MatrixRoomUtils.Core/Interfaces/IHomeServer.cs | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 MatrixRoomUtils.Core/Interfaces/IHomeServer.cs (limited to 'MatrixRoomUtils.Core/Interfaces') diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs new file mode 100644 index 0000000..84714f7 --- /dev/null +++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs @@ -0,0 +1,37 @@ +using System.Net.Http.Json; +using System.Text.Json; +using MatrixRoomUtils.Extensions; + +namespace MatrixRoomUtils; + +public class IHomeServer +{ + public string HomeServerDomain { get; set; } + public string FullHomeServerDomain { get; set; } + + private protected HttpClient _httpClient { get; set; } = new(); + public async Task ResolveHomeserverFromWellKnown(string homeserver) + { + Console.WriteLine($"Resolving homeserver: {homeserver}"); + if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; + if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client")) + { + Console.WriteLine($"Got successful response for client well-known..."); + var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/client"); + Console.WriteLine($"Response: {resp.ToString()}"); + var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString(); + return hs; + } + Console.WriteLine($"No client well-known..."); + if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server")) + { + var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); + var hs = resp.GetProperty("m.server").GetString(); + return hs; + } + Console.WriteLine($"No server well-known..."); + if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) return homeserver; + Console.WriteLine($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!"); + throw new InvalidDataException($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!"); + } +} \ No newline at end of file -- cgit 1.4.1