refactoring
This commit is contained in:
@@ -1,118 +1,131 @@
|
||||
require('isomorphic-fetch');
|
||||
const config = require('../config');
|
||||
var request = require("request");
|
||||
var token = require('./token');
|
||||
require ('isomorphic-fetch');
|
||||
const config = require ('../config');
|
||||
var request = require ('request');
|
||||
var databaseHelper = require ('./database');
|
||||
|
||||
var getBuildStatus = function (skillID) {
|
||||
try {
|
||||
fetch (
|
||||
`https://api.amazonalexa.com/v0/skills/${skillID}/interactionModel/locales/en-US/status`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: config.TOKEN,
|
||||
},
|
||||
}
|
||||
)
|
||||
.then (l => l.text ())
|
||||
.then (result => {
|
||||
return result;
|
||||
});
|
||||
} catch (e) {
|
||||
console.log ('err : ' + e);
|
||||
}
|
||||
};
|
||||
|
||||
var getBuildStatus = function(skillID){
|
||||
try{
|
||||
fetch(`https://api.amazonalexa.com/v0/skills/${skillID}/interactionModel/locales/en-US/status`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: TOKEN
|
||||
},
|
||||
}).then(l=>l.text()).then(result=>{
|
||||
return result;
|
||||
});
|
||||
}catch(e){
|
||||
console.log("err : " + e);
|
||||
}
|
||||
}
|
||||
var refreshTokens = function () {
|
||||
return new Promise ((resolve, reject) => {
|
||||
var options = {
|
||||
method: 'POST',
|
||||
url: 'https://api.amazon.com/auth/o2/token',
|
||||
headers: {
|
||||
'cache-control': 'no-cache',
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
form: {
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: config.REFRESH_TOKEN,
|
||||
client_id: config.CLIENT_ID,
|
||||
client_secret: config.CLIENT_SECRET,
|
||||
},
|
||||
};
|
||||
|
||||
var refreshToken = function(db){
|
||||
return new Promise((resolve, reject)=>{
|
||||
var options = { method: 'POST',
|
||||
url: 'https://api.amazon.com/auth/o2/token',
|
||||
headers:
|
||||
{ 'cache-control': 'no-cache',
|
||||
'content-type': 'application/x-www-form-urlencoded' },
|
||||
form:
|
||||
{ grant_type: 'refresh_token',
|
||||
refresh_token: config.REFRESH_TOKEN,
|
||||
client_id: config.CLIENT_ID,
|
||||
client_secret: config.CLIENT_SECRET } };
|
||||
|
||||
request(options, function (error, response, body) {
|
||||
if (error) reject(error);
|
||||
parsedResponse = JSON.parse(body);
|
||||
if (parsedResponse.refresh_token){
|
||||
try{
|
||||
token.updateTokens(parsedResponse.refresh_token, parsedResponse.access_token, parsedResponse.expires_in,db);
|
||||
resolve();
|
||||
}catch(e){
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
request (options, function (error, response, body) {
|
||||
if (error) reject (error);
|
||||
parsedResponse = JSON.parse (body);
|
||||
if (parsedResponse.refresh_token)
|
||||
return databaseHelper.updateTokens (
|
||||
parsedResponse.refresh_token,
|
||||
parsedResponse.access_token,
|
||||
parsedResponse.expires_in
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var generateInteractionModel = function(skill){
|
||||
let result = {};
|
||||
let allIntents = [];
|
||||
let defaultIntents = [{
|
||||
name: "AMAZON.CancelIntent",
|
||||
samples: []
|
||||
},
|
||||
{
|
||||
name: "AMAZON.HelpIntent",
|
||||
samples: []
|
||||
},
|
||||
{
|
||||
name: "AMAZON.StopIntent",
|
||||
samples: []
|
||||
}];
|
||||
var generateInteractionModel = function (skill) {
|
||||
let result = {};
|
||||
let allIntents = [];
|
||||
let defaultIntents = [
|
||||
{
|
||||
name: 'AMAZON.CancelIntent',
|
||||
samples: [],
|
||||
},
|
||||
{
|
||||
name: 'AMAZON.HelpIntent',
|
||||
samples: [],
|
||||
},
|
||||
{
|
||||
name: 'AMAZON.StopIntent',
|
||||
samples: [],
|
||||
},
|
||||
];
|
||||
|
||||
/*
|
||||
/*
|
||||
defaultIntents.map(intent=>{
|
||||
allIntents.push(intent);
|
||||
});
|
||||
*/
|
||||
|
||||
skill.intents.map(intent=>{
|
||||
allIntents.push({name: intent.intentName, samples: intent.questions});
|
||||
});
|
||||
skill.intents.map (intent => {
|
||||
allIntents.push ({name: intent.intentName, samples: intent.questions});
|
||||
});
|
||||
|
||||
result.interactionModel = {};
|
||||
result.interactionModel = {};
|
||||
|
||||
result.interactionModel.languageModel = {
|
||||
invocationName: skill.invocationName,
|
||||
intents: allIntents
|
||||
};
|
||||
result.interactionModel.languageModel = {
|
||||
invocationName: skill.invocationName,
|
||||
intents: allIntents,
|
||||
};
|
||||
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
return JSON.stringify (result);
|
||||
};
|
||||
|
||||
var uploadSkill = function(skill){
|
||||
return fetch(`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: config.TOKEN
|
||||
},
|
||||
body: generateInteractionModel(skill)
|
||||
});
|
||||
}
|
||||
var uploadSkill = function (skill) {
|
||||
return fetch (
|
||||
`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: config.TOKEN,
|
||||
},
|
||||
body: generateInteractionModel (skill),
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
updateSkill: function(skill, db){
|
||||
return new Promise((resolve,reject)=>{
|
||||
if (new Date() / 1000 > config.TOKEN_EXPIRES_IN){
|
||||
refreshToken(db).then(()=>{
|
||||
uploadSkill(skill).then(response=>{
|
||||
resolve(response.status);
|
||||
});
|
||||
}).catch(e=>{
|
||||
reject(e);
|
||||
});
|
||||
}else{
|
||||
uploadSkill(skill).then(response=>{
|
||||
resolve(response.status);
|
||||
}).catch(e=>{
|
||||
reject(e);
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
updateSkill: function (skill) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
if (new Date () / 1000 > config.TOKEN_EXPIRES_IN) {
|
||||
refreshTokens ()
|
||||
.then (() => {
|
||||
uploadSkill (skill).then (response => {
|
||||
resolve (response.status);
|
||||
});
|
||||
})
|
||||
.catch (e => {
|
||||
reject (e);
|
||||
});
|
||||
} else {
|
||||
uploadSkill (skill)
|
||||
.then (response => {
|
||||
resolve (response.status);
|
||||
})
|
||||
.catch (e => {
|
||||
reject (e);
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,27 +1,106 @@
|
||||
const config = require('../config');
|
||||
const config = require ('../config');
|
||||
var ObjectID = require ('mongodb').ObjectID;
|
||||
|
||||
var db = null;
|
||||
|
||||
|
||||
module.exports = {
|
||||
initModule : function(databaseObject){
|
||||
db=databaseObject;
|
||||
},
|
||||
initModule: function (databaseObject) {
|
||||
db = databaseObject;
|
||||
db.collection ('intent_list');
|
||||
},
|
||||
|
||||
loadTokens : function(){
|
||||
db.collection('token_list').findOne().then(tokens=>{
|
||||
if (tokens !== null){
|
||||
config.TOKEN = tokens.access_token;
|
||||
config.REFRESH_TOKEN = tokens.refresh_token;
|
||||
config.TOKEN_EXPIRES_IN = tokens.expires_in;
|
||||
}else{
|
||||
//Cannot continue without tokens
|
||||
console.log("Cannot continue without tokens in database");
|
||||
process.exit(-1);
|
||||
}
|
||||
}).catch(e=>{
|
||||
console.log("Error loading tokens ! Cannot continue without tokens in database");
|
||||
process.exit(-1);
|
||||
})
|
||||
}
|
||||
}
|
||||
loadTokens: function () {
|
||||
db
|
||||
.collection ('token_list')
|
||||
.findOne ()
|
||||
.then (tokens => {
|
||||
if (tokens !== null) {
|
||||
config.TOKEN = tokens.access_token;
|
||||
config.REFRESH_TOKEN = tokens.refresh_token;
|
||||
config.TOKEN_EXPIRES_IN = tokens.expires_in;
|
||||
} else {
|
||||
//Cannot continue without tokens
|
||||
console.log ('Cannot continue without tokens in database');
|
||||
process.exit (-1);
|
||||
}
|
||||
})
|
||||
.catch (e => {
|
||||
console.log (
|
||||
'Error loading tokens ! Cannot continue without tokens in database'
|
||||
);
|
||||
process.exit (-1);
|
||||
});
|
||||
},
|
||||
|
||||
updateTokens: function (refresh_token, access_token, expires_in) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
let newTokenDocument = {
|
||||
id: 1,
|
||||
refresh_token: refresh_token,
|
||||
access_token: access_token,
|
||||
expires_in: new Date () / 1000 + expires_in,
|
||||
};
|
||||
db
|
||||
.collection ('token_list')
|
||||
.update ({id: 1}, newTokenDocument, {upsert: true}, (err, result) => {
|
||||
if (err) reject (err);
|
||||
config.REFRESH_TOKEN = refresh_token;
|
||||
config.TOKEN = access_token;
|
||||
config.TOKEN_EXPIRES_IN = newTokenDocument.expires_in;
|
||||
resolve ();
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
loadSkill: function (skillDbID) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
db
|
||||
.collection ('skill_list')
|
||||
.findOne ({_id: ObjectID (skillDbID)}, (err, skill) => {
|
||||
if (skill) {
|
||||
resolve (skill);
|
||||
} else {
|
||||
reject (err);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
updateSkill: function (id, skill) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
db
|
||||
.collection ('skill_list')
|
||||
.update ({_id: ObjectID (id)}, skill, {upsert: true}, (err, result) => {
|
||||
if (JSON.parse (result).nModified === 1) {
|
||||
//database update ok
|
||||
resolve ();
|
||||
} else {
|
||||
reject ();
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
deleteSkill: function (id) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
db
|
||||
.collection ('skill_list')
|
||||
.remove ({_id: ObjectID (id)}, (err, result) => {
|
||||
if (err) reject (err);
|
||||
resolve (result);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
getSkill: function (id) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
db
|
||||
.collection ('skill_list')
|
||||
.find ({_id: ObjectID (id)})
|
||||
.toArray ((err, result) => {
|
||||
if (err) reject (err);
|
||||
resolve (result);
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
const config = require('../config');
|
||||
var ObjectID = require ('mongodb').ObjectID;
|
||||
|
||||
module.exports = {
|
||||
updateTokens : function (refresh_token, access_token, expires_in, db){
|
||||
let newTokenDocument = {
|
||||
id:1,
|
||||
refresh_token:refresh_token,
|
||||
access_token: access_token,
|
||||
expires_in: (new Date() / 1000) + expires_in
|
||||
}
|
||||
let result = db.collection('token_list').update({id:1}, newTokenDocument,{upsert:true}, (err, result)=>{
|
||||
if (err) throw new Error(err);
|
||||
config.REFRESH_TOKEN = refresh_token;
|
||||
config.TOKEN = access_token;
|
||||
config.TOKEN_EXPIRES_IN = newTokenDocument.expires_in;
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user