mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-17 01:46:20 +08:00
feat(wxads): 新增图片管理模块相关 API 封装
This commit is contained in:
parent
17603584b2
commit
f4d9a54abe
@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Flurl;
|
||||
using Flurl.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
{
|
||||
public static class WechatAdsClientExecuteImagesExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>异步调用 [POST] /images/add 接口。</para>
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<Models.ImagesAddResponse> ExecuteImagesAddAsync(this WechatAdsClient client, Models.ImagesAddRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (client is null) throw new ArgumentNullException(nameof(client));
|
||||
if (request is null) throw new ArgumentNullException(nameof(request));
|
||||
|
||||
if (string.IsNullOrEmpty(request.FileName))
|
||||
{
|
||||
request.FileName = Guid.NewGuid().ToString("N").ToLower();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(request.FileContentType))
|
||||
{
|
||||
if (request.FileName!.EndsWith(".swf", StringComparison.OrdinalIgnoreCase))
|
||||
request.FileContentType = "application/x-shockwave-flash";
|
||||
else if (request.FileName!.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
|
||||
request.FileContentType = "image/jpeg";
|
||||
else if (request.FileName!.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
|
||||
request.FileContentType = "image/jpeg";
|
||||
else if (request.FileName!.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
|
||||
request.FileContentType = "image/gif";
|
||||
else
|
||||
request.FileContentType = "image/png";
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(request.FileHash))
|
||||
{
|
||||
request.FileHash = Security.MD5Utility.Hash(request.FileBytes ?? new byte[0]);
|
||||
}
|
||||
|
||||
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
||||
using var fileContent = new ByteArrayContent(request.FileBytes ?? new byte[0]);
|
||||
using var httpContent = new MultipartFormDataContent(boundary);
|
||||
httpContent.Add(fileContent, "\"media\"", "\"" + HttpUtility.UrlEncode(request.FileName) + "\"");
|
||||
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
||||
fileContent.Headers.ContentLength = request.FileBytes?.Length ?? 0;
|
||||
IFlurlRequest flurlReq = client
|
||||
.CreateRequest(HttpMethod.Post, "images", "add")
|
||||
.SetOptions(request)
|
||||
.SetQueryParam("access_token", request.AccessToken);
|
||||
|
||||
return await client.SendRequestAsync<Models.ImagesAddResponse>(flurlReq, content: httpContent, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>异步调用 [GET] /images/get 接口。</para>
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<Models.ImagesGetResponse> ExecuteImagesGetAsync(this WechatAdsClient client, Models.ImagesGetRequest 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
|
||||
.CreateRequest(HttpMethod.Get, "images", "get")
|
||||
.SetOptions(request)
|
||||
.SetQueryParam("access_token", request.AccessToken);
|
||||
|
||||
if (!string.IsNullOrEmpty(request.ImageId))
|
||||
flurlReq.SetQueryParam("image_id", request.ImageId);
|
||||
|
||||
if (request.Filters != null && request.Filters.Any())
|
||||
flurlReq.SetQueryParam("filtering", client.JsonSerializer.Serialize(request.Filters));
|
||||
|
||||
if (request.PageSize.HasValue)
|
||||
flurlReq.SetQueryParam("page_size", request.PageSize.Value);
|
||||
|
||||
if (request.Page.HasValue)
|
||||
flurlReq.SetQueryParam("page", request.Page.Value);
|
||||
|
||||
return await client.SendRequestAsync<Models.ImagesGetResponse>(flurlReq, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Models.Abstractions
|
||||
{
|
||||
public sealed class CommonFilter
|
||||
{
|
||||
public const string OPERATOR_EQUALS = "EQUALS";
|
||||
public const string OPERATOR_CONTAINS = "CONTAINS";
|
||||
public const string OPERATOR_LESS = "LESS";
|
||||
public const string OPERATOR_LESSEQUALS = "LESS_EQUALS";
|
||||
public const string OPERATOR_GREATER = "GREATER";
|
||||
public const string OPERATOR_GREATEREQUALS = "GREATER_EQUALS";
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置过滤字段。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("field")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("field")]
|
||||
public string Field { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置操作符。
|
||||
/// <para>默认值:EQUALS</para>
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("operator")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("operator")]
|
||||
public string Operator { get; set; } = OPERATOR_EQUALS;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置字段取值。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("values")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("values")]
|
||||
public string[] Values { get; set; } = new string[0];
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Models.Abstractions
|
||||
{
|
||||
public sealed class CommonPagination
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置总数量。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("total_number")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("total_number")]
|
||||
public int TotalNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置总页数。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("total_page")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("total_page")]
|
||||
public int TotalPage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置页大小。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("page_size")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("page_size")]
|
||||
public int PageSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置页码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("page")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("page")]
|
||||
public int Page { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>表示 [POST] /images/add 接口的请求。</para>
|
||||
/// </summary>
|
||||
public class ImagesAddRequest : WechatAdsRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置图片文件字节数组。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public byte[] FileBytes { get; set; } = new byte[0];
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片文件名。如果不指定将由系统自动生成。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public string? FileName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片文件 Conent-Type。如果不指定将由系统自动生成。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public string? FileContentType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片文件的哈希值。如果不指定将由系统自动生成。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public string? FileHash { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>表示 [POST] /images/add 接口的响应。</para>
|
||||
/// </summary>
|
||||
public class ImagesAddResponse : WechatAdsResponse
|
||||
{
|
||||
public static class Types
|
||||
{
|
||||
public class Data : ImagesGetResponse.Types.Data.Types.Image
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置返回数据。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("data")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("data")]
|
||||
public Types.Data Data { get; set; } = default!;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>表示 [GET] /images/get 接口的请求。</para>
|
||||
/// </summary>
|
||||
public class ImagesGetRequest : WechatAdsRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置图片 ID。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public string ImageId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置过滤条件。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public IList<Abstractions.CommonFilter>? Filters { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置页大小。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int? PageSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置页码。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public int? Page { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>表示 [GET] /images/get 接口的响应。</para>
|
||||
/// </summary>
|
||||
public class ImagesGetResponse : WechatAdsResponse
|
||||
{
|
||||
public static class Types
|
||||
{
|
||||
public class Data
|
||||
{
|
||||
public static class Types
|
||||
{
|
||||
public class Image
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置图片 ID。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("image_id")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("image_id")]
|
||||
public string ImageId { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片类型。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("type")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("type")]
|
||||
public string Type { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片宽度(单位:像素)。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("width")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("width")]
|
||||
public int Width { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片高度(单位:像素)。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("height")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("height")]
|
||||
public int Height { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片文件大小(单位:字节)。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("file_size")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("file_size")]
|
||||
public int FileSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片文件的哈希值。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("signature")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("signature")]
|
||||
public string FileHash { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片预览地址 URL。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("preview_url")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("preview_url")]
|
||||
public string PreviewUrl { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图片列表。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("list")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("list")]
|
||||
public Types.Image[] ImageList { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置分页信息。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("page_info")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("page_info")]
|
||||
public Abstractions.CommonPagination Pagination { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置返回数据。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("data")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("data")]
|
||||
public Types.Data Data { get; set; } = default!;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user