U
    ^g%ix%                     @   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                   @   sx  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  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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_extensionscredential_providerc
           
      C   s   |pt j}|p|d| _|p&|d| _|p6|d| _|pF|d| _|pPg | _|	pZd| _|pf| j| _	| j| jf| _
|pt | _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
        ZTWILIO_ACCOUNT_SIDZTWILIO_AUTH_TOKENZTWILIO_EDGEZTWILIO_REGIONN)osenvirongetr   r   r   r   r   r   r   authr   r   )
selfr   r   r   r   r   r   r   r   r    r   V/var/www/html/api-medvista/venv/lib/python3.8/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k	rJ| jdk	rJ| |}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&   Zauth_strategyZfiltered_datar   r   r   r2   @   s*    


zClientBase.requestc	              
      s   | j jstd| ||}|dkr*|d= | jrH| j }	|	 |d< n$| jdk	rh| jdk	rh| 	|}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   Zis_asyncRuntimeErrorr,   r   r-   r.   r   r   r/   r0   r1   r2   r3   r   r   r   request_asyncu   s2    


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 k	r|  |qS Nr1   ).0kvr   r   r   
<dictcomp>   s    z3ClientBase.copy_non_none_values.<locals>.<dictcomp>c                    s   g | ]}|d k	r  |qS r6   r7   )r8   itemr;   r   r   
<listcomp>   s     z3ClientBase.copy_non_none_values.<locals>.<listcomp>)
isinstancedictitemslist)r   r#   r   r;   r   r1      s    



zClientBase.copy_non_none_values)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s|d	krd
|krd|d
< d|k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POSTPUTzContent-Typez!application/x-www-form-urlencodedr*   zapplication/json)r	   platformsystemmachinepython_versionformatr   )r   r    r$   Zpkg_versionos_nameos_archrH   	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   )r8   partr   r   r   r>     s      z+ClientBase.get_hostname.<locals>.<listcomp>)netloc)
r   r   r   rT   splitjoinlen_replacestrr   )r   r!   
parsed_urlpiecesprefixsuffixr   r   r   r   r   r0      s&    	

zClientBase.get_hostname)r'   c                 C   s   d | jS )zf
        Provide a friendly representation

        :returns: Machine friendly representation
        z<Twilio {}>)rI   r   r;   r   r   r   __repr__	  s    zClientBase.__repr__)	NNNNNNNNN)NNNNNF)NNNNNF)__name__
__module____qualname____doc__r   rY   r
   r   r   r   r   r   objectr   floatboolr   r2   r5   r1   r/   r,   r0   r^   r   r   r   r   r      s~            
4      9      9$	 
$"r   )r   rE   typingr   r   r   r   r   urllib.parser   r   Ztwilior	   Ztwilio.httpr
   Ztwilio.http.http_clientr   Ztwilio.http.responser   Z%twilio.credential.credential_providerr   rc   r   r   r   r   r   <module>   s   