a
    !fmk                     @   s2  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZmZmZ d dlmZmZ zd dlmZ d dlm	Z W n ey   Y n0 zd dlZW n ey   dZY n0 zd dlZW n ey   dZY n0 zDe e!d< ej"Z#e$e!d< e$e!d< e$e!d	< d
d e#%dD \Z&Z'Z(W n e)yl   Y n(0 e&e'e(fdk re j*+de#f  dZzd dl,m-Z- W n ey   dZ-Y n0 d dl.m/Z/m0Z0 dd Z1dd Z2G dd de3Z4G dd de4Z5G dd de4Z6G dd de4Z7G dd de4Z8dS )    N)errorutil)RequestMetrics)AnyDictOptionalTupleClassVarUnioncast)NoReturn	TypedDictversionmajorminorpatchc                 C   s   g | ]}t |qS  )int).0ir   r   S/var/www/html/python-backend/venv/lib/python3.9/site-packages/stripe/http_client.py
<listcomp>-       r   .)r      r   a  Warning: the Stripe library requires that your Python "requests" library be newer than version 0.8.8, but your "requests" library is version %s. Stripe will fall back to an alternate HTTP library so everything should work. We recommend upgrading your "requests" library. If you have any questions, please contact support@stripe.com. (HINT: running "pip install -U requests" should upgrade your requests library to the latest version.))urlfetch)urlparseParseResultc                   C   s   t tt d S )Ni  )r   roundtimer   r   r   r   _now_msH   s    r    c                  O   sD   t r
t}n,trt}n"trt}nt}tjdk r6t	
d || i |S )N)      	   zWarning: the Stripe library is falling back to urllib2 because neither requests nor pycurl are installed. urllib2's SSL implementation doesn't verify server certificates. For improved security, we suggest installing requests.)r   UrlFetchClientrequestsRequestsClientpycurlPycurlClientUrllib2Clientsysversion_infowarningswarn)argskwargsimplr   r   r   new_default_http_clientL   s    
r1   c                   @   s   e Zd ZU ee ed< G dd deZdZdZ	dZ
ee ed< eed< d)eeeeef  dddZd*eeeef dddZd+eeeef dddZdd Zd,ddZd-ddZdd Zdd Zd.ddZd/dd Zd!d" Zd#d$ Zd%d& Zd'd( Zd
S )0
HTTPClientnamec                   @   s&   e Zd ZU ee ed< ee ed< dS )zHTTPClient._ProxyhttphttpsN)__name__
__module____qualname__r   str__annotations__r   r   r   r   _Proxyd   s   
r;   r!         ?<   _proxy_verify_ssl_certsTNverify_ssl_certsproxyc                 C   sP   || _ |r0t|tr||d}t|ts0td|r<| nd | _t | _	d S )N)r4   r5   zqProxy(ies) must be specified as either a string URL or a dict() with string URL under the https and/or http keys.)
r?   
isinstancer9   dict
ValueErrorcopyr>   	threadinglocal_thread_localselfrA   rB   r   r   r   __init__n   s    

	zHTTPClient.__init__returnc                 C   s   | j ||||ddS NFis_streaming_request_with_retries_internalrK   methodurlheaders	post_datar   r   r   request_with_retries   s    
zHTTPClient.request_with_retriesc                 C   s   | j ||||ddS NTrP   rR   rT   r   r   r   request_stream_with_retries   s    
z&HTTPClient.request_stream_with_retriesc              
   C   s   |  | d}t }z.|r,| ||||}n| ||||}d }	W n. tjyp }
 z|
}	d }W Y d }
~
n
d }
~
0 0 | ||	|r|	rtd|	j	  |d7 }| 
||}td||||f  t| q|d ur| || |S |	d usJ |	qd S )Nr   z Encountered a retryable error %s   zBInitiating retry %i for request %s %s after sleeping %.2f seconds.)_add_telemetry_headerr    request_streamrequestr   APIConnectionError_should_retryr   Zlog_infoZuser_message_sleep_time_secondsr   sleep_record_request_metrics)rK   rU   rV   rW   rX   rQ   num_retriesrequest_startresponseZconnection_erroreZ
sleep_timer   r   r   rS      sB    

