1 Star 0 Fork 340

马小帅 / DotNetCore.SKIT.FlurlHttpClient.Wechat

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Advanced_Extensions.md 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
RHQYZ 提交于 2021-11-25 21:15 . docs: 完善文档

如何扩展额外的 API?


如果有某些接口本库尚未支持,你可按照下面的示例自行扩展:

/* 继承 WechatApiRequest 实现自定义请求类 */
public class MyFakeRequest : WechatApiRequest
{
    [Newtonsoft.Json.JsonProperty("my_fake_props")]
    [System.Text.Json.Serialization.JsonPropertyName("my_fake_props")]
    public string MyFakeProps { get; set; }
}

/* 继承 WechatApiResponse 实现自定义响应类 */
public class MyFakeResponse : WechatApiResponse
{
    [Newtonsoft.Json.JsonProperty("my_fake_props")]
    [System.Text.Json.Serialization.JsonPropertyName("my_fake_props")]
    public string MyFakeProps { get; set; }
}

/* 扩展 WechatApiClient 方法 */
public static class MyFakeClientExtensions
{
    public static async Task<MyFakeResponse> ExecuteMyFakeAsync(this WechatApiClient client, MyFakeRequest 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(request, HttpMethod.Post, "my-fake-url")
            .SetQueryParam("access_token", request.AccessToken);

        return await client.SendRequestWithJsonAsync<MyFakeResponse>(flurlReq, request, cancellationToken);
    }
}

同样地,你也可自行扩展回调通知事件模型:

/* 继承 WechatApiEvent 实现自定义的 JSON 格式的回调通知事件 */
public class MyFakeEvent : WechatApiEvent, WechatApiEvent.Serialization.IJsonSerializable
{
    [Newtonsoft.Json.JsonProperty("my_fake_props")]
    [System.Text.Json.Serialization.JsonPropertyName("my_fake_props")]
    public string MyFakeProps { get; set; }
}

/* 继承 WechatApiEvent 实现自定义的 XML 格式的回调通知事件 */
public class MyFakeEvent : WechatApiEvent, WechatApiEvent.Serialization.IXmlSerializable
{
    [System.Xml.Serialization.XmlElement("my_fake_props")]
    public string MyFakeProps { get; set; }
}
C#
1
https://gitee.com/weikety/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
git@gitee.com:weikety/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
weikety
DotNetCore.SKIT.FlurlHttpClient.Wechat
DotNetCore.SKIT.FlurlHttpClient.Wechat
main

搜索帮助