o
    ˷e8'                     @   s   d Z ddlmZ ddlZddlZddlZddlmZ zddlm	Z	 ddlm
Z
 W n ey=   ddlm	Z	 ddlm
Z
 Y nw ddlZedZdd	 ZG d
d deZdd ZG dd deZG dd dejjZdS )z^
Mailchimp v3 Api SDK

Documentation: http://developer.mailchimp.com/documentation/mailchimp/
    )unicode_literalsN)HTTPBasicAuth)urljoin)	urlencodezmailchimp3.clientc                    s   t   fdd}|S )Nc                    s    | j r | g|R i |S d S N)enabled)selfargskwargsfn Q/var/www/ideatree/venv/lib/python3.10/site-packages/mailchimp3/mailchimpclient.pywrapper   s   z!_enabled_or_noop.<locals>.wrapper)	functoolswraps)r   r   r   r   r   _enabled_or_noop   s   r   c                   @   s   e Zd ZdS )MailChimpErrorN)__name__
__module____qualname__r   r   r   r   r   "   s    r   c                 C   s4   z	|   }W t| ty   d| i}Y t|w )Nresponse)json
ValueErrorr   )r
error_datar   r   r   _raise_response_error&   s   

r   c                       sp   e Zd ZdZ		d fdd	Zdd Zedd	d
Zedd Zedd Z	edddZ
edddZ  ZS )MailChimpClientz8
    MailChimp class to communicate with the v3 API
    Npython-mailchimpTc           	         s   t t|   || _|| _|rt|| _| j d | _n)|rBt	
d|dd s.tdt||| _|d }d|| _ntd|pLtj | _|pTtj | _dS )	aO  
        Initialize the class with your optional user_id and required api_key.

        If `enabled` is not True, these methods become no-ops. This is
        particularly useful for testing or disabling with configuration.

        :param mc_api: Mailchimp API key
        :type mc_api: :py:class:`str`
        :param mc_user: Mailchimp user id
        :type mc_user: :py:class:`str`
        :param access_token: The OAuth access token
        :type access_token: :py:class:`str`
        :param enabled: Whether the API should execute any requests
        :type enabled: :py:class:`bool`
        :param timeout: (optional) How long to wait for the server to send
            data before giving up, as a float, or a :ref:`(connect timeout,
            read timeout) <timeouts>` tuple.
        :type timeout: float or tuple
        :param request_hooks: (optional) Hooks for :py:func:`requests.requests`.
        :type request_hooks: :py:class:`dict`
        :param request_headers: (optional) Headers for
            :py:func:`requests.requests`.
        :type request_headers: :py:class:`dict`
        z/3.0/z^[0-9a-f]{32}$-r   zThe API key that you have entered is not valid, did you enter a username by mistake?
