using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Yavsc.Abstract.Resources;
namespace Yavsc.Api.Client;
///
/// Thin HTTP client for the PostIt resource-usage reporting flow.
/// It follows the same pattern as the other Yavsc API clients: absolute
/// paths, JSON request/response mapping, and no application logic.
///
public sealed class ResourceUsageApiClient
{
private const string PathPrefix = Constants.ResourceUsagePath;
private readonly IYavscApiClient _api;
private readonly Func _businessBaseAddress;
public ResourceUsageApiClient(IYavscApiClient api, string businessBaseAddress)
: this(api, () => businessBaseAddress)
{
}
public ResourceUsageApiClient(IYavscApiClient api, Func businessBaseAddress)
{
_api = api ?? throw new ArgumentNullException(nameof(api));
_businessBaseAddress = businessBaseAddress ?? throw new ArgumentNullException(nameof(businessBaseAddress));
_ = ResolveBusinessBaseAddress();
}
public Task GetCurrentAsync(CancellationToken ct = default)
{
return _api.CallAsync(
HttpMethod.Get,
Absolute($"{PathPrefix}/{Constants.ResourceUsageCurrentUserRoute}"),
ct: ct);
}
public Task> GetAdminOverviewAsync(CancellationToken ct = default)
{
return _api.CallAsync>(
HttpMethod.Get,
Absolute($"{PathPrefix}/{Constants.ResourceUsageAdminRoute}"),
ct: ct);
}
private string Absolute(string relativePath) => new Uri(ResolveBusinessBaseAddress(), relativePath).ToString();
private Uri ResolveBusinessBaseAddress()
{
var raw = _businessBaseAddress();
if (string.IsNullOrWhiteSpace(raw))
throw new InvalidOperationException("Business base address is required.");
return new Uri(raw, UriKind.Absolute);
}
}