Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;

namespace TwitchLib.Api.Helix.Models.Subscriptions;

/// <summary>
/// A class to represent the request query data for a <see href="https://dev.twitch.tv/docs/api/reference/#get-broadcaster-subscriptions">Get Broadcaster Subscriptions</see> request.
/// </summary>
public class GetBroadcasterSubscriptionsRequest
{
/// <summary>
/// The broadcaster’s ID. This ID must match the user ID in the access token.
/// </summary>
public string BroadcasterId { get; set; } = null!;

/// <summary>
/// Filters the list to include only the specified subscribers.
/// </summary>
public List<string>? UserIds { get; set; }

/// <summary>
/// The maximum number of items to return per page in the response.
/// The minimum page size is 1 item per page and the maximum is 100 items per page.
/// The default is 20.
/// </summary>
public int? First { get; set; }

/// <summary>
/// The cursor used to get the next page of results.
/// Do not specify if you set the <see cref="UserIds"/> query parameter.
/// The Pagination object in the response contains the cursor’s value.
/// </summary>
public string? After { get; set; }

/// <summary>
/// The cursor used to get the previous page of results.
/// Do not specify if you set the <see cref="UserIds"/> query parameter.
/// The Pagination object in the response contains the cursor’s value.
/// </summary>
public string? Before { get; set; }

public virtual List<KeyValuePair<string, string>> ToParams()
{
var getParams = new List<KeyValuePair<string, string>>
{
new("broadcaster_id", BroadcasterId),
new("first", First?.ToString() ?? "20")
};

if (UserIds?.Count > 0)
{
foreach (var userId in UserIds)
{
getParams.Add(new("user_id", userId));
}
}

if (!string.IsNullOrEmpty(After))
getParams.Add(new("after", After));

if (!string.IsNullOrEmpty(Before))
getParams.Add(new("before", Before));

return getParams;
}
}
28 changes: 27 additions & 1 deletion TwitchLib.Api.Helix/Subscriptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -92,6 +93,7 @@ public Task<GetUserSubscriptionsResponse> GetUserSubscriptionsAsync(string broad
/// <param name="accessToken">optional access token to override the use of the stored one in the TwitchAPI instance</param>
/// <returns cref="GetBroadcasterSubscriptionsResponse"></returns>
/// <exception cref="BadParameterException"></exception>
[Obsolete("Use GetEventSubSubscriptionsAsync(GetBroadcasterSubscriptionsRequest, string) instead")]
public Task<GetBroadcasterSubscriptionsResponse> GetBroadcasterSubscriptionsAsync(string broadcasterId, int first = 20, string after = null, string accessToken = null)
{
if (string.IsNullOrWhiteSpace(broadcasterId))
Expand All @@ -106,8 +108,32 @@ public Task<GetBroadcasterSubscriptionsResponse> GetBroadcasterSubscriptionsAsyn
new("first", first.ToString())
};

if (!string.IsNullOrWhiteSpace(after))
if (!string.IsNullOrWhiteSpace(after))
getParams.Add(new KeyValuePair<string, string>("after", after));

return TwitchGetGenericAsync<GetBroadcasterSubscriptionsResponse>("/subscriptions", ApiVersion.Helix, getParams, accessToken);
}

/// <summary>
/// Gets a list of users that subscribe to the specified broadcaster.
/// <para>Required scope: channel:read:subscriptions</para>
/// </summary>
/// <param name="request">Request parameters for the call.</param>
/// <param name="accessToken">optional access token to override the use of the stored one in the TwitchAPI instance</param>
/// <returns cref="GetBroadcasterSubscriptionsResponse"></returns>
/// <exception cref="BadParameterException"></exception>
public Task<GetBroadcasterSubscriptionsResponse> GetBroadcasterSubscriptionsAsync(GetBroadcasterSubscriptionsRequest request, string accessToken = null)
{
if (string.IsNullOrWhiteSpace(request.BroadcasterId))
throw new BadParameterException("request.BroadcasterId must be set");

if (request.First > 100)
throw new BadParameterException("request.First must be 100 or less");

if (request.UserIds?.Count > 100)
throw new BadParameterException("Count of request.UserIds must be 100 or less");

var getParams = request.ToParams();

return TwitchGetGenericAsync<GetBroadcasterSubscriptionsResponse>("/subscriptions", ApiVersion.Helix, getParams, accessToken);
}
Expand Down
Loading