using System;
using System.Net.Http;
namespace StellaOps.AirGap.Policy;
///
/// Provides helpers for creating instances that respect the configured .
///
public static class EgressHttpClientFactory
{
///
/// Creates an after validating the supplied egress request against the policy.
///
/// The policy used to validate outbound requests.
/// Describes the destination and intent for the outbound call.
/// Optional configuration hook applied to the newly created client.
/// An that has been pre-authorised by the policy.
public static HttpClient Create(IEgressPolicy egressPolicy, EgressRequest request, Action? configure = null)
{
ArgumentNullException.ThrowIfNull(egressPolicy);
egressPolicy.EnsureAllowed(request);
var client = new HttpClient();
configure?.Invoke(client);
return client;
}
///
/// Creates an from a caller-provided factory after validating the supplied egress request.
///
/// The policy used to validate outbound requests.
/// Describes the destination and intent for the outbound call.
/// Factory used to supply a configured client (for example, from IHttpClientFactory).
/// Optional configuration hook applied to the newly created client.
/// An that has been pre-authorised by the policy.
public static HttpClient Create(
IEgressPolicy egressPolicy,
EgressRequest request,
Func clientFactory,
Action? configure = null)
{
ArgumentNullException.ThrowIfNull(egressPolicy);
ArgumentNullException.ThrowIfNull(clientFactory);
egressPolicy.EnsureAllowed(request);
var client = clientFactory();
if (client is null)
{
throw new InvalidOperationException("EgressHttpClientFactory received a null HttpClient from the factory.");
}
configure?.Invoke(client);
return client;
}
///
/// Creates and configures an after validating the supplied egress request against the policy.
///
/// The policy used to validate outbound requests.
/// Component initiating the request.
/// Destination that will be contacted.
/// Intent label describing why the request is needed.
/// Optional configuration hook applied to the newly created client.
/// An that has been pre-authorised by the policy.
public static HttpClient Create(
IEgressPolicy egressPolicy,
string component,
Uri destination,
string intent,
Action? configure = null)
=> Create(egressPolicy, new EgressRequest(component, destination, intent), configure);
///
/// Creates a configured using a caller-provided factory after policy validation.
///
/// The policy used to validate outbound requests.
/// Component initiating the request.
/// Destination that will be contacted.
/// Intent label describing why the request is needed.
/// Factory used to supply a configured client.
/// Optional configuration hook applied to the newly created client.
/// An that has been pre-authorised by the policy.
public static HttpClient Create(
IEgressPolicy egressPolicy,
string component,
Uri destination,
string intent,
Func clientFactory,
Action? configure = null)
=> Create(egressPolicy, new EgressRequest(component, destination, intent), clientFactory, configure);
}