Skip to content

Commit

Permalink
Adding option for TopicARN to ListSubscriptions action example.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlhagerm committed Aug 18, 2023
1 parent e4a84a7 commit 0b44577
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
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 specific 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]
}

0 comments on commit 0b44577

Please sign in to comment.