2021-05-10 15:30:00 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Flurl.Http;
|
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 为 <see cref="WechatTenpayClient"/> 提供图片上传(营销专用)相关的 API 扩展方法。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class WechatTenpayClientExecuteMarketingMediaExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /marketing/favor/media/image-upload 接口。</para>
|
|
|
|
|
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_0_1.shtml </para>
|
|
|
|
|
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter9_0_1.shtml </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.UploadMarketingMediaImageResponse> ExecuteUploadMarketingMediaImageAsync(this WechatTenpayClient client, Models.UploadMarketingMediaImageRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
2021-10-18 19:51:57 +08:00
|
|
|
|
if (request.FileName == null)
|
2021-05-10 15:30:00 +08:00
|
|
|
|
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".png";
|
|
|
|
|
|
2021-10-18 19:51:57 +08:00
|
|
|
|
if (request.FileHash == null)
|
2022-03-11 20:07:12 +08:00
|
|
|
|
request.FileHash = BitConverter.ToString(Utilities.SHA256Utility.Hash(request.FileBytes)).Replace("-", "").ToLower();
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2021-10-18 19:51:57 +08:00
|
|
|
|
if (request.FileContentType == null)
|
2021-07-11 00:55:41 +08:00
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "marketing", "favor", "media", "image-upload");
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2022-04-26 18:03:38 +08:00
|
|
|
|
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, fileMetaJson: client.JsonSerializer.Serialize(request));
|
2021-07-11 00:55:41 +08:00
|
|
|
|
return await client.SendRequestAsync<Models.UploadMarketingMediaImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
2021-05-10 15:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|