o
    Ž¨Êhk.  ã                   @   sx   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m	Z	 ze
ZW n ey1   eZY nw G dd„ deƒZdS )é    N©Ú	parseaddré   )ÚResource)ÚSparkPostExceptionc                   @   st   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTransmissionszØ
    Transmission class used to send, list and get transmissions. For detailed
    request and response formats, see the `Transmissions API documentation
    <https://www.sparkpost.com/docs/transmissions-api>`_.
    Útransmissionsc                 K   sn  i i i dœ}|  d¡|d< |  dd¡|d< |  d¡|d< |  d¡|d< |  d¡|d< |  d	¡|d
 d	< |  d¡|d
 d< |  d¡|d
 d< |  d¡|d
 d< |  d¡|d
 d< |  d¡|d
 d< |  d¡|d
 d< |  d¡|d
 d< |  dd¡|d d< |  d¡|d d< |  d¡|d d< |  d¡}t|tƒr�|  |¡}||d d< |  d¡|d d< |  d¡|d d< |  d¡|d d< |  d i ¡|d d!< |  d"¡}|rÖ||d# d$< n>|  d#g ¡}|  |¡}|  d%¡}|  d&¡}|�rd' |¡|d d! d(< |  ||¡}|| }|�r|  ||¡}	||	 }||d#< |  d)g ¡}
|  |
¡|d d)< d*|v �r5|d* }|  |¡|d d*< |S )+N)ÚcontentÚoptionsÚ
recipientsÚdescriptionÚreturn_pathzdefault@sparkpostmail.comÚcampaignÚcampaign_idÚmetadataÚsubstitution_dataÚ
start_timer
   Útrack_opensÚopen_trackingÚtrack_clicksÚclick_trackingÚtransactionalÚuse_sandboxÚsandboxÚskip_suppressionÚip_poolÚ
inline_cssÚuse_draft_templateFr	   Úreply_toÚsubjectÚ
from_emailÚfromÚhtmlÚtextÚtemplateÚtemplate_idÚcustom_headersÚheadersÚrecipient_listr   Úlist_idÚccÚbccú,ÚCCÚattachmentsÚinline_images)ÚgetÚ
isinstanceÚstring_typesÚ_parse_addressÚ_extract_recipientsÚjoinÚ_format_copiesÚ_extract_attachments)ÚselfÚkwargsÚmodelr    r(   r   r*   r+   Ú	cc_copiesÚ
bcc_copiesr.   r/   © r=   úI/var/www/html/env/lib/python3.10/site-packages/sparkpost/transmissions.pyÚ_translate_keys   sp   ýÿ

ÿ






ÿ
ÿzTransmissions._translate_keysc                 C   sl   g }t |ƒdkr4|  |¡}t |d ¡}| d¡ |D ]}|d  d|  |d ¡i¡ |jdi |¤Ž q|S )Nr   ÚaddressÚ	header_tor=   )Úlenr4   ÚcopyÚdeepcopyÚpopÚupdateÚ_format_header_to)r8   r   ÚcopiesÚformatted_copiesÚmain_recipientÚ	recipientr=   r=   r>   r6   \   s   

ÿzTransmissions._format_copiesc                 C   s6   d|d v rdj |d d |d d d�S |d d S )NÚnamer@   z"{name}" <{email}>Úemail)rL   rM   )Úformat)r8   rK   r=   r=   r>   rG   i   s   

þzTransmissions._format_header_toc                 C   sf   g }|D ],}i }|  d¡|d< |  d¡|d< d|v r$|  |d ¡|d< n|  d¡|d< | |¡ q|S )NÚtyperL   ÚfilenameÚdata)r0   Ú_get_base64_from_fileÚappend)r8   r.   Úformatted_attachmentsÚ
attachmentÚformatted_attachmentr=   r=   r>   r7   q   s   
ÿz"Transmissions._extract_attachmentsc                 C   sD   t |dƒ�}t | ¡ ¡ d¡}W d   ƒ |S 1 sw   Y  |S )NÚrbÚascii)ÚopenÚbase64Ú	b64encodeÚreadÚdecode)r8   rP   Úa_fileÚencoded_stringr=   r=   r>   rR      s   
ÿþz#Transmissions._get_base64_from_filec                 C   s$   t |ƒ\}}d|i}|r||d< |S )NrM   rL   r   )r8   r@   rL   rM   Úparsed_addressr=   r=   r>   r3   „   s   ÿzTransmissions._parse_addressc                 C   sR   t |ttfƒstdƒ‚g }|D ]}t |tƒr!| d|  |¡i¡ q| |¡ q|S )Nz!recipients must be a list or dictr@   )r1   ÚlistÚdictr   r2   rS   r3   )r8   r   Úformatted_recipientsÚrecipr=   r=   r>   r4   �   s   

ÿz!Transmissions._extract_recipientsc                 K   s0   | j di |¤Ž}t |¡}| jd| j|d�}|S )a±  
        Send a transmission based on the supplied parameters

        :param list|dict recipients: If list it is an list of email addresses,
            if dict ``{'address': {'name': 'Name', 'email': 'me' }}``
        :param str recipient_list: ID of recipient list, if set recipients
            above will be ignored
        :param cc: List of email addresses to send carbon copy to
        :param bcc: List of email addresses to send blind carbon copy to
        :param str template: ID of template. If set HTML or text will not be
            used
        :param bool use_draft_template: Default to False. Set to true if you
            want to send a template that is a draft
        :param str html: HTML part of transmission
        :param str text: Text part of transmission
        :param str subject: Subject of transmission
        :param str from_email: Email that the transmission comes from. The
            domain must be a verified sending domain to your account or
            the transmission will fail. You can pass a from email or both
            from name and from email - `testing@example.com` or
            `Test Email <testing@example.com>` will both work.
        :param str reply_to: Reply to of transmission
        :param str description: Description of transmission
        :param str campaign: Campaign of transmission
        :param dict metadata: Any data you want to send along with
            transmission, used in WebHooks
        :param dict substitution_data: Corresponds to substitutions in
            html/text content. See `substitutions reference
            <https://www.sparkpost.com/docs/substitutions-reference>`_.
        :param attachments: List of dicts. For example:

            .. code-block:: python

                dict(
                    type='application/pdf',
                    name='document.pdf',
                    data='base64 encoded string'
                )

            Replace `data` with `filename` if you want the library to perform
            the base64 conversion. For example:

            .. code-block:: python

                dict(
                    type='application/pdf',
                    name='document.pdf',
                    filename='/full/path/to/document.pdf'
                )
        :param inline_images: List of dicts. For example:

            .. code-block:: python

                dict(
                    type='image/png',
                    name='imageCID',
                    data='base64 encoded string'
                )

            Replace `data` with `filename` if you want the library to perform
            the base64 conversion. For example:

            .. code-block:: python

                dict(
                    type='image/png',
                    name='imageCID',
                    filename='/full/path/to/image.png'
                )

        :param str start_time: Delay generation of messages until this
            datetime. Format YYYY-MM-DDTHH:MM:SS+-HH:MM. Example:
            '2015-02-11T08:00:00-04:00'.
        :param bool track_opens: Defaults to True. Used to track opens of
            transmission
        :param bool track_clicks: Defaults to True. Used to track clicks of
            transmission
        :param bool use_sandbox: Flag must be set to use sandbox domain instead
            of verified sending domain. Limited to a lifetime of 50
            transmissions with this domain
        :param bool transactional: Whether message is transactional or
            non-transactional for unsubscribe and suppression purposes
        :param bool skip_suppression: Whether or not to ignore customer
            suppression rules, for this transmission only. Only applicable if
            your configuration supports this parameter. (SparkPost Elite only)
        :param str ip_pool: The name of a dedicated IP pool associated with
            your account
        :param bool inline_css: Whether or not to perform CSS inlining
        :param dict custom_headers: Used to set any headers associated with
            transmission

        :returns: a ``dict`` with the ID and number of accepted and rejected
            recipients
        :raises: :exc:`SparkPostAPIException` if transmission cannot be sent
        ÚPOST)rQ   Nr=   )r?   ÚjsonÚdumpsÚrequestÚuri)r8   r9   ÚpayloadrQ   Úresultsr=   r=   r>   Úsendœ   s   a
zTransmissions.sendc                 C   ó   d| j |f }|  d|¡}|S )Nú%s/%sÚGET©ri   rh   ©r8   Útransmission_idri   rk   r=   r=   r>   Ú
_fetch_get  s   zTransmissions._fetch_getc                 C   s   |   |¡}|d S )zý
        Get a transmission by ID

        :param str transmission_id: ID of the transmission you want to retrieve

        :returns: the requested transmission if found
        :raises: :exc:`SparkPostAPIException` if transmission is not found
        Útransmission)rs   )r8   rr   rk   r=   r=   r>   r0     s   
	zTransmissions.getc                 K   s$   d}	 t  |t¡ | jd| j|d�S )a.  
        Get a list of your transmissions

        :param campaign_id: ID of the campaign used by the transmissions
        :param template_id: ID of the template used by the transmissions

        :returns: list of transmissions
        :raises: :exc:`SparkPostAPIException` if API call fails
        z*This endpoint is deprecated. For details, ro   )Úparams)ÚwarningsÚwarnÚDeprecationWarningrh   ri   )r8   r9   Úwarn_msgr=   r=   r>   ra     s   
zTransmissions.listc                 C   rm   )a  
        Delete a transmission by ID

        :param str transmission_id: ID of the transmission you want to delete

        :returns: {}  if transmission is deleted
        :raises: :exc:`SparkPostAPIException` if transmission is not found
            or Canceled
        rn   ÚDELETErp   rq   r=   r=   r>   Údelete#  s   
zTransmissions.deleteN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úkeyr?   r6   rG   r7   rR   r3   r4   rl   rs   r0   ra   r{   r=   r=   r=   r>   r      s    B	fr   )rZ   rC   rf   rv   Úemail.utilsr   Úbaser   Ú
exceptionsr   Ú
basestringr2   Ú	NameErrorÚstrr   r=   r=   r=   r>   Ú<module>   s    ÿ