1010requests .packages .urllib3 .disable_warnings ()
1111
1212class 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
9428class 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
12357class 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
14372def 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
175106def 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 :
0 commit comments