U
    n/eD/                     @   s   d dl Z d dl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lmZ d dlmZ d dlmZ d dlmZ dd	 ZG d
d deZG dd deZdS )    N)
DEFAULT_TIMEOUTMAX_POOL_CONNECTIONSEndpointEndpointCreatorHTTPClientErrorcreate_request_objecthistory_recorderis_valid_endpoint_urlis_valid_ipv6_endpoint_urllogger)first_non_none_response)HTTPHeaderDict)handle_checksum_body)AIOHTTPSession)StreamingBodyc                    s   t dd | jjD | jd|jid}|d dkrD| jI dH |d< nH|jrV| j|d< n6|jr||d	 d
}t	| j||d< n| jI dH |d< |S )a  Convert an HTTP response object to a request dict.

    This converts the requests library's HTTP response object to
    a dictionary.

    :type http_response: botocore.vendored.requests.model.Response
    :param http_response: The HTTP response from an AWS service request.

    :rtype: dict
    :return: A response dictionary which will contain the following keys:
        * headers (dict)
        * status_code (int)
        * body (string or file-like object)

    c                 S   s&   i | ]\}}| d  | d qS )zutf-8)decodelower).0kv r   8/tmp/pip-unpacked-wheel-8mnez3y6/aiobotocore/endpoint.py
<dictcomp>-   s    z,convert_to_response_dict.<locals>.<dictcomp>operation_name)headersstatus_codecontextr   ,  Nbodyr   zcontent-length)
r   rawZraw_headersr   namecontenthas_event_stream_outputhas_streaming_outputgetr   )http_responseoperation_modelresponse_dictlengthr   r   r   convert_to_response_dict   s&     r)   c                   @   sP   e Zd Zdd ZdddZdd Zdd	 Zd
d Zdd ZdddZ	dd Z
dS )AioEndpointc                    s   | j  I d H  d S N)http_sessionclose)selfr   r   r   r-   D   s    zAioEndpoint.closeNc                    sb   t |}|rTt|j|jg|_|jj }dj||j	d}| j
j|||j	dI d H  | |}|S )Nz&request-created.{service_id}.{op_name})
service_idZop_name)requestr   )r   anyr#   r"   Zstream_outputservice_modelr/   	hyphenizeformatr    _event_emitteremitprepare_request)r.   paramsr&   r0   r/   
event_nameprepared_requestr   r   r   create_requestG   s&     
zAioEndpoint.create_requestc           	         s   d}|d }|  || | ||I d H }| |||I d H \}}| |||||I d H r|d7 }|  ||| |  | ||I d H }| |||I d H \}}qB|d k	rd|d kr|d }||d d d< |d k	r|n|S d S )N   r   ZResponseMetadataZRetryAttempts)Z_update_retries_contextr;   _get_response_needs_retryZreset_stream)	r.   request_dictr&   attemptsr   r0   success_response	exceptionZtotal_retriesr   r   r   _send_request\   sJ      
  

zAioEndpoint._send_requestc           
         s   |  |||I d H \}}d d ||d}|d k	rR|\}}||d< t||I d H |d< |jj }	| jjd|	 d|j f|I d H  ||fS )N)r'   parsed_responser   rB   rD   r'   zresponse-received..)_do_get_responser)   r2   r/   r3   r5   r6   r    )
r.   r0   r&   r   rA   rB   Zkwargs_to_emitr%   rD   r/   r   r   r   r=      s0       
zAioEndpoint._get_responsec              
      s  zt d| td|j|j|j|j|jd |j	j
 }d| d|j }| jj||dI d H }t|}|d kr| |I d H }W nh tk
r } zd |f W Y S d }~X Y n< tk
r } zt jddd	 d |f W Y S d }~X Y nX t||I d H }	t||	||I d H  |	 }
|j|
d
< td|
 |jd }| j|}t|jrl||	|jI d H }n||	|j}|jdkr| |	|||I d H  td| ||fd fS )NzSending http request: %sZHTTP_REQUEST)methodr   	streamingurlr   zbefore-send.rE   )r0   z-Exception received when sending HTTP request.T)exc_inforH   ZHTTP_RESPONSEprotocolr   ZPARSED_RESPONSE) r   debugr   recordrG   r   Zhas_streaming_inputrI   r   r2   r/   r3   r    r5   r6   r   _sendr   	Exceptionr)   r   copyr#   metadataZ_response_parser_factorycreate_parserasyncioiscoroutinefunctionparseZoutput_shaper   _add_modeled_error_fields)r.   r0   r&   r   r/   r9   	responsesr%   er'   Zhttp_response_record_dictrK   parserrD   r   r   r   rF      s    
   


  
zAioEndpoint._do_get_responsec           	         st   | di  d}|d krd S |j}||}|d kr:d S t|jrZ|||I d H }n|||}|| d S )NErrorZCode)r$   r2   Zshape_for_error_coderS   rT   rU   update)	r.   r'   rD   r&   rY   Z
error_coder2   Zerror_shapeZmodeled_parser   r   r   rV      s    
z%AioEndpoint._add_modeled_error_fieldsc           
   	      sv   |j j }d| d|j }| jj||| ||||dI d H }t|}	|	d krRdS td|	 t	
|	I d H  dS d S )Nzneeds-retry.rE   )responseZendpointZ	operationr@   caught_exceptionr?   Fz3Response received to retry, sleeping for %s secondsT)r2   r/   r3   r    r5   r6   r   r   rL   rS   sleep)
r.   r@   r&   r?   r\   r]   r/   r9   rW   Zhandler_responser   r   r   r>      s(    	zAioEndpoint._needs_retryc                    s   | j |I d H S r+   )r,   send)r.   r0   r   r   r   rN     s    zAioEndpoint._send)N)NN)__name__
__module____qualname__r-   r;   rC   r=   rF   rV   r>   rN   r   r   r   r   r*   C   s   
)D  
 r*   c                
   @   s*   e Zd Zddeeedddddf
ddZdS )AioEndpointCreatorNc              
   C   st   t |st|std| |	d kr.| |}	|j}td|| |||	| |||
|||d}t||| j	||dS )NzInvalid endpoint: %szSetting %s timeout as %s)timeoutproxiesverifymax_pool_connectionssocket_optionsclient_certproxies_configconnector_args)endpoint_prefixZevent_emitterresponse_parser_factoryr,   )
r	   r
   
ValueErrorZ_get_proxiesrl   r   rL   Z_get_verify_valuer*   r5   )r.   r2   Zregion_nameZendpoint_urlrf   rm   rd   rg   Zhttp_session_clsre   rh   ri   rj   rk   rl   r,   r   r   r   create_endpoint!  s6    
z"AioEndpointCreator.create_endpoint)r`   ra   rb   r   r   r   ro   r   r   r   r   rc      s   rc   )rS   Zbotocore.endpointr   r   r   r   r   r   r   r	   r
   r   Zbotocore.hooksr   Zurllib3.responser   Zaiobotocore.httpchecksumr   Zaiobotocore.httpsessionr   Zaiobotocore.responser   r)   r*   rc   r   r   r   r   <module>   s   0, ^