refactor: allow simulator to connect to api through auth
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user