Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
# Conflicts:
#	pyproject.toml
  • Loading branch information
mikeckennedy committed Oct 29, 2024
2 parents 7e3a392 + fb3e230 commit 10d4f18
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 125 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Listmonk Email App API Client for Python
# Listmonk Email API Client for Python

Client for the for open source, self-hosted [Listmonk email platform](https://listmonk.app) based on
[httpx](https://www.python-httpx.org) and [pydantic](https://pydantic.dev).
Expand Down
39 changes: 20 additions & 19 deletions example_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
test_list_id = settings.get('test_list_id') or input("Enter the ID for a test list with subscribers: ")

listmonk.set_url_base(url)
print(f'Base url: {listmonk.get_base_url()}')
print(f'Base url: {listmonk.get_base_url()}', flush=True)

print(f"Logged in? {listmonk.login(user, password)}")
print(f"API Healthy?: {listmonk.is_healthy()}")
print(f"Verify login: {listmonk.verify_login()}")
print(f"Logged in? {listmonk.login(user, password)}", flush=True)
print(f"API Healthy?: {listmonk.is_healthy()}", flush=True)
print(f"Verify login: {listmonk.verify_login()}", flush=True)
print()

lists = listmonk.lists()
for lst in lists:
print(f'{lst.name} list: {lst}')
print(f'{lst.name} list: {lst}', flush=True)

the_list = listmonk.list_by_id(test_list_id)
print(f'List by ID: {the_list}')

print()
subscribers = listmonk.subscribers(list_id=test_list_id)
print(f'{len(subscribers):,} subscribers returned')
print(f'{len(subscribers):,} subscribers returned', flush=True)

# id=208495 created_at=datetime.datetime(2024, 1, 19, 20, 29, 8, 477242, tzinfo=TzInfo(UTC))
# updated_at=datetime.datetime(2024, 1, 19, 20, 29, 8, 477242, tzinfo=TzInfo(UTC))
Expand All @@ -53,47 +53,48 @@

subscriber = listmonk.create_subscriber(email, 'Deletable Mkennedy',
{test_list_id}, pre_confirm=True, attribs=custom_data)
print(f'Created subscriber: {subscriber}')
print(f'Created subscriber: {subscriber}', flush=True)

subscriber = listmonk.subscriber_by_email(email)
print(f'Subscriber by email: {subscriber}')
print(f'Subscriber by email: {subscriber}', flush=True)

subscriber = listmonk.subscriber_by_id(subscriber.id)
print(f'Subscriber by id: {subscriber}')
print(f'Subscriber by id: {subscriber}', flush=True)

subscriber = listmonk.subscriber_by_uuid(subscriber.uuid)
print(f'Subscriber by uuid: {subscriber}')
print(f'Subscriber by uuid: {subscriber}', flush=True)

subscriber.name = 'Mr. ' + subscriber.name.upper()
subscriber.attribs['rating'] = 7

query = f"subscribers.email = '{email}'"
print("Searching for user with query: ", query)
print("Searching for user with query: ", query, flush=True)
sub2 = listmonk.subscribers(query)
print(f'Found {len(sub2):,} users with query.')
print(f'Found {sub2[0].name} with email {sub2[0].email}')
print(f'Found {len(sub2):,} users with query.', flush=True)
print(f'Found {sub2[0].name} with email {sub2[0].email}', flush=True)


# TODO: Choose list IDs from your instance (can be seen in the UI or from the listing above)
to_add = {the_list.id} # Add all the lists here: {1, 7, 11}
remove_from = set() # Same as above
updated_subscriber = listmonk.update_subscriber(subscriber, to_add, remove_from)
print(f'Updated subscriber: {updated_subscriber}')
print(f'Updated subscriber: {updated_subscriber}', flush=True)

print(f'Subscriber confirmed?: {listmonk.confirm_optin(subscriber.uuid, the_list.uuid)}')
print(f'Subscriber confirmed?: {listmonk.confirm_optin(subscriber.uuid, the_list.uuid)}', flush=True)

updated_subscriber.attribs['subscription_note'] = \
"They asked to be unsubscribed so we disabled their account, but no block-listing yet."

disabled_subscriber = listmonk.disable_subscriber(updated_subscriber)
print("Disabled: ", disabled_subscriber)
print("Disabled: ", disabled_subscriber, flush=True)

disabled_subscriber.attribs['blocklist_note'] = \
"They needed to be blocked!"

listmonk.block_subscriber(disabled_subscriber)

re_enabled_subscriber = listmonk.enable_subscriber(disabled_subscriber)
print("Re-enabled: ", re_enabled_subscriber)
print("Re-enabled: ", re_enabled_subscriber, flush=True)

listmonk.delete_subscriber(subscriber.email)

Expand All @@ -104,6 +105,6 @@
if to_email != 'SUBSCRIBER_EMAIL_ON_YOUR_LIST':
status = listmonk.send_transactional_email(to_email, template_id, from_email=from_email,
template_data=template_data, content_type='html')
print(f"Result of sending a tx email: {status}.")
print(f"Result of sending a tx email: {status}.", flush=True)
else:
print("Set email values to send transactional emails.")
print("Set email values to send transactional emails.", flush=True)
4 changes: 3 additions & 1 deletion example_client/settings-template.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"base_url": "Where your instance hosted? e.g. https://mail.yoursite.com",

"NOTE": "It's important in v4.0 that these credentials are an API account/key, not regular user",
"username": "user_at_listmonk_instance",
"password": "password_at_listmonk_instance",

"test_list_id": 1,
"test_list_id": 5,
"test_list info" : "Pick a list with some subscribers for the example client to pull",

"ACTION": "COPY TO settings.json (excluded from git) and enter your info THERE (NOT HERE)"
Expand Down
Loading

0 comments on commit 10d4f18

Please sign in to comment.