o
    L)j}                     @   s  U d dl mZmZ d dl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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l0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7 d dl8m9Z9m:Z: d dl;m<Z<m=Z=m>Z> d dl?m@Z@ d dlAmBZB erd dlCmDZD d dlEmFZF ed ZGdaHeeI eJd< deeIeIf ddfddZKdeIdeBdeLfddZMG dd deNZOdS )    )BytesIOIOBaseN)AnyAsyncIterableCallable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_make_suitable_for_json)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rheadersreturnc                 C   s(   |  d}|rdd l}|| d S d S )NzStripe-Noticer   )r/   warningswarn)r3   noticer5    r8   z/var/www/html/finance-dev.cargoinsureonline.com/_shared/backend-venv/lib/python3.10/site-packages/stripe/_api_requestor.py_maybe_emit_stripe_noticeK   s
   
r:   methodapi_modec                 C   s   | dko|dkS )Nr1   V2r8   )r;   r<   r8   r8   r9   is_v2_delete_respS   s   r>   c                   @   s  e Zd ZU dZed ed< 		dMd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		dMd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		dMd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		dMd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 		dMd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'g d2Z(ed3eeef defd4d5Z)de*d6e+defd7d8Z,		dMdddededeeeef  dee ded6e+d eee  fd9d:Z-			;dNdddededeeeef  dee d<e.ded6e+d eee  de/e0e1eeef f fd=d>Z2			;dNdddededeeeef  dee d<e.ded6e+d eee  de/e3e4 e1eeef f fd?d@Z5dAe1de.fdBdCZ6dDe0dAe1dEeeef d6e+de7f
dFdGZ8dHe9dAe1dEeeef d6e+def
dIdJZ:dHe3e4 dAe1dEeeef d6e+de!f
dKdLZ;dS )O_APIRequestorNz_APIRequestor|None	_instanceoptionsclientc                 C   s   |d u rt  }|| _|| _d S N)r&   _options_client)selfrA   rB   r8   r8   r9   __init__Z   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.r8   )
rE   stripedefault_http_clientrH   rI   r)   r*   r2   r5   r6   )rF   rB   kwargsr5   r8   r8   r9   _get_http_clienth   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rA   rB   r8   )rD   to_dictr#   r?   r&   rE   )rF   rA   new_optionskeyr8   r8   r9   _new_requestor_with_options   s   
z)_APIRequestor._new_requestor_with_optionsc                 C      | j jS rC   )rD   api_keyrF   r8   r8   r9   rU         z_APIRequestor.api_keyc                 C   rT   rC   )rD   stripe_accountrV   r8   r8   r9   rX      rW   z_APIRequestor.stripe_accountc                 C   rT   rC   )rD   stripe_versionrV   r8   r8   r9   rY      rW   z_APIRequestor.stripe_versionc                 C   rT   rC   )rD   base_addressesrV   r8   r8   r9   rZ      rW   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(...)
        NrO   )r@   r'   )clsr8   r8   r9   _global_instance   s   
z_APIRequestor._global_instanceparamsc                  K   s   t  | S rC   )r?   r\   rS   )r]   r8   r8   r9   _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)r8   )r[   infostrr8   r8   r9   _format_app_info   s   z_APIRequestor._format_app_info)usager;   ra   base_addressre   r.   c             
   C   sj   t |}| |}|j| ||d||||d\}	}
}t| ||	|
||}t||||t||d}|S NFis_streamingr<   rf   rA   re   )respr]   	requestorr<   is_v2_deleted_object)r   rS   request_rawlowerr:   _interpret_responser   r>   rF   r;   ra   r]   rA   rf   re   r<   rk   rbodyrcoder3   rj   objr8   r8   r9   request   s,   


z_APIRequestor.requestc             
      sr   t |}| |}|j| ||d||||dI d H \}	}
}t| ||	|
||}t||||t||d}|S rg   )r   rS   request_raw_asyncrn   r:   ro   r   r>   rp   r8   r8   r9   request_async   s.   


z_APIRequestor.request_asyncc             
   C   sF   t |}| j| ||d||||d\}}	}
| tt||	|
|}|S NTrh   )r   rm   rn   _interpret_streaming_responser   r   rF   r;   ra   r]   rA   rf   re   r<   streamrr   r3   rj   r8   r8   r9   request_stream  s$   

z_APIRequestor.request_streamc             
      sN   t |}| j| ||d||||dI d H \}}	}
