mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor(clients): ♻📦 add Refit
This commit is contained in:
parent
2637e2e387
commit
e0c6126a9e
6 changed files with 11 additions and 57 deletions
|
|
@ -15,6 +15,7 @@ services:
|
|||
image: ghcr.io/collinbarrett/filterlists-archival-api
|
||||
restart: always
|
||||
networks:
|
||||
- reverse-proxy
|
||||
- archival
|
||||
depends_on:
|
||||
- archival-scheduling-db
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
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,6 @@
|
|||
using FilterLists.SharedKernel.Apis.Clients.Directory;
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Refit;
|
||||
|
||||
namespace FilterLists.SharedKernel.Apis.Clients
|
||||
{
|
||||
|
|
@ -7,7 +8,8 @@ public static class ConfigurationExtensions
|
|||
{
|
||||
public static void AddApiClients(this IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient<IDirectoryApiClient, DirectoryApiApiClient>();
|
||||
services.AddRefitClient<IDirectoryApiClient>()
|
||||
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://directory-api/api"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,8 +21,7 @@
|
|||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.8" />
|
||||
<PackageReference Include="System.Text.Json" Version="4.7.2" />
|
||||
<PackageReference Include="Refit.HttpClientFactory" Version="5.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -2,12 +2,16 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.SharedKernel.Apis.Contracts.Directory;
|
||||
using Refit;
|
||||
|
||||
namespace FilterLists.SharedKernel.Apis.Clients.Directory
|
||||
namespace FilterLists.SharedKernel.Apis.Clients
|
||||
{
|
||||
public interface IDirectoryApiClient
|
||||
{
|
||||
[Get("/lists")]
|
||||
Task<IEnumerable<ListVm>> GetListsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
[Get("/lists/{id}")]
|
||||
Task<ListDetailsVm> GetListDetailsAsync(int id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue