diff --git a/Pilz.Net/Api/ApiClient.cs b/Pilz.Net/Api/ApiClient.cs index 6d88460..d4cce38 100644 --- a/Pilz.Net/Api/ApiClient.cs +++ b/Pilz.Net/Api/ApiClient.cs @@ -8,6 +8,7 @@ namespace Pilz.Net.Api; public class ApiClient(string apiUrl) : IApiClient { protected readonly HttpClient httpClient = new(); + protected readonly List 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() where T : IApiSubClient + { + foreach (var c in clients) + { + if (c is T t) + return t; + } + + if (T.CreateClient(this) is T client) + { + clients.Add(client); + return client; + } + + throw new ApiException("Could not create an instance of this client."); + } + public virtual async Task SendRequest(string route, HttpMethod method, ApiMessage? message, ApiParameterCollection? @params, IApiMessageSerializer? serializer) { serializer ??= Serializer; diff --git a/Pilz.Net/Api/IApiSubClient.cs b/Pilz.Net/Api/IApiSubClient.cs new file mode 100644 index 0000000..3e9f2a7 --- /dev/null +++ b/Pilz.Net/Api/IApiSubClient.cs @@ -0,0 +1,6 @@ +namespace Pilz.Net.Api; + +public interface IApiSubClient +{ + internal protected static abstract T? CreateClient(IApiClient client) where T : IApiSubClient; +}