a
    !f                     @   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__)TwilioException)
HttpClient)TwilioHttpClient)Responsec                   @   sj  e Zd ZdZdee ee ee ee ee eeeef  ee eee  dddZ	deeee
eef  ee
eef  ee
eef  eeeef  ee eed	dd	Zdeeee
eef  ee
eef  ee
eef  eeeef  ee eed	d
dZeeeef  eeef dddZeee
eef  e
eef dddZeedddZed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_extensionsc	           	      C   s   |pt j}|p|d| _|p&|d| _|p6|d| _|pF|d| _|pPg | _| jr`| jshtd|pp| j| _	| j| jf| _
|pt | _dS )am  
        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
        ZTWILIO_ACCOUNT_SIDZTWILIO_AUTH_TOKENZTWILIO_EDGEZTWILIO_REGIONz1Credentials are required to create a TwilioClientN)osenvirongetr   r   r   r   r   r
   r   authr   r   )	selfr   r   r   r   r   r   r   r    r   X/var/www/html/python-backend/venv/lib/python3.9/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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
        r!   r"   r#   r   r$   r%   )get_authget_headersget_hostnamer   request	r   r   r    r!   r"   r#   r   r$   r%   r   r   r   r+   >   s    

zClientBase.requestc	           	   
      sR   | j jst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'   N)r   is_asyncRuntimeErrorr(   r)   r*   r+   r,   r   r   r   request_asyncg   s"    

zClientBase.request_async)r   r&   c                 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   r(      s    zClientBase.get_auth)r   r#   r&   c                 C   s   |pi }t }t }t }t }d|||||d< | jD ]}|d  d|7  < q>dt |d< d|d< |dkrd	|vrd
|d	< d|vrd|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POSTzContent-Typez!application/x-www-form-urlencodedAcceptzapplication/json)r	   platformsystemmachinepython_versionformatr   )r   r   r#   Zpkg_versionos_nameos_archr5   	extensionr   r   r   r)      s(    	
zClientBase.get_headers)r    r&   c                 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rx|d }|d }| j p|}| jp|p|od	}|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            Zus1c                 S   s   g | ]}|r|qS r   r   ).0partr   r   r   
<listcomp>       z+ClientBase.get_hostname.<locals>.<listcomp>)netloc)
r   r   r   rD   splitjoinlen_replacestrr   )r   r    
parsed_urlpiecesprefixsuffixr   r   r   r   r   r*      s&    	

zClientBase.get_hostname)r&   c                 C   s   d | jS )zf
        Provide a friendly representation

        :returns: Machine friendly representation
        z<Twilio {}>)r6   r   )r   r   r   r   __repr__   s    zClientBase.__repr__)NNNNNNNN)NNNNNF)NNNNNF)__name__
__module____qualname____doc__r   rI   r   r   r   r   r   objectr   floatboolr   r+   r/   r(   r)   r*   rN   r   r   r   r   r      sv           
2      -      /$	
$"r   )r   r2   typingr   r   r   r   r   urllib.parser   r   Ztwilior	   Ztwilio.base.exceptionsr
   Ztwilio.httpr   Ztwilio.http.http_clientr   Ztwilio.http.responser   rS   r   r   r   r   r   <module>   s   