o
    iw                     @   s  U d dl mZmZ d dlZd dlZd dl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 d dlZd dlmZmZmZ d dlZd dl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' d dl(m)Z) d d	lm*Z* d d
l+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4m5Z5 d dl6m7Z7m8Z8 d dl9m:Z:m;Z;m<Z< d dl=m>Z> d dl?m@Z@ erd dlAmBZB d dlCmDZD ed ZEdaFeeG eHd< deGde@deIfddZJG dd deKZLdS )    )BytesIOIOBaseN)
AnyAsyncIterableDictListMappingOptionalTupleUnioncastClassVar)TYPE_CHECKINGLiteralNoReturnUnpack)urlsplit
urlunsplitparse_qs)	log_debuglog_infodashboard_link_convert_to_stripe_objectget_api_mode)VERSION)MultipartDataGenerator)	urlencode)_api_encode_json_encode_date_callback)StripeResponseStripeStreamResponseStripeStreamResponseAsync)PERSISTENT_OPTIONS_KEYSRequestOptionsmerge_options)RequestorOptions_GlobalRequestorOptions)
HTTPClientnew_default_http_clientnew_http_client_async_fallback)BaseAddress)ApiMode)AppInfo)StripeObject)getpostdelete_default_proxymethodapi_modereturnc                 C   s   | dko|dkS )Nr0   V2 )r2   r3   r6   r6   u/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/stripe/_api_requestor.pyis_v2_delete_respI   s   r8   c                   @   s  e Zd ZU dZed ed< 		dIdee dee fddZ	defd	d
Z
dee dd fddZedd Zedd Zedd Zedd Zedd Zedee dd fddZedd Z		dIdddededeeeef  dee ded eee  dd!fd"d#Z		dIdddededeeeef  dee ded eee  dd!fd$d%Z		dIdddededeeeef  dee ded eee  defd&d'Z 		dIdddededeeeef  dee ded eee  de!fd(d)Z"de#fd*d+Z$d,d- Z%d.d/ Z&d0d1 Z'de(d2e)defd3d4Z*		dIdddededeeeef  dee ded2e)d eee  fd5d6Z+			7dJdddededeeeef  dee d8e,ded2e)d eee  de-e.e/eeef f fd9d:Z0			7dJdddededeeeef  dee d8e,ded2e)d eee  de-e1e2 e/eeef f fd;d<Z3d=e/de,fd>d?Z4d@e.d=e/dAeeef d2e)de5f
dBdCZ6dDe7d=e/dAeeef d2e)def
dEdFZ8dDe1e2 d=e/dAeeef d2e)de!f
dGdHZ9dS )K_APIRequestorNz_APIRequestor|None	_instanceoptionsclientc                 C   s   |d u rt  }|| _|| _d S N)r%   _options_client)selfr;   r<   r6   r6   r7   __init__P   s   
z_APIRequestor.__init__r4   c                 C   s|   | j }|d u r<tjs$tjtjd}tddtdi |i|t_tjantjtkr2dd l}|	d tjd us9J tjS |S )N)verify_ssl_certsproxyasync_fallback_clientr   zstripe.proxy was updated after sending a request - this is a no-op. To use a different proxy, set stripe.default_http_client to a new client configured with the proxy.r6   )
r?   stripedefault_http_clientrB   rC   r(   r)   r1   warningswarn)r@   r<   kwargsrG   r6   r6   r7   _get_http_client^   s,   
z_APIRequestor._get_http_clientc                 C   sT   |pi }| j  }tD ]}||v r|| dur|| ||< qttdi || jdS )z
        Returns a new _APIRequestor instance with the same HTTP client but a (potentially) updated set of options. Useful for ensuring the original isn't modified, but any options the original had are still used.
        Nr;   r<   r6   )r>   to_dictr"   r9   r%   r?   )r@   r;   new_optionskeyr6   r6   r7   _new_requestor_with_options   s   
z)_APIRequestor._new_requestor_with_optionsc                 C      | j jS r=   )r>   api_keyr@   r6   r6   r7   rQ         z_APIRequestor.api_keyc                 C   rP   r=   )r>   stripe_accountrR   r6   r6   r7   rT      rS   z_APIRequestor.stripe_accountc                 C   rP   r=   )r>   stripe_versionrR   r6   r6   r7   rU      rS   z_APIRequestor.stripe_versionc                 C   rP   r=   )r>   base_addressesrR   r6   r6   r7   rV      rS   z_APIRequestor.base_addressesc                 C   s    | j du r| t dd| _ | j S )z
        Returns the singleton instance of _APIRequestor, to be used when
        calling a static method such as stripe.Customer.create(...)
        NrK   )r:   r&   )clsr6   r6   r7   _global_instance   s   
z_APIRequestor._global_instanceparamsc                  K   s   t  | S r=   )r9   rX   rO   )rY   r6   r6   r7   _global_with_options   s   z"_APIRequestor._global_with_optionsc                 C   s@   |d }|d r|d|d f 7 }|d r|d|d f 7 }|S )Nnameversionz/%surlz (%s)r6   )rW   infostrr6   r6   r7   _format_app_info   s   z_APIRequestor._format_app_info)usager2   r]   base_addressra   r-   c             
   C   sb   t |}| |}|j| ||d||||d\}	}
}||	|
||}t||||t||d}|S NFis_streamingr3   rb   r;   ra   )resprY   	requestorr3   is_v2_deleted_object)r   rO   request_rawlower_interpret_responser   r8   r@   r2   r]   rY   r;   rb   ra   r3   rg   rbodyrcoderheadersrf   objr6   r6   r7   request   s*   


z_APIRequestor.requestc             
      sj   t |}| |}|j| ||d||||dI d H \}	}
}||	|
||}t||||t||d}|S rc   )r   rO   request_raw_asyncrj   rk   r   r8   rl   r6   r6   r7   request_async   s,   


z_APIRequestor.request_asyncc             
   C   sF   t |}| j| ||d||||d\}}	}
| tt||	|
|}|S NTrd   )r   ri   rj   _interpret_streaming_responser   r   r@   r2   r]   rY   r;   rb   ra   r3   streamrn   ro   rf   r6   r6   r7   request_stream   s$   

z_APIRequestor.request_streamc             
      sN   t |}| j| ||d||||dI d H \}}	}
| ||	|
|I d H }|S rt   )r   rr   rj   #_interpret_streaming_response_asyncrv   r6   r6   r7   request_stream_async  s&   


z"_APIRequestor.request_stream_asyncc              	   C   s   z|d }W n t tfy   td||f |||w d }t|tr,| |||||}|d u rF|dkr=| |||||n| |||||}|)Nerrorz@Invalid response object from API: %r (HTTP response code was %d)r5   )	KeyError	TypeErrorr{   APIError
isinstancer_   specific_oauth_errorspecific_v2_api_errorspecific_v1_api_error)r@   rm   rn   rf   ro   r3   
error_dataerrr6   r6   r7   handle_error_response<  s6   	




z#_APIRequestor.handle_error_responsec           
      C   s   | d}| d}| d}||||||d}	td|| d|| dd |dkr5t||||||S |d	krAtjd
i |	S | |||||S )Ntypecodemessage)r   	http_bodyhttp_status	json_bodyheadersr   zStripe v2 API error receivedparam
error_code
error_typeerror_messageerror_paramidempotency_errortemporary_session_expiredr6   )r.   r   r{   IdempotencyErrorTemporarySessionExpiredErrorr   )
r@   rm   rn   rf   ro   r   r   r   r   
error_argsr6   r6   r7   r   a  s>   


		
z#_APIRequestor.specific_v2_api_errorc              	   C   s>  t d|d|d|d|dd |dks$|dkr0|dd	kr0t|d||||S |d
v r[|ddkrGt|d||||S t|d|d|d||||S |dkrkt|d||||S |dkrt|d|d|d||||S |dkrt|d||||S t	|d||||S )NzStripe v1 API error receivedr   r   r   r   r   i    
rate_limit)r   i  r   i  i  i  )
r   r.   r{   RateLimitErrorr   InvalidRequestErrorAuthenticationError	CardErrorPermissionErrorr~   )r@   rm   rn   rf   ro   r   r6   r6   r7   r     s^   			z#_APIRequestor.specific_v1_api_errorc                 C   s   | d|}td||d ||||||g}|dkrtj| S |dkr'tj| S |dkr0tj| S |dkr9tj| S |dkrBtj| S |d	krKtj| S d S )
Nerror_descriptionzStripe OAuth error received)r   r   invalid_clientinvalid_grantinvalid_requestinvalid_scopeunsupported_grant_typeunsupported_response_type)	r.   r   oauth_errorInvalidClientErrorInvalidGrantErrorr   InvalidScopeErrorUnsupportedGrantTypeErrorUnsupportedResponseTypeError)r@   rm   rn   rf   ro   r   descriptionargsr6   r6   r7   r     s(   





z"_APIRequestor.specific_oauth_errorr3   c              	   C   st  d|  tf }tjr|d| tj 7 }tdd|  jd}dtjgdtjgdd	d
 gfD ]\}}z| }W n t	yB   d}Y nw |||< q.tjrPtj|d< t
