summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
blob: e1564dbbe295a67c9b8ba5c710018db19b298d84 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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 SetAlarm(AlarmDto alarm) {
        var res = await HttpClient.PutAsJsonAsync("/alarm/@me", alarm);
        res.EnsureSuccessStatusCode();
    }

    public async Task<AlarmDto?> GetAlarm(string userId = "@me") {
        var res = await HttpClient.GetAsync(
            // required due to express routing not being closest-match
            userId == "@me"
                ? $"/alarm/@me"
                : $"/user/{userId}/alarm"
        );
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<AlarmDto?>());
    }

    public async Task DeleteAlarm(string userId = "@me") {
        var res = await HttpClient.DeleteAsync(
            // required due to express routing not being closest-match
            userId == "@me"
                ? $"/alarm/@me"
                : $"/user/{userId}/alarm"
        );
        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 async Task LogOut() {
        var res = await HttpClient.PostAsync("/auth/logout", null);
        res.EnsureSuccessStatusCode();
    }

    public async Task AddBudget(string userId, BudgetHistoryEntry budget) {
        var res = await HttpClient.PatchAsJsonAsync($"/user/{userId}/budget", budget);
        res.EnsureSuccessStatusCode();
    }
    
    public async Task SpendBudget(BudgetHistoryEntry budget) {
        var res = await HttpClient.PatchAsJsonAsync($"/budget/@me", budget);
        res.EnsureSuccessStatusCode();
    }

    public async Task<BudgetWithHistory> GetBudget(string userId = "@me") {
        var res = await HttpClient.GetAsync(
            userId == "@me"
                ? $"/budget/@me"
                : $"/user/{userId}/budget"
        );
        res.EnsureSuccessStatusCode();
        return (await res.Content.ReadFromJsonAsync<BudgetWithHistory>())!;
    }
}

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; }
}

public class BudgetWithHistory {
    [JsonPropertyName("budget")]
    public double Amount { get; set; }

    [JsonPropertyName("history")]
    public List<BudgetHistoryEntry> History { get; set; } = new();
}

public class BudgetHistoryEntry {
    [JsonPropertyName("venue")]
    public string Venue { get; set; }

    [JsonPropertyName("amount")]
    public double Amount { get; set; }

    [JsonPropertyName("reason")]
    public string Reason { get; set; }
    
    [JsonPropertyName("createdAt")]
    public DateTime? CreatedAt { get; set; }
}