Skip to content

Commit 29acfcb

Browse files
author
Guillaume Badi
committed
every action/resource are now processed. The API manage errors
1 parent 1a80008 commit 29acfcb

File tree

2 files changed

+37
-101
lines changed

2 files changed

+37
-101
lines changed

mailjet/client.py

Lines changed: 24 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,85 +10,19 @@
1010
requests.packages.urllib3.disable_warnings()
1111

1212
class Config(object):
13-
API_URL = 'https://api.mailjet.com/v3/REST/'
13+
API_URL = 'https://api.mailjet.com/v3/'
1414
API_DOC = 'http://dev.mailjet.com/email-api/v3/'
15-
API_PATHS = { # our_name:API_URL+part (http://dev.mailjet.com/email-api/v3/%s)
16-
# api
17-
'ApiKey': 'apikey',
18-
'ApiKeyAccess': 'apikeyaccess',
19-
'ApiKeyTotals': 'apikeytotals',
20-
'ApiToken': 'apitoken',
21-
'Metadata': 'metadata',
22-
# account
23-
'MetaSender': 'metasender',
24-
'MyProfile': 'myprofile',
25-
'Sender': 'sender',
26-
'User': 'user',
27-
# domain settings
28-
'DomainStats': 'domainstatistics',
29-
'ParseRoute': 'parseroute',
30-
# campaigns
31-
'BounceStats': 'bouncestatistics',
32-
'Campaign': 'campaign',
33-
'CampaignAggregate': 'campaignaggregate',
34-
'CampaignGraphStats': 'campaigngraphstatistics',
35-
'CampaignOverview': 'campaignoverview',
36-
'CampaignStats': 'campaignstatistics',
37-
'ClickStats': 'clickstatistics',
38-
'Preferences': 'preferences',
39-
'Trigger': 'trigger',
40-
# contact list
41-
'Aggregategraphstatistics': 'aggregategraphstatistics',
42-
'Contact': 'contact',
43-
'ContactData': 'contactdata',
44-
'ContactFilter': 'contactfilter',
45-
'ContactHistoryData': 'contacthistorydata',
46-
'ContactMetadata': 'contactmetadata',
47-
'Contactslist': 'contactslist',
48-
'ContactsListSignup': 'contactslistsignup',
49-
'ContactStats': 'contactstatistics',
50-
'CSVImport': 'csvimport',
51-
'GEOStats': 'geostatistics',
52-
'GraphStats': 'graphstatistics',
53-
'ListRecipient': 'listrecipient',
54-
'ListRecipientstatistics': 'listrecipientstatistics',
55-
'ListStats': 'liststatistics',
56-
'ManyContacts': 'manycontacts',
57-
# messages
58-
'Message': 'message',
59-
'MessageHistory': 'messagehistory',
60-
'MessageInformation': 'messageinformation',
61-
'MessageSentStats': 'messagesentstatistics',
62-
'MessageState': 'messagestate',
63-
'MessageStats': 'messagestatistics',
64-
# newsletter
65-
'AXTesting': 'axtesting',
66-
'Batchjob': 'batchjob',
67-
'Newsletter': 'newsletter',
68-
'NewsletterTemplate': 'newslettertemplate',
69-
'NewsletterTemplateCategory': 'newslettertemplatecategory',
70-
# events
71-
'EventCallbackURL': 'eventcallbackurl',
72-
'OpenInformation': 'openinformation',
73-
'OpenStats': 'openstatistics',
74-
'SenderStats': 'senderstatistics',
75-
'ToplinkClicked': 'toplinkclicked',
76-
'UseragentStats': 'useragentstatistics',
77-
# widget
78-
'Widget': 'widget',
79-
'WidgetCustomValue': 'widgetcustomvalue',
80-
81-
}
82-
83-
ENDPOINTS = API_PATHS.keys()
8415

8516
def __getitem__(self, key):
86-
try:
87-
path = self.API_PATHS[key]
88-
return urljoin(self.API_URL, path), urljoin(self.API_DOC, path)
89-
except KeyError:
90-
raise NotImplementedError('Endpoint "%s" is not implemented. Valid endpoints: %s' %
91-
(key, self.ENDPOINTS))
17+
url = self.API_URL[0:]
18+
if key != 'Send':
19+
url = urljoin(url, 'REST')
20+
elif key == 'Contactslist_csvdata':
21+
url = urljoin(url, 'DATA')
22+
elif key == 'Batchjob_csverror':
23+
url = urljoin(url, 'DATA')
24+
url = url + '/' + key
25+
return url, urljoin(self.API_DOC, key)
9226

