-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (104 loc) · 4.17 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var _ = require('underscore');
var request = require('request');
var Promise = require('bluebird');
var Dotloop = function(authToken) {
this.authToken = authToken;
}
Dotloop.prototype.sendGetRequest = function(path, options, callback) {
if (_.isFunction(options)) {
callback = options;
options = {};
}
options = options || {};
var deferred = Promise.pending();
var url = 'https://www.dotloop.com/my/api/v1_0' + path;
var encoding = undefined;
if (!_.isUndefined(options.encoding)) {
encoding = options.encoding;
delete options.encoding;
}
if (!_.isEmpty(options)) {
var qp = _.pairs(options).map(function(pair) {
return pair[0] + '=' + encodeURIComponent(pair[1]);
}).join('&');
url = url + '?' + qp;
};
var reqOptions = {
url: url,
method: 'GET',
json: true,
headers: {
'Authorization': 'Bearer ' + this.authToken
}
}
if (!_.isUndefined(encoding)) {
reqOptions.encoding = encoding;
}
request(reqOptions, function(err, response, body) {
if (err) {
callback && callback(err);
deferred.reject(err);
} else if (!err && (response.statusCode < 200 || response.statusCode >= 300) && body) {
callback && callback(new Error(body));
deferred.reject(new Error(body));
} else {
try {
if (_.isString(body)) {
body = JSON.parse(body);
}
callback && callback(null, body);
deferred.resolve(body);
} catch (err) {
callback && callback(err);
deferred.reject(err);
}
}
});
return deferred.promise;
}
Dotloop.prototype.getProfiles = function(callback) {
return this.sendGetRequest('/profile', {}, callback);
}
Dotloop.prototype.getEmployees = function(profileId, options, callback) {
return this.sendGetRequest('/profile/' + profileId + '/employee', options, callback);
}
Dotloop.prototype.getProfileAdmins = function(profileId, options, callback) {
return this.sendGetRequest('/profile/' + profileId + '/admin', options, callback);
}
Dotloop.prototype.getContacts = function(profileId, options, callback) {
return this.sendGetRequest('/profile/' + profileId + '/person', options, callback);
}
Dotloop.prototype.getContactDetail = function(profileId, contactId, callback) {
return this.sendGetRequest('/profile/' + profileId + '/person/' + contactId, {}, callback);
}
Dotloop.prototype.getLoops = function(profileId, options, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop', options, callback);
}
Dotloop.prototype.getLoopSummary = function(profileId, loopViewId, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId, {}, callback);
}
Dotloop.prototype.getLoopDetails = function(profileId, loopViewId, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId + '/detail', {}, callback);
}
Dotloop.prototype.getLoopTasks = function(profileId, loopViewId, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId + '/task', {}, callback);
}
Dotloop.prototype.getLoopFolders = function(profileId, loopViewId, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId + '/folder', {}, callback);
}
Dotloop.prototype.getLoopDocuments = function(profileId, loopViewId, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId + '/document', {}, callback);
}
Dotloop.prototype.getLoopDocument = function(profileId, loopViewId, documentId, documentName, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId + '/document/' + documentId + '/' + documentName + '.pdf', {encoding: null}, callback);
}
Dotloop.prototype.getDocumentActivities = function(profileId, documentId, options, callback) {
return this.sendGetRequest('/profile/' + profileId + '/document/' + documentId + '/activity', options, callback);
}
Dotloop.prototype.getLoopParticipants = function(profileId, loopViewId, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId + '/participant', {}, callback);
}
Dotloop.prototype.getLoopActivities = function(profileId, loopViewId, options, callback) {
return this.sendGetRequest('/profile/' + profileId + '/loop/' + loopViewId + '/activity', options, callback);
}
module.exports = Dotloop;