U
    n/eZ                     @   s
  d dl mZ d dl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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lmZ d
dlmZmZ d
dlm Z  d
dl!m"Z" d
dl#m$Z$m%Z% d
dl&m'Z' e Z(G dd deZ)G dd deZ*dS )    )prepare_request_dict)
BaseClientClientCreatorClientEndpointBridgePaginatorDocstringloggerresolve_checksum_context)maybe_compress_request),block_endpoint_discovery_required_operations)OperationNotPageableErrorUnknownServiceError)get_global_history_recorder)first_non_none_response)get_service_module_name)
xform_name   )waiter)AioClientArgsCreator)AioEndpointDiscoveryHandlerAioEndpointDiscoveryManager)apply_request_checksum)AioPaginator)adaptivestandard)AioS3RegionRedirectorv2c                   @   sV   e Zd ZdddZdd Zdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd ZdS )AioClientCreatorTNc                    s$  | j jd|dI d H }t||d}| ||}z| ||}| jd}W n( tk
rr   d }d }t	d| Y nX | 
||I d H }| ||	\}}	t| j||	|jd| j|jdd}| ||||||||	||
||}|f |}| | | j|d d |	|d	 | j|d
 | |||	 |S )Nzchoose-service-name)service_namedefaultZ
partitionszSNo endpoints ruleset found for service %s, falling back to legacy endpoint routing.ZsigningNameZsignatureVersion)Zservice_signing_nameconfig_storeZservice_signature_version)clientendpoint_bridgeendpoint_urlclient_configscoped_configr    )_event_emitteremitr   Z_load_service_modelZ_load_service_endpoints_ruleset_loader	load_datar   r   info_create_client_classZ_normalize_fips_regionr   Z_endpoint_resolvermetadataget_config_store_get_client_args_register_retries_register_s3_eventsZ_register_s3_control_events_register_endpoint_discovery)selfr   region_name	is_securer"   verifycredentialsr$   api_versionr#   
auth_token	responsesservice_modelendpoints_ruleset_datapartition_dataclsr!   Zclient_argsZservice_client r?   6/tmp/pip-unpacked-wheel-8mnez3y6/aiobotocore/client.pycreate_client   s      
 



  zAioClientCreator.create_clientc           	         sh   |  |}| |}||d< tg}|j }| jjd| ||dI d H  t|}tt	|t
||}|S )N_PY_TO_OP_NAMEzcreating-client-class.%s)class_attributesZbase_classes)Z_create_methodsZ_create_name_mappingAioBaseClient
service_id	hyphenizer&   r'   r   typestrtuple)	r3   r   r;   rC   Zpy_name_to_operation_namebasesrE   
class_namer>   r?   r?   r@   r+   i   s    


z%AioClientCreator._create_client_classc                 C   sV   |j jjd }|dkr"| | n0|dkr@| | | | n|dkrR| | d S )Nmoder   r   legacy)metaconfigretries_register_v2_standard_retries_register_v2_adaptive_retries_register_legacy_retries)r3   r    Z
retry_moder?   r?   r@   r0   x   s    
z"AioClientCreator._register_retriesc                 C   s8   |j jjd}d|i}|d k	r(||d< tjf | d S )NZtotal_max_attemptsr    max_attempts)rN   rO   rP   r-   r   register_retry_handler)r3   r    rT   kwargsr?   r?   r@   rQ      s
    z.AioClientCreator._register_v2_standard_retriesc                 C   s   t | d S N)r   rU   )r3   r    r?   r?   r@   rR      s    z.AioClientCreator._register_v2_adaptive_retriesc           
      C   s   |j jj}|j jj}| }| jd}|s0d S | |j jj	}| j
