using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ReC.Client.Api
{
///
/// Provides access to RecAction endpoints.
///
public class RecActionApi : BaseCrudApi
{
///
/// Initializes a new instance of the class.
///
/// The HTTP client used for requests.
/// An optional logger used to record API call outcomes.
/// An optional set of client options. Defaults are used when omitted.
#if NETFRAMEWORK
public RecActionApi(HttpClient http, ILogger logger = null, ReCClientOptions options = null) : base(http, "api/RecAction", logger, options)
#else
public RecActionApi(HttpClient http, ILogger? logger = null, ReCClientOptions? options = null) : base(http, "api/RecAction", logger, options)
#endif
{
}
///
/// Invokes a batch of RecActions for the specified profile.
///
/// The profile identifier.
/// Optional reference values to pass through to all result records.
/// A token to cancel the operation.
/// A describing the outcome of the batch invocation.
/// Thrown when the API responds with a non-success status code.
#if NETFRAMEWORK
public async Task InvokeAsync(long profileId, InvokeReferences references = null, CancellationToken cancellationToken = default)
#else
public async Task InvokeAsync(long profileId, InvokeReferences? references = null, CancellationToken cancellationToken = default)
#endif
{
var content = references != null ? ReCClientHelpers.ToJsonContent(references) : null;
using (content)
using (var resp = await Http.PostAsync($"{ResourcePath}/invoke/{profileId}", content, cancellationToken))
{
var body = await ReCClientHelpers.HandleResponseAsync(resp, Logger, Options.LogSuccessfulRequests, cancellationToken).ConfigureAwait(false);
return ReCClientHelpers.Deserialize(body);
}
}
///
/// Invokes a batch of RecActions for the specified profile.
///
/// The profile identifier.
/// Batch identifier.
/// A token to cancel the operation.
/// A describing the outcome of the batch invocation.
/// Thrown when the API responds with a non-success status code.
#if NETFRAMEWORK
public Task InvokeAsync(long profileId, string batchId, CancellationToken cancellationToken = default)
#else
public Task InvokeAsync(long profileId, string batchId, CancellationToken cancellationToken = default)
#endif
{
return InvokeAsync(profileId, new InvokeReferences() { BatchId = batchId }, cancellationToken);
}
///
/// Retrieves Rec actions and deserializes the JSON response into .
///
#if NETFRAMEWORK
public async Task GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
#else
public async Task GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
#endif
{
var query = ReCClientHelpers.BuildQuery(("ProfileId", profileId), ("Invoked", invoked));
using (var resp = await Http.GetAsync($"{ResourcePath}{query}", cancel).ConfigureAwait(false))
{
var body = await ReCClientHelpers.HandleResponseAsync(resp, Logger, Options.LogSuccessfulRequests, cancel).ConfigureAwait(false);
return ReCClientHelpers.Deserialize(body);
}
}
///
/// Retrieves Rec actions and returns a dynamically deserialized payload
/// (typically a ). This is the non-generic
/// overload of .
///
#if NETFRAMEWORK
public Task GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
#else
public Task GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
#endif
{
return GetAsync