U
    M8cC+                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ e eZG dd deZG dd	 d	eZG d
d deZdd ZG dd dZG dd dZG dd dZdS )    N)
xform_name)BotoCoreErrorConnectionErrorHTTPClientError)OperationNotFoundError)CachedPropertyc                   @   s   e Zd ZdS )EndpointDiscoveryExceptionN)__name__
__module____qualname__ r   r   6/tmp/pip-unpacked-wheel-ozje0y8b/botocore/discovery.pyr      s   r   c                   @   s   e Zd ZdZdZdS )EndpointDiscoveryRequiredzBEndpoint Discovery is disabled but is required for this operation.zAEndpoint Discovery is not enabled but this operation requires it.Nr	   r
   r   __doc__fmtr   r   r   r   r      s   r   c                   @   s   e Zd ZdZdZdS )EndpointDiscoveryRefreshFailedz=Endpoint Discovery failed to the refresh the known endpoints.z<Endpoint Discovery failed to refresh the required endpoints.Nr   r   r   r   r   r   #   s   r   c                 K   s   | j }|r|drt d S )Nrequired)endpoint_discoverygetr   )modelkwargsr   r   r   r   ,block_endpoint_discovery_required_operations)   s    r   c                   @   sN   e Zd Zdd Zedd Zedd Zdd Zd	d
 Zdd Z	dddZ
dS )EndpointDiscoveryModelc                 C   s
   || _ d S N)_service_model)selfservice_modelr   r   r   __init__0   s    zEndpointDiscoveryModel.__init__c                 C   s   | j j}t|jS r   )r   endpoint_discovery_operationr   name)r   discovery_operationr   r   r   discovery_operation_name3   s    z/EndpointDiscoveryModel.discovery_operation_namec                 C   s&   | j j}g }|jr"t|jj }|S r   )r   r   input_shapelistmemberskeys)r   r!   r&   r   r   r   discovery_operation_keys8   s
    z/EndpointDiscoveryModel.discovery_operation_keysc                 C   s8   z| j |}|jddW S  tk
r2   Y dS X d S )Nr   F)r   operation_modelr   r   r   )r   operation_namer(   r   r   r   discovery_required_for@   s    z-EndpointDiscoveryModel.discovery_required_forc                    s>   | j  |ds(|dd  |dd   fdd| D S )NIdentifiers	Operationc                    s   i | ]\}}| kr||qS r   r   .0kvZ
input_keysr   r   
<dictcomp>O   s       zEEndpointDiscoveryModel.discovery_operation_kwargs.<locals>.<dictcomp>)r'   r   popitemsr   r   r   r1   r   discovery_operation_kwargsI   s
    
z1EndpointDiscoveryModel.discovery_operation_kwargsc                 C   s   |  |j|S r   )_gather_idsr#   r   	operationparamsr   r   r   gather_identifiersQ   s    z)EndpointDiscoveryModel.gather_identifiersNc                 C   sb   |d kri }|j  D ]F\}}|jdr8|| ||< q|jdkr||kr| ||| | q|S )NZendpointdiscoveryidZ	structure)r%   r4   metadatar   	type_namer7   )r   shaper:   idsmember_nameZmember_shaper   r   r   r7   T   s    z"EndpointDiscoveryModel._gather_ids)N)r	   r
   r   r   r   r"   r'   r*   r6   r;   r7   r   r   r   r   r   /   s   

	r   c                   @   sn   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d Zdd Zdd ZdS )EndpointDiscoveryManagerNTc                 C   sP   |d kri }|| _ i | _|d kr&tj}|| _|| _t|| _t|j	j
| _d S r   )_cache_failed_attemptstime_time_always_discoverweakrefproxy_clientr   metar   _model)r   clientcachecurrent_timeZalways_discoverr   r   r   r   d   s    z!EndpointDiscoveryManager.__init__c                 C   s8   |d }|   }|D ]}|d}||d  |d< q|S )NZ	EndpointsZCachePeriodInMinutes<   
Expiration)rE   r   )r   response	endpointsrN   endpointZ
cache_timer   r   r   _parse_endpointsu   s    
z)EndpointDiscoveryManager._parse_endpointsc                 C   s"   t |trtt| S |S d S r   )
isinstancedicttuplesortedr4   )r   valuer   r   r   _cache_item}   s    
z$EndpointDiscoveryManager._cache_itemc                    s,    j jf |}t fddt| D S )Nc                 3   s   | ]\}}  |V  qd S r   )rZ   r-   r   r   r   	<genexpr>   s     z=EndpointDiscoveryManager._create_cache_key.<locals>.<genexpr>)rK   r6   rW   rX   r4   r5   r   r[   r   _create_cache_key   s    z*EndpointDiscoveryManager._create_cache_keyc                 C   s   | j ||S r   )rK   r;   r8   r   r   r   r;      s    z+EndpointDiscoveryManager.gather_identifiersc                 K   s"   | j f |}|| jkr| j|= d S r   )r]   rB   )r   r   	cache_keyr   r   r   delete_endpoints   s    
z)EndpointDiscoveryManager.delete_endpointsc                 K   s8   | j jf |}| j j}t| j|}td| |f |S )Nz%Discovering endpoints with kwargs: %s)rK   r6   r"   getattrrI   loggerdebug)r   r   r)   r!   r   r   r   _describe_endpoints   s
    z,EndpointDiscoveryManager._describe_endpointsc                    s.   || j krd S |    fdd| j | D S )Nc                    s   g | ]} |d  k r|qS )rP   r   )r.   enowr   r   
