forked from ak2consulting/singly-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
79 lines (68 loc) · 2.36 KB
/
lib.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var request = require('request');
var querystring = require('querystring');
var apiBaseUrl = process.env.SINGLY_API_HOST || 'https://api.singly.com';
var API_VERSION = 0;
module.exports = function(clientId, clientSecret, redirectURI) {
var client = {};
client.getAuthorizeURL = function(service, options) {
if (!options.client_id) options.client_id = clientId;
if (!options.redirect_uri) options.redirect_uri = redirectURI;
options.service = service;
var cbURL = options.callbackURL || redirectURI;
return apiBaseUrl + '/oauth/authorize?' + querystring.stringify(options);
}
client.getAccessToken = function(code, callback) {
var data = {
client_id: clientId,
client_secret: clientSecret,
code: code
};
request.post({
uri: apiBaseUrl + '/oauth/access_token',
body: querystring.stringify(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function (err, resp, body) {
if(err) return callback(err, resp, body);
try {
body = JSON.parse(body);
callback(undefined, resp, body);
} catch(parseErr) {
callback(err, resp, body);
}
});
}
// build a consistent and clean URI
function getURI(path) {
var uri = apiBaseUrl;
if (path.indexOf(/\/?v[0-9]/) !== 0) uri += '/v' + API_VERSION
if (path.indexOf('/') !== 0) uri += '/';
return uri + path;
}
function setupAPICall(options) {
if (options.access_token && !(options.qs && options.qs.access_token)) {
if (!options.qs) options.qs = {};
options.qs.access_token = options.access_token;
}
}
client.post = function(path, options, callback) {
var uri = getURI(path);
setupAPICall(options);
request.post({uri:uri, json:options.body, qs:options.qs}, callback);
}
client.get = function(path, options, callback) {
var uri = getURI(path);
setupAPICall(options);
request.get({uri:uri, json:true, qs:options.qs}, callback);
}
client.apiCall = function(path, params, callback) {
console.warn('singly.apiCall is deprecated and will be removed in future verions. ' +
'Please use singly.get and singly.post.');
var uri = apiBaseUrl + path + '?' + querystring.stringify(params);
request.get({uri:uri, json:true}, function(err, resp, json) {
callback(err, json);
});
}
return client;
}