o
    e                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ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mZ ed	d
ZdZdedgZdd Zdd ZG dd deZdS )z)
Base client, used for all API requests.
    N)
namedtuple)ResponseObject)AuthenticationErrorInvalidRequestErrorPlivoRestErrorPlivoServerErrorResourceNotFoundErrorValidationError)is_valid_mainaccountis_valid_subaccount)__version__)RequestSessionAuthenticationCredentialszauth_id auth_tokenzhttps://api.plivo.com/z
v1/Accountc                   C   s   dt t f S )Nzplivo-python/%s (Python: %s))r   platformpython_version r   r   I/var/www/html/venv/lib/python3.10/site-packages/plivo/rest/base_client.pyget_user_agent   s   r   c                 C   s`   | r|szt jd } t jd }W n ty   tdw t| s*t| s*td|  t| |dS )zDFetches the right credentials either from params or from environmentPLIVO_AUTH_IDPLIVO_AUTH_TOKENz:The Plivo Python SDK could not find your auth credentials.zInvalid auth_id supplied: %sauth_id
auth_token)osenvironKeyErrorr   r
   r   r   r   r   r   r   fetch_credentials   s   
r   c                   @   sj   e Zd ZdddZdd Zdd Z		dd	d
ZdddZ			dddZdd Z						dddZ
dS )
BaseClientN   c                 C   sr   t | _t | _| jjt ddd t||| j_t | _	| j	jt ddd t||| j	_|| _
|| _dS )z\
        The Plivo API client.

        Deals with all the API requests to be made.
        zapplication/json)
User-AgentContent-TypeAcceptzno-cachezmultipart/form-data)r!   zCache-Controlr"   N)PLIVO_API_BASE_URIbase_urir   sessionheadersupdater   r   authmultipart_sessionproxiestimeout)selfr   r   r+   r,   r   r   r   __init__0   s"   
zBaseClient.__init__c                 C   s   | S Nr   )r-   r   r   r   	__enter__I   s   zBaseClient.__enter__c                 C   s   | j   | j  d S r/   )r&   closer*   )r-   exc_typeexc_valexc_tbr   r   r   __exit__L   s   
zBaseClient.__exit__c                    s  z&|j dd d}|r||j}|}d|v r% r% fdd|jD |_W n ty1   d}Y nw |jdkrK|rBd	|v rBt|jtd
j|jd|jdkrd|r[d	|v r[t	|jt	dj|jd|jdkr}|rtd	|v rtt
|jt
dj|jd|jdkr|rd	|v rt|jtdj||jd|jdkr|rd	|v rt|jtdj|jd|dkr|jdkrtdj|jd|S |jdvrtdj|j|d|S )zgProcesses the API response based on the status codes and method used
        to access the API
        c                 S   s   t | tr	t| S | S r/   )
isinstancedictr   )xr   r   r   <lambda>[   s    z-BaseClient.process_response.<locals>.<lambda>)object_hookobjectsc                    s   g | ]} |j qS r   )__dict__).0objobjects_typer-   r   r   
<listcomp>a   s    
z/BaseClient.process_response.<locals>.<listcomp>Ni  errorzFA parameter is missing or is invalid while accessing resourceat: {url})urli  z9Failed to authenticate while accessing resource at: {url}i  zResource not found at: {url}i  z?HTTP method "{method}" not allowed to access resource at: {url})methodrC   i  z:A server error occurred while accessing resource at: {url}DELETE   z&Resource at {url} could not be deleted)         zAReceived status code {status_code} for the HTTP method "{method}")status_coderD   )jsonr<   r;   
ValueErrorrJ   r	   rB   formatrC   r   r   r   r   r   )r-   rD   responseresponse_typer@   response_jsonrr   r?   r   process_responseP   s   	










	
zBaseClient.process_responsec                 C   sd   |pg }t |d| j| jjd gtdd |D  d fi |dkr&d|ind|i}| j|S )Nr   r   c                 S      g | ]}t |qS r   strr=   pr   r   r   rA          z-BaseClient.create_request.<locals>.<listcomp>GETparamsrK   )r   joinr%   r&   r)   listprepare_request)r-   rD   pathdatareqr   r   r   create_request   s   zBaseClient.create_requestc                 C   s   |pg }i }|dkr||d< n||d< |r#d|v r#|d dkr#||d< t |d| j| jjd gtd	d
 |D  d fi |}| j|S )NrY   rZ   r_   file filesr   r   c                 S   rS   r   rT   rV   r   r   r   rA      rX   z7BaseClient.create_multipart_request.<locals>.<listcomp>)r   r[   r%   r*   r)   r\   r]   )r-   rD   r^   r_   rd   	data_argsr`   r   r   r   create_multipart_request   s"   
z#BaseClient.create_multipart_requestc                 K   s:   d|v r|d }|d= n| j }|j|f| j| jd|S )Nr&   )r+   r,   )r&   sendr+   r,   )r-   requestkwargsr&   r   r   r   send_request   s   zBaseClient.send_requestc                 K   s^   |d ur|  ||||}| j}	n
| |||}| j}	|	|d< | j|fi |}
| ||
||S )Nr&   )rf   r*   ra   r&   rj   rR   )r-   rD   r^   r_   rO   r@   rd   ri   r`   r&   resr   r   r   rh      s   zBaseClient.request)NNNr    )NN)NNN)NNNNN)__name__
__module____qualname__r.   r0   r5   rR   ra   rf   rj   rh   r   r   r   r   r   /   s&    


G
r   )__doc__r   r   collectionsr   
plivo.baser   plivo.exceptionsr   r   r   r   r   r	   plivo.utilsr
   r   plivo.versionr   requestsr   r   r   	PLIVO_APIr[   r$   r   r   objectr   r   r   r   r   <module>   s"    