forked from simplyspoke/node-harvest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (135 loc) · 5.21 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var restler = require('restler'),
querystring = require('querystring'),
util = require('util'),
_isUndefined = require('./mixin'),
Harvest;
module.exports = Harvest = function (opts) {
var self = this;
if (_isUndefined(opts,'subdomain')) {
throw new Error('The Harvest API client requires a subdomain');
}
this.use_oauth = (opts.identifier !== undefined &&
opts.secret !== undefined);
this.use_basic_auth = (opts.email !== undefined &&
opts.password !== undefined);
if (!this.use_oauth && !this.use_basic_auth) {
throw new Error('The Harvest API client requires credentials for basic authentication or an identifier and secret for OAuth');
}
this.subdomain = opts.subdomain;
this.host = "https://" + this.subdomain + ".harvestapp.com";
this.email = opts.email;
this.password = opts.password;
this.identifier = opts.identifier;
this.secret = opts.secret;
this.user_agent = opts.user_agent;
this.debug = opts.debug || false;
var restService = restler.service(function (u, p) {
this.defaults.username = u;
this.defaults.password = p;
}, {
baseURL: self.host
}, {
run: function (type, url, data) {
if (self.debug) {
console.log('run', type, url, data);
}
var opts = {};
opts.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
if (data !== 'undefined') {
if (data === 'object') {
opts.headers['Content-Length'] = querystring.stringify(data).length;
} else {
opts.headers['Content-Length'] = data.length;
}
} else {
opts.headers['Content-Length'] = 0;
}
opts.data = data;
switch (type) {
case 'get':
return this.get(url, opts);
case 'post':
return this.post(url, opts);
case 'put':
return this.put(url, opts);
case 'delete':
return this.del(url, opts);
}
return this;
}
});
this.processRequest = function (res, cb) {
if (this.debug) {
console.log('processRequest', cb);
}
if (typeof cb !== "function") {
throw new Error('processRequest: Callback is not defined');
}
res.once('complete', function (data, res) {
var err;
if (self.debug) {
console.log('complete', util.inspect(data, false, 10));
}
err = null;
if (res.statusCode > 399 || data instanceof Error || data === "Authentication failed for API request.") {
err = data;
data = {};
}
cb(err, data);
});
};
this.service = new restService(this.email, this.password);
this.client = {
get: function (url, data, cb) {
self.processRequest(self.service.run('get', url, data), cb);
},
patch: function (url, data, cb) {
self.processRequest(self.service.run('patch', url, data), cb);
},
post: function (url, data, cb) {
self.processRequest(self.service.run('post', url, data), cb);
},
put: function (url, data, cb) {
self.processRequest(self.service.run('put', url, data), cb);
},
delete: function (url, data, cb) {
self.processRequest(self.service.run('delete', url, data), cb);
}
};
var Account = require('./lib/account');
var TimeTracking = require('./lib/time-tracking');
var Clients = require('./lib/clients');
var ClientContacts = require('./lib/client-contacts');
var Projects = require('./lib/projects');
var Tasks = require('./lib/tasks');
var People = require('./lib/people');
var ExpenseCategories = require('./lib/expense-categories');
var Expenses = require('./lib/expenses');
var UserAssignment = require('./lib/user-assignment');
var TaskAssignment = require('./lib/task-assignment');
var Reports = require('./lib/reports');
var Invoices = require('./lib/invoices');
var InvoiceMessages = require('./lib/invoice-messages');
var InvoicePayments = require('./lib/invoice-payments');
var InvoiceCategories = require('./lib/invoice-categories');
this.Account = new Account(this);
this.TimeTracking = new TimeTracking(this);
this.Clients = new Clients(this);
this.ClientContacts = new ClientContacts(this);
this.Projects = new Projects(this);
this.Tasks = new Tasks(this);
this.People = new People(this);
this.ExpenseCategories = new ExpenseCategories(this);
this.Expenses = new Expenses(this);
this.UserAssignment = new UserAssignment(this);
this.TaskAssignment = new TaskAssignment(this);
this.Reports = new Reports(this);
this.Invoices = new Invoices(this);
this.InvoiceMessages = new InvoiceMessages(this);
this.InvoicePayments = new InvoicePayments(this);
this.InvoiceCategories = new InvoiceCategories(this);
return this;
};