Skip to content

Commit

Permalink
Merge branch 'master' into vendor-default
Browse files Browse the repository at this point in the history
  • Loading branch information
marcy-terui committed Sep 3, 2018
2 parents 5e31877 + 4454e05 commit 25d0d03
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 28 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const initialize = require('./lib/initialize');
const createHttpServer = require('./lib/createHttpServer');
const openAuthorizationUri = require('./lib/openAuthorizationUri');
const getVendorId = require('./lib/getVendorId');
const getToken = require('./lib/getToken');
const getRemoteSkills = require('./lib/getRemoteSkills');
const outputSkills = require('./lib/outputSkills');
const createSkill = require('./lib/createSkill');
Expand Down Expand Up @@ -37,6 +38,7 @@ class AlexaSkills {
createHttpServer,
openAuthorizationUri,
getVendorId,
getToken,
getRemoteSkills,
outputSkills,
createSkill,
Expand Down
41 changes: 28 additions & 13 deletions lib/AlexaApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
const BbPromise = require('bluebird');
const rp = require('request-promise');
const qs = require('querystring');
const so2 = require('simple-oauth2');

const URL_BASE = 'https://api.amazonalexa.com';
const URL_VERSION = 'v1';

class AlexaApi {
constructor(token) {
this.headers = {
Authorization: token,
this.accessToken = token;
}
_getHeaders() {
return {
Authorization: this.accessToken.token.access_token,
};
}
getHeaders() {
if (this.accessToken.expired()) {
return this.accessToken.refresh().then(newToken => {
this.accessToken = newToken;
return BbPromise.resolve(this._getHeaders())
})
}

return BbPromise.resolve(this._getHeaders());
}
get(path, params) {
let url = URL_BASE;

Expand All @@ -25,36 +39,37 @@ class AlexaApi {
if (!(typeof params === 'undefined')) {
url = `${url}?${qs.stringify(params)}`;
}
return rp({

return this.getHeaders().then(headers => rp({
url,
method: 'GET',
headers: this.headers,
});
headers,
}));
}
_jsonRequest(url, method, params) {
return {
return this.getHeaders().then(headers => ({
url,
method,
headers: this.headers,
headers,
json: true,
body: params,
};
}));
}
post(path, params) {
const url = `${URL_BASE}/${URL_VERSION}${path}`;
return rp(this._jsonRequest(url, 'POST', params));
return this._jsonRequest(url, 'POST', params).then(rp);
}
put(path, params) {
const url = `${URL_BASE}/${URL_VERSION}${path}`;
return rp(this._jsonRequest(url, 'PUT', params));
return this._jsonRequest(url, 'PUT', params).then(rp);
}
delete(path) {
const url = `${URL_BASE}/${URL_VERSION}${path}`;
return rp({
return this.getHeaders().then(headers => rp({
url,
method: 'DELETE',
headers: this.headers,
});
headers,
}));
}
getVendors() {
return this.get('/vendors').then(function (body) {
Expand Down
1 change: 0 additions & 1 deletion lib/createSkill.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const AlexaApi = require('./AlexaApi');
const getToken = require('./getToken');

module.exports = {
createSkill() {
Expand Down
3 changes: 1 addition & 2 deletions lib/deleteSkill.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

const BbPromise = require('bluebird');
const AlexaApi = require('./AlexaApi');
const getToken = require('./getToken');

module.exports = {
deleteSkill() {
const alexaApi = new AlexaApi(getToken(this.tokenFilePath));
const alexaApi = new AlexaApi(this.getToken());
return BbPromise.bind(this)
.then(alexaApi.deleteSkill(this.options.id))
.then(function () {
Expand Down
1 change: 0 additions & 1 deletion lib/getRemoteModels.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const AlexaApi = require('./AlexaApi');
const getToken = require('./getToken');

module.exports = {
getRemoteModels() {
Expand Down
1 change: 0 additions & 1 deletion lib/getRemoteSkills.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const AlexaApi = require('./AlexaApi');
const getToken = require('./getToken');

module.exports = {
getRemoteSkills() {
Expand Down
8 changes: 5 additions & 3 deletions lib/getToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const fs = require('fs-extra');

module.exports = function (tokenFilePath) {
const accessToken = fs.readJsonSync(tokenFilePath);
return accessToken.token.access_token;
module.exports = {
getToken() {
const accessToken = fs.readJsonSync(this.tokenFilePath);
return this.oauth2.accessToken.create(accessToken.token);
}
};
2 changes: 1 addition & 1 deletion lib/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const homedir = require('os').homedir();
module.exports = {
initialize() {
this.localServerPort = this.serverless.service.custom.alexa.localServerPort || 9090;
this.tokenFilePath = path.join(homedir, '.serverless', this.TOKEN_FILE_NAME);
this.tokenFilePath = this.serverless.service.custom.alexa.tokenFilePath || path.join(homedir, '.serverless', this.TOKEN_FILE_NAME);
this.oauth2 = so2.create({
client: {
id: this.serverless.service.custom.alexa.clientId || 'amzn1.application-oa2-client.aad322b5faab44b980c8f87f94fbac56',
Expand Down
3 changes: 1 addition & 2 deletions lib/updateModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

const BbPromise = require('bluebird');
const AlexaApi = require('./AlexaApi');
const getToken = require('./getToken');

module.exports = {
updateModels(diffs) {
const alexaApi = new AlexaApi(getToken(this.tokenFilePath));
const alexaApi = new AlexaApi(this.getToken());
return BbPromise.bind(this)
.then(() => BbPromise.resolve(diffs))
.map(function (diff) {
Expand Down
3 changes: 1 addition & 2 deletions lib/updateSkills.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

const BbPromise = require('bluebird');
const AlexaApi = require('./AlexaApi');
const getToken = require('./getToken');

module.exports = {
updateSkills(diffs) {
const alexaApi = new AlexaApi(getToken(this.tokenFilePath));
const alexaApi = new AlexaApi(this.getToken());
return BbPromise.bind(this)
.then(() => BbPromise.resolve(diffs))
.map(function (diff) {
Expand Down
4 changes: 2 additions & 2 deletions test/AlexaApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ describe('AlexaApi', () => {
let alexaApi;

beforeEach(() => {
alexaApi = new AlexaApi('token');
alexaApi = new AlexaApi({ expired: () => false, token: { access_token: 'token' } });
});

describe('#constructor()', () => {

it('should have Authorization header', () => {
expect(alexaApi.headers.Authorization).to.equal('token');
return alexaApi.getHeaders().then(headers => expect(headers.Authorization).to.equal('token'));
});

});
Expand Down

0 comments on commit 25d0d03

Please sign in to comment.