about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/FederationClient.cs
blob: 22653e4dd706403d297b733ee9cee84fd5ffd6d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Text.Json.Serialization;
using LibMatrix.Extensions;
using LibMatrix.Services;
using Microsoft.Extensions.Logging.Abstractions;

namespace LibMatrix.Homeservers;

public class FederationClient {
    public FederationClient(string federationEndpoint, string? proxy = null) {
        HttpClient = new MatrixHttpClient {
            BaseAddress = new Uri(proxy?.TrimEnd('/') ?? federationEndpoint.TrimEnd('/')),
            // Timeout = TimeSpan.FromSeconds(120) // TODO: Re-implement this
        };
        if (proxy is not null) HttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", federationEndpoint);
    }

    public MatrixHttpClient HttpClient { get; set; } = null!;
    public HomeserverResolverService.WellKnownUris WellKnownUris { get; set; } = null!;

    public async Task<ServerVersionResponse> GetServerVersionAsync() => await HttpClient.GetFromJsonAsync<ServerVersionResponse>("/_matrix/federation/v1/version");
}

public class ServerVersionResponse {
    [JsonPropertyName("server")]
    public required ServerInfo Server { get; set; }

    public class ServerInfo {
        [JsonPropertyName("name")]
        public string Name { get; set; }

        [JsonPropertyName("version")]
        public string Version { get; set; }
    }
}