Add CancellationToken support to all API client methods

All API client interfaces and implementations now accept an optional CancellationToken parameter for each method. This enables consumers to cancel HTTP requests, improving responsiveness and resource management. No changes were made to core logic or return types; only method signatures and HttpClient calls were updated to support cancellation.
This commit is contained in:
OlgunR
2026-04-20 13:50:53 +02:00
parent aab6478f9a
commit fcbc66f8f5
8 changed files with 39 additions and 39 deletions

View File

@@ -13,13 +13,13 @@ public class MassDataApiClient : IMassDataApiClient
_httpClient = httpClient;
}
public async Task<int> GetCountAsync()
public async Task<int> GetCountAsync(CancellationToken ct = default)
{
var result = await _httpClient.GetFromJsonAsync<int?>("api/massdata/count");
var result = await _httpClient.GetFromJsonAsync<int?>("api/massdata/count", ct);
return result ?? 0;
}
public async Task<List<MassDataReadDto>> GetAllAsync(int? skip, int? take)
public async Task<List<MassDataReadDto>> GetAllAsync(int? skip, int? take, CancellationToken ct = default)
{
var query = new List<string>();
if (skip.HasValue)
@@ -32,13 +32,13 @@ public class MassDataApiClient : IMassDataApiClient
}
var url = query.Count == 0 ? Endpoint : $"{Endpoint}?{string.Join("&", query)}";
var result = await _httpClient.GetFromJsonAsync<List<MassDataReadDto>>(url);
var result = await _httpClient.GetFromJsonAsync<List<MassDataReadDto>>(url, ct);
return result ?? new List<MassDataReadDto>();
}
public async Task<ApiResult<MassDataReadDto?>> UpsertAsync(MassDataWriteDto dto)
public async Task<ApiResult<MassDataReadDto?>> UpsertAsync(MassDataWriteDto dto, CancellationToken ct = default)
{
var response = await _httpClient.PostAsJsonAsync($"{Endpoint}/upsert", dto);
var response = await _httpClient.PostAsJsonAsync($"{Endpoint}/upsert", dto, ct);
if (response.IsSuccessStatusCode)
{
var payload = await response.Content.ReadFromJsonAsync<MassDataReadDto>();
@@ -49,14 +49,14 @@ public class MassDataApiClient : IMassDataApiClient
return ApiResult<MassDataReadDto?>.Fail(error);
}
public async Task<MassDataReadDto?> GetByCustomerNameAsync(string customerName)
public async Task<MassDataReadDto?> GetByCustomerNameAsync(string customerName, CancellationToken ct = default)
{
if (string.IsNullOrWhiteSpace(customerName))
{
return null;
}
var response = await _httpClient.GetAsync($"{Endpoint}/{Uri.EscapeDataString(customerName)}");
var response = await _httpClient.GetAsync($"{Endpoint}/{Uri.EscapeDataString(customerName)}", ct);
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;