API getting request approval status when requesting all requests.

API getting request approval status when requesting all requests.

Hi!
We are using ServiceDesk Plus for our helpdesk at our school. I have developed a site that shows all of the requests that are waiting on the user to collect their device or are waiting on approval from leadership. I wanted to add an icon next to the request number and user name.

This all works however, I have to iterate over the requests that are waiting on approval which means sending an API request and handling the response for each one individually, which takes extra time and if there are a lot of these then it means increased network traffic. I know that it isn't a lot of extra time and traffic but sending that many requests every time the page updates (every 10 seconds at the moment), which I'm not a fan of.

I would very much like to know if there is a way to get the approval status of the requests on the initial API call that lists all of the requests where the status is equal to collection or approval. Below is the code that I'm using:
Javascript
  1. // Get the data from helpdesk using fetch()
    function getData() {
        fetch('/get-data') // Call the "get-data" function on the serverside python script
            .then(response => response.json()) // Take the full response and store it in an object in a json structure
            .then(data => populate(data))
            .catch(error => console.error(error)); // Log any errors to the console
        setTimeout(getData,10000) // Do it all again in 10 seconds
    }
  2. // Populate the tables with the data from getData()
    async function populate(data) {
        await clearData(); // Clear the data so that each item isn't added several times
        for (const item of data.requests) { // Loop through all of the jobs
            const approvalData = await getApproval(item.id); // Get the approval status of the job
            makeItem(item.site.name, item.status.name, item.requester.name, item.id, approvalData); // Send the data to be written into the correct grid
        }
        await updateData(); // After the jobs are written (as html) to the variables, write them into the grid
    }
  3. // Get the approval status of each job as this information is not provided when getting info on all jobs.
    async function getApproval(jobid) {
        let response = await fetch(`get-approval/${jobid}`); // Get the job specific data
        const data = await response.json(); // Arrange the response promise into a json data structure
        if (data.request.approval_status != null) { // If the approval status is not "null" return the status otherwise return null
            return data.request.approval_status.name;
        }
        else {
            return null;
        }
    }
Python
  1. @app.route('/get-data') # Instructions for when the javascript calls this to start the API request process
    def get_data():
        url = 'https://sub.domain.tld/api/v3/requests' # Helpdesk URL for API
        headers = {"authtoken":"XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "Content-Type":"text/html"} # Auth Token as a key value pair
        # This requests the first 200 jobs orderd by most recent and returns any with status Collection or Leadership. 200 should be enough but can be increased if needed.
        input_data = '''{
            "list_info": {
                "start_index": 1,
                "row_count": 200,
                "sort_field": "id",
                "sort_order": "asc",
                "search_criteria": [
                    {
                        "field": "status.name",
                        "condition": "is",
                        "values": [
                            "Collection",
                            "Approval"
                        ],
                        "logical_operator": "or"
                    }
                ]
            }
        }'''
        params = {'input_data': input_data} # Turn this into a key value pair
        response = requests.get(url, headers=headers, params=params) # Send the request and save the response to a variable
        return response.text # Return the response as a plain text to the calling function

    @app.route('/get-approval/<jobid>') # Instructions for when the javascript calls this to get approval status
    def get_approval(jobid):
        url = 'https://sub.domain.tld/api/v3/requests/' + str(jobid) # Convert the job id from the js fetch request to a string and make it part of the URL
        headers = {"authtoken":"XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "Content-Type":"text/html"} # Auth Token as a key value pair
        response = requests.get(url,headers=headers,verify=False)
        return response.text
Any help would be great. If this is not currently possible, I'll submit a feature request for the API to return the approval status on the call for all requests.

Cheers,
Bob

                  New to ADSelfService Plus?