Skip to content

poc of different providers implementation #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions api/fs-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var fs = require('fs');
/**
* Create directory
*
* @param directory
*/
function mkdirSyncRecursive(directory) {
var path = directory.replace(/\/$/, '').split('/');
for (var i = 1; i <= path.length; i++) {
var segment = path.slice(0, i).join('/');
segment.length > 0 && !fs.existsSync(segment) ? fs.mkdirSync(segment) : null;
}
};

class fsProvider {
constructor(path) {

if (!fs.existsSync(path)) {
mkdirSyncRecursive(path);
}
if (path.substr(-1) !== '/') {
path += '/';
}
this.path = path;
}
getToken(hashName) {
if (fs.existsSync(this.path + hashName)) {
return fs.readFileSync(this.path + hashName, {encoding: 'utf8'});
} else {
return "";
}
}
setToken(hashName, token) {
fs.writeFileSync(this.path + hashName, token);
}
}

module.exports = fsProvider;
14 changes: 14 additions & 0 deletions api/memory-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

class memoryProvider {
constructor() {
this.store = {};
}
getToken(hashName) {
return this.store[hashName] || "";
}
setToken(hashName, token) {
return this.store[hashName] = token;
}
}

module.exports = memoryProvider;
31 changes: 4 additions & 27 deletions api/sendpulse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

var https = require('https');
var crypto = require('crypto');
var fs = require('fs');

var API_URL = 'api.sendpulse.com';
var API_USER_ID = '';
var API_SECRET = '';
var TOKEN_STORAGE = '';
var TOKEN_STORAGE;
var TOKEN = '';

var ERRORS = {
Expand Down Expand Up @@ -49,18 +48,6 @@ function base64(data) {
return b.toString('base64');
}

/**
* Create directory
*
* @param directory
*/
function mkdirSyncRecursive(directory) {
var path = directory.replace(/\/$/, '').split('/');
for (var i = 1; i <= path.length; i++) {
var segment = path.slice(0, i).join('/');
segment.length > 0 && !fs.existsSync(segment) ? fs.mkdirSync(segment) : null;
}
};

/**
* Sendpulse API initialization
Expand All @@ -80,18 +67,8 @@ function init(user_id, secret, storage, callback) {
}
}

if (!fs.existsSync(TOKEN_STORAGE)) {
mkdirSyncRecursive(TOKEN_STORAGE);
}

if (TOKEN_STORAGE.substr(-1) !== '/') {
TOKEN_STORAGE += '/';
}

var hashName = md5(API_USER_ID + '::' + API_SECRET);
if (fs.existsSync(TOKEN_STORAGE + hashName)) {
TOKEN = fs.readFileSync(TOKEN_STORAGE + hashName, {encoding: 'utf8'});
}
TOKEN = TOKEN_STORAGE.getToken(hashName)

if (!TOKEN.length) {
getToken(callback);
Expand Down Expand Up @@ -207,9 +184,9 @@ function getToken(callback) {
return;
}

TOKEN = data.access_token;
TOKEN = data.access_token ?? "";
var hashName = md5(API_USER_ID + '::' + API_SECRET);
fs.writeFileSync(TOKEN_STORAGE + hashName, TOKEN);
TOKEN_STORAGE.setToken(hashName, TOKEN);
callback(TOKEN)
}
}
Expand Down
9 changes: 6 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* https://sendpulse.com/api
*/

var sendpulse = require("sendpulse-api");
var sendpulse = require("./api/sendpulse");
// var memoryProvider = require("./api/memory-provider");
var fsProvider = require("./api/fs-provider");

/*
* https://login.sendpulse.com/settings/#api
Expand All @@ -14,9 +16,10 @@ var sendpulse = require("sendpulse-api");
var API_USER_ID="USER_ID";
var API_SECRET="USER_SECRET";

var TOKEN_STORAGE="/tmp/";
var provider = new fsProvider("./tmp/");
// var provider = new memoryProvider();

sendpulse.init(API_USER_ID, API_SECRET, TOKEN_STORAGE, function(token) {
sendpulse.init(API_USER_ID, API_SECRET, provider, function(token) {
if (token && token.is_error) {
// error handling
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sendpulse-api",
"version": "1.1.7",
"version": "1.1.8",
"description": "A simple SendPulse REST client library and example for Node.js",
"main": "index.js",
"scripts": {
Expand Down