Refactor API classes to use BaseCrudApi for CRUD ops
Introduce BaseCrudApi to encapsulate common CRUD logic for API resource classes. Refactor CommonApi, EndpointAuthApi, EndpointParamsApi, EndpointsApi, ProfileApi, RecActionApi, and ResultApi to inherit from BaseCrudApi, removing duplicated CRUD methods and constructors. This centralizes CRUD operations, reduces code duplication, and improves maintainability.
This commit is contained in:
@@ -7,17 +7,14 @@ namespace ReC.Client.Api
|
||||
/// <summary>
|
||||
/// Provides access to RecAction endpoints.
|
||||
/// </summary>
|
||||
public class RecActionApi
|
||||
public class RecActionApi : BaseCrudApi
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RecActionApi"/> class.
|
||||
/// </summary>
|
||||
/// <param name="http">The HTTP client used for requests.</param>
|
||||
public RecActionApi(HttpClient http)
|
||||
public RecActionApi(HttpClient http) : base(http, "api/RecAction")
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -28,7 +25,7 @@ namespace ReC.Client.Api
|
||||
/// <returns><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
|
||||
public async Task<bool> InvokeAsync(int profileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var resp = await _http.PostAsync($"api/RecAction/invoke/{profileId}", content: null, cancellationToken);
|
||||
var resp = await Http.PostAsync($"{ResourcePath}/invoke/{profileId}", content: null, cancellationToken);
|
||||
return resp.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
@@ -42,44 +39,7 @@ namespace ReC.Client.Api
|
||||
public Task<HttpResponseMessage> GetAsync(long? profileId = null, bool? invoked = null, CancellationToken cancel = default)
|
||||
{
|
||||
var query = ReCClientHelpers.BuildQuery(("ProfileId", profileId), ("Invoked", invoked));
|
||||
return _http.GetAsync($"api/RecAction{query}", cancel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Rec action.
|
||||
/// </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> CreateAsync<T>(T procedure, CancellationToken cancel = default)
|
||||
=> _http.PostAsync("api/RecAction", ReCClientHelpers.ToJsonContent(procedure), cancel);
|
||||
|
||||
/// <summary>
|
||||
/// Updates a Rec action.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The payload type.</typeparam>
|
||||
/// <param name="id">The action identifier.</param>
|
||||
/// <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> UpdateAsync<T>(long id, T procedure, CancellationToken cancel = default)
|
||||
=> _http.PutAsync($"api/RecAction/{id}", ReCClientHelpers.ToJsonContent(procedure), cancel);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes Rec actions.
|
||||
/// </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> DeleteAsync<T>(T procedure, CancellationToken cancel = default)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Delete, "api/RecAction")
|
||||
{
|
||||
Content = ReCClientHelpers.ToJsonContent(procedure)
|
||||
};
|
||||
return _http.SendAsync(request, cancel);
|
||||
return Http.GetAsync($"{ResourcePath}{query}", cancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user