about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Responses/LoginResponse.cs
blob: 8d0d94fe699d2e19adae0817de445272d7af9aac (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
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using MatrixRoomUtils.Core.StateEventTypes;

namespace MatrixRoomUtils.Core.Responses;

public class LoginResponse {
    [JsonPropertyName("access_token")]
    public string AccessToken { get; set; }

    [JsonPropertyName("device_id")]
    public string DeviceId { get; set; }

    [JsonPropertyName("home_server")]
    public string HomeServer { get; set; }

    [JsonPropertyName("user_id")]
    public string UserId { get; set; }

    public async Task<ProfileResponse> GetProfile() {
        var hc = new HttpClient();
        var resp = await hc.GetAsync($"{HomeServer}/_matrix/client/v3/profile/{UserId}");
        var data = await resp.Content.ReadFromJsonAsync<JsonElement>();
        if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data);
        return data.Deserialize<ProfileResponse>();
    }

    public async Task<string> GetCanonicalHomeserverUrl() => (await new RemoteHomeServer(HomeServer).Configure()).FullHomeServerDomain;
}