| ||	|
|I d H }|S rw   )r   ru   rn   #_interpret_streaming_response_asyncry   r8   r8   r9   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)r=   )	KeyError	TypeErrorr~   APIError
isinstancerc   specific_oauth_errorspecific_v2_api_errorspecific_v1_api_error)rF   rq   rr   rj   r3   r<   
error_dataerrr8   r8   r9   handle_error_responseH  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 |d
krMt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
rate_limittemporary_session_expiredr8   )r/   r   r~   IdempotencyErrorRateLimitErrorTemporarySessionExpiredErrorr   )
rF   rq   rr   rj   r3   r   r   r   r   
error_argsr8   r8   r9   r   m  sB   


		
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    r   )r   i  r   i  i  i  )
r   r/   r~   r   r   InvalidRequestErrorAuthenticationError	CardErrorPermissionErrorr   )rF   rq   rr   rj   r3   r   r8   r8   r9   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)rF   rq   rr   rj   r3   r   descriptionargsr8   r8   r9   r     s(   





z"_APIRequestor.specific_oauth_error))ANTIGRAVITY_CLI_ALIASantigravity)
CLAUDECODEclaude_code)CLINE_ACTIVEcline)CODEX_SANDBOX	codex_cli)CODEX_THREAD_IDr   )CODEX_SANDBOX_NETWORK_DISABLEDr   )CODEX_CIr   )CURSOR_AGENTcursor)
GEMINI_CLI
gemini_cli)OPENCLAW_SHELLopenclaw)OPENCODE	open_codeenvironc                 C   s&   t jD ]\}}| |r|  S qdS )N )r?   	AI_AGENTSr/   )r   env_var
agent_namer8   r8   r9   _detect_ai_agent  s
   
z_APIRequestor._detect_ai_agentr<   c              	   C   s  d|  tf }tjr|d| tj 7 }| tj}|r#|d| 7 }td|  j	d}dt
jfg}tjr=|dt
j
f |D ]\}}	z|	 }
W n tyS   d}
Y nw |
||< q?tjratj|d	< |rg||d
< t||d|df d}|d}|r||d< |d}|rt|rt||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 z	 AIAgent/python)bindings_versionlanghttpliblang_versionplatformz
(disabled)applicationai_agentz	Bearer %srU   )zX-Stripe-Client-User-Agentz
User-AgentAuthorizationrX   zStripe-Accountstripe_contextzStripe-Contextidempotency_keyzIdempotency-Keyr0   r=   r1   zapplication/jsonContent-Typez!application/x-www-form-urlencodedrY   zStripe-Version)rn   r   rK   app_inford   r   osr   rN   r_   r   python_versionenable_telemetryappend	Exceptionjsondumpsr/   rc   
setdefaultuuiduuid4)rF   r;   r<   rA   
user_agentagentua
attr_funcsattrfuncvalr   rX   r   r   rY   r8   r8   r9   request_headers  sf   







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.
        rY   NrU   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/   r1   z[]   r   expandz%5B[z%5D]r=   )defaultr   r0   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%   rD   r/   r~   r   rZ   r   r   queryitemsendswithlenlistsetr   r   replacer   r   r   dictr   r   HttpVerbr   r   
add_paramsget_post_databoundaryAPIConnectionError)rF   r;   ra   r]   rA   rf   r<   re   request_optionsabs_urlexisting_paramskvencoded_paramsencoded_bodysupplied_headersr   schemenetlocpath_fragment	post_data	generatorrR   valuer   r8   r8   r9   _args_for_request_with_retriesB  s   





z,_APIRequestor._args_for_request_with_retriesFri   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rf   r<   re   Request to Stripe apir;   ra   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   rN   request_stream_with_retriesrequest_with_retriesr   rF   r;   ra   r]   rA   ri   rf   r<   re   r   r   r  r   r  r  rcontentrr   r3   
request_idr8   r8   r9   rm     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   rN   !request_stream_with_retries_asyncrequest_with_retries_asyncr   r$  r8   r8   r9   ru     sz   




z_APIRequestor.request_raw_asyncrr   c                 C   s   d|  ko
dk  S    S )N   i,  r8   )rF   rr   r8   r8   r9   _should_handle_code_as_errorr  s   z*_APIRequestor._should_handle_code_as_errorrq   r3   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    rc   r   r~   r   r+  r   data)rF   rq   rr   r3   r<   rj   r8   r8   r9   ro   u  s.   

z!_APIRequestor._interpret_responserz   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   r0  r1  NotImplementedErrorrN   r_   ro   RuntimeErrorr!   rF   rz   rr   r3   r<   json_contentr8   r8   r9   rx     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 rC   r8   ).0chunkr8   r8   r9   
<listcomp>  s    zE_APIRequestor._interpret_streaming_response_async.<locals>.<listcomp>r2  )r+  joinro   r4  r"   r5  r8   r8   r9   r|     s   
z1_APIRequestor._interpret_streaming_response_async)NN)NNF)<__name__
__module____qualname__r@   r   __annotations__r
   r&   r(   rG   rN   r$   rS   propertyrU   rX   rY   rZ   classmethodr\   staticmethodr   r^   rd   rc   r	   r   r+   r   rt   rv   r!   r{   r"   r}   r   r   r   r   r   r   r   r   r,   r   r  boolr   objectintrm   r   r.  ru   r+  r    ro   r   rx   r|   r8   r8   r8   r9   r?   W   s  
 
"







	
'
	
'
	
#
	

%)6
I	
	
 
	

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


T




r?   )Pior   r   r   r   r   typingr   r   r   r   r   r	   r
   r   r   r   r   typing_extensionsr   r   r   r   r   urllib.parser   r   r   rK   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   r2   rc   r?  r:   rC  r>   rD  r?   r8   r8   r8   r9   <module>   s<    4