From 5609ecd320bbe3f63bc9673dbef2043f2c844f6c Mon Sep 17 00:00:00 2001 From: Chuck Date: Wed, 23 Mar 2016 08:33:57 -0400 Subject: [PATCH 1/2] Python 3 compatibility I think something else needs to be done to make pip pick up the change. I'll let the maintainers handle that. Additional CRLF to LF conversion free of charge. --- example.py | 22 +++++++++++----------- infusionsoft/library.py | 23 +++++++++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/example.py b/example.py index 7bd1c20..fc01e61 100644 --- a/example.py +++ b/example.py @@ -5,13 +5,13 @@ # Example 1: Add Contact #---------------------------------------------------------------------------------------- contact = {'FirstName' : 'John', 'LastName' : 'Doe', 'Email' : 'johndoe@email.com'} -print infusionsoft.ContactService('add', contact) +print(infusionsoft.ContactService('add', contact)) # Example 2: Merge two duplicate contacts #---------------------------------------------------------------------------------------- contactId = 56 duplicateContactId = 57 -print infusionsoft.ContactService('merge', contactId, duplicateContactId) +print(infusionsoft.ContactService('merge', contactId, duplicateContactId)) # Example 3: Query a contact using data service #---------------------------------------------------------------------------------------- @@ -20,12 +20,12 @@ query = {'FirstName' : 'John'} limit = 10 page = 0 -print infusionsoft.DataService('query', table, limit, page, query, returnFields) +print(infusionsoft.DataService('query', table, limit, page, query, returnFields)) # Example 4: Return a products inventory using product service #---------------------------------------------------------------------------------------- productId = 1 -print infusionsoft.ProductService('getInventory', productId) +print(infusionsoft.ProductService('getInventory', productId)) # Example 5: Charge an invoice using the invoice service #---------------------------------------------------------------------------------------- @@ -34,7 +34,7 @@ creditCardId = 2 merchantAccountId = 1 bypassCommissions = False -print infusionsoft.InvoiceService('chargeInvoice', invoiceId, notes, creditCardId, merchantAccountId, bypassCommissions) +print(infusionsoft.InvoiceService('chargeInvoice', invoiceId, notes, creditCardId, merchantAccountId, bypassCommissions)) # Example 6: Send an email using the email service #---------------------------------------------------------------------------------------- @@ -47,17 +47,17 @@ subject = 'This is just a test email, relax!' htmlBody = '' textBody = 'This is the contant for the email' -print infusionsoft.APIEmailService('sendEmail', contactList, fromAddress, toAddress, ccAddress, bccAddress, contentType, subject, htmlBody, textBody) +print(infusionsoft.APIEmailService('sendEmail', contactList, fromAddress, toAddress, ccAddress, bccAddress, contentType, subject, htmlBody, textBody)) # Example 7: Get all report columns using the search service #---------------------------------------------------------------------------------------- savedSearchId = 3 userId = 1 -print infusionsoft.SearchService('getAllReportColumns', savedSearchId, userId) +print(infusionsoft.SearchService('getAllReportColumns', savedSearchId, userId)) # Example 8: Get all shipping options with the shipping service #---------------------------------------------------------------------------------------- -print infusionsoft.ShippingService('getAllShippingOptions') +print(infusionsoft.ShippingService('getAllShippingOptions')) # Example 9: Get affiliate payouts info using filter with the affiliate service #---------------------------------------------------------------------------------------- @@ -65,14 +65,14 @@ affiliateId = 2 filterStartDate = datetime(2012, 10, 18) filterEndDate = datetime(2012, 10, 23) -print infusionsoft.APIAffiliateService('affPayouts', affiliateId, filterStartDate, filterEndDate) +print(infusionsoft.APIAffiliateService('affPayouts', affiliateId, filterStartDate, filterEndDate)) # Example 10: Get the download URL of a particular file #---------------------------------------------------------------------------------------- fileId = 23 -print infusionsoft.FileService('getDownloadUrl', fileId) +print(infusionsoft.FileService('getDownloadUrl', fileId)) # Example 11: Using the library server method to access the API : Create a contact #---------------------------------------------------------------------------------------- contact = {'FirstName' : 'John', 'LastName' : 'Doe', 'Email' : 'johndoe@email.com'} -print infusionsoft.server().ContactService.add(infusionsoft.key, contact) +print(infusionsoft.server().ContactService.add(infusionsoft.key, contact)) diff --git a/infusionsoft/library.py b/infusionsoft/library.py index a40fe39..f1bc4bb 100644 --- a/infusionsoft/library.py +++ b/infusionsoft/library.py @@ -1,10 +1,13 @@ -from xmlrpclib import ServerProxy, Error +try: + from xmlrpclib import ServerProxy, Error +except ImportError: + from xmlrpc.client import ServerProxy, Error -class Infusionsoft(object): - base_uri = 'https://%s.infusionsoft.com/api/xmlrpc' +class Infusionsoft(object): + base_uri = 'https://%s.infusionsoft.com/api/xmlrpc' - def __init__(self, name, api_key, use_datetime=False): + def __init__(self, name, api_key, use_datetime=False): uri = self.base_uri % name self.client = ServerProxy(uri, use_datetime=use_datetime) self.client.error = Error @@ -15,7 +18,7 @@ def function(method, *args): call = getattr(self.client, service + '.' + method) try: return call(self.key, *args) - except self.client.error, v: + except self.client.error as v: return "ERROR", v return function @@ -24,11 +27,11 @@ def server(self): class InfusionsoftOAuth(Infusionsoft): - base_uri = 'https://api.infusionsoft.com/crm/xmlrpc/v1?' - - def __init__(self, access_token, use_datetime=False): - uri = '%saccess_token=%s' % (self.base_uri, access_token) + base_uri = 'https://api.infusionsoft.com/crm/xmlrpc/v1?' + + def __init__(self, access_token, use_datetime=False): + uri = '%saccess_token=%s' % (self.base_uri, access_token) self.client = ServerProxy(uri, use_datetime=use_datetime) self.client.error = Error - self.key = access_token + self.key = access_token From 6d3066d7219170e80bc643d4d06ff7408b76eb78 Mon Sep 17 00:00:00 2001 From: Chuck Date: Wed, 23 Mar 2016 18:23:05 -0400 Subject: [PATCH 2/2] getting rid of exception hiding I prefer try catch than check value on every return-- at least this way I get to see immediately what the problem is, rather than a weird ValueError down the line. --- infusionsoft/library.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/infusionsoft/library.py b/infusionsoft/library.py index f1bc4bb..1bd8538 100644 --- a/infusionsoft/library.py +++ b/infusionsoft/library.py @@ -16,16 +16,12 @@ def __init__(self, name, api_key, use_datetime=False): def __getattr__(self, service): def function(method, *args): call = getattr(self.client, service + '.' + method) - try: - return call(self.key, *args) - except self.client.error as v: - return "ERROR", v + return call(self.key, *args) return function def server(self): return self.client - class InfusionsoftOAuth(Infusionsoft): base_uri = 'https://api.infusionsoft.com/crm/xmlrpc/v1?'