mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(directory): ✨🚧 add IDirectoryClient
This commit is contained in:
parent
cc4a0b97a0
commit
cfbc12c67f
6 changed files with 70 additions and 9 deletions
|
|
@ -28,14 +28,10 @@
|
|||
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.12" />
|
||||
<PackageReference Include="HangFire.Redis.StackExchange" Version="1.8.4" />
|
||||
<PackageReference Include="LibGit2Sharp" Version="0.26.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FilterLists.SharedKernel.Apis.Clients
|
||||
{
|
||||
internal abstract class BaseApiClient
|
||||
{
|
||||
protected BaseApiClient(HttpClient httpClient)
|
||||
{
|
||||
Client = httpClient;
|
||||
}
|
||||
|
||||
protected HttpClient Client { get; }
|
||||
|
||||
protected async Task<T> GetAsync<T>(string requestUri, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var response = await Client.GetAsync(requestUri, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
await using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
return await JsonSerializer.DeserializeAsync<T>(responseStream, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using RestSharp;
|
||||
using FilterLists.SharedKernel.Apis.Clients.Directory;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FilterLists.SharedKernel.Apis.Clients
|
||||
{
|
||||
|
|
@ -7,7 +7,7 @@ public static class ConfigurationExtensions
|
|||
{
|
||||
public static void AddApiClients(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IRestClient, RestClient>();
|
||||
services.AddHttpClient<IDirectoryApiClient, DirectoryApiApiClient>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.SharedKernel.Apis.Contracts.Directory;
|
||||
|
||||
namespace FilterLists.SharedKernel.Apis.Clients.Directory
|
||||
{
|
||||
internal sealed class DirectoryApiApiClient : BaseApiClient, IDirectoryApiClient
|
||||
{
|
||||
public DirectoryApiApiClient(HttpClient httpClient, Uri directoryApiBaseUrl) : base(httpClient)
|
||||
{
|
||||
Client.BaseAddress = directoryApiBaseUrl;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ListVm>> GetListsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await GetAsync<IEnumerable<ListVm>>("/lists", cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<ListDetailsVm> GetListDetailsAsync(int id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await GetAsync<ListDetailsVm>($"/lists/{id}", cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.SharedKernel.Apis.Contracts.Directory;
|
||||
|
||||
namespace FilterLists.SharedKernel.Apis.Clients.Directory
|
||||
{
|
||||
public interface IDirectoryApiClient
|
||||
{
|
||||
Task<IEnumerable<ListVm>> GetListsAsync(CancellationToken cancellationToken = default);
|
||||
Task<ListDetailsVm> GetListDetailsAsync(int id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
|
|
@ -29,8 +29,8 @@
|
|||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.8" />
|
||||
<PackageReference Include="RestSharp" Version="106.11.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.8" />
|
||||
<PackageReference Include="System.Text.Json" Version="4.7.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
Loading…
Reference in a new issue