wanshenmean
2026-03-09 1181f9f764b14abd6e9f598f89f8507b4bbfad0d
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Http/HTTP/HttpClientHelper.cs
@@ -10,16 +10,35 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.HttpEnum;
using WIDESEAWCS_Core.Caches;
namespace WIDESEA_Core
{
    public class HttpClientHelper
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly ICacheService _cache;
        public HttpClientHelper(IHttpClientFactory httpClientFactory, IConfiguration configuration = null)
        public HttpClientHelper(IHttpClientFactory httpClientFactory, ICacheService cache, IConfiguration configuration = null)
        {
            _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
            _cache = cache ?? throw new ArgumentNullException(nameof(cache));
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.CreateTaskInboundAsync)}", $"Task/CreateTaskInbound");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.GetTasksLocation)}", $"Task/GetTasksLocation");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.OutboundFinishTaskAsync)}", $"Task/OutboundFinishTask");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.InboundFinishTaskAsync)}", $"Task/InboundFinishTask");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.GetOutBoundTrayTaskAsync)}", $"Task/GetOutBoundTrayTask");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.CreateTaskInboundTrayAsync)}", $"Task/CreateTaskInboundTray");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.GroupPalletAsync)}", $"Stock/GroupPalletAsync");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.ChangePalletAsync)}", $"Stock/ChangePalletAsync");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.SplitPalletAsync)}", $"Stock/SplitPalletAsync");
            _cache.TryAdd($"{RedisPrefix.Code}:{RedisName.API}:{nameof(ConfigKey.UpdateTaskByStatus)}", $"Task/UpdateTaskByStatus");
        }
        /// <summary>
@@ -32,25 +51,31 @@
        /// <returns></returns>
        public HttpResponseResult Post(string url, string content, string contentType = "application/json", HttpRequestConfig? config = null)
        {
            HttpResponseResult httpResponseResult = ExecuteAsync(async (client) =>
            HttpResponseResult httpResponseResult = Task.Run(async () =>
            {
                var request = new HttpRequestMessage(HttpMethod.Post, url);
                request.Content = new StringContent(content ?? string.Empty, Encoding.UTF8, contentType);
                SetRequestHeaders(request, config?.Headers);
                return await client.SendAsync(request);
            }, config, $"POST {url}").Result;
                return await ExecuteAsync(async (client) =>
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, url);
                    request.Content = new StringContent(content ?? string.Empty, Encoding.UTF8, contentType);
                    SetRequestHeaders(request, config?.Headers);
                    return await client.SendAsync(request);
                }, config, $"POST {url}");
            }).GetAwaiter().GetResult();
            httpResponseResult.ApiUrl = url;
            return httpResponseResult;
        }
        public HttpResponseResult Get(string url, HttpRequestConfig? config = null)
        {
            HttpResponseResult httpResponseResult = ExecuteAsync(async (client) =>
            HttpResponseResult httpResponseResult = Task.Run(async () =>
            {
                var request = new HttpRequestMessage(HttpMethod.Get, url);
                SetRequestHeaders(request, config?.Headers);
                return await client.SendAsync(request);
            }, config, $"GET {url}").Result;
                return await ExecuteAsync(async (client) =>
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, url);
                    SetRequestHeaders(request, config?.Headers);
                    return await client.SendAsync(request);
                }, config, $"GET {url}");
            }).GetAwaiter().GetResult();
            httpResponseResult.ApiUrl = url;
            return httpResponseResult;
@@ -68,6 +93,7 @@
        public HttpResponseResult<TResponse> Post<TResponse>(string url, string content, string contentType = "application/json", HttpRequestConfig? config = null)
        {
            url = BaseAPI.WMSBaseUrl + _cache.Get($"{RedisPrefix.Code}:{RedisName.API}:{url}");
            HttpResponseResult httpResponseResult = Post(url, content, contentType, config);
            HttpResponseResult<TResponse> result = new HttpResponseResult<TResponse>
@@ -109,6 +135,7 @@
        public HttpResponseResult<TResponse> Get<TResponse>(string url, HttpRequestConfig? config = null)
        {
            url = BaseAPI.WMSBaseUrl + _cache.Get(url);
            HttpResponseResult httpResponseResult = Get(url, config);
            HttpResponseResult<TResponse> result = new HttpResponseResult<TResponse>