-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
74 lines (65 loc) · 2.7 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
"""Example webserver endpoint using the development server of web.py.
Use a real web server for production. See for example
http://webpy.org/install#prod
"""
import urllib2
import web
import simplejson as json
class Commodities(object):
"""Receives commodity updates from SNS through POST.
"""
def POST(self, key):
try:
data = json.loads(web.data())
# Is this an SNS Notification?
notif_type = data.get('Type', '')
# Commodity updates are received with a type 'Notification'.
if notif_type == "Notification":
commodities = json.loads(data['Message'])
for commodity in commodities:
print "Commodity received: %s" % commodity
# SNS will verify this endpoint by sending a
# SubscriptionConfirmation. To which you must answer by GETing the
# given 'SubscribeURL'.
elif notif_type == "SubscriptionConfirmation":
print "Received SNS subscription confirmation to topic %s" % (
data['TopicArn'])
urllib2.urlopen(data['SubscribeURL'])
else:
print "Unsupported notification type %s" % notif_type
except Exception, exception:
print exception
class Documents(object):
"""Receives document updates from SNS through POST.
"""
def POST(self, doc_id):
try:
data = json.loads(web.data())
# Is this an SNS Notification?
notif_type = data.get('Type', '')
# Document updates are received with a type 'Notification'.
if notif_type == "Notification":
docs = json.loads(data['Message'])
for doc in docs:
print "Document received: %s" % doc
# SNS will verify this endpoint by sending a
# SubscriptionConfirmation. To which you must answer by GETing the
# given 'SubscribeURL'.
elif notif_type == "SubscriptionConfirmation":
print "Received SNS subscription confirmation to topic %s" % (
data['TopicArn'])
urllib2.urlopen(data['SubscribeURL'])
else:
print "Unsupported notification type %s" % notif_type
except Exception, exception:
print exception
# This is a regular expression for matching a UUID.
uuid_re = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
# URL mappings
urls = ("^/documents/?(%s)?/?$" % uuid_re, "Documents",
"^/commodities/?(.*)/?$", "Commodities",
)
app = web.application(urls, globals())
if __name__ == "__main__":
app.run()