Skip to content

Commit

Permalink
Add Axios
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcL committed Mar 8, 2020
1 parent bb97d57 commit 14f79b2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"test": "mocha"
},
"dependencies": {
"axios": "^0.19.2",
"is-hex": "^1.1.3",
"request": "^2.88.0",
"request-promise": "^4.2.4"
Expand Down
36 changes: 27 additions & 9 deletions src/broadcast.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import isHex from 'is-hex';
import requestPromise from 'request-promise';
import axios from 'axios';
import url from 'url';
import validateFacebookTags from './validateFacebookTags';

// http://docs.chatfuel.com/broadcasting/broadcasting-documentation/broadcasting-api

const CHATFUEL_BASE_URL = 'https://api.chatfuel.com';
const CHATFUEL_API_RATE_LIMIT_REQUESTS_PER_SECOND = 25;

const validateExpectedParameters = (options) => {
const expectedParameters = ['botId', 'token', 'userId', 'messageTag'];
Expand Down Expand Up @@ -45,6 +47,30 @@ const getBlockIdOrNameFromOptions = (options) => {

const createChatfuelBroadcastUrl = (botId, userId) => `${CHATFUEL_BASE_URL}/bots/${botId}/users/${userId}/send`;

const makeRequest = (uri) => {
const requestOptions = {
uri,
headers: {
'Content-Type': 'application/json',
},
json: true,
};

return requestPromise.post(requestOptions);
};

const makeAxiosRequest = (requestUrl) => {
const requestOptions = {
method: 'post',
url: requestUrl,
headers: {
'Content-Type': 'application/json',
},
};

return axios(requestOptions);
};

const broadcast = (options) => {
validateExpectedParameters(options);

Expand Down Expand Up @@ -73,15 +99,7 @@ const broadcast = (options) => {
query,
});

const requestOptions = {
uri: chatfuelApiUrl,
headers: {
'Content-Type': 'application/json',
},
json: true,
};

return requestPromise.post(requestOptions);
return makeRequest(chatfuelApiUrl);
};

module.exports = broadcast;
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,13 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.9.1.tgz#7e33d8f7d449b3f673cd72deb9abdc552dbe528e"
integrity sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug==

axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"

babel-plugin-dynamic-import-node@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.0.tgz#f00f507bdaa3c3e3ff6e7e5e98d90a7acab96f7f"
Expand Down Expand Up @@ -1380,6 +1387,13 @@ debug@3.2.6:
dependencies:
ms "^2.1.1"

debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

debug@^2.2.0, debug@^2.3.3, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -1873,6 +1887,13 @@ flatted@^2.0.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.1.tgz#69e57caa8f0eacbc281d2e2cb458d46fdb449e08"
integrity sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==

follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"

for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down

0 comments on commit 14f79b2

Please sign in to comment.