From 1c901548139d9282e37d03b064d6e1acf79d42b7 Mon Sep 17 00:00:00 2001 From: Luca Date: Fri, 14 Jun 2024 08:20:02 +0200 Subject: [PATCH] Add self request --- SDK.CSharp/IOpenShockApiClient.cs | 7 +++++++ SDK.CSharp/Models/RankType.cs | 10 ++++++++++ SDK.CSharp/Models/SelfResponse.cs | 10 ++++++++++ SDK.CSharp/OpenShockApiClient.cs | 27 +++++++++++++++++++++++---- SDK.CSharp/OpenShockEndpoints.cs | 5 +++++ SDK.CSharp/SDK.CSharp.csproj | 4 ++-- 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 SDK.CSharp/Models/RankType.cs create mode 100644 SDK.CSharp/Models/SelfResponse.cs diff --git a/SDK.CSharp/IOpenShockApiClient.cs b/SDK.CSharp/IOpenShockApiClient.cs index f8f279d..5f0608c 100644 --- a/SDK.CSharp/IOpenShockApiClient.cs +++ b/SDK.CSharp/IOpenShockApiClient.cs @@ -29,6 +29,13 @@ public Task, NotFound, DeviceOffline, DeviceNotConnec /// /// public Task GetRoot(CancellationToken cancellationToken = default); + + /// + /// Get user's information + /// + /// + /// + public Task, UnauthenticatedError>> GetSelf(CancellationToken cancellationToken = default); } diff --git a/SDK.CSharp/Models/RankType.cs b/SDK.CSharp/Models/RankType.cs new file mode 100644 index 0000000..3f7d4ef --- /dev/null +++ b/SDK.CSharp/Models/RankType.cs @@ -0,0 +1,10 @@ +namespace OpenShock.SDK.CSharp.Models; + +public enum RankType +{ + User = 0, + Support = 1, + Staff = 2, + Admin = 3, + System = 4 +} \ No newline at end of file diff --git a/SDK.CSharp/Models/SelfResponse.cs b/SDK.CSharp/Models/SelfResponse.cs new file mode 100644 index 0000000..6ec6f43 --- /dev/null +++ b/SDK.CSharp/Models/SelfResponse.cs @@ -0,0 +1,10 @@ +namespace OpenShock.SDK.CSharp.Models; + +public sealed class SelfResponse +{ + public required Guid Id { get; set; } + public required string Name { get; set; } + public required string Email { get; set; } + public required Uri Image { get; set; } + public required RankType Rank { get; set; } +} \ No newline at end of file diff --git a/SDK.CSharp/OpenShockApiClient.cs b/SDK.CSharp/OpenShockApiClient.cs index a49dfab..ce0e056 100644 --- a/SDK.CSharp/OpenShockApiClient.cs +++ b/SDK.CSharp/OpenShockApiClient.cs @@ -73,7 +73,7 @@ public async await gatewayResponse.Content.ReadBaseResponseAsJsonAsync(cancellationToken, JsonSerializerOptions)); } - + if (gatewayResponse.StatusCode == HttpStatusCode.Unauthorized) return new UnauthenticatedError(); if (!gatewayResponse.IsProblem()) @@ -82,8 +82,8 @@ await gatewayResponse.Content.ReadBaseResponseAsJsonAsync(cancellat var problem = await gatewayResponse.Content.ReadAsJsonAsync(cancellationToken, JsonSerializerOptions); - - return problem.Type switch + + return problem.Type switch { "Device.NotFound" => new NotFound(), "Device.NotOnline" => new DeviceOffline(), @@ -96,7 +96,26 @@ await gatewayResponse.Content.ReadAsJsonAsync(cancellationToken, public async Task GetRoot(CancellationToken cancellationToken = default) { using var rootResponse = await _httpClient.GetAsync(OpenShockEndpoints.V1.Root, cancellationToken); - return await rootResponse.Content.ReadBaseResponseAsJsonAsync(cancellationToken, JsonSerializerOptions); + return await rootResponse.Content.ReadBaseResponseAsJsonAsync(cancellationToken, + JsonSerializerOptions); + } + + /// + public async Task, UnauthenticatedError>> GetSelf( + CancellationToken cancellationToken = default) + { + using var selfResponse = await _httpClient.GetAsync(OpenShockEndpoints.V1.Users.Self, cancellationToken); + + if (!selfResponse.IsSuccess()) + { + if (selfResponse.StatusCode == HttpStatusCode.Unauthorized) return new UnauthenticatedError(); + + throw new OpenShockApiError("Failed to get user self", selfResponse.StatusCode); + } + + return new Success( + await selfResponse.Content.ReadBaseResponseAsJsonAsync(cancellationToken, + JsonSerializerOptions)); } private string GetUserAgent() diff --git a/SDK.CSharp/OpenShockEndpoints.cs b/SDK.CSharp/OpenShockEndpoints.cs index 4fcc517..7f42cde 100644 --- a/SDK.CSharp/OpenShockEndpoints.cs +++ b/SDK.CSharp/OpenShockEndpoints.cs @@ -17,5 +17,10 @@ public static class Devices public static string Get(Guid deviceId) => $"1/devices/{deviceId}"; public static string GetGateway(Guid deviceId) => $"1/devices/{deviceId}/lcg"; } + + public static class Users + { + public const string Self = "1/users/self"; + } } } \ No newline at end of file diff --git a/SDK.CSharp/SDK.CSharp.csproj b/SDK.CSharp/SDK.CSharp.csproj index b8e8d8b..edc0e04 100644 --- a/SDK.CSharp/SDK.CSharp.csproj +++ b/SDK.CSharp/SDK.CSharp.csproj @@ -8,8 +8,8 @@ OpenShock.SDK.CSharp OpenShock.SDK.CSharp OpenShock - 0.0.20 - 0.0.20 + 0.0.21 + 0.0.21 OpenShock.SDK.DotNet OpenShock .NET / C# SDK for developing OpenShock applications. Used to interact with a OpenShock backend.