o
    Bijb                     @   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 d dlZd dl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* d dl+m,Z,m-Z-m.Z. d dl/m0Z0m1Z1 d dl2m3Z3m4Z4 d dl5m6Z6m7Z7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> erd dl?m@Z@ ed ZAdaBeeC eDd< G dd deEZFdS )    )BytesIOIOBaseN)
AnyAsyncIterableDictListMappingOptionalTupleUnioncastClassVar)TYPE_CHECKINGLiteralNoReturnUnpack)urlsplit
urlunsplit)	log_debuglog_infodashboard_link_convert_to_stripe_object)VERSION)MultipartDataGenerator)	urlencode)_api_encode)StripeResponseStripeStreamResponseStripeStreamResponseAsync)RequestOptionsmerge_options)RequestorOptions_GlobalRequestorOptions)
HTTPClientnew_default_http_clientnew_http_client_async_fallback)AppInfo)BaseAddress)ApiMode)StripeObject)getpostdelete_default_proxyc                   @   s  e Zd ZU dZed ed< 		dGd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		dGd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		dGd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		dGd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 		dGd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&defd0d1Z'		dGdddededeeeef  dee ded2e(d eee  fd3d4Z)			5dHdddededeeeef  dee d6e*ded2e(d eee  de+e,e-eeef f fd7d8Z.			5dHdddededeeeef  dee d6e*ded2e(d eee  de+e/e0 e-eeef f fd9d:Z1d;d< Z2d=e,d>e-d?eeef de3fd@dAZ4dBe/e0 d>e-d?eeef de!fdCdDZ5dBe6d>e-d?eeef defdEdFZ7dS )I_APIRequestorNz_APIRequestor|None	_instanceoptionsclientc                 C   s   |d u rt  }|| _|| _d S N)r!   _options_client)selfr0   r1    r6   U/var/www/html/Trade-python/venv/lib/python3.10/site-packages/stripe/_api_requestor.py__init__I   s   
z_APIRequestor.__init__returnc                 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   )
r4   stripedefault_http_clientr:   r;   r$   r%   r-   warningswarn)r5   r1   kwargsr?   r6   r6   r7   _get_http_clientW   s,   
z_APIRequestor._get_http_clientc                 C   sT   |pi }| j  }dD ]}||v r|| d ur|| ||< qttdi || jdS )N)api_keystripe_accountstripe_versionr0   r1   r6   )r3   to_dictr.   r!   r4   )r5   r0   new_optionskeyr6   r6   r7   _replace_optionsy   s   
z_APIRequestor._replace_optionsc                 C      | j jS r2   )r3   rC   r5   r6   r6   r7   rC         z_APIRequestor.api_keyc                 C   rK   r2   )r3   rD   rL   r6   r6   r7   rD      rM   z_APIRequestor.stripe_accountc                 C   rK   r2   )r3   rE   rL   r6   r6   r7   rE      rM   z_APIRequestor.stripe_versionc                 C   rK   r2   )r3   base_addressesrL   r6   r6   r7   rN      rM   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(...)
        NrF   )r/   r"   )clsr6   r6   r7   _global_instance   s   
z_APIRequestor._global_instanceparamsc                  K   s   t  | S r2   )r.   rP   rJ   )rQ   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   )rO   infostrr6   r6   r7   _format_app_info   s   z_APIRequestor._format_app_info)usagemethodrU   base_addressrY   r)   c             
   C   sP   |  |}d}|j| ||d||||d\}	}
}||	|
|}t||||dS NV1Fis_streamingapi_moder[   r0   rY   )resprQ   	requestorr`   )rJ   request_rawlower_interpret_responser   )r5   rZ   rU   rQ   r0   r[   rY   rb   r`   rbodyrcoderheadersra   r6   r6   r7   request   s&   


z_APIRequestor.requestc             
      sX   d}|  |}|j| ||d||||dI d H \}	}
}||	|
|}t||||dS r\   )rJ   request_raw_asyncrd   re   r   )r5   rZ   rU   rQ   r0   r[   rY   r`   rb   rf   rg   rh   ra   r6   r6   r7   request_async   s(   


z_APIRequestor.request_asyncc             
   C   s@   d}| j | ||d||||d\}}	}
| tt||	|
}|S Nr]   Tr^   )rc   rd   _interpret_streaming_responser   r   r5   rZ   rU   rQ   r0   r[   rY   r`   streamrg   rh   ra   r6   r6   r7   request_stream   s"   

z_APIRequestor.request_streamc             
      sH   d}| j | ||d||||dI d H \}}	}
| ||	|
I d H }|S rl   )rj   rd   #_interpret_streaming_response_asyncrn   r6   r6   r7   request_stream_async  s$   