z)HTTPClient._request_with_retries_internalc                 C   s   t dd S )Nz.HTTPClient subclasses must implement `request`NotImplementedErrorrT   r   r   r   r_      s    zHTTPClient.requestc                 C   s   t dd S )Nz5HTTPClient subclasses must implement `request_stream`ri   rT   r   r   r   r^      s    zHTTPClient.request_streamc                 C   st   ||   krdS |d u r|jS |\}}}|d urXd|v rX|d dkrHdS |d dkrXdS |dkrddS |dkrpdS dS )NFzstripe-should-retryfalsetrueTi  i  )_max_network_retriesshould_retry)rK   rg   Zapi_connection_errorre   _status_coderheadersr   r   r   ra      s    
zHTTPClient._should_retryc                 C   s   ddl m} |S )Nr   )max_network_retries)striperr   )rK   rr   r   r   r   rm      s    zHTTPClient._max_network_retriesc              	   C   sB   |d u rd S |\}}}zt |d W S  ttfy<   Y d S 0 d S )Nzretry-after)r   KeyErrorrE   )rK   rg   ro   rq   r   r   r   _retry_after_header   s    
zHTTPClient._retry_after_headerc                 C   sV   t tjd|d   tj}| |}ttj|}| |p<d}|tjkrRt||}|S )Nr!   r\   r   )minr2   INITIAL_DELAY	MAX_DELAY_add_jitter_timemaxru   MAX_RETRY_AFTER)rK   re   rg   sleep_secondsretry_afterr   r   r   rb     s    


zHTTPClient._sleep_time_secondsc                 C   s   |ddt dd  9 }|S )Nr<   r\   r   )randomuniform)rK   r|   r   r   r   ry     s    zHTTPClient._add_jitter_timec                 C   s6   t | jdd }tjr2|r2d| i}t||d< d S )Nlast_request_metricszX-Stripe-Client-Telemetry)getattrrI   rs   enable_telemetrypayloadjsondumps)rK   rW   r   Z	telemetryr   r   r   r]     s    
z HTTPClient._add_telemetry_headerc                 C   s<   |\}}}d|v r8t jr8|d }t | }t||| j_d S )Nz
Request-Id)rs   r   r    r   rI   r   )rK   rg   rf   ro   rq   Z
request_idZrequest_duration_msr   r   r   rd   )  s    

z"HTTPClient._record_request_metricsc                 C   s   t dd S )Nz,HTTPClient subclasses must implement `close`ri   rK   r   r   r   close2  s    zHTTPClient.close)TN)N)N)N)N)N)N)r6   r7   r8   r	   r9   r:   r   r;   rx   rw   r{   r   boolr
   rL   r   r   r   rY   r[   rS   r_   r^   ra   rm   ru   rb   ry   r]   rd   r   r   r   r   r   r2   a   s@   
    /

%



	r2   c                       sT   e Zd ZdZd fdd	ZdddZddd	Zd
d ZedddZ	dd Z
  ZS )r&   r%   P   Nc                    s8   t t| jf i | || _|| _td us.J t| _d S N)superr&   rL   _session_timeoutr%   )rK   timeoutsessionr/   	__class__r   r   rL   ;  s
    zRequestsClient.__init__c                 C   s   | j ||||ddS rO   _request_internalrT   r   r   r   r_   C  s    
zRequestsClient.requestc                 C   s   | j ||||ddS rZ   r   rT   r   r   r   r^   H  s    
zRequestsClient.request_streamc              
   C   s  i }| j rtj|d< nd|d< | jr.| j|d< |r:d|d< t| jdd d u r`| jpZ| j | j_	zvz&| jj	j
||f||| jd|}W n2 ty } ztd|f W Y d }~n
d }~0 0 |r|j}	n|j}	|j}
W n0 ty } z| | W Y d }~n
d }~0 0 |	|
|jfS )	NverifyFproxiesTstreamr   )rW   datar   a  Warning: It looks like your installed version of the "requests" library is not compatible with Stripe's usage thereof. (HINT: The most likely cause is that your "requests" library is out of date. You can fix that by running "pip install -U requests".) The underlying error was: %s)r?   rs   ca_bundle_pathr>   r   rI   r   r%   Sessionr   r_   r   	TypeErrorrawcontentrp   	Exception_handle_request_errorrW   )rK   rU   rV   rW   rX   rQ   r/   resultrh   r   rp   r   r   r   r   M  sH    

	
 z RequestsClient._request_internalrM   c                 C   s   t || jjjr0d}dt|jt|f }d}nt || jjj| jjjfrjd}dt|jt|f }d}nlt || jjj	rd}dt|jt|f }d}n<d}dt|jf }t|r|dt|f 7 }n|d	7 }d}t
|d
|f  }tj||dd S )NCould not verify Stripe's SSL certificate.  Please make sure that your network is not intercepting certificates.  If this problem persists, let us know at support@stripe.com.%s: %sFziUnexpected error communicating with Stripe.  If this problem persists, let us know at support@stripe.com.TzUnexpected error communicating with Stripe. It looks like there's probably a configuration issue locally.  If this problem persists, let us know at support@stripe.com.zA %s was raisedz with error message %sz with no error messagez

(Network error: %s)rn   )rC   r%   
exceptionsSSLErrortyper6   r9   TimeoutConnectionErrorRequestExceptiontextwrapfillr   r`   )rK   rh   msgerrrn   r   r   r   r     s:    z$RequestsClient._handle_request_errorc                 C   s"   t | jdd d ur| jj  d S )Nr   )r   rI   r   r   r   r   r   r   r     s    zRequestsClient.close)r   N)N)Nr6   r7   r8   r3   rL   r_   r^   r   r   r   r   __classcell__r   r   r   r   r&   8  s   

65r&   c                       sT   e Zd ZdZd fdd	ZdddZdd	d
Zdd ZedddZ	dd Z
  ZS )r$   r   TN7   c                    sB   t t| j||d |r td|| _|| _td us8J t| _d S )Nr@   zNo proxy support in urlfetch library. Set stripe.default_http_client to either RequestsClient, PycurlClient, or Urllib2Client instance to use a proxy.)r   r$   rL   rE   r?   	_deadliner   )rK   rA   rB   deadliner   r   r   rL     s    
zUrlFetchClient.__init__c                 C   s   | j ||||ddS rO   r   rT   r   r   r   r_     s    
zUrlFetchClient.requestc                 C   s   | j ||||ddS rZ   r   rT   r   r   r   r^     s    
zUrlFetchClient.request_streamc           	   
   C   s   z | j j|||| j| j|d}W n4 | j jyT } z| || W Y d }~n
d }~0 0 |rptjt	
|j}n|j}||j|jfS )N)rV   rU   rW   Zvalidate_certificater   r   )r   fetchr?   r   Errorr   r   ioBytesIOr9   encoder   rp   rW   )	rK   rU   rV   rW   rX   rQ   r   rh   r   r   r   r   r     s    
"z UrlFetchClient._request_internalrM   c                 C   sn   t || jjrd|f }n,t || jjr.d}nt || jjrBd}nd}t|d t| d }t	|d S )NzThe Stripe library attempted to fetch an invalid URL (%r). This is likely due to a bug in the Stripe Python bindings. Please let us know at support@stripe.com.z0There was a problem retrieving data from Stripe.zThere was a problem receiving all of your data from Stripe.  This is likely due to a bug in Stripe. Please let us know at support@stripe.com.hUnexpected error communicating with Stripe. If this problem persists, let us know at support@stripe.com.

(Network error: ))
rC   r   ZInvalidURLErrorZDownloadErrorZResponseTooLargeErrorr   r   r9   r   r`   )rK   rh   rV   r   r   r   r   r     s    z$UrlFetchClient._handle_request_errorc                 C   s   d S r   r   r   r   r   r   r     s    zUrlFetchClient.close)TNr   )N)Nr   r   r   r   r   r$     s   

r$   c                       s   e Zd ZU G dd deddZdZee ed< d fd	d
	Zdd Z	dddZ
dddZdd ZedddZee dddZdd Z  ZS )r(   c                   @   s&   e Zd ZU ee ed< ee ed< dS )zPycurlClient._ParsedProxyr4   r5   N)r6   r7   r8   r   r   r:   r   r   r   r   _ParsedProxy  s   
r   F)totalr'   _parsed_proxyTNc                    sl   t t| j||d td us J t| _t | _i | _| jrh| j}| D ]\}}t	t
t|| j|< qJd S Nr@   )r   r(   rL   r'   ZCurl_curlr   r>   itemsr   r   r9   )rK   rA   rB   Zproxy_schemevaluer   r   r   rL     s    

zPycurlClient.__init__c                 C   s@   d|vri S | ddd }t|}tdd t| D S )Nz
r\   c                 s   s   | ]\}}|  |fV  qd S r   lowerr   kvr   r   r   	<genexpr>5  r   z-PycurlClient.parse_headers.<locals>.<genexpr>)splitemailmessage_from_stringrD   r   )rK   r   Zraw_headersrW   r   r   r   parse_headers0  s
    
zPycurlClient.parse_headersc                 C   s   | j ||||ddS rO   r   rT   r   r   r   r_   7  s    
zPycurlClient.requestc                 C   s   | j ||||ddS rZ   r   rT   r   r   r   r^   <  s    
zPycurlClient.request_streamc              
   C   sH  t j }t j }| j  | |}|r|jrF| j| jj	|j |j
r`| j| jj|j
 |jsl|jr| j| jjd|j|jf  |dkr| j| jjd nD|dkr| j| jjd | j| jj| n| j| jj|  | j| jj| | j| jj|j | j| jj|j | j| jjd | j| jjd | j| jjd | j| jjdd tt| D  | jr| j| jj t!j" n| j| jj#d	 z| j$  W n4 | jj%y }	 z| &|	 W Y d }	~	n
d }	~	0 0 |r|'d
 |}
n|( )d}
| j*| jj+}| ,|( )d}|
||fS )Nz%s:%sgetr\   post   r   c                 S   s   g | ]\}}d ||f qS )r   r   r   r   r   r   r   k  r   z2PycurlClient._request_internal.<locals>.<listcomp>Fr   utf-8)-r   r   r   r   reset
_get_proxyhostnameZsetoptr'   ZPROXYportZ	PROXYPORTusernamepasswordZPROXYUSERPWDZHTTPGETPOSTZ
POSTFIELDSZCUSTOMREQUESTupperURLZWRITEFUNCTIONwriteZHEADERFUNCTIONZNOSIGNALZCONNECTTIMEOUTTIMEOUTZ
HTTPHEADERiterrD   r   r?   ZCAINFOrs   r   ZSSL_VERIFYHOSTZperformr   r   seekgetvaluedecodegetinfoZRESPONSE_CODEr   )rK   rU   rV   rW   rX   rQ   brq   rB   rh   rcontentrcoder   r   r   r   A  sX    



 
zPycurlClient._request_internalrM   c                 C   s   |j d | jj| jj| jjfv r*d}d}n,|j d | jj| jjfv rNd}d}nd}d}t|d |j d  d	 }t	j
||d
d S )Nr   zCould not connect to Stripe.  Please check your internet connection and try again.  If this problem persists, you should check Stripe's service status at https://twitter.com/stripestatus, or let us know at support@stripe.com.Tr   Fr   r   r\   r   r   )r.   r'   ZE_COULDNT_CONNECTZE_COULDNT_RESOLVE_HOSTZE_OPERATION_TIMEOUTEDZE_SSL_CACERTZE_SSL_PEER_CERTIFICATEr   r   r   r`   )rK   rh   r   rn   r   r   r   r     s(    z"PycurlClient._handle_request_errorc                 C   sD   | j r@| j }|r|dd nd }|r@||||dd S d S )N:r   )r   r   r   )rK   rV   rB   r   r   r   r   r     s    zPycurlClient._get_proxyc                 C   s   d S r   r   r   r   r   r   r     s    zPycurlClient.close)TN)N)N)r6   r7   r8   r   r   r3   r   r:   rL   r   r_   r^   r   r   r   r   r   r   r   r   r   r   r   r(     s   


A#r(   c                       sT   e Zd ZdZd fdd	ZdddZddd	Zd
d ZedddZ	dd Z
  ZS )r)   zurllib.requestTNc                    sJ   t t| j||d d | _| jrFtttt	t	f | j}t
|| _d S r   )r   r)   rL   _openerr>   urllibrequestProxyHandlerr   r   r9   build_openerrJ   r   r   r   rL     s    
zUrllib2Client.__init__c                 C   s   | j ||||ddS rO   r   rT   r   r   r   r_     s    
zUrllib2Client.requestc                 C   s   | j ||||ddS rZ   r   rT   r   r   r   r^     s    
zUrllib2Client.request_streamc              
      s  t |tr|d}t|||} dvr8 fdd|_zD| jrL| j|nt|}|r`|}n|	 }|j
}	t| }W nr tjy }
 z&|
j
}	|
	 }t|
 }W Y d }
~
n<d }
~
0  tjtfy }
 z| |
 W Y d }
~
n
d }
~
0 0 tdd tt| D }||	|fS )Nr   )r   r   c                      s      S r   )r   r   rU   r   r   <lambda>  r   z1Urllib2Client._request_internal.<locals>.<lambda>c                 s   s   | ]\}}|  |fV  qd S r   r   r   r   r   r   r     r   z2Urllib2Client._request_internal.<locals>.<genexpr>)rC   r9   r   r   Request
get_methodr   openurlopenreadcoderD   infourlliberror	HTTPErrorURLErrorrE   r   r   r   )rK   rU   rV   rW   rX   rQ   reqrg   r   r   rh   Zlhr   r   r   r     s.    

  zUrllib2Client._request_internalrM   c                 C   s,   d}t |d t| d }t|d S )Nr   r   r   )r   r   r9   r   r`   )rK   rh   r   r   r   r   r     s    z#Urllib2Client._handle_request_errorc                 C   s   d S r   r   r   r   r   r   r     s    zUrllib2Client.close)TN)N)Nr   r   r   r   r   r)     s   

"r)   )9r*   r   r,   r   r   r~   rG   r   rs   r   r   Zstripe.request_metricsr   typingr   r   r   r   r	   r
   r   Ztyping_extensionsr   r   urllib.requestr_   r   urllib.errorr   ImportErrorr'   r%   r9   r:   __version__r   r   r   r   r   r   r   stderrr   Zgoogle.appengine.apir   urllib.parser   r   r    r1   objectr2   r&   r$   r(   r)   r   r   r   r   <module>   sp   
$



 X W 