o
    ~X2ib                     @  sj   d dl mZ d dlmZmZ d dlmZmZ d dlZd dlZddl	m
Z
 dddZdddZdddZdS )    )annotations)datetimetimezone)DictTupleN   )AroFloSettingssettingsr   returnstrc                 C  s   d| j  d| j d| j S )z
    Construye el header Authorization igual que en Postman:

    uencoded=...&pencoded=...&orgEncoded=...
    (ya vienen encoded desde la consola de AroFlo)
    z	uencoded=z
&pencoded=z&orgEncoded=)	u_encoded	p_encodedorg_encoded)r	    r   </var/www/html/flask_server/apps/aroflo_connector_app/auth.pybuild_authorization_value   s   
r   c                  C  s$   t tj} | jdd}|ddS )z^
    Genera timestamp ISO 8601 UTC como usa AroFlo.
    Ejemplo: 2018-07-25T01:39:57.135Z
    milliseconds)timespecz+00:00Z)r   nowr   utc	isoformatreplace)r   isor   r   r   build_timestamp   s   r   request_type
var_stringauthorizationiso_timestampTuple[str, Dict[str, str]]c           
      C  s   d}|   g}|jr||j |||j|||g d|}tj|j	d|	dt
jd }d| |j||d}	|jrE|j|	d< ||	fS )u   
    Replica la lógica del pre-request script de Postman.

    payload = [
        requestType,
        HostIP (si existe),
        urlPath (= ''),
        accept,
        Authorization,
        isoTimestamp,
        VarString,
    ]
     +zutf-8)keymsg	digestmodzHMAC )AuthenticationAcceptAuthorizationafdatetimeutcHostIP)upperhost_ipappendextendacceptjoinhmacnew
api_secretencodehashlibsha512	hexdigest)
r   r   r   r   r	   url_pathpayload_partspayload_strsignature_hexheadersr   r   r   build_hmac_signature$   s8   




r<   )r	   r   r
   r   )r
   r   )r   r   r   r   r   r   r   r   r	   r   r
   r   )
__future__r   r   r   typingr   r   r0   r4   configr   r   r   r<   r   r   r   r   <module>   s    


