Skip to content

Commit d7f9e67

Browse files
Get permissions, update permissions and get state added
1 parent 725e297 commit d7f9e67

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
using UserManagement.Contracts.Auth;
1+
using Microsoft.AspNetCore.Mvc;
2+
using UserManagement.Contracts.Auth;
23
using UserManagement.Contracts.DTO;
34

45
namespace UserManagement.Sdk.Abstractions
56
{
67
public interface IUserManagementClient
78
{
89
Task<AuthResponse> LoginAsync(LoginRequest request, CancellationToken ct = default);
10+
Task<UpdatePermissionsResponse> UpdatePermissions(UpdatePermissionsRequest operations, CancellationToken ct = default);
11+
Task<UserPermissionsDto> GetPermissions(int userId, CancellationToken ct = default);
12+
Task<object> GetState(int userId, CancellationToken ct = default);
13+
14+
915
}
1016
}

UserManagement.Sdk/UserManagementClient.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,58 @@ public async Task<AuthResponse> LoginAsync(LoginRequest request, CancellationTok
3939
var auth = JsonSerializer.Deserialize<AuthResponse>(body, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
4040
return auth;
4141
}
42+
43+
public async Task<UpdatePermissionsResponse> UpdatePermissions(UpdatePermissionsRequest operations, CancellationToken ct = default)
44+
{
45+
var resp = await _http.PostAsJsonAsync("api/users/updatepermissions", operations, ct);
46+
47+
resp.EnsureSuccessStatusCode(); // throw if failed
48+
49+
// if you expect a structured response, deserialize it
50+
var result = await resp.Content.ReadAsStringAsync(ct);
51+
if (!resp.IsSuccessStatusCode)
52+
{
53+
throw new HttpRequestException($"Failed to update permissions: {result}");
54+
}
55+
else
56+
{
57+
return new UpdatePermissionsResponse(result);
58+
}
59+
60+
}
61+
62+
public async Task<UserPermissionsDto> GetPermissions(int userId, CancellationToken ct = default)
63+
{
64+
var resp = await _http.GetAsync($"api/users/{userId}/permissions", ct);
65+
66+
resp.EnsureSuccessStatusCode();
67+
68+
return await resp.Content.ReadFromJsonAsync<UserPermissionsDto>(cancellationToken: ct);
69+
70+
}
71+
72+
public async Task<object> GetState(int userId, CancellationToken ct = default)
73+
{
74+
var resp = await _http.GetAsync($"api/users/{userId}/getstate", ct);
75+
76+
// Throws if 4xx/5xx
77+
resp.EnsureSuccessStatusCode();
78+
79+
// Read JSON
80+
var json = await resp.Content.ReadAsStringAsync(ct);
81+
82+
// Optional extra defensive check
83+
if (!resp.IsSuccessStatusCode)
84+
throw new HttpRequestException($"Failed to fetch permissions for user {userId}: {json}");
85+
86+
// Deserialize to your DTO
87+
var dto = System.Text.Json.JsonSerializer.Deserialize<object>(json);
88+
89+
// Safety check (should not happen unless server returns null JSON)
90+
if (dto == null)
91+
throw new InvalidOperationException($"Empty or invalid permissions payload for user {userId}.");
92+
93+
return dto;
94+
}
4295
}
4396
}

0 commit comments

Comments
 (0)