|
|
|
@ -1,45 +1,31 @@
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Security;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
namespace DS.Module.Core
|
|
|
|
|
{
|
|
|
|
|
internal class ApiFox
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 提供对HTTP请求的低级别访问
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ApiFox
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发票API的基URL
|
|
|
|
|
/// 基URI
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string? BaseUrl { get; set; }
|
|
|
|
|
public Uri? BaseUri { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用户Key
|
|
|
|
|
/// 默认请求头
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string? UserKey { get; set; }
|
|
|
|
|
public IDictionary<string, string> DefaultHeaders { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用户密钥
|
|
|
|
|
/// 初始化
|
|
|
|
|
/// </summary>
|
|
|
|
|
public SecureString? UserSecret { get; private set; }
|
|
|
|
|
|
|
|
|
|
public ApiFox(IConfiguration config)
|
|
|
|
|
public ApiFox()
|
|
|
|
|
{
|
|
|
|
|
BaseUrl = config.GetValue<string>("InvoiceApi:BaseUrl");
|
|
|
|
|
UserKey = config.GetValue<string>("InvoiceApi:UserKey");
|
|
|
|
|
|
|
|
|
|
string? us = config.GetValue<string>("InvoiceApi:UserSecret");
|
|
|
|
|
if (!us.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
UserSecret = new SecureString();
|
|
|
|
|
for (int i = 0; i < us.Length; i++)
|
|
|
|
|
UserSecret.AppendChar(us[i]);
|
|
|
|
|
|
|
|
|
|
UserSecret.MakeReadOnly();
|
|
|
|
|
}
|
|
|
|
|
us = null;
|
|
|
|
|
DefaultHeaders = new Dictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -52,9 +38,7 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
/// <exception cref="ArgumentException"><paramref name="url"/>为null或空字符串</exception>
|
|
|
|
|
public async Task<DataResult<T>> GetAsync<T>(string url, NameValueCollection? keyValues = null)
|
|
|
|
|
{
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(nameof(url));
|
|
|
|
|
|
|
|
|
|
string? queryString = null;
|
|
|
|
|
string queryString = string.Empty;
|
|
|
|
|
if (keyValues != null && keyValues.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
@ -93,13 +77,11 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">请求结果的类型</typeparam>
|
|
|
|
|
/// <param name="url">请求Url</param>
|
|
|
|
|
/// <param name="requestParams">查询字符串的键值对</param>
|
|
|
|
|
/// <param name="requestParams">请求参数对象</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="ArgumentException"><paramref name="url"/>为null或空字符串</exception>
|
|
|
|
|
public async Task<DataResult<T>> PostAsync<T>(string url, object? requestParams = null)
|
|
|
|
|
{
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(nameof(url));
|
|
|
|
|
|
|
|
|
|
var result = await SendRequestAsync(HttpMethod.Post, url, requestParams);
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
return DataResult<T>.Failed(result.Message, result.MultiCode);
|
|
|
|
@ -110,27 +92,42 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发起请求
|
|
|
|
|
/// 发起HTTP请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="method">请求方法</param>
|
|
|
|
|
/// <param name="url">请求Url</param>
|
|
|
|
|
/// <param name="requestParams">请求参数</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult<HttpResponseMessage>> SendRequestAsync(HttpMethod method, string url, object? requestParams = null)
|
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="method"/>为null</exception>
|
|
|
|
|
/// <exception cref="ArgumentException"><paramref name="url"/>为null或空字符串</exception>
|
|
|
|
|
public virtual async Task<DataResult<HttpResponseMessage>> SendRequestAsync(HttpMethod method, string url, object? requestParams = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Uri.TryCreate(BaseUrl, UriKind.RelativeOrAbsolute, out Uri? uri))
|
|
|
|
|
return DataResult<HttpResponseMessage>.FailedWithDesc(nameof(MultiLanguageConst.InvoiceAPIUrlNull));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(method);
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(url);
|
|
|
|
|
|
|
|
|
|
Uri? reqUri = default;
|
|
|
|
|
if (BaseUri == null)
|
|
|
|
|
{
|
|
|
|
|
if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri? uri))
|
|
|
|
|
throw new ArgumentException("给定的URL格式无效", nameof(url));
|
|
|
|
|
|
|
|
|
|
reqUri = new Uri(url);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reqUri = new(BaseUri, url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpClient http = new();
|
|
|
|
|
http.DefaultRequestHeaders.Add("User-Agent", "X-HttpClient");
|
|
|
|
|
http.DefaultRequestHeaders.Add("User-Agent", $"X-{nameof(ApiFox)}");
|
|
|
|
|
|
|
|
|
|
if (!UserKey.IsNullOrEmpty())
|
|
|
|
|
http.DefaultRequestHeaders.Add("USER_KEY", UserKey);
|
|
|
|
|
if (DefaultHeaders.Count > 0)
|
|
|
|
|
foreach (var header in DefaultHeaders)
|
|
|
|
|
http.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
|
|
|
|
|
|
|
|
if (UserSecret != null)
|
|
|
|
|
http.DefaultRequestHeaders.Add("USER_SECRET", UserSecret.ToString());
|
|
|
|
|
if (!http.DefaultRequestHeaders.Contains("Accept"))
|
|
|
|
|
http.DefaultRequestHeaders.Add("Accept", "application/json, text/plain");
|
|
|
|
|
|
|
|
|
|
Uri reqUri = new(uri, url);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
HttpResponseMessage? response = null;
|