Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify if a topic exists based on topic name

I'm trying to verify if a topic exists based on topic name.

Do you know if this is possible?

For example I want to verify if topic with name "test" already exist.

Below is what I'm trying but doesn't work because topicsList contains topicArns and not topicNames...

topics = sns.get_all_topics()   
topicsList = topics['ListTopicsResponse']['ListTopicsResult'['Topics']

if "test" in topicsList:
    print("true")
like image 923
techman Avatar asked Dec 19 '22 04:12

techman


1 Answers

This code will work if you have more than 100 topics

def get_topic(token=None):
    topics = self.sns.get_all_topics(token)
    next_token = topics['ListTopicsResponse']['ListTopicsResult']['NextToken']
    topic_list = topics['ListTopicsResponse']['ListTopicsResult']['Topics']
    for topic in topic_list:
        if "your_topic_name" in topic['TopicArn'].split(':')[5]:
            return topic['TopicArn']
    else:
        if next_token:
            get_topic(next_token)
        else:
            return None
like image 153
RustyShackleford Avatar answered Dec 21 '22 18:12

RustyShackleford