Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds logic to manage suppressions #214

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Contents:
django
tornado
spamcheck
suppression
webhooks
testing
reference
Expand Down
77 changes: 77 additions & 0 deletions docs/supression.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.. _supression:

Suppression API
==============

You can manage Postmark's Suppression lists with a few simple calls:

To view the current suppression list:
.. code-block:: python

>>> response = postmark.get_suppressions(stream_id="test")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to unify suppressions with other sub-sections, so it is postmark.suppressions.all(stream_id="test")

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See an example here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I think I switched over to a sub-section.

>>> print(response['Suppressions'])
{
"Suppressions":[
{
"EmailAddress":"[email protected]",
"SuppressionReason":"ManualSuppression",
"Origin": "Recipient",
"CreatedAt":"2019-12-17T08:58:33-05:00"
},
{
"EmailAddress":"[email protected]",
"SuppressionReason":"HardBounce",
"Origin": "Recipient",
"CreatedAt":"2019-12-17T08:58:33-05:00"
},
{
"EmailAddress":"[email protected]",
"SuppressionReason":"SpamComplaint",
"Origin": "Recipient",
"CreatedAt":"2019-12-17T08:58:33-05:00"
}
]
}
You can filter this with "SuppressionReason", "Origin", "todate", "fromdate", and "EmailAddress" lile:
.. code-block:: python

>>> response = postmark.get_suppressions(stream_id="test", EmailAddress="[email protected]")
>>> print(response['Suppressions'])
{
"Suppressions":[
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to return a list of some light-weight object instead of a dict with Suppressions inside

"EmailAddress":"[email protected]",
"SuppressionReason":"ManualSuppression",
"Origin": "Recipient",
"CreatedAt":"2019-12-17T08:58:33-05:00"
}
]
}
You can add a new suppression with:
.. code-block:: python

>>> response = postmark.add_suppressions(stream_id="test", emails=["[email protected]"])
>>> print(response['Suppressions'])
{
"Suppressions":[
{
"EmailAddress":"[email protected]",
"Status":"Suppressed",
"Message": null
},
]
}
You can delete a suppression with:
.. code-block:: python

>>> response = postmark.delete_suppressions(stream_id="test", emails=["[email protected]"])
>>> print(response['Suppressions'])
{
"Suppressions":[
{
"EmailAddress":"[email protected]",
"Status":"Deleted",
"Message": null
}
]
}
25 changes: 25 additions & 0 deletions src/postmarker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ def spamcheck(self, dump, options="long"):
raise SpamAssassinError(response["message"])
return response

def get_suppressions(self, stream_id, **kwargs):
url = DEFAULT_API + f"/message-streams/{stream_id}/suppressions/dump"
response = self._call("GET", url, **kwargs)
return response

def add_suppression(self, stream_id, emails):
url = DEFAULT_API + f"/message-streams/{stream_id}/suppressions"
if type(emails) != list:
raise ValueError("emails must be of type list")
data = {'Suppressions': []}
for email in emails:
data['Suppressions'].append({'EmailAddress': email})
response = self._call("POST", url, "", data)
return response

def delete_suppression(self, stream_id, emails):
url = DEFAULT_API + f"/message-streams/{stream_id}/suppressions/delete"
if type(emails) != list:
raise ValueError("emails must be of type list")
data = {'Suppressions': []}
for email in emails:
data['Suppressions'].append({'EmailAddress': email})
response = self._call("POST", url, "", data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using self.root_api_url instead for root and endpoint separately

return response

def _call(self, method, root, endpoint, data=None, headers=None, **kwargs):
default_headers = {"Accept": "application/json", "User-Agent": USER_AGENT}
if headers:
Expand Down