Skip to content

Commit 1c90154

Browse files
committed
Add self request
1 parent b6417e2 commit 1c90154

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

SDK.CSharp/IOpenShockApiClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public Task<OneOf<Success<LcgResponse>, NotFound, DeviceOffline, DeviceNotConnec
2929
/// <param name="cancellationToken"></param>
3030
/// <returns></returns>
3131
public Task<RootResponse> GetRoot(CancellationToken cancellationToken = default);
32+
33+
/// <summary>
34+
/// Get user's information
35+
/// </summary>
36+
/// <param name="cancellationToken"></param>
37+
/// <returns></returns>
38+
public Task<OneOf<Success<SelfResponse>, UnauthenticatedError>> GetSelf(CancellationToken cancellationToken = default);
3239

3340
}
3441

SDK.CSharp/Models/RankType.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace OpenShock.SDK.CSharp.Models;
2+
3+
public enum RankType
4+
{
5+
User = 0,
6+
Support = 1,
7+
Staff = 2,
8+
Admin = 3,
9+
System = 4
10+
}

SDK.CSharp/Models/SelfResponse.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace OpenShock.SDK.CSharp.Models;
2+
3+
public sealed class SelfResponse
4+
{
5+
public required Guid Id { get; set; }
6+
public required string Name { get; set; }
7+
public required string Email { get; set; }
8+
public required Uri Image { get; set; }
9+
public required RankType Rank { get; set; }
10+
}

SDK.CSharp/OpenShockApiClient.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public async
7373
await gatewayResponse.Content.ReadBaseResponseAsJsonAsync<LcgResponse>(cancellationToken,
7474
JsonSerializerOptions));
7575
}
76-
76+
7777
if (gatewayResponse.StatusCode == HttpStatusCode.Unauthorized) return new UnauthenticatedError();
7878

7979
if (!gatewayResponse.IsProblem())
@@ -82,8 +82,8 @@ await gatewayResponse.Content.ReadBaseResponseAsJsonAsync<LcgResponse>(cancellat
8282
var problem =
8383
await gatewayResponse.Content.ReadAsJsonAsync<ProblemDetails>(cancellationToken,
8484
JsonSerializerOptions);
85-
86-
return problem.Type switch
85+
86+
return problem.Type switch
8787
{
8888
"Device.NotFound" => new NotFound(),
8989
"Device.NotOnline" => new DeviceOffline(),
@@ -96,7 +96,26 @@ await gatewayResponse.Content.ReadAsJsonAsync<ProblemDetails>(cancellationToken,
9696
public async Task<RootResponse> GetRoot(CancellationToken cancellationToken = default)
9797
{
9898
using var rootResponse = await _httpClient.GetAsync(OpenShockEndpoints.V1.Root, cancellationToken);
99-
return await rootResponse.Content.ReadBaseResponseAsJsonAsync<RootResponse>(cancellationToken, JsonSerializerOptions);
99+
return await rootResponse.Content.ReadBaseResponseAsJsonAsync<RootResponse>(cancellationToken,
100+
JsonSerializerOptions);
101+
}
102+
103+
/// <inheritdoc />
104+
public async Task<OneOf<Success<SelfResponse>, UnauthenticatedError>> GetSelf(
105+
CancellationToken cancellationToken = default)
106+
{
107+
using var selfResponse = await _httpClient.GetAsync(OpenShockEndpoints.V1.Users.Self, cancellationToken);
108+
109+
if (!selfResponse.IsSuccess())
110+
{
111+
if (selfResponse.StatusCode == HttpStatusCode.Unauthorized) return new UnauthenticatedError();
112+
113+
throw new OpenShockApiError("Failed to get user self", selfResponse.StatusCode);
114+
}
115+
116+
return new Success<SelfResponse>(
117+
await selfResponse.Content.ReadBaseResponseAsJsonAsync<SelfResponse>(cancellationToken,
118+
JsonSerializerOptions));
100119
}
101120

102121
private string GetUserAgent()

SDK.CSharp/OpenShockEndpoints.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ public static class Devices
1717
public static string Get(Guid deviceId) => $"1/devices/{deviceId}";
1818
public static string GetGateway(Guid deviceId) => $"1/devices/{deviceId}/lcg";
1919
}
20+
21+
public static class Users
22+
{
23+
public const string Self = "1/users/self";
24+
}
2025
}
2126
}

SDK.CSharp/SDK.CSharp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<AssemblyName>OpenShock.SDK.CSharp</AssemblyName>
99
<RootNamespace>OpenShock.SDK.CSharp</RootNamespace>
1010
<Company>OpenShock</Company>
11-
<AssemblyVersion>0.0.20</AssemblyVersion>
12-
<Version>0.0.20</Version>
11+
<AssemblyVersion>0.0.21</AssemblyVersion>
12+
<Version>0.0.21</Version>
1313
<Title>OpenShock.SDK.DotNet</Title>
1414
<Authors>OpenShock</Authors>
1515
<Description>.NET / C# SDK for developing OpenShock applications. Used to interact with a OpenShock backend.</Description>

0 commit comments

Comments
 (0)