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() { var res = await HttpClient.GetAsync("/auth/whoami"); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync())!; } #region Alarm public async Task SetAlarm(AlarmDto alarm) { var res = await HttpClient.PutAsJsonAsync("/alarm/@me", alarm); res.EnsureSuccessStatusCode(); } public async Task 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()); } 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> GetAllAlarms() { var res = await HttpClient.GetAsync("/alarms"); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync>())!; } public async Task DeleteAccount(AuthDto auth) { var res = await HttpClient.DeleteAsJsonAsync("/auth/delete", auth); res.EnsureSuccessStatusCode(); } public async Task> GetAssignedUsers() { var res = await HttpClient.GetAsync("/monitor/assignedUsers"); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync>())!; } 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 GetAllUserIdsEnumerable() { var res = await HttpClient.GetAsync($"/admin/allUserIds"); res.EnsureSuccessStatusCode(); await foreach (var item in res.Content.ReadFromJsonAsAsyncEnumerable()) { yield return item!; } } public async Task MonitorAllUsers() { var res = await HttpClient.PostAsync("/admin/monitorAllUsers", null); res.EnsureSuccessStatusCode(); } public async Task> GetDevices() { var res = await HttpClient.GetAsync("/auth/devices"); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync>())!; } public async Task GetDevice(string deviceId) { var res = await HttpClient.GetAsync($"/auth/devices/{deviceId}"); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync())!; } 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 GetBudget(string userId = "@me") { var res = await HttpClient.GetAsync( userId == "@me" ? $"/budget/@me" : $"/user/{userId}/budget" ); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync())!; } } 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 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; } }