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:
2026-05-19 18:44:44 +02:00
parent a924e32291
commit 82ec333f23

View File

@@ -37,9 +37,15 @@ namespace ReC.Client.Api
/// <typeparam name="T">The payload type.</typeparam> /// <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><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
public Task<HttpResponseMessage> CreateAsync<T>(T payload, CancellationToken cancel = default) public async Task<bool> CreateAsync<T>(T payload, CancellationToken cancel = default)
=> Http.PostAsync(ResourcePath, ReCClientHelpers.ToJsonContent(payload), cancel); {
using (var content = ReCClientHelpers.ToJsonContent(payload))
using (var resp = await Http.PostAsync(ResourcePath, content, cancel))
{
return resp.IsSuccessStatusCode;
}
}
/// <summary> /// <summary>
/// Updates a resource by identifier. /// Updates a resource by identifier.
@@ -48,9 +54,15 @@ namespace ReC.Client.Api
/// <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><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
public Task<HttpResponseMessage> UpdateAsync<T>(long id, T payload, CancellationToken cancel = default) public async Task<bool> UpdateAsync<T>(long id, T payload, CancellationToken cancel = default)
=> Http.PutAsync($"{ResourcePath}/{id}", ReCClientHelpers.ToJsonContent(payload), cancel); {
using (var content = ReCClientHelpers.ToJsonContent(payload))
using (var resp = await Http.PutAsync($"{ResourcePath}/{id}", content, cancel))
{
return resp.IsSuccessStatusCode;
}
}
/// <summary> /// <summary>
/// Deletes resources with identifiers supplied in the payload. /// 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> /// <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><see langword="true"/> if the request succeeds; otherwise, <see langword="false"/>.</returns>
public Task<HttpResponseMessage> DeleteAsync<T>(T payload, CancellationToken cancel = default) 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) Content = ReCClientHelpers.ToJsonContent(payload)
}; })
return Http.SendAsync(request, cancel); using (var resp = await Http.SendAsync(request, cancel))
{
return resp.IsSuccessStatusCode;
}
} }
} }
} }