addon.httpClient with Promise?

HI,
Is it possible to use the standard addon.httpClient with promises in ACE?

1 Like

I’m afraid not, as it uses Request, which does not support promises.

From the readme:

ACE bundles and extends the request HTTP client. To make a JWT signed request back to the host, all you have to do is use request the way it was designed, but use a URL back to the host’s REST APIs.

BTW: it should not be hard to overwrite the addon.httpClient method and promisify it.

Thanks @remie
Ended up wrapping the request calls with a promisify function as detailed here: Jira-issue-field-demo returning undefined is not a valid argument

It works!


export async function getAllProjects(userAccountId, httpClient) {
	let data;
	return new Promise((resolve, reject) => {
		httpClient
			.asUserByAccountId(userAccountId)
			.get("rest/api/3/project?", function(err, response, body) {
				data = JSON.parse(body);
				if (err) reject(err);
				resolve(data);
			});
		return data;
	});
}
3 Likes

An alternative method to make a Promise is to use:
Util | Node.js v19.4.0 Documentation as mentioned in GitHub - request/request: 🏊🏾 Simplified HTTP request client.

Hi @SyedFarhan ,

after using the above-suggested code I am not able to set my global variable value.
check the below code.

async function getAllProjects(req)
 {
	let data;
	return new Promise((resolve, reject) => {
       plugin.httpClient(req)
			.get("rest/api/3/project?", function(err, response, body) 
                      {
				data = JSON.parse(body);
                                Console.log("Here its showing data value"+data);
				if (err) reject(err);
				resolve(data);
			});
                 Console.log("Here not showing data value"+data); //data= undefined its coming
		return data;
	});
}

I am writing something wrong here.

any help on this, please.

what is plugin the httpClient I am using above is:
const httpClient = addon.httpClient(req);
then passing it to requests

async function get(req, httpClient){
let data;
return new Promise((resolve, reject) => {
    httpClient.httpClient(req)
    .get("rest/api/3/project?", function(err, response, body){
        data = JSON.parse(body);
        console.log("Here its showing data value "+data);
        if (err) reject(err);
        resolve(data);
    });
    console.log("Here not showing data value " + data); //data= undefined its coming
    return data;
    });
}

@SyedFarhan
I am using a var name plugin instead of the addon.

Can you elaborate what are you doing and share a code sample

@SyedFarhan

You can check in the last console.log statement I am printing values of data but it’s coming undefined.
I have defined data globally before the promise call.

Hi @SyedFarhan why are you returning twice?!

Actually, you should just return with the promise and reject with response error and resolve with (parsed) response body?!

export const getAllProjects( userAccountId, httpClient ) => {
    return new Promise( ( resolve, reject ) => {
        httpClient
            .asUserbyAccountID(userAccountId)
            .get( "<url>", ( err, response, body ) => { 
                if( err ) reject(err);
                resolve( JSON.parse(body) )
            )}
    })
};

This will resolve with the response body.

Best Valentin

FYI: I provided a more detailed solution here: How to get return value from httpClient - Jira Development / Jira Cloud - The Atlassian Developer Community