<listcomp>   s      zCEndpointDiscoveryManager._get_current_endpoints.<locals>.<listcomp>)rB   rE   )r   keyr   re   r   _get_current_endpoints   s    
z/EndpointDiscoveryManager._get_current_endpointsc              	   K   sr   | j f |}z4| jf |}| |}|| j|< | j|d  |W S  ttfk
rl   |  d | j|< Y d S X d S )NrO   )	r]   rc   rT   rB   rC   r3   r   r   rE   )r   r   r^   rQ   rR   r   r   r   _refresh_current_endpoints   s    

z3EndpointDiscoveryManager._refresh_current_endpointsc                 C   s0   || j kr,|  }|| j | k r$dS | j |= dS )NTF)rC   rE   )r   r^   rf   r   r   r   _recently_failed   s    
z)EndpointDiscoveryManager._recently_failedc                 C   s   |d d S )Nr   ZAddressr   )r   rR   r   r   r   _select_endpoint   s    z)EndpointDiscoveryManager._select_endpointc                 K   s   |d }| j |}| js0|s0td|  d S | jf |}| |}|rT| |S | |}|s|| j	f |}|r|| |S td| | j
|d }|r| |S |r|r| j	f |}|r| |S t d S )Nr,   zAOptional discovery disabled. Skipping discovery for Operation: %sz%Endpoint Discovery has failed for: %s)rK   r*   rF   ra   rb   r]   ri   rl   rk   rj   rB   r   r   )r   r   r9   Zdiscovery_requiredr^   rR   Zrecently_failedZstale_entriesr   r   r   describe_endpoint   s:    






z*EndpointDiscoveryManager.describe_endpoint)NNT)r	   r
   r   r   rT   rZ   r]   r;   r_   rc   ri   rj   rk   rl   rm   r   r   r   r   rA   c   s        
	rA   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )EndpointDiscoveryHandlerc                 C   s
   || _ d S r   )_manager)r   managerr   r   r   r      s    z!EndpointDiscoveryHandler.__init__c                 C   s:   | d| | j |d| | j | d| | j d S )Nzbefore-parameter-build.%szrequest-created.%szneeds-retry.%s)registerr;   Zregister_firstdiscover_endpointhandle_retries)r   eventsZ
service_idr   r   r   rq      s      z!EndpointDiscoveryHandler.registerc                 K   s0   |j }|d krd S | j||}d|i|d< d S )Nidentifiers	discovery)r   ro   r;   )r   r:   r   contextr   r   r?   r   r   r   r;      s
    z+EndpointDiscoveryHandler.gather_identifiersc                 K   sn   |j di d}|d kr d S | jj||d}|d krFtd d S |dsXd| }td| ||_d S )Nrv   ru   r,   r+   z&Failed to discover and inject endpointhttpzhttps://z!Injecting discovered endpoint: %s)rw   r   ro   rm   ra   rb   
startswithurl)r   requestr)   r   r?   rS   r   r   r   rr      s     

z*EndpointDiscoveryHandler.discover_endpointc           
      K   s   |d krd S |\}}| di  d}| di  d}|dkrL|dkrLd S | di }| di  d	}	|	d krvd S | jj|j|	d
 dS )NZResponseMetadataZHTTPStatusCodeErrorZCodei  ZInvalidEndpointExceptionrw   rv   ru   rx   r   )r   ro   r_   r    )
r   Zrequest_dictrQ   r9   r   _statusZ
error_coderw   r?   r   r   r   rs     s      z'EndpointDiscoveryHandler.handle_retriesN)r	   r
   r   r   rq   r;   rr   rs   r   r   r   r   rn      s
   	rn   )loggingrD   rG   Zbotocorer   Zbotocore.exceptionsr   r   r   Zbotocore.modelr   Zbotocore.utilsr   	getLoggerr	   ra   r   r   r   r   r   rA   rn   r   r   r   r   <module>   s   
4~