|
65 | 65 | else: |
66 | 66 | print("No contacts available for pagination example") |
67 | 67 |
|
| 68 | +print("\n--- Contact Topics Examples ---") |
| 69 | + |
| 70 | +print("\nCreating a topic...") |
| 71 | +topic_create_params: resend.Topics.CreateParams = { |
| 72 | + "name": "Product Updates", |
| 73 | + "default_subscription": "opt_in", |
| 74 | + "description": "Latest product updates and features", |
| 75 | +} |
| 76 | +created_topic: resend.Topics.CreateTopicResponse = resend.Topics.create( |
| 77 | + topic_create_params |
| 78 | +) |
| 79 | +print(f"Created topic with ID: {created_topic['id']}") |
| 80 | + |
| 81 | +topic_create_params_2: resend.Topics.CreateParams = { |
| 82 | + "name": "Newsletter", |
| 83 | + "default_subscription": "opt_out", |
| 84 | + "description": "Weekly newsletter", |
| 85 | +} |
| 86 | +created_topic_2: resend.Topics.CreateTopicResponse = resend.Topics.create( |
| 87 | + topic_create_params_2 |
| 88 | +) |
| 89 | +print(f"Created topic with ID: {created_topic_2['id']}") |
| 90 | + |
| 91 | +print("\nListing topics for contact by ID...") |
| 92 | +topics_response: resend.ContactsTopics.ListResponse = resend.Contacts.Topics.list( |
| 93 | + contact_id=contact["id"] |
| 94 | +) |
| 95 | +print(f"Found {len(topics_response['data'])} topics for contact") |
| 96 | +for topic in topics_response["data"]: |
| 97 | + print(f" - {topic['name']}: {topic['subscription']}") |
| 98 | + |
| 99 | +print("\nListing topics for contact by email...") |
| 100 | +topics_by_email: resend.ContactsTopics.ListResponse = resend.Contacts.Topics.list( |
| 101 | + email="sw@exmple.com" |
| 102 | +) |
| 103 | +print(f"Found {len(topics_by_email['data'])} topics for contact") |
| 104 | + |
| 105 | +# Update topic subscriptions for a contact by ID |
| 106 | +print("\nUpdating topic subscriptions by contact ID...") |
| 107 | +update_topics_params: resend.ContactsTopics.UpdateParams = { |
| 108 | + "id": contact["id"], |
| 109 | + "topics": [ |
| 110 | + {"id": created_topic["id"], "subscription": "opt_in"}, |
| 111 | + {"id": created_topic_2["id"], "subscription": "opt_out"}, |
| 112 | + ], |
| 113 | +} |
| 114 | +update_topics_response: resend.ContactsTopics.UpdateResponse = ( |
| 115 | + resend.Contacts.Topics.update(update_topics_params) |
| 116 | +) |
| 117 | +print(f"Updated topics for contact: {update_topics_response['id']}") |
| 118 | + |
| 119 | +# Update topic subscriptions for a contact by email |
| 120 | +print("\nUpdating topic subscriptions by contact email...") |
| 121 | +update_topics_by_email: resend.ContactsTopics.UpdateParams = { |
| 122 | + "email": "sw@exmple.com", |
| 123 | + "topics": [ |
| 124 | + {"id": created_topic["id"], "subscription": "opt_in"}, |
| 125 | + {"id": created_topic_2["id"], "subscription": "opt_in"}, |
| 126 | + ], |
| 127 | +} |
| 128 | +update_by_email_response: resend.ContactsTopics.UpdateResponse = ( |
| 129 | + resend.Contacts.Topics.update(update_topics_by_email) |
| 130 | +) |
| 131 | +print(f"Updated topics for contact by email: {update_by_email_response['id']}") |
| 132 | + |
| 133 | +# List topics again to see the updates |
| 134 | +print("\nListing topics after updates...") |
| 135 | +updated_topics: resend.ContactsTopics.ListResponse = resend.Contacts.Topics.list( |
| 136 | + contact_id=contact["id"] |
| 137 | +) |
| 138 | +for topic in updated_topics["data"]: |
| 139 | + print(f" - {topic['name']}: {topic['subscription']}") |
| 140 | + |
| 141 | +# Clean up: remove the topics we created |
| 142 | +print("\nCleaning up topics...") |
| 143 | +resend.Topics.remove(created_topic["id"]) |
| 144 | +resend.Topics.remove(created_topic_2["id"]) |
| 145 | +print("Topics removed") |
| 146 | + |
68 | 147 | # remove by email |
69 | 148 | rmed: resend.Contacts.RemoveContactResponse = resend.Contacts.remove( |
70 | 149 | audience_id=audience_id, email=cont_by_email["email"] |
|
0 commit comments