Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions marketorestpython/client.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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']:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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']:
Expand Down
4 changes: 2 additions & 2 deletions marketorestpython/helper/exceptions.py
Original file line number Diff line number Diff line change
@@ -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']

Expand Down
43 changes: 26 additions & 17 deletions marketorestpython/helper/http_lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import logging
import mimetypes
import requests
import time
import mimetypes


logger = logging.getLogger('marketorestpython')


class HttpLib:
max_retries = 3
Expand All @@ -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
Expand All @@ -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']
Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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']
Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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