1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
Provides NotificationMessage and Event classes, with utility methods, for
24
implementations of the Mechanical Turk Notification API.
29
from hashlib import sha1 as sha
35
class NotificationMessage:
37
NOTIFICATION_WSDL = "http://mechanicalturk.amazonaws.com/AWSMechanicalTurk/2006-05-05/AWSMechanicalTurkRequesterNotification.wsdl"
38
NOTIFICATION_VERSION = '2006-05-05'
40
SERVICE_NAME = "AWSMechanicalTurkRequesterNotification"
41
OPERATION_NAME = "Notify"
43
EVENT_PATTERN = r"Event\.(?P<n>\d+)\.(?P<param>\w+)"
44
EVENT_RE = re.compile(EVENT_PATTERN)
46
def __init__(self, d):
48
Constructor; expects parameter d to be a dict of string parameters from a REST transport notification message
50
self.signature = d['Signature'] # vH6ZbE0NhkF/hfNyxz2OgmzXYKs=
51
self.timestamp = d['Timestamp'] # 2006-05-23T23:22:30Z
52
self.version = d['Version'] # 2006-05-05
53
assert d['method'] == NotificationMessage.OPERATION_NAME, "Method should be '%s'" % NotificationMessage.OPERATION_NAME
59
# TurboGears surprised me by 'doing the right thing' and making { 'Event': { '1': { 'EventType': ... } } } etc.
60
events_dict = d['Event']
64
if k.startswith('Event.'):
65
ed = NotificationMessage.EVENT_RE.search(k).groupdict()
67
param = str(ed['param'])
68
if n not in events_dict:
70
events_dict[n][param] = v
72
self.events.append(Event(events_dict[n]))
74
def verify(self, secret_key):
76
Verifies the authenticity of a notification message.
78
verification_input = NotificationMessage.SERVICE_NAME + NotificationMessage.OPERATION_NAME + self.timestamp
79
h = hmac.new(key=secret_key, digestmod=sha)
80
h.update(verification_input)
81
signature_calc = base64.b64encode(h.digest())
82
return self.signature == signature_calc
85
def __init__(self, d):
86
self.event_type = d['EventType']
87
self.event_time_str = d['EventTime']
88
self.hit_type = d['HITTypeId']
89
self.hit_id = d['HITId']
90
self.assignment_id = d['AssignmentId']
92
#TODO: build self.event_time datetime from string self.event_time_str
95
return "<boto.mturk.notification.Event: %s for HIT # %s>" % (self.event_type, self.hit_id)