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;
|
using System.Net.Http.Json;
|
||||||
|
|
||||||
public class VigilCareApiClient
|
public class VigilCareApiClient
|
||||||
@@ -9,6 +10,23 @@ public class VigilCareApiClient
|
|||||||
_http = http;
|
_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)
|
public async Task<PatientResponse> RegisterPatientAsync(RegisterPatientRequest req)
|
||||||
{
|
{
|
||||||
var response = await _http.PostAsJsonAsync("/api/v1/patients", req);
|
var response = await _http.PostAsJsonAsync("/api/v1/patients", req);
|
||||||
|
|||||||
@@ -7,13 +7,16 @@ public static class ReplayAllCommand
|
|||||||
var dirArg = new Argument<DirectoryInfo>("directory", "Directory containing scenario JSON files");
|
var dirArg = new Argument<DirectoryInfo>("directory", "Directory containing scenario JSON files");
|
||||||
var speedOpt = new Option<double>("--speed", () => 60);
|
var speedOpt = new Option<double>("--speed", () => 60);
|
||||||
var baseUrlOpt = new Option<string>("--base-url", () => "http://localhost:5270");
|
var baseUrlOpt = new Option<string>("--base-url", () => "http://localhost:5270");
|
||||||
|
var usernameOpt = new Option<string>("--username", () => "physician.demo", "API login username");
|
||||||
|
var passwordOpt = new Option<string>("--password", () => "DemoPhysician1!", "API login password");
|
||||||
|
|
||||||
var command = new Command("replay-all", "Replay all scenarios in a directory")
|
var command = new Command("replay-all", "Replay all scenarios in a directory")
|
||||||
{
|
{
|
||||||
dirArg, speedOpt, baseUrlOpt
|
dirArg, speedOpt, baseUrlOpt, usernameOpt, passwordOpt
|
||||||
};
|
};
|
||||||
|
|
||||||
command.SetHandler(async (DirectoryInfo dir, double speed, string baseUrl) =>
|
command.SetHandler(async (DirectoryInfo dir, double speed, string baseUrl,
|
||||||
|
string username, string password) =>
|
||||||
{
|
{
|
||||||
var files = dir.GetFiles("*.json")
|
var files = dir.GetFiles("*.json")
|
||||||
.Where(f => f.Name != "schema.json")
|
.Where(f => f.Name != "schema.json")
|
||||||
@@ -30,6 +33,11 @@ public static class ReplayAllCommand
|
|||||||
|
|
||||||
using var http = new HttpClient { BaseAddress = new Uri(baseUrl) };
|
using var http = new HttpClient { BaseAddress = new Uri(baseUrl) };
|
||||||
var client = new VigilCareApiClient(http);
|
var client = new VigilCareApiClient(http);
|
||||||
|
|
||||||
|
SimulatorConsole.Info($"Authenticating as {username}...");
|
||||||
|
await client.LoginAsync(username, password);
|
||||||
|
SimulatorConsole.Info("Authenticated.");
|
||||||
|
|
||||||
var engine = new ReplayEngine(client, poller: null);
|
var engine = new ReplayEngine(client, poller: null);
|
||||||
var results = new List<ReplayResult>();
|
var results = new List<ReplayResult>();
|
||||||
|
|
||||||
@@ -49,7 +57,7 @@ public static class ReplayAllCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
SimulatorConsole.BatchSummary(results);
|
SimulatorConsole.BatchSummary(results);
|
||||||
}, dirArg, speedOpt, baseUrlOpt);
|
}, dirArg, speedOpt, baseUrlOpt, usernameOpt, passwordOpt);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,13 +9,16 @@ public static class ReplayCommand
|
|||||||
var baseUrlOpt = new Option<string>("--base-url", () => "http://localhost:5270");
|
var baseUrlOpt = new Option<string>("--base-url", () => "http://localhost:5270");
|
||||||
var pollOpt = new Option<bool>("--poll", () => false, "Poll alerts/scores after each cluster");
|
var pollOpt = new Option<bool>("--poll", () => false, "Poll alerts/scores after each cluster");
|
||||||
var pollIntervalOpt = new Option<int>("--poll-interval", () => 5, "Seconds between polls");
|
var pollIntervalOpt = new Option<int>("--poll-interval", () => 5, "Seconds between polls");
|
||||||
|
var usernameOpt = new Option<string>("--username", () => "physician.demo", "API login username");
|
||||||
|
var passwordOpt = new Option<string>("--password", () => "DemoPhysician1!", "API login password");
|
||||||
|
|
||||||
var command = new Command("replay", "Replay a scenario against the API")
|
var command = new Command("replay", "Replay a scenario against the API")
|
||||||
{
|
{
|
||||||
fileArg, speedOpt, baseUrlOpt, pollOpt, pollIntervalOpt
|
fileArg, speedOpt, baseUrlOpt, pollOpt, pollIntervalOpt, usernameOpt, passwordOpt
|
||||||
};
|
};
|
||||||
|
|
||||||
command.SetHandler(async (FileInfo file, double speed, string baseUrl, bool poll, int pollInterval) =>
|
command.SetHandler(async (FileInfo file, double speed, string baseUrl, bool poll, int pollInterval,
|
||||||
|
string username, string password) =>
|
||||||
{
|
{
|
||||||
var scenario = ScenarioLoader.Load(file.FullName);
|
var scenario = ScenarioLoader.Load(file.FullName);
|
||||||
var errors = ScenarioValidator.Validate(scenario);
|
var errors = ScenarioValidator.Validate(scenario);
|
||||||
@@ -28,12 +31,17 @@ public static class ReplayCommand
|
|||||||
|
|
||||||
using var http = new HttpClient { BaseAddress = new Uri(baseUrl) };
|
using var http = new HttpClient { BaseAddress = new Uri(baseUrl) };
|
||||||
var client = new VigilCareApiClient(http);
|
var client = new VigilCareApiClient(http);
|
||||||
|
|
||||||
|
SimulatorConsole.Info($"Authenticating as {username}...");
|
||||||
|
await client.LoginAsync(username, password);
|
||||||
|
SimulatorConsole.Info("Authenticated.");
|
||||||
|
|
||||||
var poller = poll ? new ApiPoller(client) : null;
|
var poller = poll ? new ApiPoller(client) : null;
|
||||||
var engine = new ReplayEngine(client, poller);
|
var engine = new ReplayEngine(client, poller);
|
||||||
var options = new ReplayOptions(speed, poll, pollInterval);
|
var options = new ReplayOptions(speed, poll, pollInterval);
|
||||||
|
|
||||||
await engine.RunAsync(scenario, options);
|
await engine.RunAsync(scenario, options);
|
||||||
}, fileArg, speedOpt, baseUrlOpt, pollOpt, pollIntervalOpt);
|
}, fileArg, speedOpt, baseUrlOpt, pollOpt, pollIntervalOpt, usernameOpt, passwordOpt);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user