using Microsoft.Extensions.Logging;
|
using Newtonsoft.Json;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Text.Json;
|
using System.Threading.Tasks;
|
using WIDESEA_DTO.Basic;
|
using WIDESEA_IBasicService;
|
|
namespace WIDESEA_BasicService
|
{
|
public class ESSApiService : IESSApiService
|
{
|
private readonly IHttpClientFactory _httpClientFactory;
|
private readonly ILogger<ESSApiService> _logger;
|
public ESSApiService(IHttpClientFactory httpClientFactory, ILogger<ESSApiService> logger)
|
{
|
_httpClientFactory = httpClientFactory;
|
_logger = logger;
|
}
|
|
/// <summary>
|
/// 容器流动通知
|
/// </summary>
|
public async Task MoveContainerAsync(MoveContainerRequest request)
|
{
|
var url = "conveyor/moveContainer";
|
|
var result = await PostAsync<MoveContainerRequest, ApiResponse<string>>(url, request);
|
|
}
|
|
/// <summary>
|
/// 创建任务
|
/// </summary>
|
/// <param name="request"></param>
|
/// <returns></returns>
|
public async Task<bool> CreateTaskAsync(TaskModel request)
|
{
|
_logger.LogInformation("创建任务Request: " + JsonConvert.SerializeObject(request));
|
var url = "task/create";
|
|
var result = await PostAsync<TaskModel, ApiResponse<TasksData>>(url, request);
|
if (result != null && result.Code == 0)
|
{
|
return true;
|
}
|
return false;
|
}
|
|
|
|
private async Task<TResponse> PostAsync<TRequest, TResponse>(string url, TRequest request)
|
{
|
string json = JsonConvert.SerializeObject(request, new JsonSerializerSettings
|
{
|
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
|
});
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
var _client = _httpClientFactory.CreateClient("ESSUrl");
|
_client.DefaultRequestHeaders.Clear();
|
|
_client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
using var response = await _client.PostAsync(url, content);
|
string body = await response.Content.ReadAsStringAsync();
|
_logger.LogInformation($"ESSAPI post : {_client.BaseAddress} {url} {body}" );
|
if (!response.IsSuccessStatusCode)
|
{
|
throw new HttpRequestException(body);
|
}
|
|
return JsonConvert.DeserializeObject<TResponse>(body);
|
}
|
}
|
}
|