Writing a Custom Script Using Python


    You need working knowledge of:
    * JSON
    * Python

    Custom Script File Structure

    1. Import required packages
    2. Get input arguments
    3. Implement the logic
    4. Return JSON

    Frequently Used Packages

    Package Usage
    Sys Fetches the input arguments
    json Manipulates JSON data
    requests Makes API calls
    datetime Transforms time from milliseconds to the required date format

    Getting Input Arguments

    The script file arguments can be fetched using sys.argv[index] where index starts from 1 to the number of arguments passed.
    When the argument passed is $COMPLETE_V3_JSON_FILE (the path to the file containing the request JSON), the JSON file can be read using the following snippet:
    file_Path = sys.argv[1]
with open(file_Path) as data_file:
data = json.load(data_file)

    Implementing the Logic

    Snippet to make API call:
    with requests.Session() as s:
url = 'api_url'
r = s.post(url,verify=True, data=post_data,headers=headers)
    Construct the api_url, post_data and headers as required.
    Snippet to transform time from millisec to required date format:
    date = datetime.datetime.fromtimestamp(int(millisec)/1e3).strftime('%d %b %Y, %H:%M:%S')

    Constructing Return JSON

    A sample JSON such as {"key":"value"} construction:
    json = {}
    json["key"] = "value"
    A sample JSON array such as [{"key":"value"}] construction:
    json = {}
    json["key"] = "value"
    result = []

    Zoho Corp. All rights reserved.