77import requests
88from requests .compat import urljoin
99
10+ requests .packages .urllib3 .disable_warnings ()
1011
1112class Config (object ):
1213 API_URL = 'https://api.mailjet.com/v3/REST/'
@@ -92,31 +93,31 @@ def __getitem__(self, key):
9293
9394class Endpoint (object ):
9495
95- def __init__ (self , url , doc , auth ):
96- self ._url , self ._doc , self ._auth = url , doc , auth
96+ def __init__ (self , url , doc , auth , action = None ):
97+ self ._url , self ._doc , self ._auth , self . action = url , doc , auth , action
9798
9899 def __doc__ (self ):
99100 return self ._doc
100101
101- def _get (self , res_id = None , ** kwargs ):
102- return api_call (self ._auth , 'get' , self ._url , resource_id = res_id , ** kwargs )
102+ def _get (self , filters = None , action_id = None , id = None , ** kwargs ):
103+ return api_call (self ._auth , 'get' , self ._url , action = self . action , action_id = action_id , filters = filters , resource_id = id , ** kwargs )
103104
104- def get_many (self , ** kwargs ):
105- return self ._get (** kwargs )
105+ def get_many (self , filters = None , action_id = None , ** kwargs ):
106+ return self ._get (filters = filters , ** kwargs )
106107
107- def get (self , res_id , ** kwargs ):
108- return self ._get (res_id , ** kwargs )
108+ def get (self , id = None , filters = None , action_id = None , ** kwargs ):
109+ return self ._get (id = id , filters = filters , ** kwargs )
109110
110- def create (self , data , ** kwargs ):
111- return api_call (self ._auth , 'post' , self ._url , data = data , ** kwargs )
111+ def create (self , data , filters = None , action_id = None , ** kwargs ):
112+ return api_call (self ._auth , 'post' , self ._url , data = data , action = self . action , action_id = action_id , filters = filters , ** kwargs )
112113
113114 new = create
114115
115- def update (self , res_id , data , ** kwargs ):
116- return api_call (self ._auth , 'put' , self ._url , resource_id = res_id , data = data , ** kwargs )
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 )
117118
118119 def delete (self , res_id , ** kwargs ):
119- return api_call (self ._auth , 'delete' , self ._url , resource_id = res_id , ** kwargs )
120+ return api_call (self ._auth , 'delete' , self ._url , action = self . action , action_id = action_id , resource_id = res_id , ** kwargs )
120121
121122
122123class Client (object ):
@@ -129,21 +130,25 @@ def __dir__(self):
129130 return self .endpoints
130131
131132 def __getattr__ (self , name ):
133+ split = name .split ('_' )
134+ name = split [0 ]
135+ if (len (split ) > 1 ):
136+ action = split [1 ]
132137 if name in self .endpoints :
133138 url , doc = self .config [name ]
134- return type (name , (Endpoint ,), {})(url = url , doc = doc , auth = self .auth )
139+ return type (name , (Endpoint ,), {})(url = url , doc = doc , action = action , auth = self .auth )
135140 raise AttributeError
136141
137142
138- def api_call (auth , method , url , data = None , resource_id = None , extra_headers = None ,
139- timeout = 60 , debug = False , ** kwargs ):
140- data , url = build_body_and_url (url , method = method ,
141- data = data , resource_id = resource_id )
143+ def api_call (auth , method , url , data = None , filters = None , resource_id = None , extra_headers = None ,
144+ timeout = 60 , debug = False , action = None , action_id = None , ** kwargs ):
145+ url = build_url (url , method = method , action = action , resource_id = resource_id , action_id = action_id )
146+ print method , url , data
142147 headers = build_headers (extra_headers )
143148 req_method = getattr (requests , method )
144149
145150 try :
146- response = req_method (url , data = data , headers = headers , auth = auth ,
151+ response = req_method (url , data = data , params = filters , headers = headers , auth = auth ,
147152 timeout = timeout , verify = False , stream = False )
148153
149154 return parse_response (response , debug = debug )
@@ -167,13 +172,15 @@ def build_headers(extra_headers=None):
167172 return headers
168173
169174
170- def build_body_and_url (url , method , data = None , resource_id = None ):
171- data = json .dumps (data ) if data else None
172-
175+ def build_url (url , method , action = None , resource_id = None , action_id = None ):
173176 if resource_id :
174177 url += '/%d' % resource_id
178+ if action :
179+ url += '/%s' % action
180+ if action_id :
181+ url += '/%d' % action_id
175182
176- return data , url
183+ return url
177184
178185
179186def parse_response (response , debug = False ):
@@ -188,29 +195,29 @@ def parse_response(response, debug=False):
188195 logging .debug ('RESP_HEADERS: %s' % response .headers )
189196 logging .debug ('RESP_CODE: %s' % response .status_code )
190197
191- if response .status_code > 300 :
192- errors = data
193- exc_data = dict (message = errors .get ('ErrorMessage' , None ),
194- info = errors .get ('ErrorInfo' , None ),
195- response_parsed = data ,
196- response = response ,
197- request = response .request ,
198- )
199-
200- if response .status_code == 401 :
201- raise AuthorizationError (exc_data )
202- elif response .status_code == 403 :
203- raise ActionDeniedError (exc_data )
204- elif response .status_code == 404 :
205- raise DoesNotExistError (exc_data )
206- elif response .status_code == 422 :
207- raise ValidationError (exc_data )
208- elif response .status_code >= 500 :
209- logging .error ('Critical API error' , exc_info = True , extra = exc_data )
210- raise CriticalApiError (errors )
211- elif response .status_code >= 400 :
212- logging .error ('API error' , exc_info = True , extra = exc_data )
213- raise ApiError (exc_data )
198+ # if response.status_code > 300:
199+ # errors = data
200+ # exc_data = dict(message=errors.get('ErrorMessage', None),
201+ # info=errors.get('ErrorInfo', None),
202+ # response_parsed=data,
203+ # response=response,
204+ # request=response.request,
205+ # )
206+ #
207+ # if response.status_code == 401:
208+ # raise AuthorizationError(exc_data)
209+ # elif response.status_code == 403:
210+ # raise ActionDeniedError(exc_data)
211+ # elif response.status_code == 404:
212+ # raise DoesNotExistError(exc_data)
213+ # elif response.status_code == 422:
214+ # raise ValidationError(exc_data)
215+ # elif response.status_code >= 500:
216+ # logging.error('Critical API error', exc_info=True, extra=exc_data)
217+ # raise CriticalApiError(errors)
218+ # elif response.status_code >= 400:
219+ # logging.error('API error', exc_info=True, extra=exc_data)
220+ # raise ApiError(exc_data)
214221
215222 return data
216223
0 commit comments