a
    !f@                     @   s  d dl Z d dlZ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 d dlmZ d dlZd dlZd dl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mZm Z  d d	l!m"Z"m#Z# ejd
ddZ$dddZ%dd Z&dd Z'G dd de(Z)dS )    N)BytesIOIOBase)AnyDictMappingOptionalTuplecast)NoReturn)OrderedDict)erroroauth_errorhttp_clientversionutil)MultipartDataGenerator)	urlencodeurlsplit
urlunsplit)StripeResponseStripeStreamResponse)dttimec                 C   s<   | j r&| j | d ur&t|  }nt|  }t|S N)	tzinfo	utcoffsetcalendartimegmutctimetupletimemktime	timetupleint)r   Zutc_timestamp r"   U/var/www/html/python-backend/venv/lib/python3.9/site-packages/stripe/api_requestor.py_encode_datetime   s    r$   %s[%s]c                 C   s,   t  }| D ]\}}|||| |f < q|S r   )r   items)keydatafmtdsubkeysubvaluer"   r"   r#   _encode_nested_dict$   s    r-   c           
      c   s  |   D ]\}}|d u rqqt|dr4||jfV  qt|tsHt|trt|D ]T\}}t|trtd||f |}t	|D ]\}}||fV  q|qPd||f |fV  qPqt|trt||}t	|D ]\}}	||	fV  qqt|t
j
r|t|fV  q||fV  qd S )N	stripe_idz%s[%d])r&   hasattrr.   
isinstancelisttuple	enumeratedictr-   _api_encodedatetimer$   )
r(   r'   valueisvZsubdictkvr+   r,   r"   r"   r#   r5   +   s&    



r5   c                 C   s4   t | \}}}}}|r"d||f }t|||||fS )Nz%s&%s)r   r   )urlqueryschemenetlocpathZ
base_queryfragmentr"   r"   r#   _build_api_urlC   s    rB   c                   @   sh  e Zd ZU ee ed< eed< eed< ee ed< d$ddZedd	 Zd%eeee	ee
f  ee	eef  eeef d
ddZd&eeee	ee
f  ee	eef  eeef d
ddZedddZdd Zdd Zdd Zd'eeee	ee
f  ee	eef  eeeee	eef ef dddZdd Zeee	eef eddd Zeee	eef ed!d"d#ZdS )(APIRequestorapi_keyapi_baseapi_versionstripe_accountNc                 C   s   |pt j| _|| _|pt j| _|| _d | _ddl m} ddl m} |rN|| _nBt j	rrt j	| _|| jkrt
d ntj||dt _	t j	| _|| _d S )Nr   )verify_ssl_certs)proxyzstripe.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.)rH   rI   )striperE   rD   rF   rG   Z_default_proxyrH   rI   _clientZdefault_http_clientwarningswarnr   Znew_default_http_client)selfr'   clientrE   rF   accountverifyrI   r"   r"   r#   __init__R   s(    

zAPIRequestor.__init__c                 C   s@   |d }|d r"|d|d f 7 }|d r<|d|d f 7 }|S )Nnamer   z/%sr<   z (%s)r"   )clsinfostrr"   r"   r#   format_app_infoy   s    zAPIRequestor.format_app_info)methodr<   paramsheadersreturnc           
      C   s6   | j | |||dd\}}}}| |||}	|	|fS )NFis_streaming)request_rawlowerinterpret_response)
rN   rX   r<   rY   rZ   rbodyrcoderheaders
my_api_keyrespr"   r"   r#   request   s
    zAPIRequestor.requestc           
      C   s<   | j | |||dd\}}}}| tt|||}	|	|fS )NTr\   )r^   r_   interpret_streaming_responser	   r   )
rN   rX   r<   rY   rZ   streamrb   rc   rd   re   r"   r"   r#   request_stream   s    zAPIRequestor.request_stream)r[   c              	   C   s~   z|d }W n. t tfy:   td||f |||Y n0 d }t|tr\| |||||}|d u rv| |||||}|d S )Nr   z@Invalid response object from API: %r (HTTP response code was %d))KeyError	TypeErrorr   APIErrorr0   rV   specific_oauth_errorspecific_api_error)rN   ra   rb   re   rc   
error_dataerrr"   r"   r#   handle_error_response   s*    



z"APIRequestor.handle_error_responsec              	   C   sJ  t jd|d|d|d|dd |dksJ|dkrb|dd	krbt|d||||S |d
v r|ddkrt|d||||S t|d|d|d||||S n|dkrt|d||||S |dkrt|d|d|d||||S |dkr.t	|d||||S t
|d||||S d S )NzStripe API error receivedcodetypemessageparam)
error_codeZ
error_typeerror_messageZerror_parami    Z
rate_limit)rx   i  Zidempotency_errori  i  i  )r   log_infogetr   ZRateLimitErrorZIdempotencyErrorInvalidRequestErrorAuthenticationErrorZ	CardErrorPermissionErrorrl   )rN   ra   rb   re   rc   ro   r"   r"   r#   rn      sd    		
	
