Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement app subscriptions #111

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Disqord.Core/Discord/Limits/Rest/Discord.Limits.Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static class Rest
/// Represents the page size for fetching event users.
/// </summary>
public const int FetchGuildEventUsersPageSize = 100;

/// <summary>
/// Represents the page size for fetching entitlements.
/// </summary>
public const int FetchEntitlementsPageSize = 100;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Disqord.Models;
using Qommon;

namespace Disqord.Rest.Api;

Expand All @@ -15,10 +17,38 @@ public static Task<SkuJsonModel[]> FetchSkusAsync(this IRestApiClient client,
}

public static Task<EntitlementJsonModel[]> FetchEntitlementsAsync(this IRestApiClient client,
Snowflake applicationId,
Snowflake applicationId, int limit = Discord.Limits.Rest.FetchEntitlementsPageSize,
Snowflake? userId = null, Snowflake[]? skuIds = null,
Snowflake? beforeId = null, Snowflake? afterId = null,
Snowflake? guildId = null, bool excludeEnded = false,
Comment on lines +20 to +23
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if before and after here are mutually exclusive since it's not documented and I have no way to test it. Hence, I'm not using fetch direction here, would require someone with a verified bot to test it.

Ideally userId and guildId should also be mutually exclusive and we would use ownerId and ownerType, similar to the json parameters used by the Create Test Entitlement endpoint. However, once again there is no documentation specifying such, so I've let it be as is.

IRestRequestOptions? options = null, CancellationToken cancellationToken = default)
{
var route = Format(Route.Montetization.GetEntitlements, applicationId);
Guard.IsBetweenOrEqualTo(limit, 1, Discord.Limits.Rest.FetchEntitlementsPageSize);

var queryParameters = new Dictionary<string, object>(7)
{
["limit"] = limit
};

if (userId != null)
queryParameters["user_id"] = userId;

if (skuIds != null)
queryParameters["sku_ids"] = skuIds;

if (beforeId != null)
queryParameters["before"] = beforeId;

if (afterId != null)
queryParameters["after"] = afterId;

if (guildId != null)
queryParameters["guild_id"] = guildId;

if (excludeEnded)
queryParameters["exclude_ended"] = excludeEnded;

var route = Format(Route.Montetization.GetEntitlements, queryParameters, applicationId);
return client.ExecuteAsync<EntitlementJsonModel[]>(route, null, options, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public static Task<IReadOnlyList<ISku>> FetchSkusAsync(this IApplication applica
}

public static Task<IReadOnlyList<IEntitlement>> FetchEntitlementsAsync(this IApplication application,
int limit = Discord.Limits.Rest.FetchEntitlementsPageSize,
Snowflake? userId = null, IEnumerable<Snowflake>? skuIds = null,
Snowflake? beforeId = null, Snowflake? afterId = null,
Snowflake? guildId = null, bool excludeEnded = false,
IRestRequestOptions? options = null, CancellationToken cancellationToken = default)
{
var client = application.GetRestClient();
return client.FetchEntitlementsAsync(application.Id, options, cancellationToken);
return client.FetchEntitlementsAsync(application.Id, limit, userId, skuIds, beforeId, afterId, guildId, excludeEnded, options, cancellationToken);
}

public static Task<IEntitlement> CreateTestEntitlementAsync(this IApplication application,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Disqord.Rest.Api;
Expand All @@ -17,10 +18,13 @@ public static async Task<IReadOnlyList<ISku>> FetchSkusAsync(this IRestClient cl
}

public static async Task<IReadOnlyList<IEntitlement>> FetchEntitlementsAsync(this IRestClient client,
Snowflake applicationId,
Snowflake applicationId, int limit = Discord.Limits.Rest.FetchEntitlementsPageSize,
Snowflake? userId = null, IEnumerable<Snowflake>? skuIds = null,
Snowflake? beforeId = null, Snowflake? afterId = null,
Snowflake? guildId = null, bool excludeEnded = false,
IRestRequestOptions? options = null, CancellationToken cancellationToken = default)
{
var models = await client.ApiClient.FetchEntitlementsAsync(applicationId, options, cancellationToken).ConfigureAwait(false);
var models = await client.ApiClient.FetchEntitlementsAsync(applicationId, limit, userId, skuIds?.ToArray(), beforeId, afterId, guildId, excludeEnded, options, cancellationToken).ConfigureAwait(false);
return models.ToReadOnlyList(client, (model, client) => new TransientEntitlement(client, model));
}

Expand Down
Loading