o
    t¨ÊhÂ  ã                   @   sH   d Z ddlZddlZddlZddlmZ ddlmZ G dd„ deƒZ	dS )z;Parse data received from the SendGrid Inbound Parse webhooké    N)Ú	iteritems)Úsecure_filenamec                   @   sl   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Ze	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	dd„ ƒZdS )ÚParsec                 C   s.   |j | _|| _|jdd� |j| _|j| _d S )NT)Úas_text)ÚkeysÚ_keysÚ_requestÚget_dataÚformÚ_payloadÚdataÚ_raw_payload)ÚselfÚconfigÚrequest© r   úP/var/www/html/env/lib/python3.10/site-packages/sendgrid/helpers/inbound/parse.pyÚ__init__
   s
   zParse.__init__c                 C   s,   i }| j D ]}|| jv r| j| ||< q|S )zd
        Return a dictionary of key/values in the payload received from
        the webhook
        )r   Úpayload)r   Ú
key_valuesÚkeyr   r   r   r      s   

€zParse.key_valuesc                 C   s"   d| j v rt | j d ¡}|S dS )z·
        This only applies to raw payloads:
        https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters
        ÚemailN)r   r   Úmessage_from_string)r   Ú	raw_emailr   r   r   Úget_raw_email   s   
zParse.get_raw_emailc                 C   s8   d}d| j v r|  | j¡}|  ¡ }|dur|  |¡}|S )z‘Returns an object with:
        type = file content type
        file_name = the name of the file
        contents = base64 encoded file contentsNzattachment-info)r   Ú_get_attachmentsr   r   Ú_get_attachments_raw)r   Úattachmentsr   r   r   r   r   '   s   

zParse.attachmentsc                 C   sb   g }t |jƒD ]'\}}i }|jdvr.t|jƒ}|j|d< ||d< t | ¡ ¡|d< | |¡ q|S )N)NÚfdopenz<fdopen>ÚtypeÚ	file_nameÚcontents)	r   ÚfilesÚfilenamer   Úcontent_typeÚbase64Ú	b64encodeÚreadÚappend)r   r   r   Ú_ÚfilestorageÚ
attachmentr#   r   r   r   r   5   s   



€zParse._get_attachmentsc                 C   s�   g }d}|  ¡ D ]=}i }| ¡ dkrq| ¡ }|s*t | ¡ ¡}|s$d}d||f }|d7 }| ¡ |d< ||d< |jdd�|d	< | |¡ q|S )
Né   Ú	multipartz.binzpart-%03d%sr   r    F)Údecoder!   )ÚwalkÚget_content_maintypeÚget_filenameÚ	mimetypesÚguess_extensionÚget_content_typeÚget_payloadr(   )r   r   r   ÚcounterÚpartr+   r#   Úextr   r   r   r   A   s$   zParse._get_attachments_rawc                 C   ó   | j S ©N)r   ©r   r   r   r   r   U   ó   z
Parse.keysc                 C   r9   r:   )r   r;   r   r   r   r   Y   r<   zParse.requestc                 C   r9   r:   )r   r;   r   r   r   r   ]   r<   zParse.payloadc                 C   r9   r:   )r   r;   r   r   r   Úraw_payloada   r<   zParse.raw_payloadN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   Úpropertyr   r   r   r=   r   r   r   r   r   	   s    


r   )
Ú__doc__r%   r   r2   Úsixr   Úwerkzeug.utilsr   Úobjectr   r   r   r   r   Ú<module>   s    