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