Discussions

Ask a Question
Back to All

"Retrieve a Client" causes add-on to timeout

I'm attempting to create something very similar to the Tasks tab on the Dispatch Board. The code below works but timesout if I replace

customer_name = get_job_customer(job_uuid, auth)

with

customer_name = get_customer_name(get_job_customer(job_uuid, auth), auth)

All the code in index.py:

import urllib3
import json
from urllib.parse import quote

def get_job_customer(uuid, auth):
    http = urllib3.PoolManager()
    url = "https://api.servicem8.com/api_1.0/job/" + uuid + ".json"
    response = http.request("GET", url, headers={'Authorization': 'Bearer ' + auth})
    job = json.loads(response.data)
    return job["company_uuid"]

def get_customer_name(uuid, auth):
    http = urllib3.PoolManager()
    url = "https://api.servicem8.com/api_1.0/company/" + uuid + ".json"
    response = http.request("GET", url, headers={'Authorization': 'Bearer ' + auth})
    customer = json.loads(response.data)
    return customer["name"]
    
def get_job_number(uuid, auth):
    http = urllib3.PoolManager()
    url = "https://api.servicem8.com/api_1.0/job/" + uuid + ".json"
    response = http.request("GET", url, headers={'Authorization': 'Bearer ' + auth})
    job = json.loads(response.data)
    return job["generated_job_id"]
    
def get_staff_fullname(uuid, auth):
    http = urllib3.PoolManager()
    url = "https://api.servicem8.com/api_1.0/staff/" + uuid + ".json"
    response = http.request("GET", url, headers={'Authorization': 'Bearer ' + auth})
    staff = json.loads(response.data)
    return staff["first"] + " " + staff["last"]    

