o
    [h                     @   s   d dl Z d dlZd dlZd dlZd dl m Z  d dlmZ d dlmZmZ d dl	m
Z
mZ dd Zdd	 Zd
edededefddZG dd deZG dd dZdS )    N)datetime)	HTTPError)urlopenRequest)AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYc                 C   s   t | |dtj S )Nutf-8)hmacnewencodehashlibsha256digest)keymsg r   y/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/mturk_client.pysign   s   r   c                 C   s6   t d|  d|}t ||}t ||}t |d}|S )NZAWS4r   aws4_request)r   r   )r   
date_stampZ
regionNameZserviceNameZkDateZkRegionZkServiceZkSigningr   r   r   getSignatureKey   s
   


r   	operationrequest_parametersuse_sandboxreturnc             
   C   s  |rd}nd}d| }d}d}d}d|  }t  }	|	d}
|	d	}d
}d}d| d d | d d |
 d d | d }d}t|d}t| }d	d|||||g}d}d
	|||dg}d	||
|t|d g}t
t|||}t||dtj }| dt d
| d| d| 	}||
||d}t||d}zt||d}W n ty } zt| d}t|d }~ww | }t|dS )Nz/mturk-requester-sandbox.us-east-1.amazonaws.comz'mturk-requester.us-east-1.amazonaws.comzhttps://zmturk-requesterz	us-east-1zapplication/x-amz-json-1.1zMTurkRequesterServiceV20170117.z%Y%m%dT%H%M%SZz%Y%m%d/ zcontent-type:
zhost:zx-amz-date:zx-amz-target:z)content-type;host;x-amz-date;x-amz-targetutf8POSTzAWS4-HMAC-SHA256r   r   z Credential=z, SignedHeaders=z, Signature=)zContent-Typez
X-Amz-DatezX-Amz-TargetAuthorization)headers)data)r   utcnowstrftimejsondumpsr   r   r   	hexdigestjoinr   r   r	   r
   r   r   r   r   loadsreaddecode
MTurkError)r   r   r   hostZendpointZserviceregioncontent_typeZ
amz_targettZamz_dater   Zcanonical_uriZcanonical_querystringZcanonical_headersZsigned_headersrequest_bodyZpayload_hashZcanonical_request	algorithmZcredential_scopeZstring_to_signZsigning_key	signatureZauthorization_headerr!   requestrespexcr   Zres_bodyr   r   r   call_api   s   



	
 r7   c                   @   s   e Zd ZdS )r,   N)__name__
__module____qualname__r   r   r   r   r,      s    r,   c                   @   sd   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd ZdS )
TurkClienta  
    - Make these separate methods so they are easier to mock individually
    - Put them under a class so that they are easier to patch (don't have to worry about
    how it's imported)

    Also, in the future we might add some state, e.g. persistent connection
    c                C      t d| |dS )NZ	CreateHITr   r7   Zrequest_paramsr   r   r   r   
create_hit      zTurkClient.create_hitc                C   r<   )NZListAssignmentsForHITr=   r>   r?   r   r   r   list_assignments_for_hit      z#TurkClient.list_assignments_for_hitc                C   r<   )NZRejectAssignmentr=   r>   r?   r   r   r   reject_assignment   rA   zTurkClient.reject_assignmentc                C   r<   )NZApproveAssignmentr=   r>   r?   r   r   r   approve_assignment   rA   zTurkClient.approve_assignmentc                C   r<   )NZ	SendBonusr=   r>   r?   r   r   r   
send_bonus   rA   zTurkClient.send_bonusc                C   r<   )NZUpdateExpirationForHITr=   r>   r?   r   r   r   update_expiration   rC   zTurkClient.update_expirationc                C   r<   )NZ AssociateQualificationWithWorkerr=   r>   r?   r   r   r   assign_qualification   rC   zTurkClient.assign_qualificationN)r8   r9   r:   __doc__staticmethodr@   rB   rD   rE   rF   rG   rH   r   r   r   r   r;      s     





r;   )r   r   r	   r%   urllib.errorr   urllib.requestr   r   Zotree.settingsr   r   r   r   strdictboolr7   	Exceptionr,   r;   r   r   r   r   <module>   s     