Remove debug APIs and transport scan Hangfire; move Upsert to Order/Upsert*; keep original response shapes and put result+reason only in exception messages. Co-authored-by: Cursor <cursoragent@cursor.com>
71 lines
2.8 KiB
C#
71 lines
2.8 KiB
C#
using InterfaceForward.Domain.Shared.Helpers;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Serialization;
|
||
using TrackCenter.Domain.VEntities;
|
||
|
||
namespace TrackCenter.Application.Track2InterfaceRelay;
|
||
|
||
public class InterfaceForwardService : ITransientDependency
|
||
{
|
||
private readonly IHttpClientFactory _httpClientFactory;
|
||
private readonly IBasicRepository<VAppEntity> _appRepository;
|
||
|
||
public InterfaceForwardService(IHttpClientFactory httpClientFactory, IBasicRepository<VAppEntity> appRepository)
|
||
{
|
||
_httpClientFactory = httpClientFactory;
|
||
_appRepository = appRepository;
|
||
}
|
||
|
||
public async Task<InterfaceRelayUnifyResultDto> BatchForwardThenPushToQueueAsync(
|
||
string upStreamCode,
|
||
string serviceProviderCode,
|
||
List<object> data,
|
||
string followName,
|
||
string? other = null,
|
||
bool sourceData = false)
|
||
{
|
||
var client = _httpClientFactory.CreateClient("itfx");
|
||
|
||
var body = JsonConvert.SerializeObject(data, new JsonSerializerSettings
|
||
{
|
||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||
});
|
||
var content = new StringContent(body);
|
||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
||
if (Track2InterfaceRelayAppContext.Current == null)
|
||
{
|
||
throw new BusinessException(message: $"请在{nameof(Track2InterfaceRelayAppContext)}中调用接口通");
|
||
}
|
||
|
||
var key = Track2InterfaceRelayAppContext.Current.AppKey;
|
||
var secret = await _appRepository.GetFirstAsync(x => x.AppKey == key, x => x.AppSecret);
|
||
if (secret == null)
|
||
{
|
||
throw new BusinessException(message: $"未能找到相应app,请联系管理员");
|
||
}
|
||
|
||
content.Headers.Add(nameof(InterfaceForwardOptions.AppKey), key);
|
||
content.Headers.Add(nameof(InterfaceForwardOptions.AppSecret), secret);
|
||
content.Headers.Add(nameof(followName), followName);
|
||
content.Headers.Add(nameof(other), other);
|
||
content.Headers.Add(nameof(sourceData), sourceData.ToString());
|
||
|
||
using var response = await client.PostAsync($"InterfaceForward/BatchRequest?standardCode={upStreamCode}&supplierCode={serviceProviderCode}", content);
|
||
|
||
var res = await response.Content.ReadAsStringAsync();
|
||
if (!response.IsSuccessStatusCode)
|
||
{
|
||
throw new BusinessException(
|
||
message: $"调用接口通失败:HTTP {(int)response.StatusCode} {response.ReasonPhrase};响应:{Truncate(res, 300)}");
|
||
}
|
||
|
||
return res.ToObject<InterfaceRelayUnifyResultDto>()!;
|
||
}
|
||
|
||
private static string Truncate(string? text, int maxLen)
|
||
{
|
||
if (string.IsNullOrEmpty(text)) return string.Empty;
|
||
return text.Length <= maxLen ? text : text[..maxLen] + "...";
|
||
}
|
||
}
|