Refactor API clients to use primary constructor for HttpClient

Refactored CatalogApiClient, DashboardApiClient, LayoutApiClient, and MassDataApiClient to use C# primary constructor syntax for injecting HttpClient. Removed private _httpClient fields and updated all usages to reference the constructor parameter directly. This change simplifies the code and modernizes dependency injection without altering any API logic.
This commit is contained in:
OlgunR
2026-05-11 17:08:52 +02:00
parent 1b67d0472e
commit 45011122b2
4 changed files with 17 additions and 41 deletions

View File

@@ -3,30 +3,24 @@ using DbFirst.Contracts.Catalogs;
namespace DbFirst.BlazorWebApp.Services; namespace DbFirst.BlazorWebApp.Services;
public class CatalogApiClient : ICatalogApiClient public class CatalogApiClient(HttpClient httpClient) : ICatalogApiClient
{ {
private readonly HttpClient _httpClient;
private const string Endpoint = "api/catalogs"; private const string Endpoint = "api/catalogs";
public CatalogApiClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken ct = default) public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken ct = default)
{ {
var result = await _httpClient.GetFromJsonAsync<List<CatalogReadDto>>(Endpoint, ct); var result = await httpClient.GetFromJsonAsync<List<CatalogReadDto>>(Endpoint, ct);
return result ?? []; return result ?? [];
} }
public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken ct = default) public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken ct = default)
{ {
return await _httpClient.GetFromJsonAsync<CatalogReadDto>($"{Endpoint}/{id}", ct); return await httpClient.GetFromJsonAsync<CatalogReadDto>($"{Endpoint}/{id}", ct);
} }
public async Task<ApiResult<CatalogReadDto?>> CreateAsync(CatalogWriteDto dto, CancellationToken ct = default) public async Task<ApiResult<CatalogReadDto?>> CreateAsync(CatalogWriteDto dto, CancellationToken ct = default)
{ {
var response = await _httpClient.PostAsJsonAsync(Endpoint, dto, ct); 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>();
@@ -39,7 +33,7 @@ public class CatalogApiClient : ICatalogApiClient
public async Task<ApiResult<bool>> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken ct = default) public async Task<ApiResult<bool>> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken ct = default)
{ {
var response = await _httpClient.PutAsJsonAsync($"{Endpoint}/{id}", dto, ct); 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);
@@ -51,7 +45,7 @@ public class CatalogApiClient : ICatalogApiClient
public async Task<ApiResult<bool>> DeleteAsync(int id, CancellationToken ct = default) public async Task<ApiResult<bool>> DeleteAsync(int id, CancellationToken ct = default)
{ {
var response = await _httpClient.DeleteAsync($"{Endpoint}/{id}", ct); var response = await httpClient.DeleteAsync($"{Endpoint}/{id}", ct);
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
return ApiResult<bool>.Ok(true); return ApiResult<bool>.Ok(true);

View File

@@ -2,19 +2,13 @@ using DbFirst.Contracts.Dashboards;
namespace DbFirst.BlazorWebApp.Services; namespace DbFirst.BlazorWebApp.Services;
public class DashboardApiClient : IDashboardApiClient public class DashboardApiClient(HttpClient httpClient) : IDashboardApiClient
{ {
private readonly HttpClient _httpClient;
private const string Endpoint = "api/dashboard/dashboards"; private const string Endpoint = "api/dashboard/dashboards";
public DashboardApiClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<List<DashboardInfoDto>> GetAllAsync(CancellationToken ct = default) public async Task<List<DashboardInfoDto>> GetAllAsync(CancellationToken ct = default)
{ {
var result = await _httpClient.GetFromJsonAsync<List<DashboardInfoDto>>(Endpoint, ct); var result = await httpClient.GetFromJsonAsync<List<DashboardInfoDto>>(Endpoint, ct);
return result ?? []; return result ?? [];
} }
} }

View File

@@ -2,20 +2,14 @@ using DbFirst.Contracts.Layouts;
namespace DbFirst.BlazorWebApp.Services; namespace DbFirst.BlazorWebApp.Services;
public class LayoutApiClient : ILayoutApiClient public class LayoutApiClient(HttpClient httpClient) : ILayoutApiClient
{ {
private readonly HttpClient _httpClient;
private const string Endpoint = "api/layouts"; private const string Endpoint = "api/layouts";
public LayoutApiClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<LayoutDto?> GetAsync(string layoutType, string layoutKey, string userName, CancellationToken ct = default) 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, ct); 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;
@@ -27,7 +21,7 @@ public class LayoutApiClient : ILayoutApiClient
public async Task<LayoutDto> UpsertAsync(LayoutDto dto, CancellationToken ct = default) public async Task<LayoutDto> UpsertAsync(LayoutDto dto, CancellationToken ct = default)
{ {
var response = await _httpClient.PostAsJsonAsync(Endpoint, dto, ct); 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);
@@ -41,7 +35,7 @@ public class LayoutApiClient : ILayoutApiClient
public async Task DeleteAsync(string layoutType, string layoutKey, string userName, CancellationToken ct = default) 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, ct); var response = await httpClient.DeleteAsync(url, ct);
if (response.StatusCode == System.Net.HttpStatusCode.NotFound) if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{ {
return; return;

View File

@@ -4,19 +4,13 @@ using Microsoft.AspNetCore.WebUtilities;
namespace DbFirst.BlazorWebApp.Services; namespace DbFirst.BlazorWebApp.Services;
public class MassDataApiClient : IMassDataApiClient public class MassDataApiClient(HttpClient httpClient) : IMassDataApiClient
{ {
private readonly HttpClient _httpClient;
private const string Endpoint = "api/massdata"; private const string Endpoint = "api/massdata";
public MassDataApiClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<int> GetCountAsync(CancellationToken ct = default) public async Task<int> GetCountAsync(CancellationToken ct = default)
{ {
var result = await _httpClient.GetFromJsonAsync<int?>("api/massdata/count", ct); var result = await httpClient.GetFromJsonAsync<int?>("api/massdata/count", ct);
return result ?? 0; return result ?? 0;
} }
@@ -27,13 +21,13 @@ public class MassDataApiClient : IMassDataApiClient
if (take.HasValue) query["take"] = take.Value.ToString(); if (take.HasValue) query["take"] = take.Value.ToString();
var url = QueryHelpers.AddQueryString(Endpoint, query); var url = QueryHelpers.AddQueryString(Endpoint, query);
var result = await _httpClient.GetFromJsonAsync<List<MassDataReadDto>>(url, ct); var result = await httpClient.GetFromJsonAsync<List<MassDataReadDto>>(url, ct);
return result ?? []; return result ?? [];
} }
public async Task<ApiResult<MassDataReadDto?>> UpsertAsync(MassDataWriteDto dto, CancellationToken ct = default) public async Task<ApiResult<MassDataReadDto?>> UpsertAsync(MassDataWriteDto dto, CancellationToken ct = default)
{ {
var response = await _httpClient.PostAsJsonAsync($"{Endpoint}/upsert", dto, ct); 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>();
@@ -51,7 +45,7 @@ public class MassDataApiClient : IMassDataApiClient
return null; return null;
} }
var response = await _httpClient.GetAsync($"{Endpoint}/{Uri.EscapeDataString(customerName)}", ct); 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;