Created
March 8, 2016 18:54
-
-
Save BradRuderman/2f1e7dcf93bc8d587db7 to your computer and use it in GitHub Desktop.
Wrapper Class for Marketo API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, datetime, time | |
class PyMarketo(object): | |
def __init__(self, client_id, client_secret, instance_id): | |
self.client_id = client_id | |
self.client_secret = client_secret | |
self.instance_id = instance_id | |
self.url = "https://{0}.mktorest.com".format(self.instance_id) | |
self.get_token() | |
def get_token(self): | |
url = "{0}/identity/oauth/token?grant_type=client_credentials&client_id={1}&client_secret={2}".format(self.url, self.client_id, self.client_secret) | |
res = requests.get(url).json() | |
self.expires_in = res['expires_in'] | |
self.token_expiration_date = datetime.datetime.now() + datetime.timedelta(seconds = self.expires_in) | |
self.scope = res['scope'] | |
self.access_token = res['access_token'] | |
def validate_token(self): | |
if datetime.datetime.now() >= self.token_expiration_date: | |
time.sleep(5) | |
self.get_token() | |
def get(self, path, params={}): | |
self.validate_token() | |
params['access_token'] = self.access_token | |
res = requests.get(self.url + path, params=params, headers={"Authorization" : "Bearer {0}".format(self.access_token)}) | |
body = PyMarketo.__handle_response(res) | |
return body | |
def get_paging_token(self, dt): | |
path = '/rest/v1/activities/pagingtoken.json' | |
since = PyMarketo.convert_datetime_to_string(dt) if dt else '1900-01-01' | |
return self.get(path, {"sinceDatetime":since})['nextPageToken'] | |
def get_activity_types(self): | |
path = '/rest/v1/activities/types.json' | |
return self.get(path)['result'] | |
def get_all_activities(self, activity_types, since=None): | |
rate_limit = 0 | |
paging_token = self.get_paging_token(since) | |
path = '/rest/v1/activities.json' | |
has_more = True | |
while has_more: | |
if rate_limit > 75: | |
time.sleep(15) | |
rate_limit = 0 | |
response = self.get(path, {"activityTypeIds" : activity_types, "nextPageToken" : paging_token}) | |
rate_limit += 1 | |
for result in response['result']: | |
yield result | |
has_more = response['moreResult'] | |
paging_token = response['nextPageToken'] | |
def get_single_activity_page(self, activity_types, since=None): | |
rate_limit = 0 | |
paging_token = self.get_paging_token(since) | |
path = '/rest/v1/activities.json' | |
response = self.get(path, {"activityTypeIds" : activity_types, "nextPageToken" : paging_token}) | |
if 'result' in response: | |
for result in response['result']: | |
yield result | |
@staticmethod | |
def __handle_response(response): | |
if response.status_code == 200: | |
body = response.json() | |
if body['success']: | |
return body | |
else: | |
raise Exception(body) | |
else: | |
raise Exception(response.text) | |
@staticmethod | |
def convert_datetime_to_string(dt): | |
return dt.strftime("%Y-%m-%dT%H:%M:%S:%f%z") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment