mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2026-03-10 00:13:36 +08:00
feat(core): 支持拦截器功能
This commit is contained in:
@@ -9,6 +9,11 @@ namespace SKIT.FlurlHttpClient.Wechat
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IWechatClient : IDisposable
|
public interface IWechatClient : IDisposable
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前客户端的拦截器集合。
|
||||||
|
/// </summary>
|
||||||
|
public WechatHttpCallInterceptorCollection Interceptors { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 配置客户端。
|
/// 配置客户端。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -16,19 +16,39 @@ namespace SKIT.FlurlHttpClient.Wechat
|
|||||||
public abstract class WechatClientBase : IWechatClient
|
public abstract class WechatClientBase : IWechatClient
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
/// <inheritdoc />
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected IFlurlClient ProxyFlurlClient { get; }
|
public WechatHttpCallInterceptorCollection Interceptors { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前客户端使用的 <see cref="IFlurlClient"/> 对象。
|
||||||
|
/// </summary>
|
||||||
|
protected IFlurlClient FlurlClient { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected WechatClientBase()
|
protected WechatClientBase()
|
||||||
{
|
{
|
||||||
ProxyFlurlClient = new FlurlClient();
|
Interceptors = new WechatHttpCallInterceptorCollection();
|
||||||
ProxyFlurlClient.Configure(settings =>
|
FlurlClient = new FlurlClient();
|
||||||
|
FlurlClient.Configure(flurlSettings =>
|
||||||
{
|
{
|
||||||
settings.JsonSerializer = new FlurlSystemTextJsonSerializer();
|
flurlSettings.JsonSerializer = new FlurlSystemTextJsonSerializer();
|
||||||
|
flurlSettings.BeforeCallAsync = async (flurlCall) =>
|
||||||
|
{
|
||||||
|
for (int i = 0, len = Interceptors.Count; i < len; i++)
|
||||||
|
{
|
||||||
|
await Interceptors[i].BeforeCallAsync(flurlCall);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
flurlSettings.AfterCallAsync = async (flurlCall) =>
|
||||||
|
{
|
||||||
|
for (int i = Interceptors.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
await Interceptors[i].AfterCallAsync(flurlCall);
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +57,7 @@ namespace SKIT.FlurlHttpClient.Wechat
|
|||||||
{
|
{
|
||||||
if (configure == null) throw new ArgumentNullException(nameof(configure));
|
if (configure == null) throw new ArgumentNullException(nameof(configure));
|
||||||
|
|
||||||
ProxyFlurlClient.Configure(flurlSettings =>
|
FlurlClient.Configure(flurlSettings =>
|
||||||
{
|
{
|
||||||
var settings = new WechatClientSettings();
|
var settings = new WechatClientSettings();
|
||||||
settings.Timeout = flurlSettings.Timeout;
|
settings.Timeout = flurlSettings.Timeout;
|
||||||
@@ -64,24 +84,24 @@ namespace SKIT.FlurlHttpClient.Wechat
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public IFlurlRequest CreateRequest(HttpMethod method, params object[] urlSegments)
|
public IFlurlRequest CreateRequest(HttpMethod method, params object[] urlSegments)
|
||||||
{
|
{
|
||||||
return ProxyFlurlClient.Request(urlSegments).WithVerb(method);
|
return FlurlClient.Request(urlSegments).WithVerb(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步发起请求。
|
/// 异步发起请求。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="flurlRequest"></param>
|
||||||
/// <param name="content"></param>
|
/// <param name="content"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected virtual async Task<IFlurlResponse> SendRequestAsync(IFlurlRequest request, HttpContent? content = null, CancellationToken cancellationToken = default)
|
protected virtual async Task<IFlurlResponse> SendRequestAsync(IFlurlRequest flurlRequest, HttpContent? content = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
if (request == null) throw new ArgumentNullException(nameof(request));
|
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||||
|
|
||||||
var response = await request
|
var response = await flurlRequest
|
||||||
.WithClient(ProxyFlurlClient)
|
.WithClient(FlurlClient)
|
||||||
.AllowAnyHttpStatus()
|
.AllowAnyHttpStatus()
|
||||||
.SendAsync(request.Verb, content, cancellationToken)
|
.SendAsync(flurlRequest.Verb, content, cancellationToken)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
@@ -89,18 +109,18 @@ namespace SKIT.FlurlHttpClient.Wechat
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步发起请求。
|
/// 异步发起请求。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="flurlRequest"></param>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected virtual async Task<IFlurlResponse> SendRequestWithJsonAsync(IFlurlRequest request, object? data = null, CancellationToken cancellationToken = default)
|
protected virtual async Task<IFlurlResponse> SendRequestWithJsonAsync(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
if (request == null) throw new ArgumentNullException(nameof(request));
|
if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest));
|
||||||
|
|
||||||
var response = await request
|
var response = await flurlRequest
|
||||||
.WithClient(ProxyFlurlClient)
|
.WithClient(FlurlClient)
|
||||||
.AllowAnyHttpStatus()
|
.AllowAnyHttpStatus()
|
||||||
.SendJsonAsync(request.Verb, data, cancellationToken)
|
.SendJsonAsync(flurlRequest.Verb, data, cancellationToken)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
@@ -110,7 +130,7 @@ namespace SKIT.FlurlHttpClient.Wechat
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual void Dispose()
|
public virtual void Dispose()
|
||||||
{
|
{
|
||||||
ProxyFlurlClient?.Dispose();
|
FlurlClient?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
24
src/SKIT.FlurlHttpClient.Wechat/WechatHttpCallInterceptor.cs
Normal file
24
src/SKIT.FlurlHttpClient.Wechat/WechatHttpCallInterceptor.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Flurl.Http;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat
|
||||||
|
{
|
||||||
|
public abstract class WechatHttpCallInterceptor
|
||||||
|
{
|
||||||
|
public virtual Task BeforeCallAsync(FlurlCall flurlCall)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Task AfterCallAsync(FlurlCall flurlCall)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat
|
||||||
|
{
|
||||||
|
public sealed class WechatHttpCallInterceptorCollection : IEnumerable<WechatHttpCallInterceptor>
|
||||||
|
{
|
||||||
|
private readonly IList<WechatHttpCallInterceptor> _list;
|
||||||
|
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get { return _list.Count; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public WechatHttpCallInterceptor this[int index]
|
||||||
|
{
|
||||||
|
get { return _list[index]; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public WechatHttpCallInterceptorCollection()
|
||||||
|
{
|
||||||
|
_list = new List<WechatHttpCallInterceptor>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(WechatHttpCallInterceptor interceptor)
|
||||||
|
{
|
||||||
|
_list.Add(interceptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(WechatHttpCallInterceptor interceptor)
|
||||||
|
{
|
||||||
|
_list.Remove(interceptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<WechatHttpCallInterceptor> GetEnumerator()
|
||||||
|
{
|
||||||
|
return _list.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return ((IEnumerable)_list).GetEnumerator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user