o
    6/iE                     @   sp   d dl Zd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
Z
ddlmZ eG dd dZG dd	 d	ZdS )
    N)	dataclass)Optional   )settingsc                   @   sJ   e Zd ZU eed< eed< eed< eed< ee ed< eed< eed< dS )	AroFloAuthResultiso_timestampauthorization_headerhmac_signatureaccepthost_ipurl_path
var_stringN)__name__
__module____qualname__str__annotations__r    r   r   >/var/www/html/flask_server/apps/aroflo_connector_app/client.pyr      s   
 r   c                   @   sd   e Zd ZdddZdefddZdefddZd	ed
edefddZdefddZ	de
fddZdS )AroFloClientreturnNc                 C   s   t | _ | j jd| _d S )N/)r   base_urlrstripselfr   r   r   __init__   s   zAroFloClient.__init__c                 C   s   t j jddd S )Nmilliseconds)timespecZ)dtdatetimeutcnow	isoformatr   r   r   r   _build_iso_timestamp"   s   z!AroFloClient._build_iso_timestampc                 C   sR   t jj| jjdd}t jj| jjdd}t jj| jjdd}d| d| d| S )z
        Replica:
        'uencoded=' + encodeURIComponent(uEncoded) +
        '&pencoded=' + encodeURIComponent(pEncoded) +
        '&orgEncoded=' + encodeURIComponent(orgEncoded)
         safez	uencoded=z
&pencoded=z&orgEncoded=)urllibparsequoter   	u_encoded	p_encodedorg_encoded)r   u_encp_encorg_encr   r   r   _build_authorization_header&   s   z(AroFloClient._build_authorization_headerrequest_typer   c              	   C   s   | j j}|s
td| j j}d}| j jpd}|  }|  }|g}	|r(|	| |	|||||g d	|	}
t
|d|
dtj}| }t|||||||dS )uZ   
        Implementa la función AroFloAuth(requestType, VarString) del script JS.
        u+   AROFLO_SECRET_KEY no está definido en .envr%   z	text/json+zutf-8)r   r   r	   r
   r   r   r   )r   
secret_keyRuntimeErrorr   r
   r1   r$   appendextendjoinhmacnewencodehashlibsha512	hexdigestr   )r   r2   r   r4   r   r   r
   authorizationiso_tspartspayloadhsignature_hexr   r   r   _build_auth3   s8   

zAroFloClient._build_authc                 C   s   g d}d dd |D S )a  
        Equivalente al bloque JS:

        'zone=' + encodeURIComponent('lastupdate')
        ,'where=' + encodeURIComponent('and|lastupdateutc|>|2020/11/01')
        ,'order=' + encodeURIComponent('lastupdateutc|asc')
        ,'page=' + encodeURIComponent('1')
        ))zone
lastupdate)wherezand|lastupdateutc|>|2020/11/01)orderzlastupdateutc|asc)page1&c                 s   s.    | ]\}}| d t jj|dd V  qdS )=r%   r&   N)r(   r)   r*   ).0kvr   r   r   	<genexpr>i   s    
z<AroFloClient._build_lastupdate_var_string.<locals>.<genexpr>)r8   )r   paramsr   r   r   _build_lastupdate_var_stringZ   s   	
z)AroFloClient._build_lastupdate_var_stringc                 C   sv   d}|   }| ||}| j d|j }d|j |j|j|jd}|jr+|j|d< t	j
||dd}|  | S )za
        Llama al endpoint 'lastupdate' (modo demo, igual que en el ejemplo de Postman).
        GET?zHMAC )AuthenticationAcceptAuthorizationafdatetimeutcHostIP   )headerstimeout)rS   rE   r   r   r	   r
   r   r   r   requestsgetraise_for_statusjson)r   methodr   authurlr\   respr   r   r   get_lastupdateo   s   

zAroFloClient.get_lastupdate)r   N)r   r   r   r   r   r$   r1   r   rE   rS   dictrf   r   r   r   r   r      s    
'r   )r!   r    r9   r<   urllib.parser(   dataclassesr   typingr   r^   configr   r   r   r   r   r   r   <module>   s   
