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

.NET v3: SNS fix List the subscribes of a topic example. #5287

Merged
merged 3 commits into from
Aug 22, 2023
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
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.2.2" />
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.7.2.21" />
<PackageReference Include="AWSSDK.Core" Version="3.7.201" />
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.7.200.20" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,53 @@ public static async Task Main()
{
IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient();

var subscriptions = await GetSubscriptionsListAsync(client);
Console.WriteLine("Enter a topic ARN to list subscriptions for a specific topic, " +
"or press Enter to list subscriptions for all topics.");
var topicArn = Console.ReadLine();
Console.WriteLine();

var subscriptions = await GetSubscriptionsListAsync(client, topicArn);

DisplaySubscriptionList(subscriptions);
}

/// <summary>
/// Gets a list of the existing Amazon SNS subscriptions.
/// Gets a list of the existing Amazon SNS subscriptions, optionally by specifying a topic ARN.
/// </summary>
/// <param name="client">The initialized Amazon SNS client object used
/// to obtain the list of subscriptions.</param>
/// <returns>A List containing information about each subscription.</returns>
public static async Task<List<Subscription>> GetSubscriptionsListAsync(IAmazonSimpleNotificationService client)
/// <param name="topicArn">The optional ARN of a specific topic. Defaults to null.</param>
/// <returns>A list containing information about each subscription.</returns>
public static async Task<List<Subscription>> GetSubscriptionsListAsync(IAmazonSimpleNotificationService client, string topicArn = null)
{
var response = await client.ListSubscriptionsAsync();
var results = new List<Subscription>();

if (!string.IsNullOrEmpty(topicArn))
{
var paginateByTopic = client.Paginators.ListSubscriptionsByTopic(
new ListSubscriptionsByTopicRequest()
{
TopicArn = topicArn,
});

return response.Subscriptions;
// Get the entire list using the paginator.
await foreach (var subscription in paginateByTopic.Subscriptions)
{
results.Add(subscription);
}
}
else
{
var paginateAllSubscriptions = client.Paginators.ListSubscriptions(new ListSubscriptionsRequest());

// Get the entire list using the paginator.
await foreach (var subscription in paginateAllSubscriptions.Subscriptions)
{
results.Add(subscription);
}
}

return results;
}

/// <summary>
Expand All @@ -56,5 +87,6 @@ public static void DisplaySubscriptionList(List<Subscription> subscriptionList)
}
}
}

// snippet-end:[SNS.dotnetv3.ListSNSSubscriptionsExample]
}