9327

9428
class Endpoint(object):
@@ -113,45 +47,42 @@ def create(self, data, filters=None, action_id=None, **kwargs):
11347

11448
new = create
11549

116-
def update(self, res_id, data, filters=None, action_id=None, **kwargs):
117-
return api_call(self._auth, 'put', self._url, resource_id=res_id, data=data, action=self.action, action_id=action_id, filters=filters, **kwargs)
50+
def update(self, id, data, filters=None, action_id=None, **kwargs):
51+
return api_call(self._auth, 'put', self._url, resource_id=id, data=data, action=self.action, action_id=action_id, filters=filters, **kwargs)
11852

119-
def delete(self, res_id, **kwargs):
120-
return api_call(self._auth, 'delete', self._url, action=self.action, action_id=action_id, resource_id=res_id, **kwargs)
53+
def delete(self, id, **kwargs):
54+
return api_call(self._auth, 'delete', self._url, action=self.action, action_id=action_id, resource_id=id, **kwargs)
12155

12256

12357
class Client(object):
12458

12559
def __init__(self, auth=None, config=Config()):
12660
self.auth, self.config = auth, config
127-
self.endpoints = self.config.ENDPOINTS
128-
129-
def __dir__(self):
130-
return self.endpoints
13161

13262
def __getattr__(self, name):
13363
split = name.split('_')
134-
name = split[0]
64+
name = split[0].capitalize()
65+
action = None
13566
if (len(split) > 1):
13667
action = split[1]
137-
if name in self.endpoints:
138-
url, doc = self.config[name]
139-
return type(name, (Endpoint,), {})(url=url, doc=doc, action=action, auth=self.auth)
140-
raise AttributeError
68+
url, doc = self.config[name]
69+
return type(name, (Endpoint,), {})(url=url, doc=doc, action=action, auth=self.auth)
14170

14271

14372
def api_call(auth, method, url, data=None, filters=None, resource_id=None, extra_headers=None,
14473
timeout=60, debug=False, action=None, action_id=None, **kwargs):
14574
url = build_url(url, method=method, action=action, resource_id=resource_id, action_id=action_id)
146-
print method, url, data
14775
headers = build_headers(extra_headers)
14876
req_method = getattr(requests, method)
14977

78+
# url = 'http://requestb.in/1emzll91'
79+
15080
try:
151-
response = req_method(url, data=data, params=filters, headers=headers, auth=auth,
81+
response = req_method(url, data=json.dumps(data), params=filters, headers=headers, auth=auth,
15282
timeout=timeout, verify=False, stream=False)
15383

154-
return parse_response(response, debug=debug)
84+
# return parse_response(response, debug=debug)
85+
return response
15586

15687
except requests.exceptions.Timeout:
15788
raise TimeoutError
@@ -174,7 +105,7 @@ def build_headers(extra_headers=None):
174105

175106
def build_url(url, method, action=None, resource_id=None, action_id=None):
176107
if resource_id:
177-
url += '/%d' % resource_id
108+
url += '/%s' % str(resource_id)
178109
if action:
179110
url += '/%s' % action
180111
if action_id:

test.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
"""
2+
Create : This resource can be used to add historical data to contact.
3+
"""
14
from mailjet import Client
25
import os
3-
4-
API_KEY = os.environ['MJ_APIKEY_PUBLIC']
5-
API_SECRET = os.environ['MJ_APIKEY_PRIVATE']
6-
7-
mailjet = Client(auth=(API_KEY, API_SECRET))
8-
9-
result = mailjet.Contact_getcontactslists.get(id=10, filters={'Limit': 2})
10-
print result
6+
api_key = os.environ['MJ_APIKEY_PUBLIC']
7+
api_secret = os.environ['MJ_APIKEY_PRIVATE']
8+
mailjet = Client(auth=(api_key, api_secret))
9+
data = {
10+
'ContactID': 1,
11+
'Data': 10,
12+
'Name': 'Purchase'
13+
}
14+
result = mailjet.contacthistorydata.create(data=data)
15+
print result.text

0 commit comments

Comments
 (0)