basic sub-client implementation
This commit is contained in:
@@ -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;
|
||||
|
||||
6
Pilz.Net/Api/IApiSubClient.cs
Normal file
6
Pilz.Net/Api/IApiSubClient.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Pilz.Net.Api;
|
||||
|
||||
public interface IApiSubClient
|
||||
{
|
||||
internal protected static abstract T? CreateClient<T>(IApiClient client) where T : IApiSubClient;
|
||||
}
|
||||
Reference in New Issue
Block a user