Skip to content

Commit

Permalink
Add self request
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Jun 14, 2024
1 parent b6417e2 commit 1c90154
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
7 changes: 7 additions & 0 deletions SDK.CSharp/IOpenShockApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public Task<OneOf<Success<LcgResponse>, NotFound, DeviceOffline, DeviceNotConnec
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<RootResponse> GetRoot(CancellationToken cancellationToken = default);

/// <summary>
/// Get user's information
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<OneOf<Success<SelfResponse>, UnauthenticatedError>> GetSelf(CancellationToken cancellationToken = default);

}

Expand Down
10 changes: 10 additions & 0 deletions SDK.CSharp/Models/RankType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace OpenShock.SDK.CSharp.Models;

public enum RankType
{
User = 0,
Support = 1,
Staff = 2,
Admin = 3,
System = 4
}
10 changes: 10 additions & 0 deletions SDK.CSharp/Models/SelfResponse.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
27 changes: 23 additions & 4 deletions SDK.CSharp/OpenShockApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async
await gatewayResponse.Content.ReadBaseResponseAsJsonAsync<LcgResponse>(cancellationToken,
JsonSerializerOptions));
}

if (gatewayResponse.StatusCode == HttpStatusCode.Unauthorized) return new UnauthenticatedError();

if (!gatewayResponse.IsProblem())
Expand All @@ -82,8 +82,8 @@ await gatewayResponse.Content.ReadBaseResponseAsJsonAsync<LcgResponse>(cancellat
var problem =
await gatewayResponse.Content.ReadAsJsonAsync<ProblemDetails>(cancellationToken,
JsonSerializerOptions);
return problem.Type switch

return problem.Type switch
{
"Device.NotFound" => new NotFound(),
"Device.NotOnline" => new DeviceOffline(),
Expand All @@ -96,7 +96,26 @@ await gatewayResponse.Content.ReadAsJsonAsync<ProblemDetails>(cancellationToken,
public async Task<RootResponse> GetRoot(CancellationToken cancellationToken = default)
{
using var rootResponse = await _httpClient.GetAsync(OpenShockEndpoints.V1.Root, cancellationToken);
return await rootResponse.Content.ReadBaseResponseAsJsonAsync<RootResponse>(cancellationToken, JsonSerializerOptions);
return await rootResponse.Content.ReadBaseResponseAsJsonAsync<RootResponse>(cancellationToken,
JsonSerializerOptions);
}

/// <inheritdoc />
public async Task<OneOf<Success<SelfResponse>, 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<SelfResponse>(
await selfResponse.Content.ReadBaseResponseAsJsonAsync<SelfResponse>(cancellationToken,
JsonSerializerOptions));
}

private string GetUserAgent()
Expand Down
5 changes: 5 additions & 0 deletions SDK.CSharp/OpenShockEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}
4 changes: 2 additions & 2 deletions SDK.CSharp/SDK.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<AssemblyName>OpenShock.SDK.CSharp</AssemblyName>
<RootNamespace>OpenShock.SDK.CSharp</RootNamespace>
<Company>OpenShock</Company>
<AssemblyVersion>0.0.20</AssemblyVersion>
<Version>0.0.20</Version>
<AssemblyVersion>0.0.21</AssemblyVersion>
<Version>0.0.21</Version>
<Title>OpenShock.SDK.DotNet</Title>
<Authors>OpenShock</Authors>
<Description>.NET / C# SDK for developing OpenShock applications. Used to interact with a OpenShock backend.</Description>
Expand Down

0 comments on commit 1c90154

Please sign in to comment.