-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ Contents: | |
django | ||
tornado | ||
spamcheck | ||
suppression | ||
webhooks | ||
testing | ||
reference | ||
|
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") | ||
>>> 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":[ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
"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 | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest using |
||
return response | ||
|
||
def _call(self, method, root, endpoint, data=None, headers=None, **kwargs): | ||
default_headers = {"Accept": "application/json", "User-Agent": USER_AGENT} | ||
if headers: | ||
|
There was a problem hiding this comment.
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")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See an example here
There was a problem hiding this comment.
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.