||d|df d}	|d}
|
rj|
|	d< |d}|r{t|r{t||	d< |d}|r||	d< |dks|dkr|dkr|	dtt  |dkr|dkrd|	d< nd|	d< |d}|r||	d< |	S )NzStripe/%s PythonBindings/%s pythonrE   )bindings_versionlang	publisherhttpliblang_versionplatformunamec                   S   s   d t S )Nr   )joinr   r   r6   r6   r6   r7   <lambda>  s    z/_APIRequestor.request_headers.<locals>.<lambda>z
(disabled)applicationz	Bearer %srQ   )zX-Stripe-Client-User-Agentz
User-AgentAuthorizationrT   zStripe-Accountstripe_contextzStripe-Contextidempotency_keyzIdempotency-Keyr/   r5   r0   zapplication/jsonContent-Typez!application/x-www-form-urlencodedrU   zStripe-Version)rj   r   rE   app_infor`   rJ   r[   r   python_version	Exceptionjsondumpsr.   r_   
setdefaultuuiduuid4)r@   r2   r3   r;   
user_agentuaattrfuncvalr   rT   r   r   rU   r6   r6   r7   request_headers  s\   








z_APIRequestor.request_headersc             
   C   s  t | j|}|rd|v r|d dur|d |d< |ddu r$tdd| jj||f }	|p2i }|r|dks=|dkri }
tt|j	 D ] \}}|
drZ||
|dd	 < qHt|d
krd|d n||
|< qHd|
v rd|v rttg |
d |d |d< i |
|}ttt|pi |}|dddd}|dkrtj|pi td}n|}d}d|v r|d durt|d }| tt|||}|dks|dkr|rt|	\}}}}}t|||||f}	d}n5|dkr|dur|ddkrt }||pi  | }d|jf |d< n|}ntd|f |dur/|	 D ]	\}}|||< q%|d}||	||||||dfS )z`
        Mechanism for issuing an API call.  Used by request_raw and request_raw_async.
        rU   NrQ   zNo API key provided. (HINT: set your API key using "stripe.api_key = <API-KEY>"). You can generate API keys from the Stripe web interface.  See https://stripe.com/api for details, or email support@stripe.com if you have any questions.z%s%sr.   r0   z[]   r   expandz%5B[z%5D]r5   )defaultr   r/   content_typezmultipart/form-dataz multipart/form-data; boundary=%sr   zUnrecognized HTTP method %r.  This may indicate a bug in the Stripe bindings.  Please contact support@stripe.com for assistance.max_network_retries)r$   r>   r.   r{   r   rV   r   r   queryitemsendswithlenlistsetr   r   replacer   r   r   dictr   r   HttpVerbr   r   
add_paramsget_post_databoundaryAPIConnectionError)r@   r2   r]   rY   r;   rb   r3   ra   request_optionsabs_urlexisting_paramskvencoded_paramsencoded_bodysupplied_headersr   schemenetlocpath_fragment	post_data	generatorrN   valuer   r6   r6   r7   _args_for_request_with_retries  s   