The order of arguments for API key and username has reversed in 2.1.0z"https://{0}.api.mailchimp.com/3.0/z1You must provide an OAuth access token or API keyN)superr   __init__r   timeoutMailChimpOAuthauthget_base_urlbase_urlrematchsplitr   r   popformat	Exceptionrequestsutilsdefault_headersrequest_headershooksdefault_hooksrequest_hooks)	r   mc_apimc_useraccess_tokenr   r"   r3   r0   
datacenter	__class__r   r   r!   3   s   
zMailChimpClient.__init__c                 K   sd   t djdi | |drt djdi | tjdi |}t dj|d |jd |S )Nz{method} Request: {url}r   zPAYLOAD: {json}z{method} Response: {status}method)r:   statusr   )_loggerinfor+   getr-   requeststatus_code)r   r
   r   r   r   r   _make_request`   s   
zMailChimpClient._make_requestc                 C   s   t | j|}z| jdi td||| j| j| j| jd}W n tj	j
y- } z|d}~ww |jdkr7t| |jdkr>dS | S )aK  
        Handle authenticated POST requests

        :param url: The url for the endpoint including path parameters
        :type url: :py:class:`str`
        :param data: The request body parameters
        :type data: :py:data:`none` or :py:class:`dict`
        :returns: The JSON output from the API or an error message
        POSTr:   urlr   r$   r"   r1   headersN     r   r   r&   rA   dictr$   r"   r3   r0   r-   
exceptionsRequestExceptionr@   r   r   r   rD   datar   er   r   r   _postm   s(   
	

zMailChimpClient._postc                 K   s   t | j|}t|r|dt| 7 }z| jdi td|| j| j| j| j	d}W n t
jjy8 } z|d}~ww |jdkrBt| | S )a  
        Handle authenticated GET requests

        :param url: The url for the endpoint including path parameters
        :type url: :py:class:`str`
        :param queryparams: The query string parameters
        :returns: The JSON output from the API
        ?GETr:   rD   r$   r"   r1   rE   NrF   r   )r   r&   lenr   rA   rI   r$   r"   r3   r0   r-   rJ   rK   r@   r   r   )r   rD   queryparamsr   rN   r   r   r   _get   s&   


zMailChimpClient._getc                 C   s   t | j|}z| jdi td|| j| j| j| jd}W n tj	j
y, } z|d}~ww |jdkr6t| |jdkr=dS | S )z
        Handle authenticated DELETE requests

        :param url: The url for the endpoint including path parameters
        :type url: :py:class:`str`
        :returns: The JSON output from the API
        DELETErR   NrF   rG   r   rH   )r   rD   r   rN   r   r   r   _delete   s&   	


zMailChimpClient._deletec                 C   v   t | j|}z| jdi td||| j| j| j| jd}W n tj	j
y- } z|d}~ww |jdkr7t| | S )a8  
        Handle authenticated PATCH requests

        :param url: The url for the endpoint including path parameters
        :type url: :py:class:`str`
        :param data: The request body parameters
        :type data: :py:data:`none` or :py:class:`dict`
        :returns: The JSON output from the API
        PATCHrC   NrF   r   rH   rL   r   r   r   _patch   $   
	
zMailChimpClient._patchc                 C   rX   )a6  
        Handle authenticated PUT requests

        :param url: The url for the endpoint including path parameters
        :type url: :py:class:`str`
        :param data: The request body parameters
        :type data: :py:data:`none` or :py:class:`dict`
        :returns: The JSON output from the API
        PUTrC   NrF   r   rH   rL   r   r   r   _put   r[   zMailChimpClient._put)Nr   NTNNNr   )r   r   r   __doc__r!   rA   r   rO   rU   rW   rZ   r]   __classcell__r   r   r8   r   r   /   s     - 

r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r#   a  
    Authentication class for authentication with OAuth2. Acquiring an OAuth2
    for MailChimp can be done by following the instructions in the
    documentation found at
    http://developer.mailchimp.com/documentation/mailchimp/guides/how-to-use-oauth2/
    c                 C   s
   || _ dS )z
        Initialize the OAuth and save the access token

        :param access_token: The access token provided by OAuth authentication
        :type access_token: :py:class:`str`
        N)_access_token)r   r6   r   r   r   r!     s   
zMailChimpOAuth.__init__c                 C   s   d| j  |jd< |S )zF
        Authorize with the access token provided in __init__
        zOAuth Authorization)r`   rE   )r   r   r   r   r   __call__  s   zMailChimpOAuth.__call__c              
   C   s`   z	t jd| d}W n t jjy } z|d}~ww |  | }d|v r.t j|d |S )z@
        Get the metadata returned after authentication
        z+https://login.mailchimp.com/oauth2/metadata)r$   Nerror)r-   r>   rJ   rK   raise_for_statusr   )r   r   rN   outputr   r   r   get_metadata  s   zMailChimpOAuth.get_metadatac                 C   s$   z|   d W S  tjjy    w )zC
        Get the base_url from the authentication metadata
        api_endpoint)rf   r-   rJ   rK   )r   r   r   r   r%   .  s
   zMailChimpOAuth.get_base_urlN)r   r   r   r^   r!   rb   rf   r%   r   r   r   r   r#     s    
r#   )r^   
__future__r   r   r'   r-   requests.authr   urllib.parser   r   ImportErrorurlparseurlliblogging	getLoggerr<   r   r,   r   r   objectr   r$   AuthBaser#   r   r   r   r   <module>   s*   
	 W