Skip to content

Commit

Permalink
feat(microapp): 区分新旧版接口入口点
Browse files Browse the repository at this point in the history
  • Loading branch information
fudiwei committed Mar 5, 2024
1 parent 5d19a09 commit 4ae273f
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace SKIT.FlurlHttpClient.ByteDance.MicroApp
/// </summary>
public class DouyinMicroAppClient : CommonClientBase, ICommonClient
{
public readonly string _BASEURL_LEGACY;

/// <summary>
/// 获取当前客户端使用的抖音小程序凭证。
/// </summary>
Expand All @@ -37,6 +39,7 @@ internal protected DouyinMicroAppClient(DouyinMicroAppClientOptions options, Htt
if (options is null) throw new ArgumentNullException(nameof(options));

Credentials = new Settings.Credentials(options);
_BASEURL_LEGACY = options.EndpointForLegacy ?? DouyinMicroAppEndpoints.LEGACY_DEFAULT;

FlurlClient.BaseUrl = options.Endpoint ?? DouyinMicroAppEndpoints.DEFAULT;
FlurlClient.WithTimeout(options.Timeout <= 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(options.Timeout));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class DouyinMicroAppClientOptions
/// </summary>
public string Endpoint { get; set; } = DouyinMicroAppEndpoints.DEFAULT;

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

/// <summary>
/// 获取或设置抖音小程序 AppId。
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ public static class DouyinMicroAppEndpoints
/// <summary>
/// 默认域名。
/// </summary>
public const string DEFAULT = "https://developer.toutiao.com/api";
public const string DEFAULT = "https://open.douyin.com/api";

/// <summary>
/// 沙盒环境域名
/// 旧版接口默认域名
/// </summary>
public const string SANGBOX = "https://open-sandbox.douyin.com/api";
public const string LEGACY_DEFAULT = "https://developer.toutiao.com/api";

/// <summary>
/// 旧版接口沙盒环境域名。
/// </summary>
public const string LEGACY_SANGBOX = "https://open-sandbox.douyin.com/api";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,6 @@ namespace SKIT.FlurlHttpClient.ByteDance.MicroApp
{
public static class DouyinMicroAppClientExecuteAppsExtensions
{
/// <summary>
/// <para>异步调用 [POST] /apps/v2/token 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/interface-request-credential/get-access-token ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/server/interface-request-credential/get-access-token ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/interaction/develop/server/interface-request-credential/get-access-token ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsTokenV2Response> ExecuteAppsTokenV2Async(this DouyinMicroAppClient client, Models.AppsTokenV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v2", "token");

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

/// <summary>
/// <para>异步调用 [POST] /apps/v2/jscode2session 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/log-in/code-2-session ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/server/log-in/code-2-session ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/interaction/develop/server/log-in/code-2-session ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsJsCode2SessionV2Response> ExecuteAppsJsCode2SessionV2Async(this DouyinMicroAppClient client, Models.AppsJsCode2SessionV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v2", "jscode2session");

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

#region Capacity
/// <summary>
/// <para>异步调用 [POST] /apps/v1/capacity/upload_material 接口。</para>
Expand Down Expand Up @@ -1869,27 +1809,29 @@ public static class DouyinMicroAppClientExecuteAppsExtensions

#region Qrcode
/// <summary>
/// <para>异步调用 [POST] /apps/qrcode 接口。</para>
/// <para>异步调用 [POST] /apps/v1/qrcode/create 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/qr-code/create-qr-code ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/server/qr-code/create-qr-code ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/thirdparty/API/smallprogram/auth-app-manage/base-info/getqrcode ]]>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/url-and-qrcode/qrcode/create-qr-code-v2 ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsQrcodeResponse> ExecuteAppsQrcodeAsync(this DouyinMicroAppClient client, Models.AppsQrcodeRequest request, CancellationToken cancellationToken = default)
public static async Task<Models.AppsQrcodeCreateV1Response> ExecuteAppsQrcodeCreateV1Async(this DouyinMicroAppClient client, Models.AppsQrcodeCreateV1Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "qrcode");
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v1", "qrcode", "create")
.WithHeader("access-token", request.AccessToken);

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

Expand Down Expand Up @@ -2075,7 +2017,7 @@ public static class DouyinMicroAppClientExecuteAppsExtensions
}
#endregion

#region UrlLink
#region UrlSchema
/// <summary>
/// <para>异步调用 [POST] /apps/v1/url_link/generate 接口。</para>
/// <para>
Expand Down Expand Up @@ -2155,6 +2097,86 @@ public static class DouyinMicroAppClientExecuteAppsExtensions
}
#endregion

#region UrlLink
/// <summary>
/// <para>异步调用 [POST] /apps/v1/url/generate_schema 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/url-and-qrcode/schema/generate-schema-v2 ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsUrlGenerateSchemaV1Response> ExecuteAppsUrlGenerateSchemaV1Async(this DouyinMicroAppClient client, Models.AppsUrlGenerateSchemaV1Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v1", "url", "generate_schema")
.WithHeader("access-token", request.AccessToken);

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

/// <summary>
/// <para>异步调用 [POST] /apps/v1/url/query_schema 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/url-and-qrcode/schema/query-schema-v2 ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsUrlQuerySchemaV1Response> ExecuteAppsUrlQuerySchemaV1Async(this DouyinMicroAppClient client, Models.AppsUrlQuerySchemaV1Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v1", "url", "query_schema")
.WithHeader("access-token", request.AccessToken);

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

/// <summary>
/// <para>异步调用 [POST] /apps/v1/url/query_schema_quota 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/url-and-qrcode/schema/query-schema-quota-v2 ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsUrlQuerySchemaQuotaV1Response> ExecuteAppsUrlQuerySchemaQuotaV1Async(this DouyinMicroAppClient client, Models.AppsUrlQuerySchemaQuotaV1Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v1", "url", "query_schema_quota")
.WithHeader("access-token", request.AccessToken);

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

#region Video
/// <summary>
/// <para>异步调用 [POST] /apps/convert_video_id/video_id_to_open_item_id 接口。</para>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp
{
public static class DouyinMicroAppClientExecuteLegacyAppsExtensions
{
/// <summary>
/// <para>异步调用 [POST] /apps/v2/token 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/interface-request-credential/get-access-token ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/server/interface-request-credential/get-access-token ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/interaction/develop/server/interface-request-credential/get-access-token ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsTokenV2Response> ExecuteAppsTokenV2Async(this DouyinMicroAppClient client, Models.AppsTokenV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v2", "token")
.WithUrl($"{client._BASEURL_LEGACY}/apps/v2/token");

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

/// <summary>
/// <para>异步调用 [POST] /apps/v2/jscode2session 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/log-in/code-2-session ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/server/log-in/code-2-session ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/interaction/develop/server/log-in/code-2-session ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsJsCode2SessionV2Response> ExecuteAppsJsCode2SessionV2Async(this DouyinMicroAppClient client, Models.AppsJsCode2SessionV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

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

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

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v2", "jscode2session")
.WithUrl($"{client._BASEURL_LEGACY}/apps/v2/jscode2session");

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

#region Qrcode
/// <summary>
/// <para>异步调用 [POST] /apps/qrcode 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/server/qr-code/create-qr-code ]]> <br/>
/// <![CDATA[ https://developer.open-douyin.com/docs/resource/zh-CN/thirdparty/API/smallprogram/auth-app-manage/base-info/getqrcode ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AppsQrcodeResponse> ExecuteAppsQrcodeAsync(this DouyinMicroAppClient client, Models.AppsQrcodeRequest 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, "apps", "qrcode")
.WithUrl($"{client._BASEURL_LEGACY}/apps/qrcode");

return await client.SendFlurlRequestAsJsonAsync<Models.AppsQrcodeResponse>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"err_no": 0,
"err_tips": "success",
"data": {
Expand Down

0 comments on commit 4ae273f

Please sign in to comment.