Refactor GetAsync methods to return deserialized data

Updated GetAsync methods in ProfileApi, RecActionApi, and
ResultApi to return deserialized objects of type <T> instead
of raw HttpResponseMessage, improving usability.

Added conditional compilation (#if NETFRAMEWORK) to handle
nullable return types (T?) for non-NET Framework targets,
ensuring compatibility across .NET versions.

Replaced direct Http.GetAsync calls with using blocks for
proper disposal of HTTP responses. Introduced response
handling and deserialization via ReCClientHelpers to
streamline processing and logging.

Updated XML documentation to reflect the new behavior and
removed redundant parameters.
This commit is contained in:
2026-05-20 11:31:02 +02:00
parent 8976620205
commit b6420fcc49
3 changed files with 31 additions and 27 deletions

View File

@@ -25,16 +25,20 @@ namespace ReC.Client.Api
} }
/// <summary> /// <summary>
/// Retrieves profiles, optionally filtered by identifier. /// Retrieves profiles and deserializes the JSON response into <typeparamref name="T"/>.
/// </summary> /// </summary>
/// <param name="id">Optional profile identifier filter. When <see langword="null"/>, all profiles are returned.</param> #if NETFRAMEWORK
/// <param name="includeActions">Whether to include related actions. Defaults to <see langword="true"/> to match the API default.</param> public async Task<T> GetAsync<T>(long? id = null, bool includeActions = true, CancellationToken cancel = default)
/// <param name="cancel">A token to cancel the operation.</param> #else
/// <returns>The HTTP response message.</returns> public async Task<T?> GetAsync<T>(long? id = null, bool includeActions = true, CancellationToken cancel = default)
public Task<HttpResponseMessage> GetAsync(long? id = null, bool includeActions = true, CancellationToken cancel = default) #endif
{ {
var query = ReCClientHelpers.BuildQuery(("Id", id), ("IncludeActions", includeActions)); var query = ReCClientHelpers.BuildQuery(("Id", id), ("IncludeActions", includeActions));
return Http.GetAsync($"{ResourcePath}{query}", cancel); 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);
}
} }
} }
} }

View File

@@ -58,16 +58,20 @@ namespace ReC.Client.Api
} }
/// <summary> /// <summary>
/// Retrieves Rec actions. /// Retrieves Rec actions and deserializes the JSON response into <typeparamref name="T"/>.
/// </summary> /// </summary>
/// <param name="profileId">Optional profile filter.</param> #if NETFRAMEWORK
/// <param name="invoked">Optional invoked filter.</param> public async Task<T> GetAsync<T>(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
/// <param name="cancel">A token to cancel the operation.</param> #else
/// <returns>The HTTP response message.</returns> public async Task<T?> GetAsync<T>(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
public Task<HttpResponseMessage> GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default) #endif
{ {
var query = ReCClientHelpers.BuildQuery(("ProfileId", profileId), ("Invoked", invoked)); var query = ReCClientHelpers.BuildQuery(("ProfileId", profileId), ("Invoked", invoked));
return Http.GetAsync($"{ResourcePath}{query}", cancel); 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);
}
} }
} }
} }

View File

@@ -25,21 +25,12 @@ namespace ReC.Client.Api
} }
/// <summary> /// <summary>
/// Retrieves results with optional filters. /// Retrieves results with optional filters and deserializes the JSON response into <typeparamref name="T"/>.
/// </summary> /// </summary>
/// <param name="id">Optional result identifier.</param>
/// <param name="actionId">Optional action identifier.</param>
/// <param name="profileId">Optional profile identifier.</param>
/// <param name="batchId">Optional batch identifier.</param>
/// <param name="includeAction">Whether to include the related action. Defaults to <see langword="true"/> to match the API default.</param>
/// <param name="includeProfile">Whether to include the related profile. Defaults to <see langword="false"/> to match the API default.</param>
/// <param name="lastBatch">When <see langword="true"/>, restricts results to those belonging to the most recent batch matching the other filters.</param>
/// <param name="cancel">A token to cancel the operation.</param>
/// <returns>The HTTP response message.</returns>
#if NETFRAMEWORK #if NETFRAMEWORK
public Task<HttpResponseMessage> GetAsync(long? id = null, long? actionId = null, long? profileId = null, string batchId = null, bool includeAction = true, bool includeProfile = false, bool lastBatch = false, CancellationToken cancel = default) public async Task<T> GetAsync<T>(long? id = null, long? actionId = null, long? profileId = null, string batchId = null, bool includeAction = true, bool includeProfile = false, bool lastBatch = false, CancellationToken cancel = default)
#else #else
public Task<HttpResponseMessage> GetAsync(long? id = null, long? actionId = null, long? profileId = null, string? batchId = null, bool includeAction = true, bool includeProfile = false, bool lastBatch = false, CancellationToken cancel = default) public async Task<T?> GetAsync<T>(long? id = null, long? actionId = null, long? profileId = null, string? batchId = null, bool includeAction = true, bool includeProfile = false, bool lastBatch = false, CancellationToken cancel = default)
#endif #endif
{ {
var query = ReCClientHelpers.BuildQuery( var query = ReCClientHelpers.BuildQuery(
@@ -50,7 +41,12 @@ namespace ReC.Client.Api
("IncludeAction", includeAction), ("IncludeAction", includeAction),
("IncludeProfile", includeProfile), ("IncludeProfile", includeProfile),
("LastBatch", lastBatch)); ("LastBatch", lastBatch));
return Http.GetAsync($"{ResourcePath}{query}", cancel);
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);
}
} }
} }
} }