Skip to content

Commit 0b44577

Browse files
committed
Adding option for TopicARN to ListSubscriptions action example.
1 parent e4a84a7 commit 0b44577

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

dotnetv3/SNS/ListSNSSubscriptionsExample/ListSNSSubscriptionsExample/ListSNSSubscriptionsExample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AWSSDK.Core" Version="3.7.2.2" />
10-
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.7.2.21" />
9+
<PackageReference Include="AWSSDK.Core" Version="3.7.201" />
10+
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.7.200.20" />
1111
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
<PrivateAssets>all</PrivateAssets>

dotnetv3/SNS/ListSNSSubscriptionsExample/ListSNSSubscriptionsExample/ListSubscriptions.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,53 @@ public static async Task Main()
2020
{
2121
IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient();
2222

23-
var subscriptions = await GetSubscriptionsListAsync(client);
23+
Console.WriteLine("Enter a topic ARN to list subscriptions for a specific topic, " +
24+
"or press Enter to list subscriptions for all topics.");
25+
var topicArn = Console.ReadLine();
26+
Console.WriteLine();
27+
28+
var subscriptions = await GetSubscriptionsListAsync(client, topicArn);
2429

2530
DisplaySubscriptionList(subscriptions);
2631
}
2732

2833
/// <summary>
29-
/// Gets a list of the existing Amazon SNS subscriptions.
34+
/// Gets a list of the existing Amazon SNS subscriptions, optionally by specifying a specific topic ARN.
3035
/// </summary>
3136
/// <param name="client">The initialized Amazon SNS client object used
3237
/// to obtain the list of subscriptions.</param>
33-
/// <returns>A List containing information about each subscription.</returns>
34-
public static async Task<List<Subscription>> GetSubscriptionsListAsync(IAmazonSimpleNotificationService client)
38+
/// <param name="topicArn">The optional ARN of a specific topic. Defaults to null.</param>
39+
/// <returns>A list containing information about each subscription.</returns>
40+
public static async Task<List<Subscription>> GetSubscriptionsListAsync(IAmazonSimpleNotificationService client, string topicArn = null)
3541
{
36-
var response = await client.ListSubscriptionsAsync();
42+
var results = new List<Subscription>();
43+
44+
if (!string.IsNullOrEmpty(topicArn))
45+
{
46+
var paginateByTopic = client.Paginators.ListSubscriptionsByTopic(
47+
new ListSubscriptionsByTopicRequest()
48+
{
49+
TopicArn = topicArn,
50+
});
3751

38-
return response.Subscriptions;
52+
// Get the entire list using the paginator.
53+
await foreach (var subscription in paginateByTopic.Subscriptions)
54+
{
55+
results.Add(subscription);
56+
}
57+
}
58+
else
59+
{
60+
var paginateAllSubscriptions = client.Paginators.ListSubscriptions(new ListSubscriptionsRequest());
61+
62+
// Get the entire list using the paginator.
63+
await foreach (var subscription in paginateAllSubscriptions.Subscriptions)
64+
{
65+
results.Add(subscription);
66+
}
67+
}
68+
69+
return results;
3970
}
4071

4172
/// <summary>
@@ -56,5 +87,6 @@ public static void DisplaySubscriptionList(List<Subscription> subscriptionList)
5687
}
5788
}
5889
}
90+
5991
// snippet-end:[SNS.dotnetv3.ListSNSSubscriptionsExample]
6092
}

0 commit comments

Comments
 (0)