refactor: allow simulator to connect to api through auth

This commit is contained in:
voltsrage
2026-06-22 20:43:21 +08:00
parent 7170b6efad
commit 518cc5b99a
5 changed files with 1228 additions and 6 deletions
@@ -0,0 +1,9 @@
public record SimLoginRequest(string Username, string Password);
public record SimLoginResponse(
string AccessToken,
DateTimeOffset ExpiresAt,
Guid UserId,
string Username,
string DisplayName,
string Role);
@@ -1,3 +1,4 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
public class VigilCareApiClient
@@ -9,6 +10,23 @@ public class VigilCareApiClient
_http = http;
}
public async Task LoginAsync(string username, string password)
{
var response = await _http.PostAsJsonAsync("/api/v1/auth/login",
new SimLoginRequest(username, password));
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();
throw new HttpRequestException(
$"Login failed ({(int)response.StatusCode} {response.StatusCode}): {body}");
}
var envelope = await response.Content.ReadFromJsonAsync<ApiResponse<SimLoginResponse>>();
var token = envelope!.Data!.AccessToken;
_http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
public async Task<PatientResponse> RegisterPatientAsync(RegisterPatientRequest req)
{
var response = await _http.PostAsJsonAsync("/api/v1/patients", req);