feat(EConnectClient): created as an implementation of IEConnectClient
This commit is contained in:
parent
757ba77179
commit
9d5bf509d5
@ -4,8 +4,8 @@ namespace Leanetec.EConnect.Client.Interface;
|
|||||||
|
|
||||||
public interface IEConnectClient<TError> where TError : class
|
public interface IEConnectClient<TError> where TError : class
|
||||||
{
|
{
|
||||||
public Task<Response<TData, TError>> GetAsync<TData>(string? route, CancellationToken cancel = default) where TData : class;
|
public Task<Response<TData, TError>> GetAsync<TData>(string? route = null, CancellationToken cancel = default) where TData : class;
|
||||||
|
|
||||||
public Task<Response<IAsyncEnumerable<TData?>, TError>> GetListAsAsyncEnumerable<TData>(string? route, CancellationToken cancel = default)
|
public Task<Response<IAsyncEnumerable<TData?>, TError>> GetListAsAsyncEnumerable<TData>(string? route = null, CancellationToken cancel = default)
|
||||||
where TData : class;
|
where TData : class;
|
||||||
}
|
}
|
||||||
49
src/Leanetec.EConnect.Infrastructure/EConnectClient.cs
Normal file
49
src/Leanetec.EConnect.Infrastructure/EConnectClient.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using Leanetec.EConnect.Client.Interface;
|
||||||
|
using Leanetec.EConnect.Domain.Entities;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
|
||||||
|
namespace Leanetec.EConnect.Infrastructure;
|
||||||
|
|
||||||
|
public class EConnectClient<TError> : IEConnectClient<TError> where TError : class
|
||||||
|
{
|
||||||
|
private readonly HttpClient _http;
|
||||||
|
|
||||||
|
public EConnectClient(IHttpClientFactory factory)
|
||||||
|
{
|
||||||
|
_http = factory.CreateEConnectClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Response<TData, TError>> GetAsync<TData>(string? route = null, CancellationToken cancel = default)
|
||||||
|
where TData : class
|
||||||
|
{
|
||||||
|
var res = await _http.GetAsync(route, cancel);
|
||||||
|
|
||||||
|
if (res.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
var data = await res.Content.ReadFromJsonAsync<TData>(cancel);
|
||||||
|
return new Response<TData, TError>(true, Data: data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var error = await res.Content.ReadFromJsonAsync<TError>(cancel);
|
||||||
|
return new Response<TData, TError>(false, Error: error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Response<IAsyncEnumerable<TData?>, TError>> GetListAsAsyncEnumerable<TData>(string? route = null, CancellationToken cancel = default)
|
||||||
|
where TData : class
|
||||||
|
{
|
||||||
|
var res = await _http.GetAsync(route, cancel);
|
||||||
|
|
||||||
|
if (res.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
var data = res.Content.ReadFromJsonAsAsyncEnumerable<TData>(cancel);
|
||||||
|
return new Response<IAsyncEnumerable<TData?>, TError>(true, Data: data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var error = await res.Content.ReadFromJsonAsync<TError>(cancellationToken: cancel);
|
||||||
|
return new Response<IAsyncEnumerable<TData?>, TError>(false, Error: error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -12,6 +12,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Leanetec.EConnect.Client\Leanetec.EConnect.Client.csproj" />
|
||||||
<ProjectReference Include="..\Leanetec.EConnect.Domain\Leanetec.EConnect.Domain.csproj" />
|
<ProjectReference Include="..\Leanetec.EConnect.Domain\Leanetec.EConnect.Domain.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user