zAPIRequestor.specific_api_errorc                 C   s   | d|}tjd||d ||||||g}|dkr>tj| S |dkrPtj| S |dkrbtj| S |dkrttj| S |dkrtj| S |d	krtj	| S d S )
Nerror_descriptionzStripe OAuth error received)rv   r~   Zinvalid_clientZinvalid_grantZinvalid_requestZinvalid_scopeZunsupported_grant_typeZunsupported_response_type)
rz   r   ry   r   ZInvalidClientErrorZInvalidGrantErrorr{   ZInvalidScopeErrorZUnsupportedGrantTypeErrorZUnsupportedResponseTypeError)rN   ra   rb   re   rc   rv   descriptionargsr"   r"   r#   rm      s(    





z!APIRequestor.specific_oauth_errorc           	   	   C   s   dt jf }tjr&|d| tj 7 }t jdd| jjd}dtjgdtjgdd	d
 gfD ]2\}}z
| }W n t	y   d}Y n0 |||< qXtjrtj|d< t
||d|f d}| jr| j|d< |dkrd|d< |dtt  | j|d< |S )NzStripe/v1 PythonBindings/%s pythonrJ   )Zbindings_versionlangZ	publisherhttplibZlang_versionplatformunamec                   S   s   d t S )Nr   )joinr   r   r"   r"   r"   r#   <lambda>      z.APIRequestor.request_headers.<locals>.<lambda>z
(disabled)Zapplicationz	Bearer %s)zX-Stripe-Client-User-Agentz
User-AgentAuthorizationzStripe-Accountpostz!application/x-www-form-urlencodedContent-TypezIdempotency-KeyzStripe-Version)r   VERSIONrJ   Zapp_inforW   rK   rS   r   python_version	ExceptionjsondumpsrG   
setdefaultrV   uuiduuid4rF   )	rN   rD   rX   
user_agentZuaattrfuncvalrZ   r"   r"   r#   request_headers  s<    






zAPIRequestor.request_headersF)rX   r<   rY   supplied_headersr]   r[   c                 C   s  |durt |nd}| jr"| j}nddlm} |}|du rDtdd| j|f }	ttt|p^i }
|
	dd	dd	}
|d
ks|dkr|rt
|	|
}	d}nb|dkr|dur|ddkrt }||pi  | }d|jf |d< n|
}ntd|f | ||}|dur2| D ]\}}|||< qtjd||	d tjd|
| jd |rt| j||	||\}}}n| j||	||\}}}tjd|	|d tjd|d d|v r|d }tjdt|d ||||fS )z3
        Mechanism for issuing an API call
        Nr   )rD   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%sz%5B[z%5D]rz   deleter   r   zmultipart/form-dataz multipart/form-data; boundary=%szUnrecognized HTTP method %r.  This may indicate a bug in the Stripe bindings.  Please contact support@stripe.com for assistance.zRequest to Stripe api)rX   r@   zPost details)	post_datarF   zStripe API response)r@   Zresponse_codezAPI response body)bodyz
Request-IdzDashboard link for request)link)r4   rD   rJ   r   r|   rE   r   r1   r5   replacerB   rz   r   Z
add_paramsZget_post_databoundaryZAPIConnectionErrorr   r&   r   ry   	log_debugrF   rK   Zrequest_stream_with_retriesZrequest_with_retriesZdashboard_link)rN   rX   r<   rY   r   r]   Zsupplied_headers_dictrd   rD   Zabs_urlZencoded_paramsr   	generatorrZ   r'   r7   Zrcontentrb   rc   Z
request_idr"   r"   r#   r^   8  s    




zAPIRequestor.request_rawc                 C   s   d|  kodk n   S )N   i,  r"   )rN   rb   r"   r"   r#   _should_handle_code_as_error  s    z)APIRequestor._should_handle_code_as_error)ra   rb   rc   r[   c              	   C   s   z0t |drtt|d}ttt|||}W n0 ty`   td||f tt|||Y n0 | 	|r~| 
|||j| |S )Ndecodezutf-8z>Invalid response body from API: %s (HTTP response code was %d))r/   r	   bytesr   r   rV   r   r   rl   r   rq   r(   )rN   ra   rb   rc   re   r"   r"   r#   r`     s(    


zAPIRequestor.interpret_response)rh   rb   rc   r[   c                 C   sh   |  |rXt|dr$tt| }nt|dr8| }ntd| ||| tdnt	|||S d S )Ngetvaluereadz`HTTP client %s does not return an IOBase object which can be consumed when streaming a response.z.interpret_response should have raised an error)
r   r/   r	   r   r   r   NotImplementedErrorr`   RuntimeErrorr   )rN   rh   rb   rc   Zjson_contentr"   r"   r#   rg     s    



z)APIRequestor.interpret_streaming_response)NNNNN)NN)NN)NNF)__name__
__module____qualname__r   rV   __annotations__rR   classmethodrW   r   r   r   r   rf   r   ri   r
   rq   rn   rm   r   boolobjectr!   r^   r   r`   r   rg   r"   r"   r"   r#   rC   L   sh   
     
'
  
  
6-   grC   )r%   )*r   r6   ior   r   r   r   r   typingr   r   r   r   r   r	   Ztyping_extensionsr
   r   rL   collectionsr   rJ   r   r   r   r   r   Zstripe.multipart_data_generatorr   urllib.parser   r   r   Zstripe.stripe_responser   r   r$   r-   r5   rB   r   rC   r"   r"   r"   r#   <module>   s(    	
	