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:
@@ -14,20 +14,20 @@ public class CatalogApiClient : ICatalogApiClient
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<List<CatalogReadDto>> GetAllAsync()
|
||||
public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
var result = await _httpClient.GetFromJsonAsync<List<CatalogReadDto>>(Endpoint);
|
||||
var result = await _httpClient.GetFromJsonAsync<List<CatalogReadDto>>(Endpoint, ct);
|
||||
return result ?? new List<CatalogReadDto>();
|
||||
}
|
||||
|
||||
public async Task<CatalogReadDto?> GetByIdAsync(int id)
|
||||
public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken ct = default)
|
||||
{
|
||||
return await _httpClient.GetFromJsonAsync<CatalogReadDto>($"{Endpoint}/{id}");
|
||||
return await _httpClient.GetFromJsonAsync<CatalogReadDto>($"{Endpoint}/{id}", ct);
|
||||
}
|
||||
|
||||
public async Task<ApiResult<CatalogReadDto?>> CreateAsync(CatalogWriteDto dto)
|
||||
public async Task<ApiResult<CatalogReadDto?>> CreateAsync(CatalogWriteDto dto, CancellationToken ct = default)
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync(Endpoint, dto);
|
||||
var response = await _httpClient.PostAsJsonAsync(Endpoint, dto, ct);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var payload = await response.Content.ReadFromJsonAsync<CatalogReadDto>();
|
||||
@@ -38,9 +38,9 @@ public class CatalogApiClient : ICatalogApiClient
|
||||
return ApiResult<CatalogReadDto?>.Fail(error);
|
||||
}
|
||||
|
||||
public async Task<ApiResult<bool>> UpdateAsync(int id, CatalogWriteDto dto)
|
||||
public async Task<ApiResult<bool>> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken ct = default)
|
||||
{
|
||||
var response = await _httpClient.PutAsJsonAsync($"{Endpoint}/{id}", dto);
|
||||
var response = await _httpClient.PutAsJsonAsync($"{Endpoint}/{id}", dto, ct);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return ApiResult<bool>.Ok(true);
|
||||
@@ -50,9 +50,9 @@ public class CatalogApiClient : ICatalogApiClient
|
||||
return ApiResult<bool>.Fail(error);
|
||||
}
|
||||
|
||||
public async Task<ApiResult<bool>> DeleteAsync(int id)
|
||||
public async Task<ApiResult<bool>> DeleteAsync(int id, CancellationToken ct = default)
|
||||
{
|
||||
var response = await _httpClient.DeleteAsync($"{Endpoint}/{id}");
|
||||
var response = await _httpClient.DeleteAsync($"{Endpoint}/{id}", ct);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return ApiResult<bool>.Ok(true);
|
||||
|
||||
@@ -13,9 +13,9 @@ public class DashboardApiClient : IDashboardApiClient
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<List<DashboardInfoDto>> GetAllAsync()
|
||||
public async Task<List<DashboardInfoDto>> GetAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
var result = await _httpClient.GetFromJsonAsync<List<DashboardInfoDto>>(Endpoint);
|
||||
var result = await _httpClient.GetFromJsonAsync<List<DashboardInfoDto>>(Endpoint, ct);
|
||||
return result ?? new List<DashboardInfoDto>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ namespace DbFirst.BlazorWebApp.Services;
|
||||
|
||||
public interface ICatalogApiClient
|
||||
{
|
||||
Task<List<CatalogReadDto>> GetAllAsync();
|
||||
Task<CatalogReadDto?> GetByIdAsync(int id);
|
||||
Task<ApiResult<CatalogReadDto?>> CreateAsync(CatalogWriteDto dto);
|
||||
Task<ApiResult<bool>> UpdateAsync(int id, CatalogWriteDto dto);
|
||||
Task<ApiResult<bool>> DeleteAsync(int id);
|
||||
Task<List<CatalogReadDto>> GetAllAsync(CancellationToken ct = default);
|
||||
Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken ct = default);
|
||||
Task<ApiResult<CatalogReadDto?>> CreateAsync(CatalogWriteDto dto, CancellationToken ct = default);
|
||||
Task<ApiResult<bool>> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken ct = default);
|
||||
Task<ApiResult<bool>> DeleteAsync(int id, CancellationToken ct = default);
|
||||
}
|
||||
@@ -4,6 +4,6 @@ namespace DbFirst.BlazorWebApp.Services
|
||||
{
|
||||
public interface IDashboardApiClient
|
||||
{
|
||||
Task<List<DashboardInfoDto>> GetAllAsync();
|
||||
Task<List<DashboardInfoDto>> GetAllAsync(CancellationToken ct = default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ namespace DbFirst.BlazorWebApp.Services
|
||||
{
|
||||
public interface ILayoutApiClient
|
||||
{
|
||||
Task<LayoutDto?> GetAsync(string layoutType, string layoutKey, string userName);
|
||||
Task<LayoutDto> UpsertAsync(LayoutDto dto);
|
||||
Task DeleteAsync(string layoutType, string layoutKey, string userName);
|
||||
Task<LayoutDto?> GetAsync(string layoutType, string layoutKey, string userName, CancellationToken ct = default);
|
||||
Task<LayoutDto> UpsertAsync(LayoutDto dto, CancellationToken ct = default);
|
||||
Task DeleteAsync(string layoutType, string layoutKey, string userName, CancellationToken ct = default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ namespace DbFirst.BlazorWebApp.Services
|
||||
{
|
||||
public interface IMassDataApiClient
|
||||
{
|
||||
Task<int> GetCountAsync();
|
||||
Task<List<MassDataReadDto>> GetAllAsync(int? skip, int? take);
|
||||
Task<ApiResult<MassDataReadDto?>> UpsertAsync(MassDataWriteDto dto);
|
||||
Task<MassDataReadDto?> GetByCustomerNameAsync(string customerName);
|
||||
Task<int> GetCountAsync(CancellationToken ct = default);
|
||||
Task<List<MassDataReadDto>> GetAllAsync(int? skip, int? take, CancellationToken ct = default);
|
||||
Task<ApiResult<MassDataReadDto?>> UpsertAsync(MassDataWriteDto dto, CancellationToken ct = default);
|
||||
Task<MassDataReadDto?> GetByCustomerNameAsync(string customerName, CancellationToken ct = default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@ public class LayoutApiClient : ILayoutApiClient
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<LayoutDto?> GetAsync(string layoutType, string layoutKey, string userName)
|
||||
public async Task<LayoutDto?> GetAsync(string layoutType, string layoutKey, string userName, CancellationToken ct = default)
|
||||
{
|
||||
var url = $"{Endpoint}?layoutType={Uri.EscapeDataString(layoutType)}&layoutKey={Uri.EscapeDataString(layoutKey)}&userName={Uri.EscapeDataString(userName)}";
|
||||
var response = await _httpClient.GetAsync(url);
|
||||
var response = await _httpClient.GetAsync(url, ct);
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
return null;
|
||||
@@ -26,9 +26,9 @@ public class LayoutApiClient : ILayoutApiClient
|
||||
return await response.Content.ReadFromJsonAsync<LayoutDto>();
|
||||
}
|
||||
|
||||
public async Task<LayoutDto> UpsertAsync(LayoutDto dto)
|
||||
public async Task<LayoutDto> UpsertAsync(LayoutDto dto, CancellationToken ct = default)
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync(Endpoint, dto);
|
||||
var response = await _httpClient.PostAsJsonAsync(Endpoint, dto, ct);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var detail = await ApiClientHelper.ReadErrorAsync(response);
|
||||
@@ -39,10 +39,10 @@ public class LayoutApiClient : ILayoutApiClient
|
||||
return payload ?? dto;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(string layoutType, string layoutKey, string userName)
|
||||
public async Task DeleteAsync(string layoutType, string layoutKey, string userName, CancellationToken ct = default)
|
||||
{
|
||||
var url = $"{Endpoint}?layoutType={Uri.EscapeDataString(layoutType)}&layoutKey={Uri.EscapeDataString(layoutKey)}&userName={Uri.EscapeDataString(userName)}";
|
||||
var response = await _httpClient.DeleteAsync(url);
|
||||
var response = await _httpClient.DeleteAsync(url, ct);
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user