z"_APIRequestor.request_stream_asyncc              	   C   sv   z|d }W n t tfy   td||f |||w d }t|tr,| |||||}|d u r9| |||||}|)Nerrorz@Invalid response object from API: %r (HTTP response code was %d))KeyError	TypeErrorrs   APIError
isinstancerW   specific_oauth_errorspecific_api_error)r5   rf   rg   ra   rh   
error_dataerrr6   r6   r7   handle_error_response(  s,   	


z#_APIRequestor.handle_error_responsec              	   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 API error receivedcodetypemessageparam)
error_code
error_typeerror_messageerror_parami    
rate_limit)r   i  idempotency_errori  i  i  )
r   r*   rs   RateLimitErrorIdempotencyErrorInvalidRequestErrorAuthenticationError	CardErrorPermissionErrorrv   )r5   rf   rg   ra   rh   rz   r6   r6   r7   ry   E  s^   			z _APIRequestor.specific_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)r5   rf   rg   ra   rh   r   descriptionargsr6   r6   r7   rx   {  s(   





z"_APIRequestor.specific_oauth_errorc              	   C   s"  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y?   d}Y nw |||< q+tjrMtj|d< t	
||d|df d}|d}	|	rg|	|d< |d}
|
rr|
|d< |dkr|dtt  d|d< |d}|r||d< |S )NzStripe/v1 PythonBindings/%s pythonr=   )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 %srC   )zX-Stripe-Client-User-Agentz
User-AgentAuthorizationrD   zStripe-Accountidempotency_keyzIdempotency-Keyr+   z!application/x-www-form-urlencodedContent-TyperE   zStripe-Version)r   r=   app_inforX   rB   rS   r   python_version	Exceptionjsondumpsr*   
setdefaultrW   uuiduuid4)r5   rZ   r0   
user_agentuaattrfuncvalheadersrD   r   rE   r6   r6   r7   request_headers  sJ   







z_APIRequestor.request_headersr`   c             
   C   s  t | j|}|ddu rtdd| jj||f }	ttt|p#i }
|
	dd	dd}
|
}d}d	|v rE|d	 durEt
|d	 }| ||}|d
ksS|dkrt|rq|
}t|	\}}}}}|rhd||f }t|||||f}	d}n1|dkr|dur|ddkrt }||pi  | }d|jf |d< n|}ntd|f |dur| D ]\}}|||< q|d}||	|||||
|dfS )3
        Mechanism for issuing an API call
        rC   NzNo 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%sz%5B[z%5D]r   r*   r,   z%s&%sr+   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_retriesrE   )r    r3   r*   rs   r   rN   r   listr   replacedictr   r   r   r   
add_paramsget_post_databoundaryAPIConnectionErroritems)r5   rZ   rU   rQ   r0   r[   r`   rY   request_optionsabs_urlencoded_paramsencoded_bodysupplied_headersr   queryschemenetlocpath
base_queryfragment	post_data	generatorrI   valuer   r6   r6   r7   _args_for_request_with_retries  sh   


z,_APIRequestor._args_for_request_with_retriesFr_   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r[   r`   rY   Request to Stripe apirZ   rU   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   rB   request_stream_with_retriesrequest_with_retriesr   r5   rZ   rU   rQ   r0   r_   r[   r`   rY   r   r   r   r   r   r   rcontentrg   rh   
request_idr6   r6   r7   rc   &  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 )r   asyncr   r   r   r   r   r   Nr   r   r   r   r   r   r   )r   r   r   rB   !request_stream_with_retries_asyncrequest_with_retries_asyncr   r   r6   r6   r7   rj   q  sz   




z_APIRequestor.request_raw_asyncc                 C   s   d|  ko
dk  S    S )N   i,  r6   )r5   rg   r6   r6   r7   _should_handle_code_as_error  s   z*_APIRequestor._should_handle_code_as_errorrf   rg   rh   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   rW   r   rs   rv   r   r|   data)r5   rf   rg   rh   ra   r6   r6   r7   re     s*   

z!_APIRequestor._interpret_responsero   c                    sH   |  |rddd |2 I d H }| ||| tdt|||S )N    c                    s   g | z3 d H W }|q6 S r2   r6   ).0chunkr6   r6   r7   
<listcomp>  s    zE_APIRequestor._interpret_streaming_response_async.<locals>.<listcomp>/_interpret_response should have raised an error)r   r   re   RuntimeErrorr   r5   ro   rg   rh   json_contentr6   r6   r7   rq     s   
z1_APIRequestor._interpret_streaming_response_asyncc                 C   sl   |  |r0t|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.r   )r   r   r   r   r   r   NotImplementedErrorrB   rS   re   r   r   r   r6   r6   r7   rm     s   




z+_APIRequestor._interpret_streaming_response)NN)NNF)8__name__
__module____qualname__r/   r   __annotations__r	   r!   r#   r8   rB   r   rJ   propertyrC   rD   rE   rN   classmethodrP   staticmethodr   rR   rX   rW   r   r   r'   r   ri   rk   r   rp   r   rr   r   r|   ry   rx   r   r(   r   boolr
   objectintrc   r   r   rj   r   r   re   rq   r   rm   r6   r6   r6   r7   r.   F   s  
 
"







	
#
	
#
	
"
	
64	
	
e
	

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


T




r.   )Gior   r   r   r   typingr   r   r   r   r   r	   r
   r   r   r   typing_extensionsr   r   r   r   r   urllib.parser   r   r=   stripe._utilr   r   r   r   stripe._versionr   stripe._error_errorrs   stripe.oauth_errorr    stripe._multipart_data_generatorr   r   stripe._encoder   stripe._stripe_responser   r   r   stripe._request_optionsr   r    stripe._requestor_optionsr!   r"   stripe._http_clientr#   r$   r%   stripe._app_infor&   stripe._base_addressr'   stripe._api_moder(   stripe._stripe_objectr)   HttpVerbr-   rW   r  r
  r.   r6   r6   r6   r7   <module>   s6    0