o
    f>hx%                     @   s   d dl Z d dlZd dlmZmZmZmZmZ d dl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 G d	d
 d
eZdS )    N)DictListMutableMappingOptionalTuple)urlparse
urlunparse)__version__)
HttpClient)TwilioHttpClient)Response)CredentialProviderc                   @   s  e Zd ZdZ									d&dee dee dee dee dee deeeef  d	ee d
eee  dee	 fddZ
						d'dededeeeef  deeeef  deeeef  deeeef  dee dedefddZ						d'dededeeeef  deeeef  deeeef  deeeef  dee dedefddZdd Zdeeeef  deeef fddZdedeeeef  deeef fd d!Zdedefd"d#Zdefd$d%ZdS )(
ClientBasez&A client for accessing the Twilio API.Nusernamepasswordaccount_sidregionhttp_clientenvironmentedgeuser_agent_extensionscredential_providerc
           
      C   s   |pt j}|p|d| _	 |p|d| _	 |p|d| _	 |p&|d| _	 |p,g | _	 |	p2d| _	 |p9| j| _		 | j| jf| _
	 |pHt | _dS )a  
        Initializes the Twilio Client

        :param username: Username to authenticate with
        :param password: Password to authenticate with
        :param account_sid: Account SID, defaults to Username
        :param region: Twilio Region to make requests to, defaults to 'us1' if an edge is provided
        :param http_client: HttpClient, defaults to TwilioHttpClient
        :param environment: Environment to look for auth details, defaults to os.environ
        :param edge: Twilio Edge to make requests to, defaults to None
        :param user_agent_extensions: Additions to the user agent string
        :param credential_provider: credential provider for authentication method that needs to be used
        TWILIO_ACCOUNT_SIDTWILIO_AUTH_TOKENTWILIO_EDGETWILIO_REGIONN)osenvirongetr   r   r   r   r   r   r   authr   r   )
selfr   r   r   r   r   r   r   r   r    r!   J/var/www/html/venv/lib/python3.10/site-packages/twilio/base/client_base.py__init__   s&   


zClientBase.__init__Fmethoduriparamsdataheadersr   timeoutallow_redirectsreturnc	              
   C   s   |  ||}| jr| j }	|	 |d< n| jdur%| jdur%| |}nd}|dkr.|d= | |}| |}
| j	j
||||
||||dS )a(  
        Makes a request to the Twilio API using the configured http client
        Authentication information is automatically added if none is provided

        :param method: HTTP Method
        :param uri: Fully qualified url
        :param params: Query string parameters
        :param data: POST body data
        :param headers: HTTP Headers
        :param auth: Authentication
        :param timeout: Timeout in seconds
        :param allow_redirects: Should the client follow redirects

        :returns: Response from the Twilio API
        AuthorizationNDELETEAcceptr&   r'   r(   r   r)   r*   )get_headersr   to_auth_strategyget_auth_stringr   r   get_authget_hostnamecopy_non_none_valuesr   requestr    r$   r%   r&   r'   r(   r   r)   r*   auth_strategyfiltered_datar!   r!   r"   r6   @   s*   


zClientBase.requestc	              
      s   | j js	td| ||}|dkr|d= | jr%| j }	|	 |d< n| jdur5| jdur5| 	|}nd}| 
|}| |}
| j j||||
||||dI dH S )a  
        Asynchronously makes a request to the Twilio API  using the configured http client
        The configured http client must be an asynchronous http client
        Authentication information is automatically added if none is provided

        :param method: HTTP Method
        :param uri: Fully qualified url
        :param params: Query string parameters
        :param data: POST body data
        :param headers: HTTP Headers
        :param auth: Authentication
        :param timeout: Timeout in seconds
        :param allow_redirects: Should the client follow redirects

        :returns: Response from the Twilio API
        z>http_client must be asynchronous to support async API requestsr-   r.   r,   Nr/   )r   is_asyncRuntimeErrorr0   r   r1   r2   r   r   r3   r4   r5   r6   r7   r!   r!   r"   request_asyncu   s4   


zClientBase.request_asyncc                    s@   t |tr fdd| D S t |tr fdd|D S |S )Nc                    s$   i | ]\}}|d ur|  |qS Nr5   ).0kvr    r!   r"   
<dictcomp>   s
    
z3ClientBase.copy_non_none_values.<locals>.<dictcomp>c                    s   g | ]}|d ur  |qS r=   r>   )r?   itemrB   r!   r"   
<listcomp>   s    z3ClientBase.copy_non_none_values.<locals>.<listcomp>)
isinstancedictitemslist)r    r'   r!   rB   r"   r5      s   



zClientBase.copy_non_none_valuesc                 C   s
   |p| j S )z
        Get the request authentication object
        :param auth: Authentication (username, password)
        :returns: The authentication object
        )r   )r    r   r!   r!   r"   r3      s   
zClientBase.get_authc                 C   s   |pi }t }t }t }t }d|||||d< | jD ]}|d  d|7  < qdt |d< d|d< |dks@|d	krHd
|vrHd|d
< d|vrPd|d< |S )z
        Get the request headers including user-agent, extensions, encoding, content-type, MIME type
        :param method: HTTP method
        :param headers: HTTP headers
        :returns: HTTP headers
        z"twilio-python/{} ({} {}) Python/{}z
User-Agentz {}z	python-{}zX-Twilio-Clientzutf-8zAccept-CharsetPOSTPUTzContent-Typez!application/x-www-form-urlencodedr.   zapplication/json)r	   platformsystemmachinepython_versionformatr   )r    r$   r(   pkg_versionos_nameos_archrO   	extensionr!   r!   r"   r0      s(   	
zClientBase.get_headersc                 C   s   | j s| js|S t|}|jd}|d }d|dd }d}d}t|dkr.|d }nt|dkr<|d }|d }| j p@|}| jpI|pI|oId	}|jdd
d ||||fD d}tt	|S )z
        Determines the proper hostname given edge and region preferences
        via client configuration or uri.

        :param uri: Fully qualified url

        :returns: The final uri used to make the request
        .r   N            us1c                 S   s   g | ]}|r|qS r!   r!   )r?   partr!   r!   r"   rE     s    z+ClientBase.get_hostname.<locals>.<listcomp>)netloc)
r   r   r   r]   splitjoinlen_replacestrr   )r    r%   
parsed_urlpiecesprefixsuffixr   r   r!   r!   r"   r4      s&   	

zClientBase.get_hostnamec                 C   s   d | jS )zf
        Provide a friendly representation

        :returns: Machine friendly representation
        z<Twilio {}>)rP   r   rB   r!   r!   r"   __repr__	  s   zClientBase.__repr__)	NNNNNNNNN)NNNNNF)__name__
__module____qualname____doc__r   rb   r
   r   r   r   r#   r   objectr   floatboolr   r6   r<   r5   r3   r0   r4   rg   r!   r!   r!   r"   r      s    
	

4	

9	

9&

$"r   )r   rL   typingr   r   r   r   r   urllib.parser   r   twilior	   twilio.httpr
   twilio.http.http_clientr   twilio.http.responser   %twilio.credential.credential_providerr   rl   r   r!   r!   r!   r"   <module>   s    