diff --git a/marketorestpython/client.py b/marketorestpython/client.py index b95b657..ef63738 100644 --- a/marketorestpython/client.py +++ b/marketorestpython/client.py @@ -1,18 +1,15 @@ +import json import time + from datetime import datetime -import json + from marketorestpython.helper.http_lib import HttpLib from marketorestpython.helper.exceptions import MarketoException def has_empty_warning(result): - if 'result' not in result \ - and 'warnings' in result \ - and len(result['warnings']) \ - and result['warnings'][0] == 'No assets found for the given search criteria.': - return True - - return False + return 'result' not in result and 'warnings' in result and len(result['warnings']) \ + and result['warnings'][0] == 'No assets found for the given search criteria.' class MarketoClient: @@ -428,8 +425,8 @@ def get_multiple_leads_by_program_id(self, programId, fields=None, batchSize=Non self.authenticate() # for long-running processes, this updates the access token args['access_token'] = self.token - result = self._api_call('post', self.host + "/rest/v1/leads/programs/" + str(programId) + ".json", args, data, - mode='nojsondumps') + result = self._api_call('post', self.host + "/rest/v1/leads/programs/" + str(programId) + ".json", args, + data, mode='nojsondumps') if result is None: raise Exception("Empty Response") if not result['success']: @@ -2303,7 +2300,8 @@ def get_email_full_content(self, id, status=None, leadId=None, type=None): # -------LANDING PAGES ---------# def create_landing_page(self, name, folderId, folderType, template, description=None, title=None, keywords=None, - robots=None, customHeadHTML=None, facebookOgTags=None, prefillForm=None, mobileEnabled=None): + robots=None, customHeadHTML=None, facebookOgTags=None, prefillForm=None, + mobileEnabled=None): self.authenticate() if name is None: raise ValueError( @@ -2494,7 +2492,8 @@ def get_landing_pages_yield(self, offset=0, maxReturn=20, status=None, folderId= result = self._api_call('get', self.host + "/rest/asset/v1/landingPages.json", args) if result is None: raise Exception("Empty Response") - #if not result['success']: raise MarketoException(result['errors'][0] + ". Request ID: " + result['requestId']) + # if not result['success']: raise MarketoException(result['errors'][0] + + # ". Request ID: " + result['requestId']) if not result['success']: raise MarketoException(result['errors'][0]) if 'result' in result: @@ -2588,8 +2587,8 @@ def create_landing_page_content_section(self, id, type, value, backgroundColor=N def update_landing_page_content_section(self, id, contentId, type, value, index=None, backgroundColor=None, borderColor=None, borderStyle=None, borderWidth=None, height=None, - zIndex=None, left=None, opacity=None, top=None, width=None, hideDesktop=None, - hideMobile=None, imageOpenNewWindow=None, linkUrl=None): + zIndex=None, left=None, opacity=None, top=None, width=None, + hideDesktop=None, hideMobile=None, imageOpenNewWindow=None, linkUrl=None): self.authenticate() if id is None: raise ValueError("Invalid argument: required argument id is none.") @@ -2659,8 +2658,8 @@ def delete_landing_page_content_section(self, id, contentId): args = { 'access_token': self.token } - result = self._api_call('post', self.host + "/rest/asset/v1/landingPage/" + str(id) + "/content/" + str(contentId) + - "/delete.json", args) + result = self._api_call('post', self.host + "/rest/asset/v1/landingPage/" + str(id) + "/content/" + + str(contentId) + "/delete.json", args) if result is None: raise Exception("Empty Response") if not result['success']: diff --git a/marketorestpython/helper/exceptions.py b/marketorestpython/helper/exceptions.py index 1809043..535c92a 100644 --- a/marketorestpython/helper/exceptions.py +++ b/marketorestpython/helper/exceptions.py @@ -1,8 +1,8 @@ - class MarketoException(Exception): message = None code = None - def __init__(self, exc = {'message':None,'code':None}): + + def __init__(self, exc={'message': None, 'code': None}): self.message = exc['message'] self.code = exc['code'] diff --git a/marketorestpython/helper/http_lib.py b/marketorestpython/helper/http_lib.py index ae2dd67..37cc13a 100644 --- a/marketorestpython/helper/http_lib.py +++ b/marketorestpython/helper/http_lib.py @@ -1,6 +1,11 @@ +import logging +import mimetypes import requests import time -import mimetypes + + +logger = logging.getLogger('marketorestpython') + class HttpLib: max_retries = 3 @@ -9,14 +14,16 @@ class HttpLib: def _rate_limited(maxPerSecond): minInterval = 1.0 / float(maxPerSecond) + def decorate(func): lastTimeCalled = [0.0] - def rateLimitedFunction(*args,**kargs): + + def rateLimitedFunction(*args, **kargs): elapsed = time.clock() - lastTimeCalled[0] leftToWait = minInterval - elapsed - if leftToWait>0: + if leftToWait > 0: time.sleep(leftToWait) - ret = func(*args,**kargs) + ret = func(*args, **kargs) lastTimeCalled[0] = time.clock() return ret return rateLimitedFunction @@ -37,8 +44,8 @@ def get(self, endpoint, args=None, mode=None): r_json = r.json() # if we still hit the rate limiter, do not return anything so the call will be retried if 'success' in r_json: # this is for all normal API calls (but not the access token call) - if r_json['success'] == False: - print('error from http_lib.py: ' + str(r_json['errors'][0])) + if r_json['success'] is False: + logger.error('error from http_lib.py: ' + str(r_json['errors'][0])) if r_json['errors'][0]['code'] in ('606', '615', '604'): # this handles Marketo exceptions; HTTP response is still 200, but error is in the JSON error_code = r_json['errors'][0]['code'] @@ -47,9 +54,11 @@ def get(self, endpoint, args=None, mode=None): '615': 'concurrent call limit', '604': 'timeout'} if retries < self.max_retries: - print('Attempt %s. Error %s, %s. Pausing, then trying again.' % (retries, error_code, error_description[error_code])) + logger.warning('Attempt %s. Error %s, %s. Pausing, then trying again.' % + (retries, error_code, error_description[error_code])) else: - print('Attempt %s. Error %s, %s. This was the final attempt.' % (retries, error_code, error_description[error_code])) + logger.warning('Attempt %s. Error %s, %s. This was the final attempt.' % + (retries, error_code, error_description[error_code])) time.sleep(self.sleep_duration) retries += 1 else: @@ -60,7 +69,7 @@ def get(self, endpoint, args=None, mode=None): else: return r_json # this is only for the access token call except Exception as e: - print("HTTP Get Exception! Retrying.....") + logger.error("HTTP Get Exception! Retrying.....") time.sleep(self.sleep_duration) retries += 1 @@ -83,8 +92,8 @@ def post(self, endpoint, args, data=None, files=None, filename=None, mode=None): r_json = r.json() # if we still hit the rate limiter, do not return anything so the call will be retried if 'success' in r_json: # this is for all normal API calls (but not the access token call) - if r_json['success'] == False: - print('error from http_lib.py: ' + str(r_json['errors'][0])) + if r_json['success'] is False: + logger.error('error from http_lib.py: ' + str(r_json['errors'][0])) if r_json['errors'][0]['code'] in ('606', '615', '604'): # this handles Marketo exceptions; HTTP response is still 200, but error is in the JSON error_code = r_json['errors'][0]['code'] @@ -93,11 +102,11 @@ def post(self, endpoint, args, data=None, files=None, filename=None, mode=None): '615': 'concurrent call limit', '604': 'timeout'} if retries < self.max_retries: - print('Attempt %s. Error %s, %s. Pausing, then trying again.' % ( - retries, error_code, error_description[error_code])) + logger.warning('Attempt %s. Error %s, %s. Pausing, then trying again.' % + (retries, error_code, error_description[error_code])) else: - print('Attempt %s. Error %s, %s. This was the final attempt.' % ( - retries, error_code, error_description[error_code])) + logger.warning('Attempt %s. Error %s, %s. This was the final attempt.' % + (retries, error_code, error_description[error_code])) time.sleep(self.sleep_duration) retries += 1 else: @@ -108,7 +117,7 @@ def post(self, endpoint, args, data=None, files=None, filename=None, mode=None): else: return r_json except Exception as e: - print("HTTP Post Exception! Retrying....."+ str(e)) + logger.error("HTTP Post Exception! Retrying....." + str(e)) time.sleep(self.sleep_duration) retries += 1 @@ -123,6 +132,6 @@ def delete(self, endpoint, args, data): r = requests.delete(endpoint, params=args, json=data, headers=headers) return r.json() except Exception as e: - print("HTTP Delete Exception! Retrying....."+ str(e)) + logger.error("HTTP Delete Exception! Retrying....." + str(e)) time.sleep(self.sleep_duration) retries += 1