/storefront/boto/mturk/notification.py

http://github.com/linkedin/indextank-service · Python · 95 lines · 50 code · 12 blank · 33 comment · 8 complexity · c0f9789d058cb80ce5f76c4fab0bef05 MD5 · raw file

  1. # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
  2. #
  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-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  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
  20. # IN THE SOFTWARE.
  21. """
  22. Provides NotificationMessage and Event classes, with utility methods, for
  23. implementations of the Mechanical Turk Notification API.
  24. """
  25. import hmac
  26. try:
  27. from hashlib import sha1 as sha
  28. except ImportError:
  29. import sha
  30. import base64
  31. import re
  32. class NotificationMessage:
  33. NOTIFICATION_WSDL = "http://mechanicalturk.amazonaws.com/AWSMechanicalTurk/2006-05-05/AWSMechanicalTurkRequesterNotification.wsdl"
  34. NOTIFICATION_VERSION = '2006-05-05'
  35. SERVICE_NAME = "AWSMechanicalTurkRequesterNotification"
  36. OPERATION_NAME = "Notify"
  37. EVENT_PATTERN = r"Event\.(?P<n>\d+)\.(?P<param>\w+)"
  38. EVENT_RE = re.compile(EVENT_PATTERN)
  39. def __init__(self, d):
  40. """
  41. Constructor; expects parameter d to be a dict of string parameters from a REST transport notification message
  42. """
  43. self.signature = d['Signature'] # vH6ZbE0NhkF/hfNyxz2OgmzXYKs=
  44. self.timestamp = d['Timestamp'] # 2006-05-23T23:22:30Z
  45. self.version = d['Version'] # 2006-05-05
  46. assert d['method'] == NotificationMessage.OPERATION_NAME, "Method should be '%s'" % NotificationMessage.OPERATION_NAME
  47. # Build Events
  48. self.events = []
  49. events_dict = {}
  50. if 'Event' in d:
  51. # TurboGears surprised me by 'doing the right thing' and making { 'Event': { '1': { 'EventType': ... } } } etc.
  52. events_dict = d['Event']
  53. else:
  54. for k in d:
  55. v = d[k]
  56. if k.startswith('Event.'):
  57. ed = NotificationMessage.EVENT_RE.search(k).groupdict()
  58. n = int(ed['n'])
  59. param = str(ed['param'])
  60. if n not in events_dict:
  61. events_dict[n] = {}
  62. events_dict[n][param] = v
  63. for n in events_dict:
  64. self.events.append(Event(events_dict[n]))
  65. def verify(self, secret_key):
  66. """
  67. Verifies the authenticity of a notification message.
  68. """
  69. verification_input = NotificationMessage.SERVICE_NAME + NotificationMessage.OPERATION_NAME + self.timestamp
  70. h = hmac.new(key=secret_key, digestmod=sha)
  71. h.update(verification_input)
  72. signature_calc = base64.b64encode(h.digest())
  73. return self.signature == signature_calc
  74. class Event:
  75. def __init__(self, d):
  76. self.event_type = d['EventType']
  77. self.event_time_str = d['EventTime']
  78. self.hit_type = d['HITTypeId']
  79. self.hit_id = d['HITId']
  80. self.assignment_id = d['AssignmentId']
  81. #TODO: build self.event_time datetime from string self.event_time_str
  82. def __repr__(self):
  83. return "<boto.mturk.notification.Event: %s for HIT # %s>" % (self.event_type, self.hit_id)