I was trying to play with the ApiV3 of mailchimp.
I didn't wanted to recode everything, rewrappe everything, so i did a little tricks,
class Automations(object):
def __init__(self, master):
self.master = master
def automations(self):
"""
:return:
"""
root = "%s%s" % (self.master.root_api3, 'automations')
return self.master.call_api3(root, None)
def automation(self, id="d3f0ffe2f2"):
root = "%s%s" % (self.master.root_api3, 'automations/%s' % id)
return self.master.call_api3(root, None)
def pause_all_emails(self, id="d3f0ffe2f2", order='actions/pause-all-emails'):
return self.start_all_emails(id=id, order=order)
def start_all_emails(self, id="d3f0ffe2f2", order='actions/start-all-emails'):
root = "%s%s" % (self.master.root_api3, 'automations/%s/%s' % (id, order))
try:
print "Essaye1:\n",self.master.call_api3(root, None, call_attr="post")
except Exception, e:
print "Exception1:\n", e
try:
print "Essaye2:\n", self.master.call_api3(root, None, call_attr="post")
except Exception, b:
print "Exception2:\n", b
return ""
class Api3Mailchimp(Mailchimp):
def __init__(self, apikey=None, debug=False):
super(Api3Mailchimp, self).__init__(apikey=apikey, debug=debug)
self.root_api3 = 'https://us3.api.mailchimp.com/3.0/'
self.automations = Automations(self)
def call_api3(self, url, params=None, call_attr="get"):
'''Actually make the API call with the given params - this should only be called by the namespace methods - use the helpers in regular usage like m.helper.ping()'''
if params is None: params = {}
#params['apikey'] = self.apikey
params = json.dumps(params)
self.log('%s to %s: %s' % (call_attr, url, params))
start = time.time()
r = getattr(self.session, call_attr)(url, auth=('apikey', self.apikey), data=params, headers={'content-type': 'application/json', 'user-agent': 'MailChimp-Python/3.0'})
try:
remote_addr = r.raw._original_response.fp._sock.getpeername() # grab the remote_addr before grabbing the text since the socket will go away
except:
remote_addr = (None, None) #we use two private fields when getting the remote_addr, so be a little robust against errors
response_body = r.text
complete_time = time.time() - start
self.log('Received %s in %.2fms: %s' % (r.status_code, complete_time * 1000, r.text))
self.last_request = {'url': url, 'request_body': params, 'response_body': r.text, 'remote_addr': remote_addr, 'response': r, 'time': complete_time}
result = json.loads(response_body)
if r.status_code != requests.codes.ok:
raise self.cast_error(result)
return result
if __name__ == '__main__':
chimp = Api3Mailchimp(key, debug=True)
print chimp.automations.automation()
print chimp.automations.pause_all_emails()
print chimp.automations.start_all_emails()
I have the pip mailchimp 2.0.9 package installed.
My code is working for simple request like chimp.automations.automations(), it work also with automations.automation(id='XXXXX')
but when i want to start_all_emails or pause_all_email i have to make twice the request to get different unexcepted response.
Exception1:
No JSON object could be decoded
(debug output from mailchimp Content:)
Received 400 in 17.83ms: <HTML><HEAD>
<TITLE>Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
Reference #7.780fdd58.1433168958.0
</BODY>
</HTML>
Response 2:
We received an unexpected error: {u'status': 400, u'instance': u'1045824c-7dea-463a-a33c-217239580010', u'type': u'http://kb.mailchimp.com/api/error-docs/400-bad-request', u'detail': u'This automation is already paused or in a draft state.', u'title': u'Bad Request'}
And it does the same think with start, this automations is already started.
My point is firt always receive an unexcepted error Your browser sent a request that this server could not understand.<P>