Skip to content
Merged
17 changes: 17 additions & 0 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,6 +63,23 @@ mapi = MailSnake('YOUR MANDRILL API KEY', api='mandrill')
mapi.messages.send(message={'html':'email html', 'subject':'email subject', 'from_email':'from@example.com', 'from_name':'From Name', 'to':[{'email':'to@example.com', 'name':'To Name'}]}) # returns 'PONG!'
```

OAuth Tokens
------------

You can use a MailChimp OAuth token in place of an API key, but
because MailChimp's tokens don't include the datacenter as a suffix,
you must supply it yourself (substitute an actual number for X below):

```python
ms = MailSnake('A MAILCHIMP OAUTH TOKEN', dc='usX')
ms.ping() # returns "Everything's Chimpy!" just like with an API key
```

If you completed the OAuth dance as described in the
[MailChimp OAuth2 documentation](http://apidocs.mailchimp.com/oauth2/),
you should have discovered the datacenter associated with the token
when making the metadata request.

Additional Request Options
--------------------------

Expand Down
7 changes: 5 additions & 2 deletions mailsnake/mailsnake.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,8 @@ def __init__(self,
extra_params=None,
api='api',
api_section='',
requests_opts={}):
requests_opts={},
dc=None):
"""Cache API key and address. For additional control over how
requests are made, supply a dictionary for requests_opts. This will
be passed through to requests.post() as kwargs.
Expand All@@ -53,7 +54,9 @@ def __init__(self,
api, x))
self.default_params.update(extra_params)

if '-' in self.apikey:
if dc:
self.dc = dc
elif '-' in self.apikey:
self.dc = self.apikey.split('-')[1]
api_info = {
'api': (self.dc, '.api.', 'mailchimp', '1.3/?method='),
Expand Down
28 changes: 25 additions & 3 deletions mailsnake/tests/tests.py
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
import unittest

from collections import MutableSequence
from mailsnake import MailSnake
from mailsnake import MailSnake, exceptions
from random import random

from .secret_keys import MAILCHIMP_API_KEY, MAILCHIMP_LIST_ID, TEST_EMAIL
Expand DownExpand Up@@ -38,6 +38,14 @@ def _unsubscribe(self, email=None):
id=MAILCHIMP_LIST_ID, email_address=email if email else TEST_EMAIL,
send_goodbye=False, send_notify=False)

def _set_api_key(self, apikey='', reset=False):

# If dc is true then add the datacenter provided by the MAILCHIMP_API_KEY variable
if reset is True:
apikey = MAILCHIMP_API_KEY

self.mcapi = MailSnake(apikey)


class TestMailChimpAPI(TestMailChimp):
# Helper Methods
Expand DownExpand Up@@ -91,6 +99,20 @@ def test_lists(self):
assert 'total' in lists
assert 'data' in lists

def test_lists_exception(self):

# Test with key including dc
self._set_api_key("WRONGKEY-us1")
self.assertRaises(exceptions.InvalidApiKeyException, self.mcapi.lists)

# Test with key without dc
self._set_api_key("WRONGKEY")
self.assertRaises(exceptions.InvalidApiKeyException, self.mcapi.lists)


# Reset apikey to MAILCHIMP_API_KEY
self._set_api_key(reset=True)

def test_listActivity(self):
activity = self.mcapi.listActivity(id=MAILCHIMP_LIST_ID)
assert isinstance(activity, list)
Expand All@@ -113,7 +135,7 @@ def test_templateAddDel(self):
assert self.mcapi.templateDel(id=template_id)

def _add_test_template(self):
html = open('mailsnake/tests/template.html', 'r').read()
html = open('./template.html', 'r').read()
templates = self.mcapi.templates(inactives={'include': True})
template_names = [t['name'] for t in templates['user']]
index = 0
Expand All@@ -130,7 +152,7 @@ def setUp(self):
super(TestExportAPI, self).setUp()
self.export = MailSnake(MAILCHIMP_API_KEY, api='export')
self.export_stream = MailSnake(MAILCHIMP_API_KEY, api='export',
requests_opts={'prefetch': False})
requests_opts={'stream': True})

def test_list(self):
member_list = self.export.list(id=MAILCHIMP_LIST_ID)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,7 @@

setup(
name='mailsnake',
version='1.6.1',
version='1.6.2',
description='MailChimp API v1.3, STS, Export, Mandrill wrapper for Python.',
long_description=open('README.rst').read(),
author='John-Kim Murphy',
Expand Down