o
    e                     @   sz   d dl 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 edg d	ZG d
d de
ZdS )    )
namedtuple)RequestSession)TwilioRestException)urlparse)
HttpClient)Response)ClientValidationJwtValidationPayloadmethodpathquery_stringall_headerssigned_headersbodyc                   @   sD   e Zd ZddgZdddZ		ddd	Zd
d Zdd Zdd ZdS )ValidationClientauthorizationhostTc                 C   s2   || _ || _|| _|| _|rt | _dS d| _dS )a+  
        Build a ValidationClient which signs requests with private_key and allows Twilio to
        validate request has not been tampered with.

        :param str account_sid: A Twilio Account Sid starting with 'AC'
        :param str api_key_sid: A Twilio API Key Sid starting with 'SK'
        :param str credential_sid: A Credential Sid starting with 'CR',
                                   corresponds to public key Twilio will use to verify the JWT.
        :param str private_key: The private key used to sign the Client Validation JWT.
        N)account_sidcredential_sidapi_key_sidprivate_keyr   session)selfr   r   r   r   pool_connections r   P/var/www/html/venv/lib/python3.10/site-packages/twilio/http/validation_client.py__init__   s
   zValidationClient.__init__NFc	                 C   s   | j pt }	t| |||||d}
|	|
}d|jvr)d|jvr)| ||jd< | |}t| j	| j
| j| j|}| |jd< |	j|||d}tt|j|jS )a  
        Make a signed HTTP Request

        :param str method: The HTTP method to use
        :param str url: The URL to request
        :param dict params: Query parameters to append to the URL
        :param dict data: Parameters to go in the body of the HTTP request
        :param dict headers: HTTP Headers to send with the request
        :param tuple auth: Basic Auth arguments
        :param float timeout: Socket/Read timeout for the request
        :param boolean allow_redirects: Whether or not to allow redirects
        See the requests documentation for explanation of all these parameters

        :return: An http response
        :rtype: A :class:`Response <twilio.rest.http.response.Response>` object
        )paramsdataheadersauthHostr   zTwilio-Client-Validation)allow_redirectstimeout)r   r   r   upperprepare_requestr!   	_get_host_build_validation_payloadr	   r   r   r   r   to_jwtsendr   intstatus_codetext)r   r   urlr   r    r!   r"   r%   r$   r   requestprepared_requestvalidation_payloadjwtresponser   r   r   r0   $   s    

zValidationClient.requestc                 C   s:   t |j}|j}|jpd}t|j|||jtj|j	pddS )z
        Extract relevant information from request to build a ClientValidationJWT
        :param PreparedRequest request: request we will extract information from.
        :return: ValidationPayload
         r   )
r   r/   r   queryr
   r   r!   r   !_ValidationClient__SIGNED_HEADERSr   )r   r0   parsedr   r   r   r   r   r)   J   s   

z*ValidationClient._build_validation_payloadc                 C   s   t |j}|jS )z Pull the Host out of the request)r   r/   netloc)r   r0   r8   r   r   r   r(   ]   s   
zValidationClient._get_hostc                 C   s2   | dd}|jdk s|jdkrt|jdddS )z
        Validate that a request to the new SSL certificate is successful
        :return: null on success, raise TwilioRestException if the request fails
        GETzhttps://api.twilio.com:8443   i,  z"Failed to validate SSL certificateN)r0   r-   r   )r   clientr4   r   r   r   validate_ssl_certificateb   s   z)ValidationClient.validate_ssl_certificate)T)NNNNNF)	__name__
__module____qualname__r7   r   r0   r)   r(   r=   r   r   r   r   r      s    

&r   N)collectionsr   requestsr   r   twilio.base.exceptionsr   urllib.parser   twilio.httpr   twilio.http.responser   twilio.jwt.validationr	   r
   r   r   r   r   r   <module>   s    