mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-12-30 02:14:44 +08:00
feat(wxads): 导入微信广告平台 API 客户端项目
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Flurl.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
internal static class FlurlHttpRequestOptionsExtensions
|
||||
{
|
||||
public static IFlurlRequest SetOptions(this IFlurlRequest request, WechatAdsRequest options)
|
||||
{
|
||||
if (request == null) throw new ArgumentNullException(nameof(request));
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
if (options.Timeout.HasValue)
|
||||
{
|
||||
request.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout.Value));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(options.Version))
|
||||
{
|
||||
request.SetQueryParam("version", options.Version);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Flurl;
|
||||
using Flurl.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Interceptors
|
||||
{
|
||||
internal class WechatAdsAuthenticator
|
||||
{
|
||||
private readonly string _agencyId;
|
||||
private readonly string _agencyApiKey;
|
||||
|
||||
public WechatAdsAuthenticator(string agencyId, string agencyApiKey)
|
||||
{
|
||||
_agencyId = agencyId;
|
||||
_agencyApiKey = agencyApiKey;
|
||||
}
|
||||
|
||||
public void Authenticate(FlurlCall flurlCall)
|
||||
{
|
||||
if (flurlCall == null) throw new ArgumentNullException(nameof(flurlCall));
|
||||
|
||||
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
|
||||
string nonce = Guid.NewGuid().ToString("N");
|
||||
string sign = Security.MD5Utility.Hash($"{_agencyId}{timestamp}{nonce}{_agencyApiKey}").ToLower();
|
||||
string token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_agencyId},{timestamp},{nonce},{sign}"));
|
||||
|
||||
flurlCall.Request.RemoveQueryParam("agency_token");
|
||||
flurlCall.Request.SetQueryParam("agency_token", token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net461; netstandard2.0</TargetFrameworks>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<NullableReferenceTypes>true</NullableReferenceTypes>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<PackageId>SKIT.FlurlHttpClient.Wechat.Ads</PackageId>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageProjectUrl>https://github.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat</PackageProjectUrl>
|
||||
<PackageTags>Flurl.Http Wechat Weixin MicroMessage WechatAds WechatAdvertisting 微信 广告平台 微信广告平台 腾讯广告平台 广点通 微信广点通 腾讯广点通</PackageTags>
|
||||
<Version>0.1.0-alpha</Version>
|
||||
<Description>Flurl.Http client for Wechat Ads Open API. 基于 Flurl.Http 的微信广告平台(广点通)API 客户端。</Description>
|
||||
<Authors>Fu Diwei</Authors>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git</RepositoryUrl>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461'" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SKIT.FlurlHttpClient.Wechat\SKIT.FlurlHttpClient.Wechat.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
145
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsClient.cs
Normal file
145
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsClient.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
using Flurl.Http.Configuration;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
/// <summary>
|
||||
/// 一个微信广告平台 API HTTP 客户端。
|
||||
/// </summary>
|
||||
public class WechatAdsClient : WechatClientBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前客户端使用的微信广告平台服务商 ID。
|
||||
/// </summary>
|
||||
public string AgencyId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前客户端使用的微信广告平台服务商 AppId。
|
||||
/// </summary>
|
||||
public string AgencyAppId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前客户端使用的微信广告平台服务商 ApiKey。
|
||||
/// </summary>
|
||||
internal string AgencyApiKey { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前客户端使用的 JSON 序列化器。
|
||||
/// </summary>
|
||||
internal ISerializer JsonSerializer
|
||||
{
|
||||
get { return ProxyFlurlClient.Settings?.JsonSerializer ?? new FlurlNewtonsoftJsonSerializer(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用指定的配置项初始化 <see cref="WechatAdsClient"/> 类的新实例。
|
||||
/// </summary>
|
||||
/// <param name="options">配置项。</param>
|
||||
public WechatAdsClient(WechatAdsClientOptions options)
|
||||
: base()
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
AgencyId = options.AgencyId;
|
||||
AgencyAppId = options.AgencyAppId;
|
||||
AgencyApiKey = options.AgencyApiKey;
|
||||
|
||||
ProxyFlurlClient.BaseUrl = options.Endpoints ?? WechatAdsEndpoints.DEFAULT;
|
||||
ProxyFlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));
|
||||
|
||||
var interceptorAuthenticator = new Interceptors.WechatAdsAuthenticator(
|
||||
agencyId: options.AgencyId,
|
||||
agencyApiKey: options.AgencyApiKey
|
||||
);
|
||||
ProxyFlurlClient.BeforeCall((call) => interceptorAuthenticator.Authenticate(call));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用指定的微信广告平台服务商 ID、微信广告平台服务商 AppId、微信广告平台服务商 ApiKey 初始化 <see cref="WechatAdsClient"/> 类的新实例。
|
||||
/// </summary>
|
||||
/// <param name="agencyId">微信广告平台服务商 ID。</param>
|
||||
/// <param name="agencyAppId">微信广告平台服务商 AppId。</param>
|
||||
/// <param name="agencyApiKey">微信广告平台服务商 ApiKey。</param>
|
||||
public WechatAdsClient(string agencyId, string agencyAppId, string agencyApiKey)
|
||||
: this(new WechatAdsClientOptions() { AgencyId = agencyId, AgencyAppId = agencyAppId, AgencyApiKey = agencyApiKey })
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步发起请求。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> SendRequestAsync<T>(IFlurlRequest request, HttpContent? content = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatAdsResponse, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
using IFlurlResponse response = await base.SendRequestAsync(request, content, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(response).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
throw new WechatAdsException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步发起请求。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest request, object? data = null, CancellationToken cancellationToken = default)
|
||||
where T : WechatAdsResponse, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
using IFlurlResponse response = await base.SendRequestWithJsonAsync(request, data, cancellationToken).ConfigureAwait(false);
|
||||
return await GetResposneAsync<T>(response).ConfigureAwait(false);
|
||||
}
|
||||
catch (FlurlHttpException ex)
|
||||
{
|
||||
throw new WechatAdsException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse response)
|
||||
where T : WechatAdsResponse, new()
|
||||
{
|
||||
string contentType = response.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||
string contentDisposition = response.Headers.GetAll("Content-Disposition").FirstOrDefault() ?? string.Empty;
|
||||
bool contentTypeIsNotJson =
|
||||
(response.StatusCode != (int)HttpStatusCode.OK) ||
|
||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json")) ||
|
||||
(contentDisposition.StartsWith("attachment"));
|
||||
|
||||
T result = contentTypeIsNotJson ? new T() : await response.GetJsonAsync<T>().ConfigureAwait(false);
|
||||
result.RawStatus = response.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
response.Headers
|
||||
.GroupBy(e => e.Name)
|
||||
.ToDictionary(
|
||||
k => k.Key,
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = await response.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
/// <summary>
|
||||
/// 一个用于构造 <see cref="WechatAdsClient"/> 时使用的配置项。
|
||||
/// </summary>
|
||||
public class WechatAdsClientOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置请求超时时间(单位:毫秒)。
|
||||
/// <para>默认值:30000</para>
|
||||
/// </summary>
|
||||
public int Timeout { get; set; } = 30 * 1000;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信广告平台 API 域名。
|
||||
/// <para>默认值:<see cref="WechatAdsEndpoints.DEFAULT"/></para>
|
||||
/// </summary>
|
||||
public string? Endpoints { get; set; } = WechatAdsEndpoints.DEFAULT;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信广告平台服务商 ID。
|
||||
/// </summary>
|
||||
public string AgencyId { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信广告平台服务商 AppId。
|
||||
/// </summary>
|
||||
public string AgencyAppId { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信广告平台服务商 ApiKey。
|
||||
/// </summary>
|
||||
public string AgencyApiKey { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
15
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsEndpoints.cs
Normal file
15
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsEndpoints.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
/// <summary>
|
||||
/// 微信广告平台 API 接口域名。
|
||||
/// </summary>
|
||||
public static class WechatAdsEndpoints
|
||||
{
|
||||
/// <summary>
|
||||
/// 主域名(默认)。
|
||||
/// </summary>
|
||||
public const string DEFAULT = "https://api.weixin.qq.com";
|
||||
}
|
||||
}
|
||||
27
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsException.cs
Normal file
27
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsException.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
/// <summary>
|
||||
/// 微信广告平台 API 出错时引发的异常。
|
||||
/// </summary>
|
||||
public class WechatAdsException : WechatExceptionBase
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public WechatAdsException()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public WechatAdsException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public WechatAdsException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsRequest.cs
Normal file
32
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsRequest.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
/// <summary>
|
||||
/// 微信广告平台 API 请求的基类。
|
||||
/// </summary>
|
||||
public abstract class WechatAdsRequest : IWechatRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置请求超时时间(单位:毫秒)。如果不指定将使用构造 <see cref="WechatAdsClient"/> 时的 <see cref="WechatAdsClientOptions.Timeout"/> 参数,这在需要指定特定耗时请求(比如上传或下载文件)的超时时间时很有用。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public virtual int? Timeout { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信广告平台的 AccessToken。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public virtual string? AccessToken { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置微信广告平台的版本号。
|
||||
/// <para>默认值:v1.0</para>
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public virtual string? Version { get; set; } = "v1.0";
|
||||
}
|
||||
}
|
||||
55
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsResponse.cs
Normal file
55
src/SKIT.FlurlHttpClient.Wechat.Ads/WechatAdsResponse.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
/// <summary>
|
||||
/// 微信广告平台 API 响应的基类。
|
||||
/// </summary>
|
||||
public abstract class WechatAdsResponse : IWechatResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应状态码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int RawStatus { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应表头集合。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始的 HTTP 响应正文。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] RawBytes { get; internal set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信广告平台 API 返回的错误码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("errcode")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("errcode")]
|
||||
public virtual int ErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信广告平台 API 返回的错误描述。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("errmsg")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("errmsg")]
|
||||
public virtual string? ErrorMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值,该值指示调用微信广告平台 API 是否成功(即 HTTP 状态码为 200、且 errcode 值为 0)。
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool IsSuccessful()
|
||||
{
|
||||
return RawStatus == 200 && ErrorCode == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user