diff --git a/Pilz.Net/Api/ApiClient.cs b/Pilz.Net/Api/ApiClient.cs index fe3626c..a1e6e8c 100644 --- a/Pilz.Net/Api/ApiClient.cs +++ b/Pilz.Net/Api/ApiClient.cs @@ -1,4 +1,7 @@ using Castle.Core.Logging; +using Pilz.Extensions.Collections; +using System.Text.Encodings.Web; +using System.Web; namespace Pilz.Net.Api; @@ -14,64 +17,24 @@ public class ApiClient(string apiUrl) : IApiClient public ILogger Log { get; set; } = NullLogger.Instance; - public virtual Task SendRequest(string route) - { - return SendRequest(route, HttpMethod.Post); - } - - public virtual Task SendRequest(string route, HttpMethod method) - { - return SendRequest(route, method, null); - } - - public virtual Task SendRequest(string route, ApiMessage? message) - { - return SendRequest(route, HttpMethod.Post, message); - } - - public virtual Task SendRequest(string route, HttpMethod method, ApiMessage? message) - { - return SendRequest(route, method, message, null); - } - - public virtual async Task SendRequest(string route, HttpMethod method, ApiMessage? message, IApiMessageSerializer? serializer) + public virtual async Task SendRequest(string route, HttpMethod method, ApiMessage? message, IDictionary? @params, IApiMessageSerializer? serializer) { serializer ??= Serializer; Log.InfoFormat("Send message to {0}", route); - var res = await Send(route, method, message, serializer); + var res = await Send(route, method, message, @params, serializer); return new(res.StatusCode); } - public virtual Task> SendRequest(string route) where TResponse : ApiMessage - { - return SendRequest(route, HttpMethod.Post); - } - - public virtual Task> SendRequest(string route, HttpMethod method) where TResponse : ApiMessage - { - return SendRequest(route, method, null); - } - - public virtual Task> SendRequest(string route, ApiMessage? message) where TResponse : ApiMessage - { - return SendRequest(route, HttpMethod.Post, message); - } - - public virtual Task> SendRequest(string route, HttpMethod method, ApiMessage? message) where TResponse : ApiMessage - { - return SendRequest(route, method, message, null); - } - - public virtual async Task> SendRequest(string route, HttpMethod method, ApiMessage? message, IApiMessageSerializer? serializer) where TResponse : ApiMessage + public virtual async Task> SendRequest(string route, HttpMethod method, ApiMessage? message, IDictionary? @params, IApiMessageSerializer? serializer) where TResponse : ApiMessage { serializer ??= Serializer; Log.InfoFormat("Send request to {0}", route); - var res = await Send(route, method, message, serializer); + var res = await Send(route, method, message, @params, serializer); TResponse? result = null; if (res.IsSuccessStatusCode) @@ -80,9 +43,9 @@ public class ApiClient(string apiUrl) : IApiClient return new(res.StatusCode, result); } - protected virtual async Task Send(string route, HttpMethod method, ApiMessage? message, IApiMessageSerializer serializer) + protected virtual async Task Send(string route, HttpMethod method, ApiMessage? message, IDictionary? @params, IApiMessageSerializer serializer) { - var url = ApiUrl + route; + var url = ApiUrl + route + BuildParameters(@params); HttpContent content; Log.DebugFormat("Api endpoint url is {0}", url); @@ -108,6 +71,24 @@ public class ApiClient(string apiUrl) : IApiClient return await httpClient.SendAsync(httpmsg); } + protected virtual string BuildParameters(IDictionary? @params) + { + if (@params == null || @params.Count == 0) + return string.Empty; + + return "?" + string.Join("&", @params.Select(kvp => + { + var key = kvp.Key; + var value = HttpUtility.UrlEncode(ConvertParameter(kvp.Value)); + return $"{key}={value}"; + })); + } + + protected virtual string ConvertParameter(object @param) + { + return (string)Convert.ChangeType(@param, typeof(string)); + } + protected virtual string? EncodeAuthKey() { return AuthKey; diff --git a/Pilz.Net/Api/IApiClient.cs b/Pilz.Net/Api/IApiClient.cs index e7173df..47f208c 100644 --- a/Pilz.Net/Api/IApiClient.cs +++ b/Pilz.Net/Api/IApiClient.cs @@ -12,23 +12,67 @@ public interface IApiClient ILogger Log { get; set; } - Task SendRequest(string route); + Task SendRequest(string route, HttpMethod method, ApiMessage? message, IDictionary? @params, IApiMessageSerializer? serializer); - Task SendRequest(string route, HttpMethod method); + Task> SendRequest(string route, HttpMethod method, ApiMessage? message, IDictionary? @params, IApiMessageSerializer? serializer) where TResponse : ApiMessage; - Task SendRequest(string route, ApiMessage? message); + public virtual Task SendRequest(string route) + { + return SendRequest(route, HttpMethod.Post, null, null, null); + } - Task SendRequest(string route, HttpMethod method, ApiMessage? message); + public virtual Task SendRequest(string route, HttpMethod method) + { + return SendRequest(route, method, null, null, null); + } - Task SendRequest(string route, HttpMethod method, ApiMessage? message, IApiMessageSerializer? serializer); + public virtual Task SendRequest(string route, HttpMethod method, IDictionary? @params) + { + return SendRequest(route, method, null, @params, null); + } - Task> SendRequest(string route) where TResponse : ApiMessage; + public virtual Task SendRequest(string route, ApiMessage? message) + { + return SendRequest(route, HttpMethod.Post, message, null, null); + } - Task> SendRequest(string route, HttpMethod method) where TResponse : ApiMessage; + public virtual Task SendRequest(string route, HttpMethod method, ApiMessage? message) + { + return SendRequest(route, method, message, null, null); + } - Task> SendRequest(string route, ApiMessage? message) where TResponse : ApiMessage; + public virtual Task SendRequest(string route, HttpMethod method, ApiMessage? message, IDictionary? @params) + { + return SendRequest(route, method, message, @params, null); + } - Task> SendRequest(string route, HttpMethod method, ApiMessage? message) where TResponse : ApiMessage; + public virtual Task> SendRequest(string route) where TResponse : ApiMessage + { + return SendRequest(route, HttpMethod.Post, null, null, null); + } - Task> SendRequest(string route, HttpMethod method, ApiMessage? message, IApiMessageSerializer? serializer) where TResponse : ApiMessage; + public virtual Task> SendRequest(string route, HttpMethod method) where TResponse : ApiMessage + { + return SendRequest(route, method, null, null, null); + } + + public virtual Task> SendRequest(string route, HttpMethod method, IDictionary? @params) where TResponse : ApiMessage + { + return SendRequest(route, method, null, @params, null); + } + + public virtual Task> SendRequest(string route, ApiMessage? message) where TResponse : ApiMessage + { + return SendRequest(route, HttpMethod.Post, message, null, null); + } + + public virtual Task> SendRequest(string route, HttpMethod method, ApiMessage? message) where TResponse : ApiMessage + { + return SendRequest(route, method, message, null, null); + } + + public virtual Task> SendRequest(string route, HttpMethod method, ApiMessage? message, IDictionary? @params) where TResponse : ApiMessage + { + return SendRequest(route, method, message, @params, null); + } }