SugarCRM Python REST

Do you like this?

Summary:
This article shows you how to get access to SugarCRM's features using Web Services such as SOAP and REST for Python. Doing this gives you the ability to write custom code with greater flexibility.


Content:

There are lots of differences between SOAP and REST. Hence, from the coding perspective, the way you interact with SOAP services is also different from the way you interact with REST services. However, for SugarCRM, custom wrappers have been created so that you can have seemingly similar ways of coding. In other words, they expose similar sets of methods. For example, regardless of whether SOAP or REST is used, you can call something between the lines like this sugarcrm_connection.search_by_module("Accounts", filter).

At present, the official website of SugarCRM only publishes a Python-based REST library for Web Services. If you decide to use REST with Python, you can download the setup file from https://github.com/sugarcrm/python_webservices_library. If you want to use SOAP with Python for this purpose, there are also a number of libraries out there built by independent developers.

After downloading the packages, you can just open them and run this command: python setup.py install. Bear in mind at present Python REST API doesn't provide backward compatibility so you have to use SugarCRM REST v4 at least.

The following example shows you how to use the Python REST API

import sugarcrm

sugarcrm_user = "username"
sugarcrm_pass = "password"
sugarcrm_url  = "http://localhost:8080/service/v4/rest.php"

sugarcrm_conn= None

try:
    sugarcrm_conn = sugarcrm.Sugarcrm(url = sugarcrm_url, username = sugarcrm_user, password = sugarcrm_pass)
except ValueError:
    print "cann't connect to " + sugarcrm_url
    exit(1)



#1.a - Method 1: Using Wrappers
accounts = sugarcrm_conn.module('Accounts')
#we just show the first ten of them.
for account in accounts[:10]:
    print "name=%s,email=%s" % (account.name,account.email)
#adding filters
filtered_accounts = accounts.get_entries_where ("name = 'peter'")
for account in filtered_accounts[:10]:
    print "name=%s,email=%s" % (account.name,account.email)

#1.b - Method 2: Using pure JSON
filtered_accounts = sugarcrm_conn.get_entry_list('Accounts','accounts.name like "%peter%"', 'accounts.name DESC', 0, ['name','shipping_address_postalcode']) #here offset is 0
for account in filtered_accounts['entry_list']:
    name_value_list = account['name_value_list']
    print "name=%s,email=%s" % (name_value_list['name']['value'],name_value_list['email1']['value'])

#2 - UPDATE AN EXISTING ENTRY
accounts = sugarcrm_conn.module('Accounts')
#we just show the first ten of them.
for account in accounts[:10]:
    account.setValue(['shipping_address_postalcode','email'],['4144','test@gmail.com'])


#3 - CREATE A NEW ENTRY
new_account = sugarcrm_conn.set_entry('Accounts',{'name' : 'account name','email' : 'test@gmail.com'})

They also published an online documentation for the API but surprisingly many of the features there didn't work at all. I had to spend a fair amount of time on playing around with their code before I could get it work for myself. You can find the documentation here: https://github.com/sugarcrm/python_webservices_library/wiki/Manipulating-Entries.

Because it didn't go smoothly as documented at first, I had to dig into their code to find the answers. You can also do the same if you get errors. Here how I did it. Open the file sugarcrm.py under the folder "C:\Python27\Lib\site-packages\sugarcrm" (depending on your OS.) Then I added the following piece of code to see the output from the server:
    def _sendRequest(self, method, data):
        ................       
        f = open('log.txt', 'w')
        f.write(response + "\n")
        f.close()
        ................       


        return result

And you can view the sql statements on the server side in the sugarcrm.log file. Remeber to change the Log Level to Warn. Further information can be found in SugarCRM How To Debug

Previous version of this library worked as follows:
#1.a - Method 1: Using Wrappers
accounts_query = sugarcrm_conn.modules['Accounts'].query()
#we just show the first ten of them.
for account in accounts_query[:10]:
    print "name=%s,email=%s" % (account['name'],account['email'])
#adding filters
filtered_accounts = accounts_query.filter(name__contains='peter')
for account in filtered_accounts[:10]:
    print "name=%s,email=%s" % (account['name'],account['email'])

#2 - UPDATE AN EXISTING ENTRY
accounts = sugarcrm_conn.module('Accounts')
#we just show the first ten of them.
for account in accounts[:10]:
    account.shipping_address_postalcode= '4144'
    account.save()

 
comments powered by Disqus