||di |di |}td|j jj | j||}d| }	|j jjd| ||	d d S )NZ_retryretryZdefinitionsz*Registering retry handlers for service: %szretry-config-%szneeds-retry.)	unique_id)rN   r;   endpoint_prefixrE   rF   r(   r)   Z_transform_legacy_retriesrO   rP   Z_retry_config_translatorZbuild_retry_configr-   r   debugr   Z_retry_handler_factoryZcreate_retry_handlereventsregister)
r3   r    rZ   rE   Zservice_event_nameZoriginal_configrP   Zretry_confighandlerrY   r?   r?   r@   rS      s6    



   z)AioClientCreator._register_legacy_retriesc           
      C   s   |d k	rd S |j jjd krd S |j j}|j jj }d}|rN|jd k	rN|j}n| jr`| jd}| 	|}|r| 
||r|dk}t||d}t|}	|	|| n|dt d S )NFendpoint_discovery_enabledT)Zalways_discoverzbefore-parameter-build)rN   r;   Zendpoint_discovery_operationr\   rE   rF   r_   r.   Zget_config_variableZ$_normalize_endpoint_discovery_configZ_requires_endpoint_discoveryr   r   r]   r
   )
r3   r    r"   rO   r\   rE   ZenabledZdiscovermanagerr^   r?   r?   r@   r2      s4    
 z-AioClientCreator._register_endpoint_discoveryc                 C   s4   |j jjdkrd S td |  | |j || d S )NZs3)rN   r;   r   r   r]   Z!_set_s3_presign_signature_version)r3   r    r!   r"   r#   r$   r?   r?   r@   r1      s      z$AioClientCreator._register_s3_eventsc                 C   sD   t | j| j| j| j| j| j| jd}||||||||||	|
||S )N)r   Zuser_agent_creator)	r   r&   Z_user_agentZ_response_parser_factoryr(   Z_exceptions_factoryr.   _user_agent_creatorZget_client_args)r3   r;   r4   r5   r"   r6   r7   r$   r#   r!   r9   r<   r=   Zargs_creatorr?   r?   r@   r/      s.    	z!AioClientCreator._get_client_args)TNNNNNNN)__name__
__module____qualname__rA   r+   r0   rQ   rR   rS   r2   r1   r/   r?   r?   r?   r@   r      s"           
K r   c                   @   sp   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdddZdd Z	dddZ
dd Zdd Zdd Zdd ZdS )rD   c                    s4   d | jj |}| jjj|| dI d H \}}|S )Nzgetattr.{}.{}r%   )format_service_modelrE   rF   rN   r\   emit_until_response)r3   itemZ
event_namer^   event_responser?   r?   r@   _async_getattr  s    
  zAioBaseClient._async_getattrc                 C   s   t d| jj|d S )Nz!'{}' object has no attribute '{}')AttributeErrorre   	__class__rb   )r3   rh   r?   r?   r@   __getattr__&  s     zAioBaseClient.__getattr__c                    s   | j  I dH  dS )z'Closes underlying endpoint connections.N)	_endpointcloser3   r?   r?   r@   ro   0  s    zAioBaseClient.closec                    s  | j |}| j j}td|||d |jr<td|| | jj	| jj
|j|jd}| j|||dI d H }| |||I d H \}}| j|||||dI d H }t||| | j j }	| jjjdj|	|d||| j|d	I d H \}
}|d k	r|\}}n0t| jj
|| t| | |||I d H \}}| jjjd
j|	|d||||dI d H  |jdkr|di d}| j|}|||n|S d S )NZAPI_CALL)ZserviceZ	operationparamszWarning: %s.%s() is deprecated)Zclient_regionr#   has_streaming_input	auth_type)
api_paramsoperation_modelcontext)rt   ru   r"   rv   headersz)before-call.{service_id}.{operation_name}rE   operation_name)modelrq   Zrequest_signerrv   z(after-call.{service_id}.{operation_name})http_responseparsedrz   rv   i,  ErrorZCode)rf   ru   r   history_recorderrecord
deprecatedr   r[   rN   r4   rO   rr   rs   _emit_api_params_resolve_endpoint_ruleset_convert_to_request_dictr   rE   rF   r\   rg   re   Z_request_signerr	   r   _make_requestr'   status_coder-   
exceptionsZ	from_code)r3   ry   rt   ru   r   request_contextr"   additional_headersrequest_dictrE   r^   ri   httpZparsed_responseZ
error_codeZerror_classr?   r?   r@   _make_api_call4  s         

     
zAioBaseClient._make_api_callc              
      sn   z| j ||I d H W S  tk
rh } z4| jjjdj| jj	 |j
d||dI d H   W 5 d }~X Y nX d S )Nz.after-call-error.{service_id}.{operation_name}rx   )	exceptionrv   )rn   Zmake_request	ExceptionrN   r\   r'   re   rf   rE   rF   name)r3   ru   r   r   er?   r?   r@   r     s     
zAioBaseClient._make_requestNTc           	         s`   | j ||}| jjs"|dd  |d k	r8|d | |rH| j }nd }t||||d |S )Nhost_prefixrw   )r"   
user_agentrv   )	Z_serializerZserialize_to_requestZ_client_configZinject_host_prefixpopupdatera   Z	to_stringr   )	r3   rt   ru   r"   rv   rw   Zset_user_agent_headerr   r   r?   r?   r@   r     s$    	 z&AioBaseClient._convert_to_request_dictc                    sr   |j }| jj }| jjjd| d| |||dI d H }t||d}| jjjd| d| |||dI d H  |S )Nzprovide-client-params..)rq   rz   rv   r   zbefore-parameter-build.)r   rf   rE   rF   rN   r\   r'   r   )r3   rt   ru   rv   ry   rE   r:   r?   r?   r@   r     s     zAioBaseClient._emit_api_paramsFc                    s   | j dkr| jj}i }n| j j|||dI dH }|j}|j}|jd}|dk	r| j |}	|	\}
}|
|d< d|kr~|r~|d= d|kr|d 	| n||d< ||fS )a  Returns endpoint URL and list of additional headers returned from
        EndpointRulesetResolver for the given operation and params. If the
        ruleset resolver is not available, for example because the service has
        no endpoints ruleset file, the legacy endpoint resolver's value is
        returned.

        Use ignore_signing_region for generating presigned URLs or any other
        situtation where the signing region information from the ruleset
        resolver should be ignored.

        Returns tuple of URL and headers dictionary. Additionally, the
        request_context dict is modified in place with any signing information
        returned from the ruleset resolver.
        N)ru   Z	call_argsr   ZauthSchemesrs   ZregionZsigning)
