- Site detail: generate registration token with copy/paste docker-compose snippet - Docker snippet: pre-filled with token + agent hostname, ready to copy - Agents integration tests: register with valid token, heartbeat with API key, invalid token rejection (3 new tests) - TokenResult DTO for API client - IJSRuntime clipboard copy for docker-compose snippet
222 lines
8.6 KiB
C#
222 lines
8.6 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Heimdall.Web.Services;
|
|
|
|
public class HeimdallApiClient
|
|
{
|
|
private readonly IHttpClientFactory _httpFactory;
|
|
private readonly AuthState _authState;
|
|
private static readonly JsonSerializerOptions Json = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
};
|
|
|
|
public HeimdallApiClient(IHttpClientFactory httpFactory, AuthState authState)
|
|
{
|
|
_httpFactory = httpFactory;
|
|
_authState = authState;
|
|
}
|
|
|
|
private HttpClient CreateClient()
|
|
{
|
|
var client = _httpFactory.CreateClient("HeimdallApi");
|
|
if (_authState.AccessToken is not null)
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", _authState.AccessToken);
|
|
return client;
|
|
}
|
|
|
|
// ─── Auth ─────────────────────────────────────────────────────
|
|
|
|
public async Task<AuthResponse?> LoginAsync(string email, string password)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsJsonAsync("api/auth/login", new { email, password });
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
return await response.Content.ReadFromJsonAsync<AuthResponse>(Json);
|
|
}
|
|
|
|
public async Task<AuthResponse?> RegisterAsync(string email, string password, string displayName)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsJsonAsync("api/auth/register",
|
|
new { email, password, displayName });
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
return await response.Content.ReadFromJsonAsync<AuthResponse>(Json);
|
|
}
|
|
|
|
// ─── Users ────────────────────────────────────────────────────
|
|
|
|
public async Task<PagedData<UserItem>?> GetUsersAsync(int page = 1, int pageSize = 20)
|
|
{
|
|
var client = CreateClient();
|
|
return await client.GetFromJsonAsync<PagedData<UserItem>>(
|
|
$"api/users?page={page}&pageSize={pageSize}", Json);
|
|
}
|
|
|
|
public async Task<UserItem?> GetUserAsync(Guid id)
|
|
{
|
|
var client = CreateClient();
|
|
return await client.GetFromJsonAsync<UserItem>($"api/users/{id}", Json);
|
|
}
|
|
|
|
public async Task<UserItem?> CreateUserAsync(string email, string password, string displayName, IList<string> roles)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsJsonAsync("api/users",
|
|
new { email, password, displayName, roles }, Json);
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
return await response.Content.ReadFromJsonAsync<UserItem>(Json);
|
|
}
|
|
|
|
public async Task<bool> UpdateUserAsync(Guid id, string displayName, string? theme, string? language)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PutAsJsonAsync($"api/users/{id}",
|
|
new { displayName, theme, language }, Json);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> AssignRolesAsync(Guid userId, IList<string> roles)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PutAsJsonAsync($"api/users/{userId}/roles",
|
|
new { roles }, Json);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> AssignSitesAsync(Guid userId, IList<Guid> siteIds)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PutAsJsonAsync($"api/users/{userId}/sites",
|
|
new { siteIds }, Json);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<PasswordResetResult?> ResetPasswordAsync(Guid userId)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsync($"api/users/{userId}/reset-password", null);
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
return await response.Content.ReadFromJsonAsync<PasswordResetResult>(Json);
|
|
}
|
|
|
|
public async Task<bool> LockUserAsync(Guid userId)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsync($"api/users/{userId}/lock", null);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> UnlockUserAsync(Guid userId)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsync($"api/users/{userId}/unlock", null);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
// ─── Sites ────────────────────────────────────────────────────
|
|
|
|
public async Task<PagedData<SiteItem>?> GetSitesAsync(int page = 1, int pageSize = 20)
|
|
{
|
|
var client = CreateClient();
|
|
return await client.GetFromJsonAsync<PagedData<SiteItem>>(
|
|
$"api/sites?page={page}&pageSize={pageSize}", Json);
|
|
}
|
|
|
|
public async Task<SiteItem?> GetSiteAsync(Guid id)
|
|
{
|
|
var client = CreateClient();
|
|
return await client.GetFromJsonAsync<SiteItem>($"api/sites/{id}", Json);
|
|
}
|
|
|
|
public async Task<SiteWithTokenResult?> CreateSiteAsync(string name, string? address, string? timezone)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsJsonAsync("api/sites",
|
|
new { name, address, timezone }, Json);
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
return await response.Content.ReadFromJsonAsync<SiteWithTokenResult>(Json);
|
|
}
|
|
|
|
public async Task<bool> DeleteSiteAsync(Guid id)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.DeleteAsync($"api/sites/{id}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<TokenResult?> GenerateSiteTokenAsync(Guid siteId)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsync($"api/sites/{siteId}/tokens", null);
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
return await response.Content.ReadFromJsonAsync<TokenResult>(Json);
|
|
}
|
|
|
|
// ─── Cameras ────────────────────────────────────────────────────
|
|
|
|
public async Task<PagedData<CameraItem>?> GetCamerasBySiteAsync(Guid siteId, int page = 1, int pageSize = 20)
|
|
{
|
|
var client = CreateClient();
|
|
return await client.GetFromJsonAsync<PagedData<CameraItem>>(
|
|
$"api/cameras/by-site/{siteId}?page={page}&pageSize={pageSize}", Json);
|
|
}
|
|
|
|
public async Task<CameraItem?> CreateCameraAsync(Guid siteId, string name, string? rtspUrl, string? manufacturer, string? model)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.PostAsJsonAsync("api/cameras",
|
|
new { siteId, name, rtspUrl, manufacturer, model }, Json);
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
return await response.Content.ReadFromJsonAsync<CameraItem>(Json);
|
|
}
|
|
|
|
public async Task<bool> DeleteCameraAsync(Guid id)
|
|
{
|
|
var client = CreateClient();
|
|
var response = await client.DeleteAsync($"api/cameras/{id}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
// ─── Agents ───────────────────────────────────────────────────
|
|
|
|
public async Task<PagedData<AgentItem>?> GetAgentsBySiteAsync(Guid siteId, int page = 1, int pageSize = 20)
|
|
{
|
|
var client = CreateClient();
|
|
return await client.GetFromJsonAsync<PagedData<AgentItem>>(
|
|
$"api/agents/by-site/{siteId}?page={page}&pageSize={pageSize}", Json);
|
|
}
|
|
}
|
|
|
|
public class CameraItem
|
|
{
|
|
public Guid Id { get; set; }
|
|
public Guid SiteId { get; set; }
|
|
public string Name { get; set; } = "";
|
|
public string? Manufacturer { get; set; }
|
|
public string? Model { get; set; }
|
|
public string? RtspUrl { get; set; }
|
|
public string Status { get; set; } = "Unknown";
|
|
public DateTime CreatedAt { get; set; }
|
|
}
|
|
|
|
public record TokenResult(string Token, DateTime ExpiresAt);
|
|
|
|
public class AgentItem
|
|
{
|
|
public Guid Id { get; set; }
|
|
public Guid SiteId { get; set; }
|
|
public string Name { get; set; } = "";
|
|
public string Hostname { get; set; } = "";
|
|
public string Version { get; set; } = "";
|
|
public string Status { get; set; } = "Offline";
|
|
public DateTime? LastHeartbeat { get; set; }
|
|
public string? PublicIp { get; set; }
|
|
public DateTime CreatedAt { get; set; }
|
|
}
|