add subject support (#1387)
This commit is contained in:
parent
279efc6b12
commit
9d087b0729
3 changed files with 19 additions and 18 deletions
|
|
@ -42,11 +42,11 @@ class Topic(BaseModel):
|
|||
self.subscriptions_confimed = 0
|
||||
self.subscriptions_deleted = 0
|
||||
|
||||
def publish(self, message):
|
||||
def publish(self, message, subject=None):
|
||||
message_id = six.text_type(uuid.uuid4())
|
||||
subscriptions, _ = self.sns_backend.list_subscriptions(self.arn)
|
||||
for subscription in subscriptions:
|
||||
subscription.publish(message, message_id)
|
||||
subscription.publish(message, message_id, subject=subject)
|
||||
return message_id
|
||||
|
||||
def get_cfn_attribute(self, attribute_name):
|
||||
|
|
@ -83,27 +83,27 @@ class Subscription(BaseModel):
|
|||
self.attributes = {}
|
||||
self.confirmed = False
|
||||
|
||||
def publish(self, message, message_id):
|
||||
def publish(self, message, message_id, subject=None):
|
||||
if self.protocol == 'sqs':
|
||||
queue_name = self.endpoint.split(":")[-1]
|
||||
region = self.endpoint.split(":")[3]
|
||||
enveloped_message = json.dumps(self.get_post_data(message, message_id), sort_keys=True, indent=2, separators=(',', ': '))
|
||||
enveloped_message = json.dumps(self.get_post_data(message, message_id, subject), sort_keys=True, indent=2, separators=(',', ': '))
|
||||
sqs_backends[region].send_message(queue_name, enveloped_message)
|
||||
elif self.protocol in ['http', 'https']:
|
||||
post_data = self.get_post_data(message, message_id)
|
||||
post_data = self.get_post_data(message, message_id, subject)
|
||||
requests.post(self.endpoint, json=post_data)
|
||||
elif self.protocol == 'lambda':
|
||||
# TODO: support bad function name
|
||||
function_name = self.endpoint.split(":")[-1]
|
||||
region = self.arn.split(':')[3]
|
||||
lambda_backends[region].send_message(function_name, message)
|
||||
lambda_backends[region].send_message(function_name, message, subject=subject)
|
||||
|
||||
def get_post_data(self, message, message_id):
|
||||
def get_post_data(self, message, message_id, subject):
|
||||
return {
|
||||
"Type": "Notification",
|
||||
"MessageId": message_id,
|
||||
"TopicArn": self.topic.arn,
|
||||
"Subject": "my subject",
|
||||
"Subject": subject or "my subject",
|
||||
"Message": message,
|
||||
"Timestamp": iso_8601_datetime_with_milliseconds(datetime.datetime.utcnow()),
|
||||
"SignatureVersion": "1",
|
||||
|
|
@ -270,7 +270,7 @@ class SNSBackend(BaseBackend):
|
|||
|
||||
try:
|
||||
topic = self.get_topic(arn)
|
||||
message_id = topic.publish(message)
|
||||
message_id = topic.publish(message, subject=subject)
|
||||
except SNSNotFoundError:
|
||||
endpoint = self.get_endpoint(arn)
|
||||
message_id = endpoint.publish(message)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue