o
    i9                     @   s<  d dl m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 d dlZd dlmZ d dlmZ d dlmZ d dlmZmZmZ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!m"Z"m#Z#m$Z$m%Z%m&Z& e!rd d	l'm(Z( zd d
l)m*Z+ W n	 e,y   Y nw zd dl-m.Z/ d dl-m0Z1 W n	 e,y   Y nw zd dl2m3Z4 d dl2m5Z6 W n	 e,y   Y nw dd Z7dededdfddZ8dededdfddZ9G dd de:Z;G dd de;Z<G dd de;Z=G dd de$Z>G d d! d!e;Z?G d"d# d#e;Z@G d$d% d%e;ZAG d&d' d'e;ZBG d(d) d)e;ZCdS )*    )BytesION)HTTPResponse)_util)RequestMetrics)APIConnectionError)AnyDictIterableListMappingMutableMappingOptionalTupleClassVarUnioncastoverloadAsyncIterable)TYPE_CHECKINGLiteralNoReturn	TypedDict	AwaitableNever)ParseResult)Session)Timeout)Client)ClientTimeout)StreamReaderc                   C   s   t tt d S )Ni  )introundtime r#   r#   s/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/stripe/_http_client.py_now_ms?   s   r%   argskwargsreturn
HTTPClientc                  O   s   zddl m} W n	 ty   Y nw t| i |S zddl}W n	 ty(   Y nw t| i |S zddl}W n	 ty?   Y nw t| i |S t| i |S )a  
    This method creates and returns a new HTTPClient based on what libraries are available. It uses the following precedence rules:

    1. Urlfetch (this is provided by Google App Engine, so if it's present you probably want it)
    2. Requests (popular library, the top priority for all environments outside Google App Engine, but not always present)
    3. Pycurl (another library, not always present, not as preferred as Requests but at least it verifies SSL certs)
    4. urllib with a warning (basically always present, a reasonable final default)

    For performance, it only imports what it's actually going to use. But, it re-calculates every time its called, so probably save its result instead of calling it multiple times.
    r   urlfetchN)	google.appengine.apir+   ImportErrorUrlFetchClientrequestsRequestsClientpycurlPycurlClientUrllibClient)r&   r'   r+   r/   r1   r#   r#   r$   new_default_http_clientC   s&   r4   c                  O   sr   z
ddl }ddl}W n	 ty   Y nw t| i |S zddl}W n	 ty*   Y nw t| i |S t| i |S )z~
    Similar to `new_default_http_client` above, this returns a client that can handle async HTTP requests, if available.
    r   N)httpxanyior-   HTTPXClientaiohttpAIOHTTPClientNoImportFoundAsyncClient)r&   r'   r5   r6   r8   r#   r#   r$   new_http_client_async_fallbackf   s   r;   c                   @   s$  e Zd ZU dZee ed< G dd deZdZ	dZ
dZee ed< eed	< 	
			dHdedeeeef  ded  fddZdeeeeeeeef  f  dee dedee fddZ	dIdeeeeeeef f  fddZ	dIdedeeeeeeef f  defddZdedefddZdeeef deeef fd d!Zd"d# Z		dJdd$d%ed&edeeef d'edee d(eee  deeeeeef f fd)d*Z		dJdd$d%ed&edeeef d(eee  deeeeeef f f
d+d,Zdd$d%ed&edeeef d'ed-edee d(eee  deeeeeef f fd.d/Z	dIdd$d%ed&edeeeef  d'ed(eee  deeeeeef f fd0d1Z 	dIdd$d%ed&edeeeef  d'ed(eee  deeeeeef f fd2d3Z!d4d5 Z"		dJdd$d%ed&edeeef dee d(eee  deeeef fd6d7Z#		dJdd$d%ed&edeeef d(eee  dee$e% eef f
d8d9Z&e'dd$d%ed&edeeef d-e(d: dee d(eee  deeeeeef f fd;d<Z)e'dd$d%ed&edeeef d-e(d
 dee d(eee  dee$e% eeeef f fd=d<Z)dd$d%ed&edeeef d-edee d(eee  deeeeeef f fd>d<Z)	dId%ed&edeeef dee%eeeef f fd?d@Z*	dId%ed&edeeef dee$e% eeeef f fdAdBZ+dCdD Z,dEede-d fdFdGZ.dS )Kr)   z@
    Base HTTP client that custom clients can inherit from.
    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$   _Proxy      
 rD            ?<   _proxy_verify_ssl_certsTNverify_ssl_certsproxyasync_fallback_clientc                 C   sV   || _ |rt|tr||d}t|tstd|r| nd | _|| _t	 | _
d S )N)r=   r>   zqProxy(ies) must be specified as either a string URL or a dict() with string URL under the https and/or http keys.)rJ   
isinstancerB   dict
ValueErrorcopyrI   _async_fallback_client	threadinglocal_thread_local)selfrK   rL   rM   _libr#   r#   r$   __init__   s   


	zHTTPClient.__init__responseapi_connection_errornum_retriesmax_network_retriesc                 C   s   |d ur|nd}||krdS |d u r|d usJ |j S |\}}}|d ur8d|v r8|d dkr0dS |d dkr8dS |dkr>dS |dkrDdS dS )	Nr   Fzstripe-should-retryfalsetrueTi  i  should_retry)rV   rY   rZ   r[   r\   _status_coderheadersr#   r#   r$   _should_retry   s$   
zHTTPClient._should_retryc              	   C   s>   |d u rd S |\}}}zt |d W S  ttfy   Y d S w )Nzretry-after)r    KeyErrorrP   )rV   rY   ra   rc   r#   r#   r$   _retry_after_header   s   
zHTTPClient._retry_after_headerr(   c                 C   sV   t tjd|d   tj}| |}ttj|}| |pd}|tjkr)t||}|S )z
        Apply exponential backoff with initial_network_retry_delay on the number of num_retries so far as inputs.
        Do not allow the number to exceed `max_network_retry_delay`.
              r   )minr)   INITIAL_DELAY	MAX_DELAY_add_jitter_timemaxrf   MAX_RETRY_AFTER)rV   r[   rY   sleep_secondsretry_afterr#   r#   r$   _sleep_time_seconds   s   	


zHTTPClient._sleep_time_secondsro   c                 C   s   |ddt dd  9 }|S )z
        Randomize the value in `[(sleep_seconds/ 2) to (sleep_seconds)]`.
        Also separated method here to isolate randomness for tests
        rG   rh   r   )randomuniform)rV   ro   r#   r#   r$   rl      s   zHTTPClient._add_jitter_timeheadersc                 C   sB   t | jdd }tjr|rd| i}t|}t||d< |S |S )Nlast_request_metricszX-Stripe-Client-Telemetry)getattrrU   stripeenable_telemetrypayloadrO   jsondumps)rV   rt   ru   	telemetryretr#   r#   r$   _add_telemetry_header  s   
z HTTPClient._add_telemetry_headerc                 C   sH   |\}}}d|v r t jr"|d }t | }t|||d| j_d S d S d S )Nz
Request-Idusage)rw   rx   r%   r   rU   ru   )rV   rY   request_startr   ra   rc   
request_idrequest_duration_msr#   r#   r$   _record_request_metrics  s   

z"HTTPClient._record_request_metrics)_usagemethodurl	post_datar   c             	   C      | j ||||d||dS NFis_streamingr\   r   _request_with_retries_internalrV   r   r   rt   r   r\   r   r#   r#   r$   request_with_retries     
zHTTPClient.request_with_retriesc             	   C   r   NTr   r   r   r#   r#   r$   request_stream_with_retries1  r   z&HTTPClient.request_stream_with_retriesr   c             
   C   s   |  |}d}	 t }	z|r| ||||}
n| ||||}
d }W n ty8 } z
|}d }
W Y d }~nd }~ww | |
|||rf|rKtd|j  |d7 }| 	||
}td||||f  t
| n|
d urt| j|
|	|d |
S |d uszJ |qNr   Tz Encountered a retryable error %srh   zBInitiating retry %i for request %s %s after sleeping %.2f seconds.r   )r~   r%   request_streamrequestr   rd   r   log_infouser_messagerq   r"   sleepr   rV   r   r   rt   r   r   r\   r   r[   r   rY   connection_errore
sleep_timer#   r#   r$   r   E  sR   

z)HTTPClient._request_with_retries_internalc                C      t d)Nz.HTTPClient subclasses must implement `request`NotImplementedErrorrV   r   r   rt   r   r   r#   r#   r$   r        	zHTTPClient.requestc                C   r   )Nz5HTTPClient subclasses must implement `request_stream`r   r   r#   r#   r$   r     r   zHTTPClient.request_streamc                 C   r   )Nz,HTTPClient subclasses must implement `close`r   rV   r#   r#   r$   close     zHTTPClient.closec             	          | j ||||d||dI d H S r   $_request_with_retries_internal_asyncr   r#   r#   r$   request_with_retries_async     
z%HTTPClient.request_with_retries_asyncc             	      r   r   r   r   r#   r#   r$   !request_stream_with_retries_async  r   z,HTTPClient.request_stream_with_retries_asyncFc                      d S Nr#   rV   r   r   rt   r   r   r\   r   r#   r#   r$   r        z/HTTPClient._request_with_retries_internal_asyncc                   r   r   r#   r   r#   r#   r$   r     r   c             
      s  |  |}d}	 t }	z|r| ||||I d H }
n| ||||I d H }
d }W n ty? } z
|}d }
W Y d }~nd }~ww | |
|||rp|rRtd|j  |d7 }| 	||
}td||||f  | 
|I d H  n|
d ur~| j|
|	|d |
S |d usJ |q	r   )r~   r%   request_stream_asyncrequest_asyncr   rd   r   r   r   rq   sleep_asyncr   r   r#   r#   r$   r     sX   


c                    ,   | j d ur| j ||||I d H S td)Nz4HTTPClient subclasses must implement `request_async`)rR   r   r   rV   r   r   rt   r   r#   r#   r$   r        

zHTTPClient.request_asyncc                    r   )Nz;HTTPClient subclasses must implement `request_stream_async`)rR   r   r   r   r#   r#   r$   r   )  r   zHTTPClient.request_stream_asyncc                    s$   | j d ur| j  I d H S td)Nz2HTTPClient subclasses must implement `close_async`)rR   close_asyncr   r   r#   r#   r$   r   4  s   
zHTTPClient.close_asyncsecsc                 C   s   | j d ur| j |S td)Nz,HTTPClient subclasses must implement `sleep`)rR   r   r   rV   r   r#   r#   r$   r   ;  s
   
zHTTPClient.sleep_asyncTNNNr   )NN)/r?   r@   rA   __doc__r   rB   rC   r   rD   rk   rj   rn   r   boolr   rX   r   r   r    r   r   rd   rf   floatrq   rl   r~   r   r
   r   r   r   r   r   r   r   r   bytesr   r   r   r   r   r   r   r   r   r#   r#   r#   r$   r)   }   s  
 

0






	


	


	

@






	


	



	



	


	

>



c                       s  e Zd ZdZ						d deeeeef f ded ded	eee	e
jf  d
ee
 f
 fddZ	d!de	de	deee	e	f  deeeee	e	f f fddZ	d!de	de	deee	e	f  deeeee	e	f f fddZede	de	deee	e	f  ded deeeee	e	f f f
ddZede	de	deee	e	f  ded deeeee	e	f f f
ddZde	de	deee	e	f  dedeeeef eee	e	f f f
ddZdedefddZdd Z  ZS )"r0   r/   P   NTtimeoutsessionRequestsSessionrK   rL   rM   c           	         s@   t t| j|||d || _|| _|d u rdd l}|}|| _d S NrK   rL   rM   r   )superr0   rX   _session_timeoutr/   )	rV   r   r   rK   rL   rM   rW   r'   r/   	__class__r#   r$   rX   F  s   


zRequestsClient.__init__r   r   rt   r(   c                 C      | j ||||ddS NFr   _request_internalr   r#   r#   r$   r   _     
zRequestsClient.requestc                 C   r   NTr   r   r   r#   r#   r$   r   j  r   zRequestsClient.request_streamr   c                 C      d S r   r#   rV   r   r   rt   r   r   r#   r#   r$   r   u     z RequestsClient._request_internalFc                 C   r   r   r#   r   r#   r#   r$   r     r   c              
   C   s  i }| j rtj|d< nd|d< | jr| j|d< |rd|d< t| jdd d u r0| jp-| j | j_	z8zt
d| jj	j||f||| jd|}W n tyZ } ztd	|f d }~ww |ra|j}	n|j}	|j}
W n ty } z| | W Y d }~nd }~ww |	|
|jfS )
NverifyFproxiesTstreamr   r   )rt   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)rJ   rw   ca_bundle_pathrI   rv   rU   r   r/   r   r   r   r   r   	TypeErrorrawcontentrb   	Exception_handle_request_errorrt   )rV   r   r   rt   r   r   r'   resultr   r   rb   r#   r#   r$   r     sV   




r   c                 C   s   t || jjjrd}dt|jt|f }d}nSt || jjj| jjjfr5d}dt|jt|f }d}n6t || jjj	rMd}dt|jt|f }d}nd}dt|jf }t|re|dt|f 7 }n|d	7 }d}t
|d
|f  }t||d|)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.A %s was raisedz with error message %sz with no error message

(Network error: %s)r_   )rN   r/   
exceptionsSSLErrortyper?   rB   r   ConnectionErrorRequestExceptiontextwrapfillr   rV   r   msgerrr`   r#   r#   r$   r     s:   z$RequestsClient._handle_request_errorc                 C   s&   t | jdd d ur| jj  d S d S )Nr   )rv   rU   r   r   r   r#   r#   r$   r     s   zRequestsClient.close)r   NTNNNr   )r?   r@   rA   r<   r   r   r   r   r   rB   r)   rD   rX   r   r   r    r   r   r   r   r   r   r   r   r   r   __classcell__r#   r#   r   r$   r0   C  s    

		
?5r0   c                       sd  e Zd ZdZ					ddedeej dedee f fd	d
Z		dde
de
dee
e
f dee
eee
e
f f fddZ	dde
de
dee
e
f deeeee
e
f f fddZede
de
dee
e
f ded deeeef f
ddZede
de
dee
e
f ded dee
eef f
ddZde
de
dee
e
f fddZdede
defddZdd Z  ZS ) r.   r+   TN7   rK   rL   deadlinerM   c                    sP   t t| j|||d |rtd|| _|| _|d u r#ddlm} |}|| _d S )Nr   zNo proxy support in urlfetch library. Set stripe.default_http_client to either RequestsClient, PycurlClient, or UrllibClient instance to use a proxy.r   r*   )r   r.   rX   rP   rJ   	_deadliner,   r+   )rV   rK   rL   r   rM   rW   r+   r   r#   r$   rX     s   

zUrlFetchClient.__init__r   r   rt   r(   c                 C   r   r   r   r   r#   r#   r$   r   (     
zUrlFetchClient.requestc                 C   r   r   r   r   r#   r#   r$   r   /  r   zUrlFetchClient.request_streamr   c                 C   r   r   r#   r   r#   r#   r$   r   6  r   z UrlFetchClient._request_internalFc                 C   r   r   r#   r   r#   r#   r$   r   @  r   c           	   
   C   s   z| j j|||| j| j|d}W n | j jy* } z| || W Y d }~nd }~ww |r6tt|j	}n|j	}||j
|jfS )N)r   r   rt   validate_certificater   ry   )r+   fetchrJ   r   Errorr   r   rB   encoder   rb   rt   )	rV   r   r   rt   r   r   r   r   r   r#   r#   r$   r   J  s"   
r   c                 C   sj   t || jjrd|f }nt || jjrd}nt || jjr!d}nd}t|d t| d }t||)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: ))	rN   r+   InvalidURLErrorDownloadErrorResponseTooLargeErrorr   r   rB   r   )rV   r   r   r   r#   r#   r$   r   i  s   
z$UrlFetchClient._handle_request_errorc                 C   r   r   r#   r   r#   r#   r$   r        zUrlFetchClient.close)TNr   NNr   )r?   r@   rA   r<   r   r   r)   rD   r    rX   rB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r   r$   r.     s    $




	
	

r.   c                   @   &   e Zd ZU ed ed< ed ed< dS )rD   r   r=   r>   Nr?   r@   rA   r   rC   r#   r#   r#   r$   rD     rE   rD   c                       s  e Zd ZU G dd deddZdZee ed< 				d%d	ed
ee	j
 dee	 f fddZdd Z	d&deeef deeeeeef f fddZ	d&deeef deeeeeef f fddZedededeeef ded deeeef f
ddZedededeeef ded deeeeeef f f
ddZdededeeef deeeef eeeef f fddZdedefddZded  fd!d"Zd#d$ Z  ZS )'r2   c                   @   r   )zPycurlClient._ParsedProxyr   r=   r>   Nr   r#   r#   r#   r$   _ParsedProxy  rE   r   F)totalr1   _parsed_proxyTNrK   rL   rM   c           
         s   t t| j|||d |d u rdd l}|}|| _| | _i | _| jr?ddlm	} | j}|
 D ]\}}	|tt|	| j|< q0d S d S )Nr   r   )urlparse)r   r2   rX   r1   Curl_curlr   rI   urllib.parser  itemsr   rB   )
rV   rK   rL   rM   rW   r1   r  proxy_schemevaluer   r#   r$   rX     s$   

zPycurlClient.__init__c                 C   s@   d|vri S | ddd }t|}tdd t| D S )Nz
rh   c                 s        | ]\}}|  |fV  qd S r   lower.0kvr#   r#   r$   	<genexpr>      z-PycurlClient.parse_headers.<locals>.<genexpr>)splitemailmessage_from_stringrO   r  )rV   r   raw_headersrt   r#   r#   r$   parse_headers  s
   
zPycurlClient.parse_headersrt   r(   c                 C   r   r   r   r   r#   r#   r$   r     r   zPycurlClient.requestc                 C   r   r   r   r   r#   r#   r$   r     r   zPycurlClient.request_streamr   r   r   c                 C   r   r   r#   r   r#   r#   r$   r     r   zPycurlClient._request_internalc                 C   r   r   r#   r   r#   r#   r$   r     r   c              
   C   s6  t  }t  }| j  | |}|rA|jr| j| jj|j |jr,| j| jj	|j |j
s2|jrA| j| jjd|j
|jf  |dkrO| j| jjd n"|dkrf| 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| D  | jr| j| jjtj n	| j| jj d	 z| j!  W n | jj"y }	 z| #|	 W Y d }	~	nd }	~	ww |r|$d
 |}
n|% &d}
| j'| jj(}| )|% &d}|
||fS )Nz%s:%sgetrh   post   r   c                 S   s   g | ]
\}}d ||f qS )r   r#   r  r#   r#   r$   
<listcomp>  s    z2PycurlClient._request_internal.<locals>.<listcomp>Fr   utf-8)*r   r  reset
_get_proxyhostnamesetoptr1   PROXYport	PROXYPORTusernamepasswordPROXYUSERPWDHTTPGETPOST
POSTFIELDSCUSTOMREQUESTupperURLWRITEFUNCTIONwriteHEADERFUNCTIONNOSIGNALCONNECTTIMEOUTTIMEOUT
HTTPHEADERrO   r  rJ   CAINFOrw   r   SSL_VERIFYHOSTperformerrorr   seekgetvaluedecodegetinfoRESPONSE_CODEr  )rV   r   r   rt   r   r   brc   rL   r   rcontentrcoder#   r#   r$   r     s\   



r   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 r'd}d}nd}d}t|d |j d  d	 }t	||d
|)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   rh   r   r_   )
r&   r1   E_COULDNT_CONNECTE_COULDNT_RESOLVE_HOSTE_OPERATION_TIMEOUTEDE_SSL_CACERTE_SSL_PEER_CERTIFICATEr   r   r   )rV   r   r   r`   r#   r#   r$   r   &  s(   z"PycurlClient._handle_request_errorr   c                 C   sD   | j r | j }|r|dd nd }|r ||||dd S d S )N:r   )r   r  r  )rV   r   rL   r  r#   r#   r$   r  I  s   zPycurlClient._get_proxyc                 C   r   r   r#   r   r#   r#   r$   r   Q  r   zPycurlClient.closer   r   )r?   r@   rA   r   r   r<   r   rC   r   r)   rD   rX   r  r   rB   r   r    r   r   r   r   r   r   r   r   r   r   r   r  r   r   r#   r#   r   r$   r2     s   
 "




	
	

H#r2   c                       sZ  e Zd ZdZ				ddedeej dee f fddZ	dd	e	d
e	de
e	e	f dee	ee
e	e	f f fddZ	dd	e	d
e	de
e	e	f deeee
e	e	f f fddZed	e	d
e	de
e	e	f ded dee	eef f
ddZed	e	d
e	de
e	e	f ded deeeef f
ddZd	e	d
e	de
e	e	f fddZdedefddZdd Z  ZS )r3   zurllib.requestTNrK   rL   rM   c                    s   t t| j|||d |d u rdd lm} |}|| _dd lm} || _d | _	| j
r@| jttttf | j
}| j|| _	d S d S r   )r   r3   rX   urllib.requestr   urllibrequesturllib.errorr6  urlliberror_openerrI   ProxyHandlerr   r   rB   build_opener)rV   rK   rL   rM   rW   rG  rI  proxy_handlerr   r#   r$   rX   X  s$   
zUrllibClient.__init__r   r   rt   r(   c                 C   r   r   r   r   r#   r#   r$   r   y  r   zUrllibClient.requestc                 C   r   r   r   r   r#   r#   r$   r     r   zUrllibClient.request_streamr   Fc                 C   r   r   r#   r   r#   r#   r$   r     r   zUrllibClient._request_internalc                 C   r   r   r#   r   r#   r#   r$   r     r   c              
      s.  t |tr
|d}| j||ttttf |} dvr$ fdd|_z#| jr.| j	|n| j
|}|r9|}n| }|j}	t| }W n; | jjyh }
 z|
j}	|
 }t|
 }W Y d }
~
nd }
~
w | jjtfy }
 z| |
 W Y d }
~
nd }
~
ww tdd tt| D }||	|fS )Nr  )r  r  c                      s      S r   )r*  r#   r   r#   r$   <lambda>  s    z0UrllibClient._request_internal.<locals>.<lambda>c                 s   r	  r   r
  r  r#   r#   r$   r    r  z1UrllibClient._request_internal.<locals>.<genexpr>)rN   rB   r   rG  Requestr   r   
get_methodrJ  openurlopenreadcoderO   inforI  	HTTPErrorURLErrorrP   r   iterr  )rV   r   r   rt   r   r   reqrY   r=  r>  r   lhr#   rN  r$   r     s8   



r   c                 C   s(   d}t |d t| d }t||)Nr   r   r   )r   r   rB   r   )rV   r   r   r#   r#   r$   r     s   
z"UrllibClient._handle_request_errorc                 C   r   r   r#   r   r#   r#   r$   r     r   zUrllibClient.closer   r   )r?   r@   rA   r<   r   r   r)   rD   rX   rB   r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r   r$   r3   U  s    "




	
	

+r3   c                       sx  e Zd ZU dZed ed< 			d"deeedf  f fd	d
Zdd Z	de
de
dee
e
f fddZ	d#de
de
dee
e
f deeeee
e
f f fddZ	d#de
de
dee
e
f deeeee
e
f f fddZdedefddZ	d#de
de
dee
e
f deee eee
e
f f fddZ	d#de
de
dee
e
f deee eee
e
f f fddZdd Zd d! Z  ZS )$r7   r5   HTTPXClientType_clientr   FNr   HTTPXTimeoutc                    s   t t| jdi | |d u rdd l}|}|| _dd l}|| _i }| jr.tjtj	d|d< nd|d< | jj
di || _d | _|rK| jjdi || _|| _d S )Nr   cafiler   Fr#   )r   r7   rX   r5   r6   rJ   sslcreate_default_contextrw   r   AsyncClient_client_asyncr]  r   r   )rV   r   allow_sync_methodsrW   r'   r5   r6   r   r#   r$   rX     s$   
zHTTPXClient.__init__c                 C   s   | j |S r   )r6   r   r   r#   r#   r$   r     s   zHTTPXClient.sleep_asyncr   r   rt   c                 C   s>   i }| j r
| j |d< | jr| j|d< ||f||pi d|gS )Nr   r   )rt   r   )rI   r   )rV   r   r   rt   r   r'   r#   r#   r$   _get_request_args_kwargs  s   

z$HTTPXClient._get_request_args_kwargsr(   c              
   C   s   | j d u r	td| ||||\}}z| j j|i |}W n ty5 } z| | W Y d }~nd }~ww |j}	|j}
|j}|	|
|fS )NzqStripe: HTTPXClient was initialized with allow_sync_methods=False, so it cannot be used for synchronous requests.)	r]  RuntimeErrorrf  r   r   r   r   rb   rt   rV   r   r   rt   r   r&   r'   rY   r   r   rb   response_headersr#   r#   r$   r   
  s"   

zHTTPXClient.requestc              
      ~   |  ||||\}}z| jj|i |I d H }W n ty0 } z| | W Y d }~nd }~ww |j}	|j}
|j}|	|
|fS r   )rf  rd  r   r   r   r   rb   rt   rh  r#   r#   r$   r   #  s   
zHTTPXClient.request_asyncr   c                 C   :   d}dt |jf }d}t|d|f  }t||d|Nr   r   Tr   r_   r   r?   r   r   r   r   r#   r#   r$   r   7     z!HTTPXClient._handle_request_errorc              
   C   s   | j d u r	td| ||||\}}z| j j| jj|i |dd}W n ty; } z| | W Y d }~nd }~ww | }	|j	}
|j
}|	|
|fS )NztStripe: HTTPXClient was not initialized with allow_sync_methods=True, so it cannot be used for synchronous requests.Tr   r   )r]  rg  rf  sendrd  build_requestr   r   
iter_bytesrb   rt   rV   r   r   rt   r   r&   r'   rY   r   r   rb   r#   r#   r$   r   B  s(   


zHTTPXClient.request_streamc              
      s   |  ||||\}}z| jj| jj|i |ddI d H }W n ty6 } z| | W Y d }~nd }~ww | }	|j}
|j}|	|
|fS )NTro  )	rf  rd  rp  rq  r   r   aiter_bytesrb   rt   rs  r#   r#   r$   r   Z  s"   
z HTTPXClient.request_stream_asyncc                 C   s   | j d ur| j   d S d S r   )r]  r   r   r#   r#   r$   r   m  s   
zHTTPXClient.closec                       | j  I d H  d S r   )rd  acloser   r#   r#   r$   r   q     zHTTPXClient.close_async)r   FNr   )r?   r@   rA   r<   r   rC   r   r   rX   r   rB   r   rf  r   r   r    r   r   r   r   r   r	   r   r   r   r   r   r   r#   r#   r   r$   r7     sx   
 !









r7   c                       s@  e Zd ZdZ		d"deeedf  f fddZedd	 Z	d
d Z
deeeeeef f fddZdededeeef fddZ	d#dededeeef deeeeeef f fddZdedefddZdeee eeeef f fddZ	d#dededeeef dedeeeef f fddZdd Zd d! Z  ZS )$r9   r8   r   Nr   AIOHTTPTimeoutc                    s@   t t| jdi | |d u rdd l}|}|| _|| _d | _d S )Nr   r#   )r   r9   rX   r8   r   _cached_session)rV   r   rW   r'   r8   r   r#   r$   rX   x  s   
zAIOHTTPClient.__init__c                 C   sb   | j d u r.i }| jrtjtjd}| jj|d|d< n	| jjdd|d< | jjdi || _ | j S )Nr_  )ra  	connectorF)
verify_sslr#   )	ry  rJ   ra  rb  rw   r   r8   TCPConnectorClientSession)rV   r'   ssl_contextr#   r#   r$   r     s   

zAIOHTTPClient._sessionc                 C   s
   t |S r   )asyncior   r   r#   r#   r$   r     s   
zAIOHTTPClient.sleep_asyncr(   c                 C   r   Nz4AIOHTTPClient does not support synchronous requests.r   r   r#   r#   r$   r     r   zAIOHTTPClient.requestr   r   rt   c                 C   sd   ||f}i }| j r| j d | j d krtd| j d |d< | jr&| j|d< ||d< ||d< ||fS )Nr=   r>   zDAIOHTTPClient does not support different proxies for HTTP and HTTPS.rL   r   rt   r   )rI   rP   r   )rV   r   r   rt   r   r&   r'   r#   r#   r$   rf    s   
z&AIOHTTPClient._get_request_args_kwargsc                    s4   | j ||||dI d H \}}}| I d H ||fS )N)r   )r   rT  )rV   r   r   rt   r   r   rb   ri  r#   r#   r$   r     s   
zAIOHTTPClient.request_asyncr   c                 C   rk  rl  rm  r   r#   r#   r$   r     rn  z#AIOHTTPClient._handle_request_errorc                 C   r   r  r   r   r#   r#   r$   r     r   zAIOHTTPClient.request_streamAIOHTTPStreamReaderc              
      rj  r   )rf  r   r   r   r   r   statusrt   rh  r#   r#   r$   r     s   
z"AIOHTTPClient.request_stream_asyncc                 C   r   r   r#   r   r#   r#   r$   r     r   zAIOHTTPClient.closec                    ru  r   )r   r   r   r#   r#   r$   r     rw  zAIOHTTPClient.close_async)r   Nr   )r?   r@   rA   r<   r   r   r   rX   propertyr   r   r   r   r    r   rB   r   rf  r   r   r   r   r	   r   r   r   r   r   r#   r#   r   r$   r9   u  sT    
 



$

r9   c                       s   e Zd Z fddZedefddZ	ddeded	eeef de	e
eeeef f fd
dZ	ddeded	eeef fddZdd Z  ZS )r:   c                    s   t t| jdi | d S )Nr#   )r   r:   rX   )rV   r'   r   r#   r$   rX     s   z!NoImportFoundAsyncClient.__init__r(   c                   C   r   )NzImport httpx not found. To make async http requests,You must either install httpx or define your ownasync http client by subclassing stripe.HTTPClientand setting stripe.default_http_client to an instance of it.)r-   r#   r#   r#   r$   raise_async_client_import_error  s   z8NoImportFoundAsyncClient.raise_async_client_import_errorNr   r   rt   c                       |    d S r   r  r   r#   r#   r$   r        z&NoImportFoundAsyncClient.request_asyncc                    r  r   r  r   r#   r#   r$   r     r  z-NoImportFoundAsyncClient.request_stream_asyncc                    r  r   r  r   r#   r#   r$   r   	  s   z$NoImportFoundAsyncClient.close_asyncr   )r?   r@   rA   rX   staticmethodr   r  rB   r   r   r   r    r   r   r   r   r#   r#   r   r$   r:     s.    



r:   )Dior   r   r  r"   rr   rS   rz   r  ra  http.clientr   rw   r   stripe._request_metricsr   stripe._errorr   typingr   r   r	   r
   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r   r   r   r   r  r   r/   r   r   r-   r5   r   r^  r   r\  r8   r   rx  r   r  r%   r4   r;   objectr)   r0   r.   rD   r2   r3   r7   r9   r:   r#   r#   r#   r$   <module>   sl    < 	#   I @  K} ${