Refactor API methods to return bool for success status
Updated `CreateAsync<T>`, `UpdateAsync<T>`, and `DeleteAsync<T>` methods to return a `bool` indicating success instead of `HttpResponseMessage`. Added `using` statements to ensure proper disposal of HTTP content and response objects. Simplified the interface for better usability by leveraging `IsSuccessStatusCode` to determine operation success.
This commit is contained in:
@@ -37,9 +37,15 @@ 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>The HTTP response message.</returns>
|
||||
public Task<HttpResponseMessage> CreateAsync<T>(T payload, CancellationToken cancel = default)
|
||||
=> Http.PostAsync(ResourcePath, ReCClientHelpers.ToJsonContent(payload), cancel);
|
||||
/// <returns><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
|
||||
public async Task<bool> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a resource by identifier.
|
||||
@@ -48,9 +54,15 @@ 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>The HTTP response message.</returns>
|
||||
public Task<HttpResponseMessage> UpdateAsync<T>(long id, T payload, CancellationToken cancel = default)
|
||||
=> Http.PutAsync($"{ResourcePath}/{id}", ReCClientHelpers.ToJsonContent(payload), cancel);
|
||||
/// <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)
|
||||
{
|
||||
using (var content = ReCClientHelpers.ToJsonContent(payload))
|
||||
using (var resp = await Http.PutAsync($"{ResourcePath}/{id}", content, cancel))
|
||||
{
|
||||
return resp.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes resources with identifiers supplied in the payload.
|
||||
@@ -58,14 +70,17 @@ 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>The HTTP response message.</returns>
|
||||
public Task<HttpResponseMessage> DeleteAsync<T>(T payload, CancellationToken cancel = default)
|
||||
/// <returns><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
|
||||
public async Task<bool> DeleteAsync<T>(T payload, CancellationToken cancel = default)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Delete, ResourcePath)
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Delete, ResourcePath)
|
||||
{
|
||||
Content = ReCClientHelpers.ToJsonContent(payload)
|
||||
};
|
||||
return Http.SendAsync(request, cancel);
|
||||
})
|
||||
using (var resp = await Http.SendAsync(request, cancel))
|
||||
{
|
||||
return resp.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user