basic sub-client implementation

This commit is contained in:
Pilzinsel64
2024-12-12 07:25:18 +01:00
parent 4bdeaadbcb
commit 2b0b094731
2 changed files with 24 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ namespace Pilz.Net.Api;
public class ApiClient(string apiUrl) : IApiClient
{
protected readonly HttpClient httpClient = new();
protected readonly List<object> clients = [];
public virtual string ApiUrl { get; } = apiUrl;
@@ -17,6 +18,23 @@ public class ApiClient(string apiUrl) : IApiClient
public ILogger Log { get; set; } = NullLogger.Instance;
public T GetClient<T>() where T : IApiSubClient
{
foreach (var c in clients)
{
if (c is T t)
return t;
}
if (T.CreateClient<T>(this) is T client)
{
clients.Add(client);
return client;
}
throw new ApiException("Could not create an instance of this client.");
}
public virtual async Task<ApiResponse> SendRequest(string route, HttpMethod method, ApiMessage? message, ApiParameterCollection? @params, IApiMessageSerializer? serializer)
{
serializer ??= Serializer;