B
    ?F[b                 @   sn   d Z ddlZyddlmZ W n ek
r8   ddlZY nX ddlZddlZG dd deZ	G dd deZ
dS )z
Provides NotificationMessage and Event classes, with utility methods, for
implementations of the Mechanical Turk Notification API.
    N)sha1c               @   s:   e Zd ZdZdZdZdZdZe	eZ
dd Zdd	 Zd
S )NotificationMessagezlhttp://mechanicalturk.amazonaws.com/AWSMechanicalTurk/2006-05-05/AWSMechanicalTurkRequesterNotification.wsdlz
2006-05-05Z&AWSMechanicalTurkRequesterNotificationZNotifyz!Event\.(?P<n>\d+)\.(?P<param>\w+)c             C   s   |d | _ |d | _|d | _|d tjks:tdtj g | _i }d|krV|d }ndxb|D ]Z}|| }|dr\tj	|
 }t|d }t|d	 }||kri ||< ||| |< q\W x |D ]}| jt||  qW d
S )z
        Constructor; expects parameter d to be a dict of string parameters from a REST transport notification message
        Z	SignatureZ	TimestampZVersionmethodzMethod should be '%s'EventzEvent.nparamN)	signature	timestampversionr   OPERATION_NAMEAssertionErrorZevents
startswithEVENT_REsearch	groupdictintstrappendr   )selfdZevents_dictkvZedr   r    r   6lib/python3.7/site-packages/boto/mturk/notification.py__init__.   s&    






zNotificationMessage.__init__c             C   sJ   t j}|t j7 }|| j7 }tj|td}|| t	|
 }| j|kS )a  
        Verifies the authenticity of a notification message.

        TODO: This is doing a form of authentication and
              this functionality should really be merged
              with the pluggable authentication mechanism
              at some point.
        )keyZ	digestmod)r   SERVICE_NAMEr   r	   hmacnewshaupdatebase64Z	b64encodeZdigestr   )r   Z
secret_keyZverification_inputhZsignature_calcr   r   r   verifyJ   s    	


zNotificationMessage.verifyN)__name__
__module____qualname__ZNOTIFICATION_WSDLZNOTIFICATION_VERSIONr   r   ZEVENT_PATTERNrecompiler   r   r#   r   r   r   r   r   #   s   
r   c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s>   |d | _ |d | _|d | _|d | _d|kr:|d | _d S )NZ	EventTypeZ	EventTimeZ	HITTypeIdZHITIdZAssignmentId)
event_typeZevent_time_strZhit_typehit_idZassignment_id)r   r   r   r   r   r   \   s    



zEvent.__init__c             C   s   d| j | jf S )Nz0<boto.mturk.notification.Event: %s for HIT # %s>)r)   r*   )r   r   r   r   __repr__f   s    zEvent.__repr__N)r$   r%   r&   r   r+   r   r   r   r   r   [   s   
r   )__doc__r   Zhashlibr   r   ImportErrorr!   r'   objectr   r   r   r   r   r   <module>   s   8