Skip to content

Commit

Permalink
feat(microapp): 区分服务商平台新旧版接口入口点
Browse files Browse the repository at this point in the history
  • Loading branch information
fudiwei committed Mar 8, 2024
1 parent f0517fc commit e643ae5
Show file tree
Hide file tree
Showing 136 changed files with 1,195 additions and 899 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
/// </summary>
public class DouyinMicroAppOpenApiClient : CommonClientBase, ICommonClient
{
public readonly string _BASEURL_LEGACY;

/// <summary>
/// 获取当前客户端使用的抖音小程序凭证。
/// </summary>
Expand Down Expand Up @@ -40,6 +42,8 @@ internal protected DouyinMicroAppOpenApiClient(DouyinMicroAppOpenApiClientOption

FlurlClient.BaseUrl = options.Endpoints ?? DouyinMicroAppOpenApiEndpoints.DEFAULT;
FlurlClient.WithTimeout(options.Timeout <= 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(options.Timeout));

_BASEURL_LEGACY = options.EndpointForLegacy ?? DouyinMicroAppEndpoints.LEGACY_DEFAULT;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ public class DouyinMicroAppOpenApiClientOptions
public int Timeout { get; set; } = 30 * 1000;

/// <summary>
/// 获取或设置抖音小程序服务商平台 API 域名
/// 获取或设置抖音小程序服务商平台 API 入口点
/// <para>默认值:<see cref="DouyinMicroAppOpenApiEndpoints.DEFAULT"/></para>
/// </summary>
public string Endpoints { get; set; } = DouyinMicroAppOpenApiEndpoints.DEFAULT;

/// <summary>
/// 获取或设置抖音小程序服务商平台旧版 API 入口点。
/// <para>默认值:<see cref="DouyinMicroAppOpenApiEndpoints.LEGACY_DEFAULT"/></para>
/// </summary>
public string EndpointForLegacy { get; set; } = DouyinMicroAppOpenApiEndpoints.LEGACY_DEFAULT;

/// <summary>
/// 获取或设置抖音小程序服务商 AppId。
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
public static class DouyinMicroAppOpenApiEndpoints
{
/// <summary>
/// 服务商平台接口域名(默认)
/// 默认域名
/// </summary>
public const string DEFAULT = "https://open.microapp.bytedance.com/openapi";
public const string DEFAULT = "https://open.douyin.com/api";

/// <summary>
/// 旧版接口服务商平台接口域名。
/// </summary>
public const string LEGACY_DEFAULT = "https://open.microapp.bytedance.com/openapi";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
{
public static class DouyinMicroAppOpenApiClientExecuteLegacyAuthExtensions
{
/// <summary>
/// <para>异步调用 [GET] /v1/auth/tp/token 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://partner.open-douyin.com/docs/resource/zh-CN/thirdparty/API/smallprogram/authorization/componentaccesstoken ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.OpenApiAuthThirdPartyTokenV1Response> ExecuteOpenApiAuthThirdPartyTokenV1Async(this DouyinMicroAppOpenApiClient client, Models.OpenApiAuthThirdPartyTokenV1Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

if (request.ComponentAppSecret is null)
request.ComponentAppSecret = client.Credentials.ComponentAppSecret;

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Get, "v1", "auth", "tp", "token")
.WithUrl(url => new Url(client._BASEURL_LEGACY).AppendPathSegments("v1", "auth", "tp", "token"))
.SetQueryParam("component_appid", request.ComponentAppId)
.SetQueryParam("component_appsecret", request.ComponentAppSecret)
.SetQueryParam("component_ticket", request.ComponentTicket);

return await client.SendFlurlRequestAsJsonAsync<Models.OpenApiAuthThirdPartyTokenV1Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// <para>异步调用 [POST] /v2/auth/pre_auth_code 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://partner.open-douyin.com/docs/resource/zh-CN/thirdparty/API/smallprogram/authorization/preauthcode ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.OpenApiAuthPreAuthCodeV2Response> ExecuteOpenApiAuthPreAuthCodeV2Async(this DouyinMicroAppOpenApiClient client, Models.OpenApiAuthPreAuthCodeV2Request 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
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "auth", "pre_auth_code")
.WithUrl(url => new Url(client._BASEURL_LEGACY).AppendPathSegments("v2", "auth", "pre_auth_code"))
.SetQueryParam("component_appid", request.ComponentAppId)
.SetQueryParam("component_access_token", request.ComponentAccessToken);

return await client.SendFlurlRequestAsJsonAsync<Models.OpenApiAuthPreAuthCodeV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// <para>异步调用 [POST] /v2/auth/gen_link 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://partner.open-douyin.com/docs/resource/zh-CN/thirdparty/API/smallprogram/authorization/genlink ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.OpenApiAuthGenerateLinkV2Response> ExecuteOpenApiAuthGenerateLinkV2Async(this DouyinMicroAppOpenApiClient client, Models.OpenApiAuthGenerateLinkV2Request 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
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "auth", "gen_link")
.WithUrl(url => new Url(client._BASEURL_LEGACY).AppendPathSegments("v2", "auth", "gen_link"))
.SetQueryParam("component_appid", request.ComponentAppId)
.SetQueryParam("component_access_token", request.ComponentAccessToken);

return await client.SendFlurlRequestAsJsonAsync<Models.OpenApiAuthGenerateLinkV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// <para>异步调用 [POST] /v1/auth/retrieve 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://partner.open-douyin.com/docs/resource/zh-CN/thirdparty/API/smallprogram/authorization/findauthorizationcode ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.OpenApiAuthRetrieveV1Response> ExecuteOpenApiAuthRetrieveV1Async(this DouyinMicroAppOpenApiClient client, Models.OpenApiAuthRetrieveV1Request 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
.CreateFlurlRequest(request, HttpMethod.Post, "v1", "auth", "retrieve")
.WithUrl(url => new Url(client._BASEURL_LEGACY).AppendPathSegments("v1", "auth", "retrieve"))
.SetQueryParam("component_appid", request.ComponentAppId)
.SetQueryParam("component_access_token", request.ComponentAccessToken)
.SetQueryParam("authorization_appid", request.AuthorizerAppId);

return await client.SendFlurlRequestAsJsonAsync<Models.OpenApiAuthRetrieveV1Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}
Loading

0 comments on commit e643ae5

Please sign in to comment.