U
    bhE                     @  s   d dl mZ d dlmZ d dlmZ d dlZd dlmZm	Z	 d dl
mZ ddlmZmZ d	Zd
dlmZ dZedddgZdddddZG dd deZdS )    )annotations)deque)BytesION)Hubget_event_loop)	HttpError   )
BaseClientRequest)Urllib3Client   )bytes_to_strz!Mozilla/5.0 (compatible; urllib3)DELETEOPTIONSPATCHr
   z	list[str])requestreturnc                 C  s   g }| j r|d| j   | jr.|d n
|d | jrP|d| j  | jrh|d| j  | jr|d| j  |S )Nz
interface=zvalidate_cert=Truezvalidate_cert=Falsez	ca_certs=zclient_cert=zclient_key=)network_interfaceappendvalidate_certca_certsclient_cert
client_key)r   _pool_key_parts r   J/tmp/pip-unpacked-wheel-earovzxb/kombu/asynchronous/http/urllib3_client.py_get_pool_key_parts   s    
r   c                      s   e Zd ZdZi Zd!ddd fddZd	d
 Zdd Zdd ZddddZ	dd Z
dd ZddddZdd Zdd Zdd Zdd  Z  ZS )"r   zUrllib3 HTTP Client.N
   z
Hub | Noneint)hubmax_clientsc                   s:   |pt  }t | || _t | _| jd| j| _	d S )Ng      ?)
r   super__init__r    r   _pendingr   Zcall_repeatedly_timeout_check_timeout_check_tref)selfr   r    	__class__r   r   r"   1   s    
 zUrllib3Client.__init__c                 C  s&   | j  D ]}|  q
| j   d S N)_poolsvaluescloseclear)r&   poolr   r   r   pools_close:   s    
zUrllib3Client.pools_closec                 C  s   | j   |   d S r)   )r%   cancelr/   r&   r   r   r   r,   ?   s    
zUrllib3Client.closec                 C  s   | j | |   |S r)   )r#   r   _process_queuer&   r   r   r   r   add_requestC   s    zUrllib3Client.add_requestr
   r   c                 C  s  t |d}d }d }|jrtjjd |j|jd}|jrPtj|j d|j d}nd }|j	}|
d|  |r|
dt|  d|}|| jkr| j| S |rtj|| j|d}ntj| jd	}|jr|jd
f|jd< |jrd|jd< n
d|jd< |jd k	r|j|jd< n@|jdkrRzd
dlm} | |jd< W n tk
rP   Y nX |jd k	rj|j|jd< |jd k	r|j|jd< || j|< |S )Nr5   )schemehostport:)proxy_basic_authzproxy=zproxy_headers=|)	proxy_url	num_poolsproxy_headers)r=   r   source_addressCERT_REQUIRED	cert_reqs	CERT_NONEr   T)where	cert_filekey_file)r   Z
proxy_hosturllib3utilUrlZ
proxy_portZproxy_usernamemake_headersZproxy_passwordurlr   strjoinr*   ProxyManagerr    PoolManagerr   connection_pool_kwr   r   certifirC   ImportErrorr   r   )r&   r   r   Z
_proxy_urlr>   Z	_pool_key_poolrC   r   r   r   get_poolH   sb    






zUrllib3Client.get_poolc                 C  s   |    d S r)   _process_pending_requestsr1   r   r   r   r$      s    zUrllib3Client._timeout_checkc                 C  s    | j r| j  }| | q d S r)   )r#   popleft_process_requestr3   r   r   r   rU      s    
z'Urllib3Client._process_pending_requestsc                 C  s  |j }|dt|jpt |d|jr,dnd |jd k	rb|tj	j
|j d|jpVd d zV| j|d}|j|j|j||jd	|jd
}t|j}| j||j|j || d d}W nF tjjk
r } z$| j|di d d tdt|d}W 5 d }~X Y nX || d S )Nz
User-AgentzAccept-Encodingzgzip,deflatenoner9    )
basic_authr5   F)headersbodypreload_contentredirect)r   coder[   bufferZeffective_urlerroriW  )r[   
setdefaultr   
user_agentDEFAULT_USER_AGENTZuse_gzipZauth_usernameupdaterF   rG   rI   Zauth_passwordrS   r   methodrJ   r\   Zfollow_redirectsr   dataResponsestatusgeturl
exceptions	HTTPErrorr   rK   Zon_ready)r&   r   r[   rR   responser`   Zresponse_objer   r   r   rW      sX    



	zUrllib3Client._process_requestc                 C  s   |    d S r)   rT   r1   r   r   r   r2      s    zUrllib3Client._process_queuec                 C  s   d S r)   r   r&   fdr   r   r   on_readable   s    zUrllib3Client.on_readablec                 C  s   d S r)   r   ro   r   r   r   on_writable   s    zUrllib3Client.on_writablec                 C  s   d S r)   r   )r&   Zcurlr   r`   r[   r   r   r   _setup_request   s    zUrllib3Client._setup_request)Nr   )__name__
__module____qualname____doc__r*   r"   r/   r,   r4   rS   r$   rU   rW   r2   rq   rr   rs   __classcell__r   r   r'   r   r   ,   s   	J7r   )
__future__r   collectionsr   ior   rF   Zkombu.asynchronous.hubr   r   Zkombu.exceptionsr   baser	   r
   __all__Zutils.encodingr   rd   	frozensetZEXTRA_METHODSr   r   r   r   r   r   <module>   s   