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