-
Notifications
You must be signed in to change notification settings - Fork 36
Initializing a sugarcrm object
You can see the the API definition for initializing an object here: sugarcrm.py __init__
As you can see, def __init__ is how we initialize a sugarcrm object. In this function we have 4 parameters, self, host_name, user_name, and password. The self object is used in all of our function calls. The “self” is a pointer to the function itself and we don't need to actually put anything in here. The host_name is the actual url you are logging into. user_name is the user name of your sugarcrm account and password is the password of your sugarcrm account. Both the username and password are default to a null variable, making sure that the values are always empty to avoid any error.
There are two ways to create a sugarcrm object. The easier method would be passing all of the values directly into the function call themselves like this:
session = sugarcrm.Sugarcrm(“http://ruttanvm.cs.kent.edu:4080/service/v2/rest.php”, “class”, “class123”)
Notice, the original function call looks like this: def __init__(self, hostname, username=None, password=None): We simply placed the values in place there and it works perfectly fine.
So now a sugarcrm object will be created which is now the session variable. You can now use the session as an object to call any of the sugarcrm functions. Such as session.login(), session.get_entry_list(), etc.
A second way of calling this same function is as such:
url = “http://ruttanvm.cs.kent.edu:4080/service/v2/rest.php"
user = “class”
pass = “class123”`
session = sugarcrm.Sugarcrm(url, user, password)
Both calls will give you the same solution.
Now take a look at the very bottom of the initialization definition, specifically this line.
We make a call to another function in the API, the login function which can be found further down in the class, here.
This function is simply an extension of our actual initialization and handles the login call for us. It takes the user and password and confirms with the server that they are legitimate values. Otherwise they will throw an exception and warn you that something is incorrect. Assuming everything works out correctly, the data will be resent back to our __init__ definition. This allows us to check if we are actually connected to the server. Using our same session object, we can show this in the following example:
modules = session.get_available_modules()
if modules:
print "Connection successful to " url
The if statements will inform the user that a connection was successful to the server assuming session.connected is true. The second if statement is used to make sure that the id value isn't 0, as long as that is the case a connection was successful.