-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
52 lines (42 loc) · 1.17 KB
/
client.js
File metadata and controls
52 lines (42 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var crypto = require('crypto') , hash;
var request = require('request');
var qs = require("querystring");
function Client(key, secret) {
this.base_url = 'https://api.heyloyalty.com/loyalty/v1';
this.key = key;
this.secret = secret;
};
Client.prototype.call = function(endpoint, params, method, callback) {
var date = new Date(), time = date.toISOString();
var hash = crypto.createHmac('sha256', this.secret).update(time).digest('hex')
endpoint = endpoint || 'lists';
params = params || {};
method = method || "GET";
callback = callback || function() {};
hash = new Buffer(hash).toString('base64');
var querystring = '';
if(method=='GET') {
querystring = '?'+qs.stringify(params);
}
var options = {
method: method,
url: this.base_url + '/' + endpoint + querystring,
auth: {
'user': this.key,
'pass': hash,
},
headers: {
'X-Request-Timestamp': time,
'User-Agent': 'request'
}
};
if(method=='POST'||method=='PUT') {
options['form'] = params;
}
request(options, callback);
}
List = require('./List.js');
Client.prototype.getListClient = function() {
return new List(this);
}
module.exports = Client;