1- """Usabilla API Python Client."""
2-
31# Copyright (c) 2016 Usabilla.com
42#
53# Permission is hereby granted, free of charge, to any person obtaining a
@@ -74,7 +72,7 @@ class APIClient(object):
7472 """
7573
7674 resources = {
77- 'scopes' : {
75+ 'scopes' : {
7876 'live' : {
7977 'products' : {
8078 'websites' : {
@@ -104,23 +102,23 @@ class APIClient(object):
104102 }
105103 }
106104 }
107-
105+
108106 """ Scope constants """
109107 SCOPE_LIVE = 'live'
110-
108+
111109 """ Product contants """
112110 PRODUCT_WEBSITES = 'websites'
113111 PRODUCT_EMAIL = 'email'
114112 PRODUCT_APPS = 'apps'
115-
113+
116114 """ Resource contants """
117115 RESOURCE_FEEDBACK = 'feedback'
118116 RESOURCE_APP = 'app'
119117 RESOURCE_BUTTON = 'button'
120- RESOURCE_CAMPAIGN = 'campaign'
118+ RESOURCE_CAMPAIGN = 'campaign'
121119 RESOURCE_CAMPAIGN_RESULT = 'campaign_result'
122120 RESOURCE_CAMPAIGN_STATS = 'campaign_stats'
123- RESOURCE_INPAGE = 'inpage'
121+ RESOURCE_INPAGE = 'inpage'
124122 RESOURCE_INPAGE_RESULT = 'inpage_result'
125123
126124 method = 'GET'
@@ -148,7 +146,7 @@ def set_query_parameters(self, parameters):
148146 :param parameters: A `dict` representing the query parameters to be used for the request.
149147 :type parameters: dict
150148 """
151-
149+
152150 self .query_parameters = urllib .urlencode (OrderedDict (sorted (parameters .items ())))
153151
154152 def get_query_parameters (self ):
@@ -169,7 +167,7 @@ def send_signed_request(self, scope):
169167 :type scope: str
170168
171169 :returns: A `dict` of the data.
172- :rtype: dict
170+ :rtype: dict
173171 """
174172 if self .credentials .client_key is None or self .credentials .secret_key is None :
175173 raise GeneralError ('Invalid Access Key.' , 'The Access Key supplied is invalid.' )
@@ -194,7 +192,7 @@ def send_signed_request(self, scope):
194192
195193 # Create payload hash (hash of the request body content).
196194 payload_hash = hashlib .sha256 ('' .encode ('utf-8' )).hexdigest ()
197-
195+
198196 # Combine elements to create canonical request.
199197 canonical_request = '{method}\n {uri}\n {query}\n {can_headers}\n {signed_headers}\n {hash}' .format (
200198 method = self .method ,
@@ -234,52 +232,52 @@ def send_signed_request(self, scope):
234232 )
235233
236234 headers = {'date' : usbldate , 'Authorization' : authorization_header }
237-
235+
238236 # Send the request.
239- request_url = self .host + scope + '?' + canonical_querystring
237+ request_url = self .host + scope + '?' + canonical_querystring
240238 r = requests .get (self .host_protocol + request_url , headers = headers )
241-
242-
239+
240+
243241 if r .status_code != 200 :
244242 return r
245243 else :
246244 return r .json ()
247-
248245
249- def check_resource_validity (self ,scope ,product ,resource ):
250- """Checks whether the resource exists
251-
246+
247+ def check_resource_validity (self , scope , product , resource ):
248+ """Checks whether the resource exists
249+
252250 :param scope: A `string` that specifies the resource scope
253251 :param product: A `string` that specifies the product type
254252 :param resource: A `string` that specifies the resource type
255-
253+
256254 :type scope: str
257255 :type product: str
258256 :type resource: str
259-
257+
260258 :returns: An `string` that represents the resource request url
261259 :rtype: string
262260 """
263261 if scope not in self .resources ['scopes' ].keys ():
264262 raise GeneralError ('invalid scope' , 'Invalid scope name' )
265263 if product not in self .resources ['scopes' ][scope ]['products' ].keys ():
266- raise GeneralError ('invalid product' , 'Invalid product name' )
264+ raise GeneralError ('invalid product' , 'Invalid product name' )
267265 if resource not in self .resources ['scopes' ][scope ]['products' ][product ]['resources' ].keys ():
268- raise GeneralError ('invalid resource' , 'Invalid resource name' )
269-
266+ raise GeneralError ('invalid resource' , 'Invalid resource name' )
267+
270268 url = '/' + scope + '/' + product + self .resources ['scopes' ][scope ]['products' ][product ]['resources' ][resource ]
271-
269+
272270 return url
273-
274- def handle_id (self ,url ,resource_id ):
271+
272+ def handle_id (self , url , resource_id ):
275273 """Replaces the :id pattern in the url
276-
274+
277275 :param url: A `string` that specifies the resource request url
278276 :param resource_id: A `string` that specifies the resource id
279-
277+
280278 :type url: str
281279 :type resource_id: str
282-
280+
283281 :returns: An `string` that represents the resource request url
284282 :rtype: string
285283 """
@@ -288,18 +286,18 @@ def handle_id(self,url,resource_id):
288286 raise GeneralError ('invalid id' , 'Invalid resource ID' )
289287 if resource_id == '*' :
290288 resource_id = '%2A'
291-
289+
292290 url = url .replace (':id' , str (resource_id ))
293-
291+
294292 return url
295-
293+
296294 def item_iterator (self , url ):
297295 """Get items using an iterator.
298-
296+
299297 :param url: A `string` that specifies the resource request url
300-
298+
301299 :type url: str
302-
300+
303301 :returns: An `generator` that yeilds the requested data.
304302 :rtype: generator
305303 """
@@ -313,32 +311,29 @@ def item_iterator(self, url):
313311 self .set_query_parameters ({'since' : results ['lastTimestamp' ]})
314312 except :
315313 return
316-
317-
314+
315+
318316 def get_resource (self , scope , product , resource , resource_id = None , iterate = False ):
319317 """Retrieves resources of the specified type
320-
318+
321319 :param scope: A `string` that specifies the resource scope
322320 :param product: A `string` that specifies the product type
323321 :param resource: A `string` that specifies the resource type
324322 :param resource_id: A `string` that specifies the resource id
325- :param iterate: A `boolean` that specifies whether the you want to use an iterator
326-
323+ :param iterate: A `boolean` that specifies whether the you want to use an iterator
324+
327325 :type scope: str
328326 :type product: str
329327 :type resource: str
330328 :type resource_id: str
331- :type iterate: bool
332-
329+ :type iterate: bool
330+
333331 :returns: An `generator` that yeilds the requested data or a single resource
334332 :rtype: generator or single resource
335333 """
336334 url = self .handle_id (self .check_resource_validity (scope , product , resource ), resource_id )
337-
335+
338336 if iterate :
339337 return self .item_iterator (url )
340338 else :
341339 return self .send_signed_request (url )
342-
343-
344-
0 commit comments