z,_APIRequestor._args_for_request_with_retriesFre   c             	   C   s   | j |||||||d\}}	}
}}}}}td||	d td||d |r5|  j||	|
|||d\}}}n|  j||	|
|||d\}}}td|	|d td	|d
 d|v rb|d }tdt|d |||fS )Nrb   r3   ra   Request to Stripe apir2   r]   Post detailsr   api_versionr   _usageStripe API responser   response_codeAPI response bodybody
Request-IdDashboard link for requestlink)r   r   r   rJ   request_stream_with_retriesrequest_with_retriesr   r@   r2   r]   rY   r;   re   rb   r3   ra   r   r   r   r   r   r   rcontentrn   ro   
request_idr6   r6   r7   ri     sp   

z_APIRequestor.request_rawc             	      s   |pg }|dg }| j |||||||d\}}	}
}}}}}td||	d td||d |rB|  j||	|
|||dI dH \}}}n|  j||	|
|||dI dH \}}}td	|	|d
 td|d d|v rr|d }tdt|d |||fS )z3
        Mechanism for issuing an API call
        asyncr   r   r   r   r   r   Nr   r   r   r   r   r  r  )r   r   r   rJ   !request_stream_with_retries_asyncrequest_with_retries_asyncr   r  r6   r6   r7   rr     sz   




z_APIRequestor.request_raw_asyncrn   c                 C   s   d|  ko
dk  S    S )N   i,  r6   )r@   rn   r6   r6   r7   _should_handle_code_as_errorG  s   z*_APIRequestor._should_handle_code_as_errorrm   ro   c              	   C   s   zt |drtt|d}ttt|||}W n ty.   td||f tt|||w | 	|r>| 
|||j|| |S )Ndecodezutf-8z>Invalid response body from API: %s (HTTP response code was %d))hasattrr   bytesr  r   r_   r   r{   r~   r  r   data)r@   rm   rn   ro   r3   rf   r6   r6   r7   rk   J  s.   

z!_APIRequestor._interpret_responserw   c                 C   sn   |  |r1t|drtt| }nt|dr| }n	td|  j | 	|||| t
dt|||S )Ngetvaluereadz`HTTP client %s does not return an IOBase object which can be consumed when streaming a response./_interpret_response should have raised an error)r  r  r   r   r  r  NotImplementedErrorrJ   r[   rk   RuntimeErrorr    r@   rw   rn   ro   r3   json_contentr6   r6   r7   ru   i  s   



z+_APIRequestor._interpret_streaming_responsec                    sJ   |  |rddd |2 I d H }| |||| tdt|||S )N    c                    s   g | z3 d H W }|q6 S r=   r6   ).0chunkr6   r6   r7   
<listcomp>  s    zE_APIRequestor._interpret_streaming_response_async.<locals>.<listcomp>r  )r  r   rk   r  r!   r  r6   r6   r7   ry     s   
z1_APIRequestor._interpret_streaming_response_async)NN)NNF):__name__
__module____qualname__r:   r   __annotations__r	   r%   r'   rA   rJ   r#   rO   propertyrQ   rT   rU   rV   classmethodrX   staticmethodr   rZ   r`   r_   r   r   r*   r   rq   rs   r    rx   r!   rz   r   r   r   r   r   r   r+   r   r   boolr
   objectintri   r   r  rr   r  r   rk   r   ru   ry   r6   r6   r6   r7   r9   M   s  
 
"







	
&
	
&
	
#
	

%'6
C	
	
 
	


O
	


T




r9   )Mior   r   r   r   typingr   r   r   r   r   r	   r
   r   r   r   typing_extensionsr   r   r   r   r   urllib.parser   r   r   rE   stripe._utilr   r   r   r   r   stripe._versionr   stripe._error_errorr{   stripe.oauth_errorr    stripe._multipart_data_generatorr   r   stripe._encoder   r   stripe._stripe_responser   r    r!   stripe._request_optionsr"   r#   r$   stripe._requestor_optionsr%   r&   stripe._http_clientr'   r(   r)   stripe._base_addressr*   stripe._api_moder+   stripe._app_infor,   stripe._stripe_objectr-   r   r1   r_   r   r$  r8   r%  r9   r6   r6   r6   r7   <module>   s8    0