在上一篇《Nodejs获取Azure Active Directory AccessToken》中,已经获取到了accessToken,现时需要获取WebAPI的数据,选择了node-fetch来获取数据
node-fetch是使用promise的写法,对于习惯了promise写法的人来说,还是非常容易的
这里提醒一下,公司上网是通过代理的方式来上网的,那么在获取外网的地址时,如果没通过代理,则获取不到数据,在这里,我加入了代理node-https-proxy-agent,关于使用,可以看文章最后的参考地址
var fetch = require('node-fetch');
var defaultConfig = require('../config/default.json');
var HttpsProxyAgent = require('https-proxy-agent');
module.exports = {
searchGarmentStyle: function (garmentStyleNo, access_token) {
var queryEntity = {
"filterType": "LEAF",
"filters": [{}],
"attributeName": "item_number",
"searchOperator": "eq",
"filterValue": garmentStyleNo
}
var garmentstyles = [];
// Set up the request
return new Promise(function (resolve, reject) {
try {
//-------------------------------------------------------------------------------------------------------------
fetch(defaultConfig.apiUrl.getGarmentStyleSigleApi,
{
agent: new HttpsProxyAgent('http://192.168.27.4:8083'),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + access_token
},
body: JSON.stringify(queryEntity)
})
.then((response) => {
return response.json();
})
.then((responseJsonData) => {
var getresponse = responseJsonData;
if (getresponse.resultType === "SUCCESS") {
if (getresponse.results) {
if (getresponse.results[0].data) {
var getdata = getresponse.results[0].data;
for (let i = 0; i < getdata.length; i++) {
garmentstyles.push(getdata[i]);
}
resolve(garmentstyles);
}
else {
reject(new Error('WebAPI Error :resultType.results[0].data Value Is Null Or Empty'));
}
}
else {
reject(new Error('WebAPI Error :resultType.results Value Is Null Or Empty'));
}
}
else {
reject(new Error('WebAPI Error :resultType Value Is ' + getresponse.resultType));
}
})
.catch((error) => {
reject(new Error('WebAPI Error :' + error.message));
});
} catch (e) {
reject(e);
}
});
}
};
调用(这里与获取token的方法进行合并):
var garmentstyle_helper = require('./service/garment_style_search');
//get token
adal_manage.getToken()
.then((token_object) => {
// console.log(token_object.accessToken);
//call webapi
garmentstyle_helper
.searchGarmentStyle(garmentStyleNo, token_object.accessToken)
.then((GarmentStyles) => {
if (GarmentStyles && GarmentStyles.length > 0) {
//foreach data
for (var getstyle of GarmentStyles) {
// add message
var message = new builder.Message()
.text(getstyle.linePlanProducts.productID + '(' + getstyle.linePlanProducts.productVersion + getstyle.linePlanProducts.productVersionSerialNo + ')')
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(getstyle.linePlanProducts.productMaterialConfigs.map(garmentStyleColorwayAttachment));
session.send(message);
}
session.endDialog();
}
else {
// no found
session.send('can not found garment style \"%s\"', garmentStyleNo);
session.endDialog();
}
},
(err) => {
session.send('[searchGarmentStyle Error:]' + err.message ? err.message : '');
session.endDialog();
});
}, (error) => {
session.send('[getToken Error:]' + err.message ? err.message : '');
session.endDialog();
});
参考:https://github.com/bitinn/node-fetch
https://github.com/TooTallNate/node-https-proxy-agent