Ejemplo n.º 1
0
        public virtual (bool, string TopicArn) Validate(string topicArn)
        {
            //List topics does not work across accounts - GetTopicAttributesRequest works within the region
            //List Topics is rate limited to 30 ListTopic transactions per second, and can be rate limited
            //So where we can, we validate a topic using GetTopicAttributesRequest

            bool exists = false;

            try
            {
                var topicAttributes = _snsClient.GetTopicAttributesAsync(
                    new GetTopicAttributesRequest(topicArn)
                    ).GetAwaiter().GetResult();

                exists = ((topicAttributes.HttpStatusCode == HttpStatusCode.OK) && (topicAttributes.Attributes["TopicArn"] == topicArn));
            }
            catch (InternalErrorException)
            {
                exists = false;
            }
            catch (NotFoundException)
            {
                exists = false;
            }
            catch (AuthorizationErrorException)
            {
                exists = false;
            }

            return(exists, topicArn);
        }
Ejemplo n.º 2
0
 private bool ValidateTopicByArn(string topicArn)
 {
     using (var snsClient = new AmazonSimpleNotificationServiceClient(_awsConnection.Credentials, _awsConnection.Region))
     {
         var response = snsClient.GetTopicAttributesAsync(new GetTopicAttributesRequest(topicArn))
                        .GetAwaiter()
                        .GetResult();
         return((response.HttpStatusCode == HttpStatusCode.OK) && (response.Attributes["TopicArn"] == topicArn));
     }
 }
Ejemplo n.º 3
0
        public async Task GetAWSTopics(AWSCredentials credentials)
        {
            var client   = new AmazonSimpleNotificationServiceClient(credentials.AccessKey, credentials.SecretKey, credentials.Region);
            var request  = new ListTopicsRequest();
            var response = new ListTopicsResponse();

            do
            {
                response = await client.ListTopicsAsync(request);

                foreach (var topic in response.Topics)
                {
                    Console.WriteLine("Topic: {0}", topic.TopicArn);
                    await SendAWSNotification(credentials, topic.TopicArn, String.Format("Message from topic: {0}", topic.TopicArn));

                    var subs = await client.ListSubscriptionsByTopicAsync(
                        new ListSubscriptionsByTopicRequest
                    {
                        TopicArn = topic.TopicArn
                    });

                    var ss = subs.Subscriptions;

                    if (ss.Any())
                    {
                        Console.WriteLine("  Subscriptions:");
                        foreach (var sub in ss)
                        {
                            Console.WriteLine("    {0}", sub.SubscriptionArn);
                        }
                    }

                    var attrs = await client.GetTopicAttributesAsync(
                        new GetTopicAttributesRequest
                    {
                        TopicArn = topic.TopicArn
                    });

                    if (attrs.Attributes.Any())
                    {
                        Console.WriteLine("  Attributes:");

                        foreach (var attr in attrs.Attributes)
                        {
                            Console.WriteLine("    {0} = {1}", attr.Key, attr.Value);
                        }
                    }
                    Console.WriteLine();
                }
                request.NextToken = response.NextToken;
            } while (!string.IsNullOrEmpty(response.NextToken));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Get()
        {
            var topicList = new List <Dictionary <string, string> >();
            Dictionary <string, string> dictAttributes = null;

            using (_SnsClient = new AmazonSimpleNotificationServiceClient(_AwsCredentials, RegionEndpoint.USEast2))
            {
                try
                {
                    _SnsRequest = new ListTopicsRequest();
                    do
                    {
                        _SnsResponse = await _SnsClient.ListTopicsAsync(_SnsRequest);

                        foreach (var topic in _SnsResponse.Topics)
                        {
                            // Get topic attributes
                            var topicAttributes = await _SnsClient.GetTopicAttributesAsync(request : new GetTopicAttributesRequest
                            {
                                TopicArn = topic.TopicArn
                            });

                            // this is see attributes of topic
                            if (topicAttributes.Attributes.Count > 0)
                            {
                                dictAttributes = new Dictionary <string, string>();
                                foreach (var topicAttribute in topicAttributes.Attributes)
                                {
                                    dictAttributes.Add(topicAttribute.Key, topicAttribute.Value.ToString());
                                }
                                topicList.Add(dictAttributes);
                            }
                        }
                        _SnsRequest.NextToken = _SnsResponse.NextToken;
                    } while (_SnsResponse.NextToken != null);
                }
                catch (AmazonSimpleNotificationServiceException ex)
                {
                    return(Content(HandleSNSError(ex)));
                }
            }

            return(Ok(JsonConvert.SerializeObject(topicList)));
        }