feat(work): 新增 Credentials 类型,存储初始化客户端时的凭证副本

This commit is contained in:
Fu Diwei
2021-07-27 00:50:28 +08:00
parent 02a06c8106
commit d039a74f3d
12 changed files with 79 additions and 64 deletions

View File

@@ -21,7 +21,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (client == null) throw new ArgumentNullException(nameof(client));
if (string.IsNullOrEmpty(callbackJson)) throw new ArgumentNullException(callbackJson);
return client.FlurlJsonSerializer.Deserialize<TEvent>(callbackJson);
return client.JsonSerializer.Deserialize<TEvent>(callbackJson);
}
/// <summary>

View File

@@ -27,7 +27,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "agent", "get")
@@ -73,7 +73,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "agent", "set")
@@ -97,7 +97,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "agent", "set_scope")
@@ -122,7 +122,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "agent", "set_workbench_template")
@@ -146,7 +146,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "agent", "get_workbench_template")
@@ -170,7 +170,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "agent", "set_workbench_data")

View File

@@ -26,7 +26,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.ParentAgentId.HasValue)
request.ParentAgentId = client.WechatAgentId;
request.ParentAgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "corpgroup", "corp", "list_app_share_info")

View File

@@ -26,8 +26,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "gettoken")
.SetQueryParam("corpid", client.WechatCorpId)
.SetQueryParam("corpsecret", client.WechatAgentSecret);
.SetQueryParam("corpid", client.Credentials.CorpId)
.SetQueryParam("corpsecret", client.Credentials.AgentSecret);
return await client.SendRequestWithJsonAsync<Models.CgibinGetTokenResponse>(flurlReq, cancellationToken: cancellationToken);
}

View File

@@ -972,7 +972,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "externalcontact", "message", "send")

View File

@@ -126,7 +126,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "linkedcorp", "message", "send")

View File

@@ -25,7 +25,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "menu", "create")
@@ -49,7 +49,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "menu", "get")
@@ -73,7 +73,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "menu", "delete")

View File

@@ -27,7 +27,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "message", "send");
@@ -50,7 +50,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (!request.AgentId.HasValue)
request.AgentId = client.WechatAgentId;
request.AgentId = client.Credentials.AgentId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "message", "update_taskcard");

View File

@@ -30,7 +30,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "service", "get_provider_token");
var requestData = new { corpid = client.WechatCorpId, provider_secret = client.WechatAgentSecret };
var requestData = new { corpid = client.Credentials.CorpId, provider_secret = client.Credentials.AgentSecret };
return await client.SendRequestWithJsonAsync<Models.CgibinServiceGetProviderTokenResponse>(flurlReq, data: requestData, cancellationToken: cancellationToken);
}
@@ -49,10 +49,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.SuiteId))
request.SuiteId = client.WechatSuiteId;
request.SuiteId = client.Credentials.SuiteId;
if (string.IsNullOrEmpty(request.SuiteSecret))
request.SuiteSecret = client.WechatSuiteSecret;
request.SuiteSecret = client.Credentials.SuiteSecret;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "service", "get_suite_token");

View File

@@ -39,7 +39,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
return new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{ "appId", client.WechatCorpId },
{ "appId", client.Credentials.CorpId },
{ "timestamp", timestamp },
{ "nonceStr", nonce },
{ "signature", sign }
@@ -68,8 +68,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
return new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{ "corpid", client.WechatCorpId },
{ "agentid", client.WechatAgentId?.ToString() ?? string.Empty },
{ "corpid", client.Credentials.CorpId },
{ "agentid", client.Credentials.AgentId?.ToString() ?? string.Empty },
{ "timestamp", timestamp },
{ "nonceStr", nonce },
{ "signature", sign }

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Work.Settings
{
public class Credentials
{
/// <summary>
/// 初始化客户端时 <see cref="WechatWorkClientOptions.CorpId"/> 的副本。
/// </summary>
public string CorpId { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatWorkClientOptions.AgentId"/> 的副本。
/// </summary>
public int? AgentId { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatWorkClientOptions.AgentSecret"/> 的副本。
/// </summary>
public string? AgentSecret { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatWorkClientOptions.ProviderSecret"/> 的副本。
/// </summary>
public string? ProviderSecret { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatWorkClientOptions.SuiteId"/> 的副本。
/// </summary>
public string? SuiteId { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatWorkClientOptions.SuiteId"/> 的副本。
/// </summary>
public string? SuiteSecret { get; }
internal Credentials(WechatWorkClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
CorpId = options.CorpId;
AgentId = options.AgentId;
AgentSecret = options.AgentSecret;
ProviderSecret = options.ProviderSecret;
SuiteId = options.SuiteId;
SuiteSecret = options.SuiteSecret;
}
}
}

View File

@@ -18,42 +18,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
public class WechatWorkClient : WechatClientBase
{
/// <summary>
/// 获取当前客户端使用的企业微信 CorpId
/// 获取当前客户端使用的企业微信凭证
/// </summary>
public string WechatCorpId { get; }
/// <summary>
/// 获取当前客户端使用的企业微信应用的 AgentId。
/// </summary>
internal int? WechatAgentId { get; }
/// <summary>
/// 获取当前客户端使用的企业微信应用的 AgentSecret。
/// </summary>
internal string? WechatAgentSecret { get; }
/// <summary>
/// 获取当前客户端使用的企业微信服务商 Secret。
/// </summary>
internal string? WechatProviderSecret { get; set; }
/// <summary>
/// 获取当前客户端使用的企业微信第三方应用的 SuiteId。
/// </summary>
internal string? WechatSuiteId { get; set; }
/// <summary>
/// 获取当前客户端使用的企业微信第三方应用的 SuiteSecret。
/// </summary>
internal string? WechatSuiteSecret { get; set; }
/// <summary>
/// 获取当前客户端使用的 JSON 序列化器。
/// </summary>
internal ISerializer FlurlJsonSerializer
{
get { return FlurlClient.Settings?.JsonSerializer ?? new FlurlNewtonsoftJsonSerializer(); }
}
public Settings.Credentials Credentials { get; }
/// <summary>
/// 用指定的配置项初始化 <see cref="WechatWorkClient"/> 类的新实例。
@@ -64,12 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
{
if (options == null) throw new ArgumentNullException(nameof(options));
WechatCorpId = options.CorpId;
WechatAgentId = options.AgentId;
WechatAgentSecret = options.AgentSecret;
WechatProviderSecret = options.ProviderSecret;
WechatSuiteId = options.SuiteId;
WechatSuiteSecret = options.SuiteSecret;
Credentials = new Settings.Credentials(options);
FlurlClient.BaseUrl = options.Endpoints ?? WechatWorkEndpoints.DEFAULT;
FlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));