def lambda_handler(event, context):
    try:
        job_uuid = event['eventArgs']['jobUUID']
        auth = event['auth']['accessToken']

        http = urllib3.PoolManager()
        url = 'https://api.servicem8.com/api_1.0/task.json?' + quote('$filter') + '=' + quote("related_object_uuid eq ") +"'" + job_uuid + "'"
        response = http.request("GET", url, headers={'Authorization': 'Bearer ' + auth})
        tasks = json.loads(response.data)
        for task in tasks:
            name = task["name"]

            customer_name = get_job_customer(job_uuid, auth)
            # ********************************************** Timesout if line above is replaced by line below **************************
            #customer_name = get_customer_name(get_job_customer(job_uuid, auth), auth)
            
            job_number = get_job_number(job_uuid, auth)
            assigned = get_staff_fullname(task["assigned_to_staff_uuid"], auth)
            due_date = task["due_date"]
            
            if task["task_complete"] == "0":
                task_complete = "Not Yet Done"
            else:
                task_complete = "Done"
            
            response_html = '''
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="gr__ap-southeast-2_go_servicem8_com  ext-strict x-viewport">
                <head>
    		        <link rel="stylesheet" type="text/css" href="//static-ap-southeast-2.servicem8.com/37039/css/css_style_rendered_beta.css">
    		        <link rel="stylesheet" type="text/css" href="//static-ap-southeast-2.servicem8.com/37039/jscripts/extjs/resources/css/ext-compile-2017.css">
            		<link rel="stylesheet" type="text/css" href="//static-ap-southeast-2.servicem8.com/37039/css/themes/2017/main.css">
            		
            		<script src="https://platform.servicem8.com/sdk/1.0/sdk.js"></script>
                    <script>
                        var client = SMClient.init();
                        client.resizeWindow(1150, 400);
                    </script>
                    
                </head>
                <body>
                    <div class="x-grid3" hidefocus="true" id="ext-gen138" style="width: 1119px; height: 100px;">
                        <div class="x-grid3-viewport" id="ext-gen139">
                            <div class="x-grid3-header" id="ext-gen140">
                                <div class="x-grid3-header-inner" id="ext-gen142" style="width: 1119px;">
                                    <div class="x-grid3-header-offset" style="width: 1119px;">
                                        <table border="0" cellspacing="0" cellpadding="0" style="width: 1100px;">
                                            <thead>
                                                <tr class="x-grid3-hd-row">
                                                    <td class="x-grid3-hd x-grid3-cell x-grid3-td-task-title x-grid3-cell-first sort-asc" style="width: 558px;"><div class="x-grid3-hd-inner x-grid3-hd-task-title" unselectable="on" style=""><a class="x-grid3-hd-btn" href="#" id="ext-gen480" style="height: 30px;"></a>Task<img alt="" class="x-grid3-sort-icon" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div></td>
                                                    <td class="x-grid3-hd x-grid3-cell x-grid3-td-1" style="width: 170px;"><div class="x-grid3-hd-inner x-grid3-hd-1" unselectable="on" style=""><a class="x-grid3-hd-btn" href="#" id="ext-gen494" style="height: 30px;"></a>Customer<img alt="" class="x-grid3-sort-icon" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div></td>
                                                    <td class="x-grid3-hd x-grid3-cell x-grid3-td-2" style="width: 60px;"><div class="x-grid3-hd-inner x-grid3-hd-2" unselectable="on" style=""><a class="x-grid3-hd-btn" href="#" id="ext-gen497" style="height: 30px;"></a>Job #<img alt="" class="x-grid3-sort-icon" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div></td>
                                                    <td class="x-grid3-hd x-grid3-cell x-grid3-td-3" style="width: 100px;"><div class="x-grid3-hd-inner x-grid3-hd-3" unselectable="on" style=""><a class="x-grid3-hd-btn" href="#" id="ext-gen496" style="height: 30px;"></a>Assigned Staff<img alt="" class="x-grid3-sort-icon" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div></td>
                                                    <td class="x-grid3-hd x-grid3-cell x-grid3-td-4" style="width: 100px;"><div class="x-grid3-hd-inner x-grid3-hd-4" unselectable="on" style=""><a class="x-grid3-hd-btn" href="#" id="ext-gen501" style="height: 30px;"></a>Due Date<img alt="" class="x-grid3-sort-icon" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div></td>
                                                    <td class="x-grid3-hd x-grid3-cell x-grid3-td-5 x-grid3-cell-last" style="width: 100px;"><div class="x-grid3-hd-inner x-grid3-hd-5" unselectable="on" style=""><a class="x-grid3-hd-btn" href="#"></a>Task Status<img alt="" class="x-grid3-sort-icon" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div></td>
                                                </tr>
                                            </thead>
                                        </table>
                                    </div>
                                </div>
                                <div class="x-clear"></div>
                            </div>
                            <div class="x-grid3-scroller" id="ext-gen141" style="width: 1119px; height: 100px;">
                                <div class="x-grid3-body" style="width: 1100px;" id="ext-gen143">
                                    <div class="x-grid3-row  x-grid3-row-first" style="width: 1100px;">
                                        <table class="x-grid3-row-table" border="0" cellspacing="0" cellpadding="0" style="width: 1100px;">
                                            <tbody>
                                                <tr>
                                                    <td class="x-grid3-col x-grid3-cell x-grid3-td-task-title x-grid3-cell-first " style="width: 558px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-task-title" unselectable="on">{name}</div></td>
                                                    <td class="x-grid3-col x-grid3-cell x-grid3-td-1 " style="width: 170px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-1" unselectable="on">{customer_name}</div></td>
                                                    <td class="x-grid3-col x-grid3-cell x-grid3-td-2 " style="width: 60px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-2" unselectable="on">{job_number}</div></td>
                                                    <td class="x-grid3-col x-grid3-cell x-grid3-td-3 " style="width: 100px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-3" unselectable="on">{assigned}</div></td>
                                                    <td class="x-grid3-col x-grid3-cell x-grid3-td-4 " style="width: 100px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-4" unselectable="on">{due_date}</div></td>
                                                    <td class="x-grid3-col x-grid3-cell x-grid3-td-5 x-grid3-cell-last " style="width: 100px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-5" unselectable="on">{task_complete}</div></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </body>
            </html>'''.format(name=name, customer_name=customer_name, job_number=job_number, assigned=assigned, due_date=due_date, task_complete=task_complete)

        return {'eventResponse': response_html}

    except:
        print('Function failed')
        raise