Z_ruleset_resolverrN   r"   Zconstruct_endpointurlrw   Z
propertiesr-   Zauth_schemes_to_signing_ctxr   )r3   ru   rq   r   Zignore_signing_regionr"   r   Zendpoint_infoZauth_schemesZ	auth_infors   Zsigning_contextr?   r?   r@   r     s.    
z'AioBaseClient._resolve_endpoint_rulesetc           
      C   s   |  |st|dn| j| }dd }| jd | }t|| jj| jj|dd|_t	| jj}| d| }t
|tfd|i}| j|}|t| |||}	|	S d	S )
a  Create a paginator for an operation.

        :type operation_name: string
        :param operation_name: The operation name.  This is the same name
            as the method name on the client.  For example, if the
            method name is ``create_foo``, and you'd normally invoke the
            operation as ``client.create_foo(**kwargs)``, if the
            ``create_foo`` operation can be paginated, you can use the
            call ``client.get_paginator("create_foo")``.

        :raise OperationNotPageableError: Raised if the operation is not
            pageable.  You can use the ``client.can_paginate`` method to
            check if an operation is pageable.

        :rtype: L{botocore.paginate.Paginator}
        :return: A paginator object.

        )ry   c                 [   s   t j| f|S rW   )r   paginate)r3   rV   r?   r?   r@   r     s    z-AioBaseClient.get_paginator.<locals>.paginateZpage_configF)Zpaginator_nameZevent_emitterr;   paginator_configZinclude_signaturez.Paginator.r   N)Zcan_paginater   rB   _cacher   rN   r\   r;   __doc__r   rG   r   rf   ru   getattr)
r3   ry   Zactual_operation_namer   r   Zservice_module_nameZpaginator_class_nameZdocumented_paginator_clsru   Z	paginatorr?   r?   r@   get_paginator  sB    

	  zAioBaseClient.get_paginatorc                 C   sd   |   }|std| t|}i }|jD ]}||t|< q,||krRtd| t|| || S )aG  Returns an object that can wait for some condition.

        :type waiter_name: str
        :param waiter_name: The name of the waiter to get. See the waiters
            section of the service docs for a list of available waiters.

        :returns: The specified waiter object.
        :rtype: botocore.waiter.Waiter
        zWaiter does not exist: %s)Z_get_waiter_config
ValueErrorr   ZWaiterModelZwaiter_namesr   Zcreate_waiter_with_client)r3   Zwaiter_namerO   rz   mappingr   r?   r?   r@   
get_waiter?  s    


  zAioBaseClient.get_waiterc                    s   | j j I d H  | S rW   )rn   http_session
__aenter__rp   r?   r?   r@   r   W  s    zAioBaseClient.__aenter__c                    s   | j j|||I d H  d S rW   )rn   r   	__aexit__)r3   exc_typeexc_valexc_tbr?   r?   r@   r   [  s    zAioBaseClient.__aexit__)NNT)F)rb   rc   rd   rj   rm   ro   r   r   r   r   r   r   r   r   r   r?   r?   r?   r@   rD     s    

O   
 
2CrD   N)+Zbotocore.awsrequestr   Zbotocore.clientr   r   r   r   r   r   Zbotocore.compressr	   Zbotocore.discoveryr
   Zbotocore.exceptionsr   r   Zbotocore.historyr   Zbotocore.hooksr   Zbotocore.utilsr   Zbotocore.waiterr    r   argsr   Z	discoveryr   r   Zhttpchecksumr   r   r   rP   r   r   utilsr   r~   r   rD   r?   r?   r?   r@   <module>   s(       