mirror of
https://github.com/caronc/apprise.git
synced 2024-11-22 16:13:12 +01:00
SparkPost cc and bcc improvements + test cases (#309)
This commit is contained in:
parent
89eaffa286
commit
3091669b85
@ -133,7 +133,7 @@ class NotifySparkPost(NotifyBase):
|
|||||||
# batch transfer based on:
|
# batch transfer based on:
|
||||||
# https://www.sparkpost.com/docs/tech-resources/\
|
# https://www.sparkpost.com/docs/tech-resources/\
|
||||||
# smtp-rest-api-performance/#sending-via-the-transmission-rest-api
|
# smtp-rest-api-performance/#sending-via-the-transmission-rest-api
|
||||||
default_batch_size = 10000
|
default_batch_size = 2000
|
||||||
|
|
||||||
# A URL that takes you to the setup/help of the specific protocol
|
# A URL that takes you to the setup/help of the specific protocol
|
||||||
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_sparkpost'
|
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_sparkpost'
|
||||||
@ -209,7 +209,7 @@ class NotifySparkPost(NotifyBase):
|
|||||||
# Define any kwargs we're using
|
# Define any kwargs we're using
|
||||||
template_kwargs = {
|
template_kwargs = {
|
||||||
'headers': {
|
'headers': {
|
||||||
'name': _('HTTP Header'),
|
'name': _('Email Header'),
|
||||||
'prefix': '+',
|
'prefix': '+',
|
||||||
},
|
},
|
||||||
'tokens': {
|
'tokens': {
|
||||||
@ -541,9 +541,6 @@ class NotifySparkPost(NotifyBase):
|
|||||||
else:
|
else:
|
||||||
payload['content']['text'] = body
|
payload['content']['text'] = body
|
||||||
|
|
||||||
if self.headers:
|
|
||||||
payload['content']['headers'] = self.headers
|
|
||||||
|
|
||||||
if attach:
|
if attach:
|
||||||
# Prepare ourselves an attachment object
|
# Prepare ourselves an attachment object
|
||||||
payload['content']['attachments'] = []
|
payload['content']['attachments'] = []
|
||||||
@ -595,9 +592,6 @@ class NotifySparkPost(NotifyBase):
|
|||||||
# Create a copy of the targets list
|
# Create a copy of the targets list
|
||||||
emails = list(self.targets)
|
emails = list(self.targets)
|
||||||
|
|
||||||
# Initialize our headers
|
|
||||||
headers = self.headers.copy()
|
|
||||||
|
|
||||||
for index in range(0, len(emails), batch_size):
|
for index in range(0, len(emails), batch_size):
|
||||||
# Generate our email listing
|
# Generate our email listing
|
||||||
payload['recipients'] = list()
|
payload['recipients'] = list()
|
||||||
@ -608,6 +602,9 @@ class NotifySparkPost(NotifyBase):
|
|||||||
# Initialize our bcc list
|
# Initialize our bcc list
|
||||||
bcc = set(self.bcc)
|
bcc = set(self.bcc)
|
||||||
|
|
||||||
|
# Initialize our headers
|
||||||
|
headers = self.headers.copy()
|
||||||
|
|
||||||
for addr in self.targets[index:index + batch_size]:
|
for addr in self.targets[index:index + batch_size]:
|
||||||
entry = {
|
entry = {
|
||||||
'address': {
|
'address': {
|
||||||
@ -616,10 +613,10 @@ class NotifySparkPost(NotifyBase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Strip target out of cc list if in To
|
# Strip target out of cc list if in To
|
||||||
cc = (cc - set([addr]))
|
cc = (cc - set([addr[1]]))
|
||||||
|
|
||||||
# Strip target out of bcc list if in To
|
# Strip target out of bcc list if in To
|
||||||
bcc = (bcc - set([addr]))
|
bcc = (bcc - set([addr[1]]))
|
||||||
|
|
||||||
if addr[0]:
|
if addr[0]:
|
||||||
entry['address']['name'] = addr[0]
|
entry['address']['name'] = addr[0]
|
||||||
@ -633,7 +630,10 @@ class NotifySparkPost(NotifyBase):
|
|||||||
entry = {
|
entry = {
|
||||||
'address': {
|
'address': {
|
||||||
'email': addr,
|
'email': addr,
|
||||||
}
|
'header_to':
|
||||||
|
# Take the first email in the To
|
||||||
|
self.targets[index:index + batch_size][0][1],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.names.get(addr):
|
if self.names.get(addr):
|
||||||
@ -644,7 +644,7 @@ class NotifySparkPost(NotifyBase):
|
|||||||
|
|
||||||
headers['CC'] = ','.join(cc)
|
headers['CC'] = ','.join(cc)
|
||||||
|
|
||||||
# Handle our bcc List
|
# Handle our bcc
|
||||||
for addr in bcc:
|
for addr in bcc:
|
||||||
# Add our recipient to our list
|
# Add our recipient to our list
|
||||||
payload['recipients'].append({
|
payload['recipients'].append({
|
||||||
@ -657,7 +657,7 @@ class NotifySparkPost(NotifyBase):
|
|||||||
})
|
})
|
||||||
|
|
||||||
if headers:
|
if headers:
|
||||||
payload['headers'] = headers
|
payload['content']['headers'] = headers
|
||||||
|
|
||||||
# Send our message
|
# Send our message
|
||||||
status_code, response = \
|
status_code, response = \
|
||||||
@ -693,19 +693,16 @@ class NotifySparkPost(NotifyBase):
|
|||||||
# from_name specified; pass it back on the url
|
# from_name specified; pass it back on the url
|
||||||
params['name'] = self.from_name
|
params['name'] = self.from_name
|
||||||
|
|
||||||
if len(self.cc) > 0:
|
if self.cc:
|
||||||
# Handle our Carbon Copy Addresses
|
# Handle our Carbon Copy Addresses
|
||||||
params['cc'] = ','.join(
|
params['cc'] = ','.join(
|
||||||
['{}{}'.format(
|
['{}{}'.format(
|
||||||
'' if not e not in self.names
|
'' if not e not in self.names
|
||||||
else '{}:'.format(self.names[e]), e) for e in self.cc])
|
else '{}:'.format(self.names[e]), e) for e in self.cc])
|
||||||
|
|
||||||
if len(self.bcc) > 0:
|
if self.bcc:
|
||||||
# Handle our Blind Carbon Copy Addresses
|
# Handle our Blind Carbon Copy Addresses
|
||||||
params['bcc'] = ','.join(
|
params['bcc'] = ','.join(self.bcc)
|
||||||
['{}{}'.format(
|
|
||||||
'' if not e not in self.names
|
|
||||||
else '{}:'.format(self.names[e]), e) for e in self.bcc])
|
|
||||||
|
|
||||||
# a simple boolean check as to whether we display our target emails
|
# a simple boolean check as to whether we display our target emails
|
||||||
# or not
|
# or not
|
||||||
@ -781,5 +778,7 @@ class NotifySparkPost(NotifyBase):
|
|||||||
|
|
||||||
# Get Batch Mode Flag
|
# Get Batch Mode Flag
|
||||||
results['batch'] = \
|
results['batch'] = \
|
||||||
parse_bool(results['qsd'].get('batch', False))
|
parse_bool(results['qsd'].get(
|
||||||
|
'batch', NotifySparkPost.template_args['batch']['default']))
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
@ -44,7 +44,7 @@ TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), 'var')
|
|||||||
@mock.patch('requests.post')
|
@mock.patch('requests.post')
|
||||||
def test_notify_sparkpost_plugin_throttling(mock_post):
|
def test_notify_sparkpost_plugin_throttling(mock_post):
|
||||||
"""
|
"""
|
||||||
API: NotifySpark() Throttling
|
API: NotifySparkPost() Throttling
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ def test_notify_sparkpost_plugin_throttling(mock_post):
|
|||||||
@mock.patch('requests.post')
|
@mock.patch('requests.post')
|
||||||
def test_notify_sparkpost_plugin_attachments(mock_post):
|
def test_notify_sparkpost_plugin_attachments(mock_post):
|
||||||
"""
|
"""
|
||||||
API: NotifySpark() Attachments
|
API: NotifySparkPost() Attachments
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# Disable Throttling to speed testing
|
# Disable Throttling to speed testing
|
||||||
@ -161,3 +161,28 @@ def test_notify_sparkpost_plugin_attachments(mock_post):
|
|||||||
assert obj.notify(
|
assert obj.notify(
|
||||||
body='body', title='title', notify_type=NotifyType.INFO,
|
body='body', title='title', notify_type=NotifyType.INFO,
|
||||||
attach=attach) is False
|
attach=attach) is False
|
||||||
|
|
||||||
|
obj = Apprise.instantiate(
|
||||||
|
'sparkpost://no-reply@example.com/{}/'
|
||||||
|
'user1@example.com/user2@example.com?batch=yes'.format(apikey))
|
||||||
|
assert isinstance(obj, plugins.NotifySparkPost)
|
||||||
|
|
||||||
|
# Force our batch to break into separate messages
|
||||||
|
obj.default_batch_size = 1
|
||||||
|
# We'll send 2 messages
|
||||||
|
mock_post.reset_mock()
|
||||||
|
|
||||||
|
assert obj.notify(
|
||||||
|
body='body', title='title', notify_type=NotifyType.INFO,
|
||||||
|
attach=attach) is True
|
||||||
|
assert mock_post.call_count == 2
|
||||||
|
|
||||||
|
# single batch
|
||||||
|
mock_post.reset_mock()
|
||||||
|
# We'll send 1 message
|
||||||
|
obj.default_batch_size = 2
|
||||||
|
|
||||||
|
assert obj.notify(
|
||||||
|
body='body', title='title', notify_type=NotifyType.INFO,
|
||||||
|
attach=attach) is True
|
||||||
|
assert mock_post.call_count == 1
|
||||||
|
Loading…
Reference in New Issue
Block a user