U
    Fc                     @   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)Zas_text)keys_keys_requestget_dataform_payloaddata_raw_payload)selfconfigrequest r   B/tmp/pip-unpacked-wheel-7j3043uk/sendgrid/helpers/inbound/parse.py__init__   s
    zParse.__init__c                 C   s,   i }| j D ]}|| jk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 krt| j d }|S 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 kr| | j}|  }|dk	r4| |}|S )zReturns 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 ]N\}}i }|jdk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   _Zfilestorage
attachmentr"   r   r   r   r   6   s    


zParse._get_attachmentsc                 C   s   g }d}|  D ]z}i }| dkr&q| }|sTt| }|sHd}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   B   s$    zParse._get_attachments_rawc                 C   s   | j S N)r   r   r   r   r   r   V   s    z
Parse.keysc                 C   s   | j S r7   )r   r8   r   r   r   r   Z   s    zParse.requestc                 C   s   | j S r7   )r
   r8   r   r   r   r   ^   s    zParse.payloadc                 C   s   | j S r7   )r   r8   r   r   r   raw_payloadb   s    zParse.raw_payloadN)__name__
__module____qualname__r   r   r   r   r   r   propertyr   r   r   r9   r   r   r   r   r   	   s   


r   )
__doc__r$   r   r0   sixr   Zwerkzeug.utilsr   objectr   r   r   r   r   <module>   s   