Files
ReC/src/ReC.Client/Api/RecActionApi.cs
TekH 99bdf77ead Add BatchRecActionViewResponse and update InvokeAsync
Introduce the `BatchRecActionViewResponse` class to represent the
result of batch RecAction invocations, including total actions
processed and exceptions encountered.

Update `InvokeAsync` methods in `RecActionApi` to return a
`BatchRecActionViewResponse` instead of `void`. Modify the
implementation to deserialize API responses into this new class.

Add XML documentation for the updated return type and use
conditional compilation to handle nullable reference types
for .NET Framework and other target frameworks.
2026-08-04 14:05:50 +02:00

99 lines
5.0 KiB
C#

using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ReC.Client.Api
{
/// <summary>
/// Provides access to RecAction endpoints.
/// </summary>
public class RecActionApi : BaseCrudApi
{
/// <summary>
/// Initializes a new instance of the <see cref="RecActionApi"/> class.
/// </summary>
/// <param name="http">The HTTP client used for requests.</param>
/// <param name="logger">An optional logger used to record API call outcomes.</param>
/// <param name="options">An optional set of client options. Defaults are used when omitted.</param>
#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
{
}
/// <summary>
/// Invokes a batch of RecActions for the specified profile.
/// </summary>
/// <param name="profileId">The profile identifier.</param>
/// <param name="references">Optional reference values to pass through to all result records.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A <see cref="BatchRecActionViewResponse"/> describing the outcome of the batch invocation.</returns>
/// <exception cref="ReCApiException">Thrown when the API responds with a non-success status code.</exception>
#if NETFRAMEWORK
public async Task<BatchRecActionViewResponse> InvokeAsync(long profileId, InvokeReferences references = null, CancellationToken cancellationToken = default)
#else
public async Task<BatchRecActionViewResponse?> 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<BatchRecActionViewResponse>(body);
}
}
/// <summary>
/// Invokes a batch of RecActions for the specified profile.
/// </summary>
/// <param name="profileId">The profile identifier.</param>
/// <param name="batchId">Batch identifier.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A <see cref="BatchRecActionViewResponse"/> describing the outcome of the batch invocation.</returns>
/// <exception cref="ReCApiException">Thrown when the API responds with a non-success status code.</exception>
#if NETFRAMEWORK
public Task<BatchRecActionViewResponse> InvokeAsync(long profileId, string batchId, CancellationToken cancellationToken = default)
#else
public Task<BatchRecActionViewResponse?> InvokeAsync(long profileId, string batchId, CancellationToken cancellationToken = default)
#endif
{
return InvokeAsync(profileId, new InvokeReferences() { BatchId = batchId }, cancellationToken);
}
/// <summary>
/// Retrieves Rec actions and deserializes the JSON response into <typeparamref name="T"/>.
/// </summary>
#if NETFRAMEWORK
public async Task<T> GetAsync<T>(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
#else
public async Task<T?> GetAsync<T>(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<T>(body);
}
}
/// <summary>
/// Retrieves Rec actions and returns a dynamically deserialized payload
/// (typically a <see cref="System.Text.Json.JsonElement"/>). This is the non-generic
/// overload of <see cref="GetAsync{T}"/>.
/// </summary>
#if NETFRAMEWORK
public Task<dynamic> GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
#else
public Task<dynamic?> GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
#endif
{
return GetAsync<object>(profileId, invoked, cancel);
}
}
}