2021-05-28 19:23:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-07-11 00:55:24 +08:00
|
|
|
|
using System.Web;
|
2021-05-28 19:23:28 +08:00
|
|
|
|
using Flurl;
|
|
|
|
|
using Flurl.Http;
|
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.Api
|
|
|
|
|
{
|
|
|
|
|
public static class WechatApiClientExecuteCgibinMaterialExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/add_news 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html#%E6%96%B0%E5%A2%9E%E6%B0%B8%E4%B9%85%E5%9B%BE%E6%96%87%E7%B4%A0%E6%9D%90 </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-02-16 11:41:46 +08:00
|
|
|
|
[Obsolete("相关接口或字段于 2022-02-25 下线。")]
|
2021-05-28 19:23:28 +08:00
|
|
|
|
public static async Task<Models.CgibinMaterialAddNewsResponse> ExecuteCgibinMaterialAddNewsAsync(this WechatApiClient client, Models.CgibinMaterialAddNewsRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "add_news")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialAddNewsResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/add_material 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html#%E6%96%B0%E5%A2%9E%E5%85%B6%E4%BB%96%E7%B1%BB%E5%9E%8B%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90 </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-02-16 11:41:46 +08:00
|
|
|
|
[Obsolete("相关接口或字段于 2022-02-25 下线。")]
|
2021-05-28 19:23:28 +08:00
|
|
|
|
public static async Task<Models.CgibinMaterialAddMaterialResponse> ExecuteCgibinMaterialAddMaterialAsync(this WechatApiClient client, Models.CgibinMaterialAddMaterialRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
2021-07-11 00:55:24 +08:00
|
|
|
|
const string TYPE_IMAGE = "image";
|
|
|
|
|
const string TYPE_THUMB = "thumb";
|
|
|
|
|
const string TYPE_VOICE = "voice";
|
|
|
|
|
const string TYPE_VIDEO = "video";
|
|
|
|
|
|
2021-10-18 17:50:37 +08:00
|
|
|
|
if (request.FileName == null)
|
2021-05-28 19:23:28 +08:00
|
|
|
|
{
|
|
|
|
|
string ext = "";
|
2021-07-11 00:55:24 +08:00
|
|
|
|
if (TYPE_IMAGE.Equals(request.Type))
|
2021-05-28 19:23:28 +08:00
|
|
|
|
ext = ".png";
|
2021-07-11 00:55:24 +08:00
|
|
|
|
else if (TYPE_THUMB.Equals(request.Type))
|
2021-05-28 19:23:28 +08:00
|
|
|
|
ext = ".jpg";
|
2021-07-11 00:55:24 +08:00
|
|
|
|
else if (TYPE_VOICE.Equals(request.Type))
|
2021-05-28 19:23:28 +08:00
|
|
|
|
ext = ".mp3";
|
2021-07-11 00:55:24 +08:00
|
|
|
|
else if (TYPE_VIDEO.Equals(request.Type))
|
2021-05-28 19:23:28 +08:00
|
|
|
|
ext = ".mp4";
|
|
|
|
|
|
|
|
|
|
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ext;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 17:50:37 +08:00
|
|
|
|
if (request.FileContentType == null)
|
2021-05-28 19:23:28 +08:00
|
|
|
|
{
|
2021-07-11 00:55:24 +08:00
|
|
|
|
if (TYPE_IMAGE.Equals(request.Type) || TYPE_THUMB.Equals(request.Type))
|
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
|
|
|
|
|
else if (TYPE_VOICE.Equals(request.Type))
|
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForVoice(request.FileName!) ?? "audio/mp3";
|
|
|
|
|
else if (TYPE_VIDEO.Equals(request.Type))
|
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForVideo(request.FileName!) ?? "video/mp4";
|
2021-05-28 19:23:28 +08:00
|
|
|
|
else
|
|
|
|
|
request.FileContentType = "application/octet-stream";
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 00:55:24 +08:00
|
|
|
|
IFlurlRequest flurlReq = client
|
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "add_material")
|
|
|
|
|
.SetQueryParam("access_token", request.AccessToken)
|
|
|
|
|
.SetQueryParam("type", request.Type);
|
|
|
|
|
|
2021-05-28 19:23:28 +08:00
|
|
|
|
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
2022-03-11 20:07:12 +08:00
|
|
|
|
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
|
2021-07-27 00:49:38 +08:00
|
|
|
|
using var descContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request)));
|
2021-05-28 19:23:28 +08:00
|
|
|
|
using var httpContent = new MultipartFormDataContent(boundary);
|
2021-07-11 00:55:24 +08:00
|
|
|
|
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
2021-05-28 19:23:28 +08:00
|
|
|
|
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
|
|
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
2021-07-11 00:55:24 +08:00
|
|
|
|
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
2021-05-28 19:23:28 +08:00
|
|
|
|
|
2021-07-11 00:55:24 +08:00
|
|
|
|
if (TYPE_VIDEO.Equals(request.Type))
|
2021-05-28 19:23:28 +08:00
|
|
|
|
{
|
|
|
|
|
httpContent.Add(descContent, "\"description\"");
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 00:55:24 +08:00
|
|
|
|
return await client.SendRequestAsync<Models.CgibinMaterialAddMaterialResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
2021-05-28 19:23:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/get_material 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Getting_Permanent_Assets.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMaterialGetMaterialResponse> ExecuteCgibinMaterialGetMaterialAsync(this WechatApiClient client, Models.CgibinMaterialGetMaterialRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "get_material")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialGetMaterialResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/get_material 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Getting_Permanent_Assets.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-02-16 11:41:46 +08:00
|
|
|
|
[Obsolete("相关接口或字段于 2022-02-25 下线。")]
|
2021-05-28 19:23:28 +08:00
|
|
|
|
public static async Task<Models.CgibinMaterialGetMaterialAsNewsResponse> ExecuteCgibinMaterialGetMaterialAsNewsAsync(this WechatApiClient client, Models.CgibinMaterialGetMaterialAsNewsRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "get_material")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialGetMaterialAsNewsResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/get_material 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Getting_Permanent_Assets.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMaterialGetMaterialAsVideoResponse> ExecuteCgibinMaterialGetMaterialAsVideoAsync(this WechatApiClient client, Models.CgibinMaterialGetMaterialAsVideoRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "get_material")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialGetMaterialAsVideoResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/del_material 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Deleting_Permanent_Assets.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMaterialDeleteMaterialResponse> ExecuteCgibinMaterialDeleteMaterialAsync(this WechatApiClient client, Models.CgibinMaterialDeleteMaterialRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "del_material")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialDeleteMaterialResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/update_news 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Editing_Permanent_Rich_Media_Assets.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-02-16 11:41:46 +08:00
|
|
|
|
[Obsolete("相关接口或字段于 2022-02-25 下线。")]
|
2021-05-28 19:23:28 +08:00
|
|
|
|
public static async Task<Models.CgibinMaterialUpdateNewsResponse> ExecuteCgibinMaterialUpdateNewsAsync(this WechatApiClient client, Models.CgibinMaterialUpdateNewsRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "update_news")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialUpdateNewsResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [GET] /cgi-bin/material/get_materialcount 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_the_total_of_all_materials.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMaterialGetMaterialCountResponse> ExecuteCgibinMaterialGetMaterialCountAsync(this WechatApiClient client, Models.CgibinMaterialGetMaterialCountRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "material", "get_materialcount")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
2021-07-11 00:55:24 +08:00
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialGetMaterialCountResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
2021-05-28 19:23:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/material/batchget_material 接口。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_materials_list.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMaterialBatchGetMaterialResponse> ExecuteCgibinMaterialBatchGetMaterialAsync(this WechatApiClient client, Models.CgibinMaterialBatchGetMaterialRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:24 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "material", "batchget_material")
|
2021-05-28 19:23:28 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
|
|
|
|
|
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMaterialBatchGetMaterialResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|