Add CommonApi for generic HTTP object operations
Introduced the CommonApi class in the ReC.Client.Api namespace to provide generic methods for creating, updating, and deleting objects via HTTP. The class uses an injected HttpClient and supports asynchronous operations with optional cancellation tokens. All methods serialize payloads to JSON using ReCClientHelpers.ToJsonContent. XML documentation was added for clarity.
This commit is contained in:
59
src/ReC.Client/Api/CommonApi.cs
Normal file
59
src/ReC.Client/Api/CommonApi.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ReC.Client.Api
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides access to common object endpoints.
|
||||
/// </summary>
|
||||
public class CommonApi
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CommonApi"/> class.
|
||||
/// </summary>
|
||||
/// <param name="http">The HTTP client used for requests.</param>
|
||||
public CommonApi(HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The payload type.</typeparam>
|
||||
/// <param name="procedure">The payload to send.</param>
|
||||
/// <param name="cancel">A token to cancel the operation.</param>
|
||||
/// <returns>The HTTP response message.</returns>
|
||||
public Task<HttpResponseMessage> CreateObjectAsync<T>(T procedure, CancellationToken cancel = default)
|
||||
=> _http.PostAsync("api/Common", ReCClientHelpers.ToJsonContent(procedure), cancel);
|
||||
|
||||
/// <summary>
|
||||
/// Updates an object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The payload type.</typeparam>
|
||||
/// <param name="procedure">The payload to send.</param>
|
||||
/// <param name="cancel">A token to cancel the operation.</param>
|
||||
/// <returns>The HTTP response message.</returns>
|
||||
public Task<HttpResponseMessage> UpdateObjectAsync<T>(T procedure, CancellationToken cancel = default)
|
||||
=> _http.PutAsync("api/Common", ReCClientHelpers.ToJsonContent(procedure), cancel);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes objects.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The payload type containing identifiers.</typeparam>
|
||||
/// <param name="procedure">The payload to send.</param>
|
||||
/// <param name="cancel">A token to cancel the operation.</param>
|
||||
/// <returns>The HTTP response message.</returns>
|
||||
public Task<HttpResponseMessage> DeleteObjectAsync<T>(T procedure, CancellationToken cancel = default)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Delete, "api/Common")
|
||||
{
|
||||
Content = ReCClientHelpers.ToJsonContent(procedure)
|
||||
};
|
||||
return _http.SendAsync(request, cancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user