2022-11-09 20:48:03 +08:00
|
|
|
using System;
|
2021-05-10 15:30:00 +08:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Flurl.Http;
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|
|
|
{
|
2024-02-05 10:53:59 +08:00
|
|
|
using SKIT.FlurlHttpClient.Primitives;
|
|
|
|
|
2021-05-10 15:30:00 +08:00
|
|
|
public static class WechatTenpayClientExecuteMarketingMediaExtensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// <para>异步调用 [POST] /marketing/favor/media/image-upload 接口。</para>
|
2024-01-03 17:17:42 +08:00
|
|
|
/// <para>REF: https://pay.weixin.qq.com/docs/merchant/apis/cash-coupons/upload-image.html </para>
|
|
|
|
/// <para>REF: https://pay.weixin.qq.com/docs/merchant/apis/merchant-exclusive-coupon/upload-image.html </para>
|
|
|
|
/// <para>REF: https://pay.weixin.qq.com/docs/merchant/apis/gift-activity/upload-image.html </para>
|
|
|
|
/// <para>REF: https://pay.weixin.qq.com/docs/partner/apis/cash-coupons/upload-image.html </para>
|
|
|
|
/// <para>REF: https://pay.weixin.qq.com/docs/partner/apis/merchant-exclusive-coupon/upload-image.html </para>
|
|
|
|
/// <para>REF: https://pay.weixin.qq.com/docs/partner/apis/gift-activity/upload-image.html </para>
|
2021-05-10 15:30:00 +08:00
|
|
|
/// </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));
|
|
|
|
|
2024-01-29 23:12:37 +08:00
|
|
|
if (request.FileName is null)
|
2021-05-10 15:30:00 +08:00
|
|
|
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".png";
|
|
|
|
|
2024-01-29 23:12:37 +08:00
|
|
|
if (request.FileHash is null)
|
2024-02-05 10:53:59 +08:00
|
|
|
request.FileHash = EncodedString.ToHexString(Utilities.SHA256Utility.Hash(request.FileBytes)).Value!.ToLower();
|
2021-05-10 15:30:00 +08:00
|
|
|
|
2024-01-29 23:12:37 +08:00
|
|
|
if (request.FileContentType is null)
|
2021-07-11 00:55:41 +08:00
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2024-01-29 23:12:37 +08:00
|
|
|
.CreateFlurlRequest(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));
|
2024-01-29 23:12:37 +08:00
|
|
|
return await client.SendFlurlRequestAsync<Models.UploadMarketingMediaImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
2021-05-10 15:30:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|