Refactor BaseCrudApi to use generic type parameters
BaseCrudApi is now defined as BaseCrudApi<TCreate, TUpdate, TDelete>, with each CRUD method using its respective type parameter. This improves type safety and clarity for API payloads. Method signatures and XML documentation have been updated accordingly.
This commit is contained in:
@@ -8,7 +8,7 @@ namespace ReC.Client.Api
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides shared CRUD operations for API resources.
|
/// Provides shared CRUD operations for API resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class BaseCrudApi
|
public abstract class BaseCrudApi<TCreate, TUpdate, TDelete>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The HTTP client used to send requests.
|
/// The HTTP client used to send requests.
|
||||||
@@ -21,7 +21,7 @@ namespace ReC.Client.Api
|
|||||||
protected readonly string ResourcePath;
|
protected readonly string ResourcePath;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="BaseCrudApi"/> class.
|
/// Initializes a new instance of the <see cref="BaseCrudApi{TCreate, TUpdate, TDelete}"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="http">The HTTP client used for requests.</param>
|
/// <param name="http">The HTTP client used for requests.</param>
|
||||||
/// <param name="resourcePath">The base resource path for the API endpoint.</param>
|
/// <param name="resourcePath">The base resource path for the API endpoint.</param>
|
||||||
@@ -34,32 +34,29 @@ namespace ReC.Client.Api
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a resource.
|
/// Creates a resource.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The payload type.</typeparam>
|
|
||||||
/// <param name="payload">The payload to send.</param>
|
/// <param name="payload">The payload to send.</param>
|
||||||
/// <param name="cancel">A token to cancel the operation.</param>
|
/// <param name="cancel">A token to cancel the operation.</param>
|
||||||
/// <returns>The HTTP response message.</returns>
|
/// <returns>The HTTP response message.</returns>
|
||||||
public Task<HttpResponseMessage> CreateAsync<T>(T payload, CancellationToken cancel = default)
|
public Task<HttpResponseMessage> CreateAsync(TCreate payload, CancellationToken cancel = default)
|
||||||
=> Http.PostAsync(ResourcePath, ReCClientHelpers.ToJsonContent(payload), cancel);
|
=> Http.PostAsync(ResourcePath, ReCClientHelpers.ToJsonContent(payload), cancel);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates a resource by identifier.
|
/// Updates a resource by identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The payload type.</typeparam>
|
|
||||||
/// <param name="id">The resource identifier.</param>
|
/// <param name="id">The resource identifier.</param>
|
||||||
/// <param name="payload">The payload to send.</param>
|
/// <param name="payload">The payload to send.</param>
|
||||||
/// <param name="cancel">A token to cancel the operation.</param>
|
/// <param name="cancel">A token to cancel the operation.</param>
|
||||||
/// <returns>The HTTP response message.</returns>
|
/// <returns>The HTTP response message.</returns>
|
||||||
public Task<HttpResponseMessage> UpdateAsync<T>(long id, T payload, CancellationToken cancel = default)
|
public Task<HttpResponseMessage> UpdateAsync(long id, TUpdate payload, CancellationToken cancel = default)
|
||||||
=> Http.PutAsync($"{ResourcePath}/{id}", ReCClientHelpers.ToJsonContent(payload), cancel);
|
=> Http.PutAsync($"{ResourcePath}/{id}", ReCClientHelpers.ToJsonContent(payload), cancel);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deletes resources with identifiers supplied in the payload.
|
/// Deletes resources with identifiers supplied in the payload.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The payload type containing identifiers.</typeparam>
|
|
||||||
/// <param name="payload">The payload to send.</param>
|
/// <param name="payload">The payload to send.</param>
|
||||||
/// <param name="cancel">A token to cancel the operation.</param>
|
/// <param name="cancel">A token to cancel the operation.</param>
|
||||||
/// <returns>The HTTP response message.</returns>
|
/// <returns>The HTTP response message.</returns>
|
||||||
public Task<HttpResponseMessage> DeleteAsync<T>(T payload, CancellationToken cancel = default)
|
public Task<HttpResponseMessage> DeleteAsync(TDelete payload, CancellationToken cancel = default)
|
||||||
{
|
{
|
||||||
var request = new HttpRequestMessage(HttpMethod.Delete, ResourcePath)
|
var request = new HttpRequestMessage(HttpMethod.Delete, ResourcePath)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user