Refactor BaseCrudApi methods to improve error handling
Updated `CreateAsync`, `UpdateAsync`, and `DeleteAsync` methods to return `Task` instead of `Task<bool>`. Removed `<returns>` documentation and added `<exception>` tags to indicate that a `ReCApiException` is thrown for non-successful API responses. Replaced `resp.IsSuccessStatusCode` checks with `ReCClientHelpers.EnsureSuccessAsync` to enforce exception-based error handling. These changes align with modern asynchronous error-handling practices.
This commit is contained in:
@@ -37,13 +37,13 @@ namespace ReC.Client.Api
|
||||
/// <typeparam name="T">The payload type.</typeparam>
|
||||
/// <param name="payload">The payload to send.</param>
|
||||
/// <param name="cancel">A token to cancel the operation.</param>
|
||||
/// <returns><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
|
||||
public async Task<bool> CreateAsync<T>(T payload, CancellationToken cancel = default)
|
||||
/// <exception cref="ReCApiException">Thrown when the API responds with a non-success status code.</exception>
|
||||
public async Task CreateAsync<T>(T payload, CancellationToken cancel = default)
|
||||
{
|
||||
using (var content = ReCClientHelpers.ToJsonContent(payload))
|
||||
using (var resp = await Http.PostAsync(ResourcePath, content, cancel))
|
||||
{
|
||||
return resp.IsSuccessStatusCode;
|
||||
await ReCClientHelpers.EnsureSuccessAsync(resp, cancel).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,13 +54,13 @@ namespace ReC.Client.Api
|
||||
/// <param name="id">The resource identifier.</param>
|
||||
/// <param name="payload">The payload to send.</param>
|
||||
/// <param name="cancel">A token to cancel the operation.</param>
|
||||
/// <returns><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
|
||||
public async Task<bool> UpdateAsync<T>(long id, T payload, CancellationToken cancel = default)
|
||||
/// <exception cref="ReCApiException">Thrown when the API responds with a non-success status code.</exception>
|
||||
public async Task UpdateAsync<T>(long id, T payload, CancellationToken cancel = default)
|
||||
{
|
||||
using (var content = ReCClientHelpers.ToJsonContent(payload))
|
||||
using (var resp = await Http.PutAsync($"{ResourcePath}/{id}", content, cancel))
|
||||
{
|
||||
return resp.IsSuccessStatusCode;
|
||||
await ReCClientHelpers.EnsureSuccessAsync(resp, cancel).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,8 +70,8 @@ namespace ReC.Client.Api
|
||||
/// <typeparam name="T">The payload type containing identifiers.</typeparam>
|
||||
/// <param name="payload">The payload to send.</param>
|
||||
/// <param name="cancel">A token to cancel the operation.</param>
|
||||
/// <returns><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
|
||||
public async Task<bool> DeleteAsync<T>(T payload, CancellationToken cancel = default)
|
||||
/// <exception cref="ReCApiException">Thrown when the API responds with a non-success status code.</exception>
|
||||
public async Task DeleteAsync<T>(T payload, CancellationToken cancel = default)
|
||||
{
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Delete, ResourcePath)
|
||||
{
|
||||
@@ -79,7 +79,7 @@ namespace ReC.Client.Api
|
||||
})
|
||||
using (var resp = await Http.SendAsync(request, cancel))
|
||||
{
|
||||
return resp.IsSuccessStatusCode;
|
||||
await ReCClientHelpers.EnsureSuccessAsync(resp, cancel).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user