summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
blob: 9ea8073be11c1eeca286e9b9db91267d730c83fe (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System.Net.Http.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace SafeNSound.Sdk;

public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken) {
    public WrappedHttpClient HttpClient { get; } = new() {
        BaseAddress = new Uri(config.BaseUri),
        DefaultRequestHeaders = {
            Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken)
        }
    };

    public async Task<WhoAmI> WhoAmI() {
        var res = await HttpClient.GetAsync("/auth/whoami");
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<WhoAmI>())!;
    }

#region Alarm

    public async Task<AlarmDto> GetAlarm(string userId = "@me") {
        var res = await HttpClient.GetAsync($"/alarm/{userId}");
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<AlarmDto>())!;
    }

    public async Task SetAlarm(AlarmDto alarm, string userId = "@me") {
        var res = await HttpClient.PutAsJsonAsync("/alarm/@me", alarm);
        res.EnsureSuccessStatusCode();
    }

    public async Task DeleteAlarm(string userId = "@me") {
        var res = await HttpClient.DeleteAsync($"/alarm/{userId}");
        res.EnsureSuccessStatusCode();
    }

#endregion

#region Budget

#endregion

    public async Task<Dictionary<string, AlarmDto>> GetAllAlarms() {
        var res = await HttpClient.GetAsync("/alarms");
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<Dictionary<string, AlarmDto>>())!;
    }

    public async Task DeleteAccount(AuthDto auth) {
        var res = await HttpClient.DeleteAsJsonAsync("/auth/delete", auth);
        res.EnsureSuccessStatusCode();
    }

    public async Task<List<string>> GetAssignedUsers() {
        var res = await HttpClient.GetAsync("/monitor/assignedUsers");
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<List<string>>())!;
    }

    public async Task AddAssignedUser(string targetUserId) {
        var res = await HttpClient.PatchAsJsonAsync("/monitor/assignedUsers", new { userId = targetUserId });
        res.EnsureSuccessStatusCode();
    }

    public async Task RemoveAssignedUser(string targetUserId) {
        var res = await HttpClient.DeleteAsJsonAsync($"/monitor/assignedUsers", new { userId = targetUserId });
        res.EnsureSuccessStatusCode();
    }

    public async IAsyncEnumerable<string> GetAllUserIdsEnumerable() {
        var res = await HttpClient.GetAsync($"/admin/allUserIds");
        res.EnsureSuccessStatusCode();
        await foreach (var item in res.Content.ReadFromJsonAsAsyncEnumerable<string>()) {
            yield return item!;
        }
    }

    public async Task MonitorAllUsers() {
        var res = await HttpClient.PostAsync("/admin/monitorAllUsers", null);
        res.EnsureSuccessStatusCode();
    }

    public async Task<List<DeviceDto>> GetDevices() {
        var res = await HttpClient.GetAsync("/auth/devices");
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<List<DeviceDto>>())!;
    }

    public async Task<DeviceDto> GetDevice(string deviceId) {
        var res = await HttpClient.GetAsync($"/auth/devices/{deviceId}");
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<DeviceDto>())!;
    }

    public async Task DeleteDevice(string deviceId) {
        var res = await HttpClient.DeleteAsync($"/auth/devices/{deviceId}");
        res.EnsureSuccessStatusCode();
    }

    public async Task UpdateDevice(string deviceId, DeviceDto device) {
        var res = await HttpClient.PatchAsJsonAsync($"/auth/devices/{deviceId}", device);
        res.EnsureSuccessStatusCode();
    }
}

public class AlarmDto {
    [JsonPropertyName("reason")]
    public required string Reason { get; set; }

    [JsonPropertyName("createdAt")]
    public DateTime CreatedAt { get; set; }
}

public class DeviceDto {
    [JsonPropertyName("_id")]
    public string? Id { get; set; }

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

    [JsonPropertyName("createdAt")]
    public DateTime CreatedAt { get; set; }

    [JsonPropertyName("lastSeen")]
    public DateTime LastSeen { get; set; }
}