addon.httpClient with Promise?

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

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;
	});
}
2 Likes

An alternative method to make a Promise is to use:
https://nodejs.org/api/util.html#util_util_promisify_original as mentioned in https://github.com/request/request#readme

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