summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
diff options
context:
space:
mode:
Diffstat (limited to 'testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs')
-rw-r--r--testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs63
1 files changed, 45 insertions, 18 deletions
diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs

index 8291178..9ea8073 100644 --- a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs +++ b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
@@ -4,24 +4,20 @@ using System.Text.Json.Serialization; namespace SafeNSound.Sdk; -public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken) -{ - public WrappedHttpClient HttpClient { get; } = new() - { +public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken) { + public WrappedHttpClient HttpClient { get; } = new() { BaseAddress = new Uri(config.BaseUri), - DefaultRequestHeaders = - { + DefaultRequestHeaders = { Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken) } }; - - public async Task<WhoAmI> WhoAmI() - { + + 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") { @@ -29,25 +25,21 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken 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() { @@ -89,13 +81,48 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken 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; } } \ No newline at end of file