Threading in Python SDK
Threads in a Python program help you achieve parallelism. By using multiple threads, you can make a Python program run faster and do multiple things simultaneously.
The Python SDK (from version 3.x.x) supports both single user and a multi-user app.
Multithreading in a Multi-user App
Multi-users functionality is achieved using Initializer's static switch_user() method.
# without proxy
Initializer.switch_user(user=user, environment=environment, token=token, sdk_config=sdk_config_instance)
# with proxy
Initializer.switch_user(user=user, environment=environment, token=token, sdk_config=sdk_config_instance, proxy=request_proxy)
Here is a sample code to depict multi-threading for a multi-user app.
import threading
from zcrmsdk.src.com.zoho.crm.api.user_signature import UserSignature
from zcrmsdk.src.com.zoho.crm.api.dc import USDataCenter, EUDataCenter
from zcrmsdk.src.com.zoho.api.authenticator.store import DBStore
from zcrmsdk.src.com.zoho.api.logger import Logger
from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer
from zcrmsdk.src.com.zoho.api.authenticator.oauth_token import OAuthToken, TokenType
from zcrmsdk.src.com.zoho.crm.api.record import *
from zcrmsdk.src.com.zoho.crm.api.request_proxy import RequestProxy
from zcrmsdk.src.com.zoho.crm.api.sdk_config import SDKConfig
class MultiThread(threading.Thread):
def __init__(self, environment, token, user, module_api_name, sdk_config, proxy=None):
super().__init__()
self.environment = environment
self.token = token
self.user = user
self.module_api_name = module_api_name
self.sdk_config = sdk_config
self.proxy = proxy
def run(self):
try:
Initializer.switch_user(user=self.user, environment=self.environment, token=self.token, sdk_config=self.sdk_config, proxy=self.proxy)
print('Getting records for User: ' + Initializer.get_initializer().user.email)
response = RecordOperations().get_records(self.module_api_name)
if response is not None:
# Get the status code from response
print('Status Code: ' + str(response.get_status_code()))
if response.get_status_code() in [204, 304]:
print('No Content' if response.get_status_code() == 204 else 'Not Modified')
return
# Get object from response
response_object = response.get_object()
if response_object is not None:
# Check if expected ResponseWrapper instance is received.
if isinstance(response_object, ResponseWrapper):
# Get the list of obtained Record instances
record_list = response_object.get_data()
for record in record_list:
for key, value in record.get_key_values().items():
print(key + " : " + str(value))
# Check if the request returned an exception
elif isinstance(response_object, APIException):
# Get the Status
print("Status: " + response_object.get_status().get_value())
# Get the Code
print("Code: " + response_object.get_code().get_value())
print("Details")
# Get the details dict
details = response_object.get_details()
for key, value in details.items():
print(key + ' : ' + str(value))
# Get the Message
print("Message: " + response_object.get_message().get_value())
except Exception as e:
print(e)
@staticmethod
def call():
logger = Logger.get_instance(level=Logger.Levels.INFO, file_path="/Users/user_name/Documents/python_sdk_log.log")
user1 = UserSignature(email="abc@zoho.com")
token1 = OAuthToken(client_id="clientId1", client_secret="clientSecret1", token="GRANT Token", token_type=TokenType.GRANT)
environment1 = USDataCenter.PRODUCTION()
store = DBStore()
sdk_config_1 = SDKConfig(auto_refresh_fields=True, pick_list_validation=False)
resource_path = '/Users/user_name/Documents/python-app'
user1_module_api_name = 'Leads'
user2_module_api_name = 'Contacts'
environment2 = EUDataCenter.SANDBOX()
user2 = UserSignature(email="abc@zoho.eu")
sdk_config_2 = SDKConfig(auto_refresh_fields=False, pick_list_validation=True)
token2 = OAuthToken(client_id="clientId2", client_secret="clientSecret2",
token="REFRESH Token", token_type=TokenType.REFRESH, redirect_url="redirectURL")
request_proxy_user_2 = RequestProxy(host="host", port=8080)
Initializer.initialize(user=user1, environment=environment1, token=token1, store=store, sdk_config=sdk_config_1, resource_path=resource_path, logger=logger)
t1 = MultiThread(environment1, token1, user1, user1_module_api_name, sdk_config_1)
t2 = MultiThread(environment2, token2, user2, user2_module_api_name, sdk_config_2, request_proxy_user_2)
t1.start()
t2.start()
t1.join()
t2.join()
MultiThread.call()The program execution starts from call().
The details of user1 is given in the variables user1, token1, environment1.
Similarly, the details of another user user2 is given in the variables user2, token2, environment2.
When the start() is called which in-turn invokes the run(), the details of user1 are passed to the switch_user method through the MultiThread object. Therefore, this creates a thread for user1.
Similarly, When the start() is invoked again, the details of user2 are passed to the switch_user function through the MultiThread object. Therefore, this creates a thread for user2.
Multi-threading in a Single User App
Here is a sample code to depict multi-threading for a single-user app.
import threading
from zcrmsdk.src.com.zoho.crm.api.user_signature import UserSignature
from zcrmsdk.src.com.zoho.crm.api.dc import USDataCenter
from zcrmsdk.src.com.zoho.api.authenticator.store import DBStore
from zcrmsdk.src.com.zoho.api.logger import Logger
from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer
from zcrmsdk.src.com.zoho.api.authenticator.oauth_token import OAuthToken, TokenType
from zcrmsdk.src.com.zoho.crm.api.sdk_config import SDKConfig
from zcrmsdk.src.com.zoho.crm.api.record import *
class MultiThread(threading.Thread):
def __init__(self, module_api_name):
super().__init__()
self.module_api_name = module_api_name
def run(self):
try:
print("Calling Get Records for module: " + self.module_api_name)
response = RecordOperations().get_records(self.module_api_name)
if response is not None:
# Get the status code from response
print('Status Code: ' + str(response.get_status_code()))
if response.get_status_code() in [204, 304]:
print('No Content' if response.get_status_code() == 204 else 'Not Modified')
return
# Get object from response
response_object = response.get_object()
if response_object is not None:
# Check if expected ResponseWrapper instance is received.
if isinstance(response_object, ResponseWrapper):
# Get the list of obtained Record instances
record_list = response_object.get_data()
for record in record_list:
for key, value in record.get_key_values().items():
print(key + " : " + str(value))
# Check if the request returned an exception
elif isinstance(response_object, APIException):
# Get the Status
print("Status: " + response_object.get_status().get_value())
# Get the Code
print("Code: " + response_object.get_code().get_value())
print("Details")
# Get the details dict
details = response_object.get_details()
for key, value in details.items():
print(key + ' : ' + str(value))
# Get the Message
print("Message: " + response_object.get_message().get_value())
except Exception as e:
print(e)
@staticmethod
def call():
logger = Logger.get_instance(level=Logger.Levels.INFO, file_path="/Users/user_name/Documents/python_sdk_log.log")
user = UserSignature(email="abc@zoho.com")
token = OAuthToken(client_id="clientId", client_secret="clientSecret",
token="GRANT Token", token_type=TokenType.GRANT, redirect_url="redirectURL")
environment = USDataCenter.PRODUCTION()
store = DBStore()
sdk_config = SDKConfig()
resource_path = '/Users/user_name/Documents/python-app'
Initializer.initialize(user=user, environment=environment, token=token, store=store, sdk_config=sdk_config, resource_path=resource_path, logger=logger)
t1 = MultiThread('Leads')
t2 = MultiThread('Quotes')
t1.start()
t2.start()
t1.join()
t2.join()
MultiThread.call()
The program execution starts from call(), where the SDK is initialized with the details of the user.
When the start() is called which in-turn invokes the run(), the module_api_name is switched through the MultiThread object. Therefore, this creates a thread for the particular MultiThread instance.
Similarly, the details of another user user2 is given in the variables user2, token2, environment2.
SDK Sample Code
from datetime import datetime
from zcrmsdk.src.com.zoho.crm.api.user_signature import UserSignature
from zcrmsdk.src.com.zoho.crm.api.dc import USDataCenter
from zcrmsdk.src.com.zoho.api.authenticator.store import DBStore
from zcrmsdk.src.com.zoho.api.logger import Logger
from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer
from zcrmsdk.src.com.zoho.api.authenticator.oauth_token import OAuthToken, TokenType
from zcrmsdk.src.com.zoho.crm.api.record import *
from zcrmsdk.src.com.zoho.crm.api import HeaderMap, ParameterMap
from zcrmsdk.src.com.zoho.crm.api.sdk_config import SDKConfig
class Record(object):
def __init__(self):
pass
@staticmethod
def get_records():
"""
Create an instance of Logger Class that takes two parameters
1 -> Level of the log messages to be logged. Can be configured by typing Logger.Levels "." and choose any level from the list displayed.
2 -> Absolute file path, where messages need to be logged.
"""
logger = Logger.get_instance(level=Logger.Levels.INFO,
file_path="/Users/user_name/Documents/python_sdk_log.log")
# Create an UserSignature instance that takes user Email as parameter
user = UserSignature(email="abc@zoho.com")
"""
Configure the environment
which is of the pattern Domain.Environment
Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
Available Environments: PRODUCTION(), DEVELOPER(), SANDBOX()
"""
environment = USDataCenter.PRODUCTION()
"""
Create a Token instance that takes the following parameters
1 -> OAuth client id.
2 -> OAuth client secret.
3 -> REFRESH/GRANT token.
4 -> token type.
5 -> OAuth redirect URL.
"""
token = OAuthToken(client_id="clientId", client_secret="clientSecret",
token="REFRESH/ GRANT Token", token_type=TokenType.REFRESH / TokenType.GRANT, redirect_url="redirectURL")
"""
Create an instance of TokenStore
1 -> DataBase host name. Default value "localhost"
2 -> DataBase name. Default value "zohooauth"
3 -> DataBase user name. Default value "root"
4 -> DataBase password. Default value ""
5 -> DataBase port number. Default value "3306"
"""
store = DBStore()
"""
auto_refresh_fields
if True - all the modules' fields will be auto-refreshed in the background, every hour.
if False - the fields will not be auto-refreshed in the background. The user can manually delete the file(s) or refresh the fields using methods from ModuleFieldsHandler(zcrmsdk/src/com/zoho/crm/api/util/module_fields_handler.py)
pick_list_validation
A boolean field that validates user input for a pick list field and allows or disallows the addition of a new value to the list.
if True - the SDK validates the input. If the value does not exist in the pick list, the SDK throws an error.
if False - the SDK does not validate the input and makes the API request with the user’s input to the pick list
"""
config = SDKConfig(auto_refresh_fields=True, pick_list_validation=False)
"""
The path containing the absolute directory path (in the key resource_path) to store user-specific files containing information about fields in modules.
"""
resource_path = '/Users/user_name/Documents/python-app'
"""
Call the static initialize method of Initializer class that takes the following arguments
1 -> UserSignature instance
2 -> Environment instance
3 -> Token instance
4 -> TokenStore instance
5 -> SDKConfig instance
6 -> resource_path
7 -> Logger instance
"""
Initializer.initialize(user=user, environment=environment, token=token, store=store, sdk_config=config, resource_path=resource_path, logger=logger)
try:
module_api_name = 'Leads'
param_instance = ParameterMap()
param_instance.add(GetRecordsParam.converted, 'both')
param_instance.add(GetRecordsParam.cvid, '12712717217218')
header_instance = HeaderMap()
header_instance.add(GetRecordsHeader.if_modified_since, datetime.now())
response = RecordOperations().get_records(module_api_name, param_instance, header_instance)
if response is not None:
# Get the status code from response
print('Status Code: ' + str(response.get_status_code()))
if response.get_status_code() in [204, 304]:
print('No Content' if response.get_status_code() == 204 else 'Not Modified')
return
# Get object from response
response_object = response.get_object()
if response_object is not None:
# Check if expected ResponseWrapper instance is received.
if isinstance(response_object, ResponseWrapper):
# Get the list of obtained Record instances
record_list = response_object.get_data()
for record in record_list:
# Get the ID of each Record
print("Record ID: " + record.get_id())
# Get the createdBy User instance of each Record
created_by = record.get_created_by()
# Check if created_by is not None
if created_by is not None:
# Get the Name of the created_by User
print("Record Created By - Name: " + created_by.get_name())
# Get the ID of the created_by User
print("Record Created By - ID: " + created_by.get_id())
# Get the Email of the created_by User
print("Record Created By - Email: " + created_by.get_email())
# Get the CreatedTime of each Record
print("Record CreatedTime: " + str(record.get_created_time()))
if record.get_modified_time() is not None:
# Get the ModifiedTime of each Record
print("Record ModifiedTime: " + str(record.get_modified_time()))
# Get the modified_by User instance of each Record
modified_by = record.get_modified_by()
# Check if modified_by is not None
if modified_by is not None:
# Get the Name of the modified_by User
print("Record Modified By - Name: " + modified_by.get_name())
# Get the ID of the modified_by User
print("Record Modified By - ID: " + modified_by.get_id())
# Get the Email of the modified_by User
print("Record Modified By - Email: " + modified_by.get_email())
# Get the list of obtained Tag instance of each Record
tags = record.get_tag()
if tags is not None:
for tag in tags:
# Get the Name of each Tag
print("Record Tag Name: " + tag.get_name())
# Get the Id of each Tag
print("Record Tag ID: " + tag.get_id())
# To get particular field value
print("Record Field Value: " + str(record.get_key_value('Last_Name')))
print('Record KeyValues: ')
for key, value in record.get_key_values().items():
print(key + " : " + str(value))
# Check if the request returned an exception
elif isinstance(response_object, APIException):
# Get the Status
print("Status: " + response_object.get_status().get_value())
# Get the Code
print("Code: " + response_object.get_code().get_value())
print("Details")
# Get the details dict
details = response_object.get_details()
for key, value in details.items():
print(key + ' : ' + str(value))
# Get the Message
print("Message: " + response_object.get_message().get_value())
except Exception as e:
print(e)
Record.get_records()
Record Response
