U
    n/e                     @   s   d dl mZmZmZmZmZmZmZmZm	Z	m
Z
mZmZ ddlmZ dddZdddZd	d
 Zdd ZG dd deZG dd deZG dd deZG dd deZdS )    )ChecksumErrorCRC32CheckerExceptionRaiserHTTPStatusCodeCheckerMaxAttemptsDecoratorMultiCheckerRetryHandlerServiceErrorCodeChecker_extract_retryable_exceptioncrc32create_retry_action_from_configlogger   )resolve_awaitableNc                 C   s$   t | |d}t| |d}t||dS )N)operation_name)checkeraction) create_checker_from_retry_configr   AioRetryHandler)configr   r   r    r   </tmp/pip-unpacked-wheel-8mnez3y6/aiobotocore/retryhandler.pycreate_retry_handler   s      r   c                 C   s   g }d }g }d| krj| d  dg }| d d }|D ]4}|| }|t| t|}|d k	r4|| q4|d k	r|  |d k	r| | d }	|	D ]4}|t|	|  t|	| }|d k	r|| qt|dkrt|d |dS t|}
t|
|t|dS d S )NZ__default__policiesmax_attemptsr   r   )r   )r   retryable_exceptions)	getappend_create_single_checkerr
   extendlenAioMaxAttemptsDecoratorAioMultiCheckertuple)r   r   Zcheckersr   r   r   keyZcurrent_configZretry_exceptionZoperation_policiesZmulti_checkerr   r   r   r      s:    r   c                 C   s2   d| d krt | d d S d| d kr.t S d S )NresponseZapplies_whenZsocket_errors)_create_single_response_checkerr   )r   r   r   r   r   ?   s    
r   c                 C   sZ   d| krt | d | d d}n8d| kr6t| d d}n d| krNt| d d}ntd|S )NZservice_error_codeZhttp_status_code)status_codeZ
error_code)r'   Z	crc32body)headerzUnknown retry policy)r	   r   AioCRC32Checker
ValueError)r%   r   r   r   r   r&   H   s    r&   c                   @   s   e Zd Zdd Zdd ZdS )r   c                    sx   |||d}t | jtr8|d d d}|d|i t| jf |I dH rj| j|d}td| |S td	 dS )
zHandler for a retry.

        Intended to be hooked up to an event handler (hence the **kwargs),
        this will process retries appropriately.

        )attempt_numberr%   caught_exceptionZrequest_dictcontextretriesretries_contextN)attemptszRetry needed, action of: %szNo retry needed.)	
isinstance_checkerr   r   updater   _actionr   debug)selfr0   r%   r,   kwargsZchecker_kwargsr/   resultr   r   r   _call[   s    zAioRetryHandler._callc                 O   s   | j ||S Nr9   r6   argsr7   r   r   r   __call__q   s    zAioRetryHandler.__call__N__name__
__module____qualname__r9   r>   r   r   r   r   r   Z   s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r!   c                    s   |rt |dd| j|d< | |||I d H }|rx|| jkrr|d k	rbd|d krbd|d d d< td| dS |S ndS d S )	Nmaxr   ZResponseMetadatar   TZMaxAttemptsReachedz0Reached the maximum number of retry attempts: %sF)rC   r   _max_attempts_should_retryr   r5   )r6   r+   r%   r,   r/   Zshould_retryr   r   r   r9   v   s0    
   


zAioMaxAttemptsDecorator._callc                 O   s   | j ||S r:   r;   r<   r   r   r   r>      s    z AioMaxAttemptsDecorator.__call__c              
      s   | j rh|| jk rhzt| |||I d H W S  | j k
rd } ztjd|dd W Y dS d }~X Y qX nt| |||I d H S d S )Nz,retry needed, retryable exception caught: %sT)exc_info)Z_retryable_exceptionsrD   r   r2   r   r5   )r6   r+   r%   r,   er   r   r   rE      s    z%AioMaxAttemptsDecorator._should_retryN)r@   rA   rB   r9   r>   rE   r   r   r   r   r!   u   s   r!   c                   @   s   e Zd Zdd Zdd ZdS )r"   c                    s2   | j D ]&}t||||I d H }|r|  S qdS )NF)Z	_checkersr   )r6   r+   r%   r,   r   Zchecker_responser   r   r   r9      s    



zAioMultiChecker._callc                 O   s   | j ||S r:   r;   r<   r   r   r   r>      s    zAioMultiChecker.__call__Nr?   r   r   r   r   r"      s   	r"   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r)   c                    s:   |d k	r|  ||I d H S |d k	r.| ||S tdd S )Nz,Both response and caught_exception are None.)_check_responseZ_check_caught_exceptionr*   )r6   r+   r%   r,   r   r   r   r9      s     zAioCRC32Checker._callc                 O   s   | j ||S r:   r;   r<   r   r   r   r>      s    zAioCRC32Checker.__call__c                    sz   |d }|j | j}|d kr.td| j nHt|d jI d H d@ }|t|ksvtdt|| tdt||dd S )Nr   z?crc32 check skipped, the %s header is not in the http response.l    z>retry needed: crc32 check failed, expected != actual: %s != %sr   )Zchecksum_typeZexpected_checksumZactual_checksum)	headersr   Z_header_namer   r5   r   contentintr   )r6   r+   r%   http_responseZexpected_crcZactual_crc32r   r   r   rH      s&    zAioCRC32Checker._check_responseN)r@   rA   rB   r9   r>   rH   r   r   r   r   r)      s   
r)   )N)N)Zbotocore.retryhandlerr   r   r   r   r   r   r   r	   r
   r   r   r   Z_helpersr   r   r   r   r&   r   r!   r"   r)   r   r   r   r   <module>   s   8


"	5