using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
using ReC.Client.Api;
namespace ReC.Client
{
///
/// A client for interacting with the ReC API.
///
public class ReCClient
{
private readonly HttpClient _http;
///
/// A unique name for the HttpClient used by the ReCClient.
///
public static readonly string ClientName = Guid.NewGuid().ToString();
///
/// Provides access to RecAction endpoints.
///
public RecActionApi RecActions { get; }
///
/// Provides access to Result endpoints.
///
public ResultApi Results { get; }
///
/// Provides access to Profile endpoints.
///
public ProfileApi Profiles { get; }
///
/// Provides access to EndpointAuth endpoints.
///
public EndpointAuthApi EndpointAuth { get; }
///
/// Provides access to EndpointParams endpoints.
///
public EndpointParamsApi EndpointParams { get; }
///
/// Provides access to Endpoints endpoints.
///
public EndpointsApi Endpoints { get; }
///
/// Provides access to Common endpoints.
///
public CommonApi Common { get; }
///
/// Initializes a new instance of the class.
///
/// The factory to create HttpClients.
/// An optional set of client options. Defaults are used when omitted.
/// An optional logger used to record API call outcomes.
#if NETFRAMEWORK
public ReCClient(IHttpClientFactory httpClientFactory, IOptions options = null, ILogger logger = null)
#else
public ReCClient(IHttpClientFactory httpClientFactory, IOptions? options = null, ILogger? logger = null)
#endif
{
_http = httpClientFactory.CreateClient(ClientName);
var opts = options?.Value ?? new ReCClientOptions();
if (opts.LogSuccessfulRequests && logger == null)
throw new InvalidOperationException(
$"{nameof(ReCClientOptions.LogSuccessfulRequests)} is enabled, but no {nameof(ILogger)} was injected into {nameof(ReCClient)}. " +
$"Register a logging provider (e.g. services.AddLogging()) so that an {nameof(ILogger)} can be resolved, " +
$"or set {nameof(ReCClientOptions.LogSuccessfulRequests)} to false.");
RecActions = new RecActionApi(_http, logger, opts);
Results = new ResultApi(_http, logger, opts);
Profiles = new ProfileApi(_http, logger, opts);
EndpointAuth = new EndpointAuthApi(_http, logger, opts);
EndpointParams = new EndpointParamsApi(_http, logger, opts);
Endpoints = new EndpointsApi(_http, logger, opts);
Common = new CommonApi(_http, logger, opts);
}
#region Static
private static readonly IServiceCollection Services = new ServiceCollection();
#if NET8_0_OR_GREATER
private static IServiceProvider? Provider = null;
#else
private static IServiceProvider Provider = null;
#endif
///
/// Configures and builds the static for creating instances.
///
///
/// This method should only be called once during application startup.
///
/// The base URI of the ReC API.
/// An optional callback to configure .
/// Thrown if the static provider has already been built.
[Obsolete("Use a local service collection instead of the static provider.")]
#if NETFRAMEWORK
public static void BuildStaticClient(string apiUri, Action configureOptions = null)
#else
public static void BuildStaticClient(string apiUri, Action? configureOptions = null)
#endif
{
if(Provider != null)
throw new InvalidOperationException("Static Provider is already built.");
Services.AddRecClient(apiUri, configureOptions);
Provider = Services.BuildServiceProvider();
}
///
/// Configures and builds the static for creating instances.
///
///
/// This method should only be called once during application startup.
///
/// An action to configure the .
/// An optional callback to configure .
/// Thrown if the static provider has already been built.
[Obsolete("Use a local service collection instead of the static provider.")]
#if NETFRAMEWORK
public static void BuildStaticClient(Action configureClient, Action configureOptions = null)
#else
public static void BuildStaticClient(Action configureClient, Action? configureOptions = null)
#endif
{
if (Provider != null)
throw new InvalidOperationException("Static Provider is already built.");
Services.AddRecClient(configureClient, configureOptions);
Provider = Services.BuildServiceProvider();
}
///
/// Creates a new instance using the statically configured provider.
///
/// A new instance of the .
/// Thrown if has not been called yet.
[Obsolete("Use a local service collection instead of the static provider.")]
public static ReCClient Create()
{
if (Provider == null)
throw new InvalidOperationException("Static Provider is not built. Call BuildStaticClient first.");
return Provider.GetRequiredService();
}
#endregion
}
///
/// Specifies which part of the result to return for result view endpoints.
///
public enum ResultType
{
///
/// Returns both header and body.
///
Full,
///
/// Returns only the header portion of the result.
///
OnlyHeader,
///
/// Returns only the body portion of the result.
///
OnlyBody
}
}