-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathclient.js
More file actions
612 lines (551 loc) · 18.3 KB
/
Copy pathclient.js
File metadata and controls
612 lines (551 loc) · 18.3 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
var querystring = require("querystring");
var oauth = require("oauth");
/**
* @callback client~connectCallback
* @param {(string|null)} error - error message or null
* @param {(string|null)} authentication_url - the oauth authentication url or null
*/
/**
* @callback client~verifyCallback
* @param {(string|null)} error - error message or null
*/
/**
* @callback client~resultsCallback
* @param {(string|null)} error - error message or null
* @param {(object|null)} results - the results from the api call
*/
/**
* Represents an API Client
*
* @constructor
* @param {object} [options={}] - setup options
* @param {client~connectCallback} [callback] - call client.connect immediately after setup, passing it callback
*/
var client = function(options, callback){
if(!(this instanceof client)){
return new client(options, callback);
}
if(typeof(options) == "function"){
callback = options;
options = {};
}
options = options || {};
this.consumerKey = options.consumerKey;
this.consumerSecret = options.consumerSecret;
this.oauthToken = options.oauthToken || null;
this.oauthSecret = options.oauthSecret || null;
this.authorizationCallback = options.authorizationCallback || null;
this.baseUrl = options.baseUrl || "https://api.shapeways.com";
this.apiVersion = options.apiVersion || "v1";
// Remove trailing slash from baseUrl
this.baseUrl = this.baseUrl.replace(/\/$/, "");
this.connection = new oauth.OAuth(
this.url("/oauth1/request_token/"),
this.url("/oauth1/access_token/"),
this.consumerKey,
this.consumerSecret,
"1.0A",
this.authorizationCallback,
"HMAC-SHA1"
);
if(typeof(callback) == "function"){
this.connect(callback);
}
};
/**
* Build a full API url from the portion that is given
*
* @method url
* @memberof client
* @instance client
* @param {string} path - the api path portion to build
* @param {object} [params] - Object of query string parameters to use
* @return {string} Returns full API url from path and params
**/
client.prototype.url = function(path, params){
if(path.indexOf("/") !== 0){
path = "/" + path;
}
if(path.indexOf("/", path.length - 1) === -1){
path += "/";
}
var url = this.baseUrl + path + this.apiVersion;
if(params){
url += "?" + querystring.stringify(params);
}
return url;
};
/**
* Connect to API server and get OAuth request token and authentication url
*
* @method connect
* @memberof client
* @instance client
* @param {client~connectCallback} [callback] - function to call when finished
**/
client.prototype.connect = function(callback){
var self = this;
if(typeof(callback) != "function"){
callback = function(){};
}
this.connection.getOAuthRequestToken(function(error, token, secret, results){
if(error){
return callback(error, null);
}
self.oauthToken = token;
self.oauthSecret = secret;
callback(null, results.authentication_url);
});
};
/**
* Parse the required parameters from OAuth authentication response url and
* pass to client.verify
*
* @method verifyUrl
* @memberof client
* @instance client
* @param {client~verifyCallback} [callback] - function to call when finished
**/
client.prototype.verifyUrl = function(url, callback){
var query = querystring.parse(url.split("?", 2)[1]);
return this.verify(query.oauth_token, query.oauth_verifier, callback);
};
/**
* Request an OAuth access token with the provided oauth_token and oauth_verifier parameters
*
* @method verify
* @memberof client
* @instance client
*
* @param {string} token - oauth_token from authentication response query string
* @param {string} verifier - oauth_verifier from authentication response query string
* @param {client~verifyCallback} [callback] - function to call when finished
**/
client.prototype.verify = function(token, verifier, callback){
if(typeof(callback) != "function"){
callback = function(){};
}
var self = this;
this.connection.getOAuthAccessToken(token, this.oauthSecret, verifier, function(error, token, secret, results){
if(error){
return callback(error);
}
self.oauthToken = token;
self.oauthSecret = secret;
callback(null);
});
};
/**
* Make a HTTP GET request to the url using the OAuth credentials
*
* @method get
* @memberof client
* @instance client
*
* @param {string} url - the url to make the request to
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.get = function(url, callback){
if(!callback){
callback = function(){};
}
if(!this.oauthToken){
return callback("no oauth token, make sure to call client.connect and client.verify");
}
this.connection.get(url, this.oauthToken, this.oauthSecret, function(error, data, result){
if(error){
return callback(error);
}
callback(null, JSON.parse(data));
});
};
/**
* Make a HTTP DELETE request to the url using the OAuth credentials
*
* @method delete
* @memberof client
* @instance client
*
* @param {string} url - the url to make the request to
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.delete = function(url, callback){
if(!callback){
callback = function(){};
}
if(!this.oauthToken){
return callback("no oauth token, make sure to call client.connect and client.verify");
}
this.connection.delete(url, this.oauthToken, this.oauthSecret, function(error, data, result){
if(error){
return callback(error);
}
callback(null, JSON.parse(data));
});
};
/**
* Make a HTTP POST request to the url using the OAuth credentials
*
* @method post
* @memberof client
* @instance client
*
* @param {string} url - the url to make the request to
* @param {string} [body] - the POST body data to send with the request
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.post = function(url, body, callback){
if(typeof(body) == "function"){
callback = body;
body = null;
}
if(!callback){
callback = function(){};
}
if(!this.oauthToken){
return callback("no oauth token, make sure to call client.connect and client.verify");
}
this.connection.post(url, this.oauthToken, this.oauthSecret, body, 'application/json', function(error, data, result){
if(error){
return callback(error);
}
callback(null, JSON.parse(data));
});
};
/**
* Make a HTTP PUT request to the url using the OAuth credentials
*
* @method put
* @memberof client
* @instance client
*
* @param {string} url - the url to make the request to
* @param {string} [body] - the PUT body data to send with the request
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.put = function(url, body, callback){
if(typeof(body) == "function"){
callback = body;
body = null;
}
if(!callback){
callback = function(){};
}
if(!this.oauthToken){
return callback("no oauth token, make sure to call client.connect and client.verify");
}
this.connection.put(url, this.oauthToken, this.oauthSecret, body, function(error, data, result){
if(error){
return callback(error);
}
callback(null, JSON.parse(data));
});
};
/**
* Make an API call [GET /api/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-api-v1-1}
*
* @method getApiInfo
* @memberof client
* @instance client
*
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getApiInfo = function(callback){
this.get(this.url("/api/"), callback);
};
/**
* Make an API call [GET /orders/cart/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-orders-cart-v1}
*
* @method getCart
* @memberof client
* @instance client
*
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getCart = function(callback){
this.get(this.url("/orders/cart/"), callback);
};
/**
* Make an API call [POST /orders/cart/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#POST_-orders-cart-v1}
*
* @method addToCart
* @memberof client
* @instance client
*
* @param {object} params - parameters to send with the query
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.addToCart = function(params, callback){
if(!params.modelId){
return callback("params is missing required modelId parameter");
}
this.post(this.url("/orders/cart/", params), null, callback);
};
/**
* Make an API call [GET /materials/{materialId}/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-materials-materialId-v1}
*
* @method getMaterial
* @memberof client
* @instance client
*
* @param {number} materialId - material id of the material to get
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getMaterial = function(materialId, callback){
this.get(this.url("/materials/" + materialId), callback);
};
/**
* Make an API call [GET /materials/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-materials-v1}
*
* @method getMaterials
* @memberof client
* @instance client
*
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getMaterials = function(callback){
this.get(this.url("/materials/"), callback);
};
/**
* Make an API call [GET /models/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-models-v1}
*
* @method getModels
* @memberof client
* @instance client
*
* @param {number} [page] - which page to fetch
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getModels = function(page, callback){
var params = {};
if(typeof(page) == "function"){
callback = page;
page = null;
}
if(page){
params = {page: page};
}
this.get(this.url("/models/", params), callback);
};
/**
* Make an API call [GET /models/{modelId}/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-models-modelId-v1}
*
* @method getModel
* @memberof client
* @instance client
*
* @param {number} modelId - the model to fetch
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getModel = function(modelId, callback){
this.get(this.url("/models/" + modelId), callback);
};
/**
* Make an API call [POST /models/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#POST_-models-v1}
*
* The `file` parameter is base64 + URI encoded before sending to server
*
* @method addModel
* @memberof client
* @instance client
*
* @param {object} params - the model data to use
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.addModel = function(params, callback){
var required = ["file", "fileName", "hasRightsToModel", "acceptTermsAndConditions"];
var missing = [];
required.forEach(function(property){
if(!(property in params)){
missing.push(property);
}
});
if(missing.length){
return callback("error, params missing required parameters: " + missing);
}
params.file = this.connection._encodeData(new Buffer(params.file).toString("base64"));
this.post(this.url("/models/"), JSON.stringify(params), callback);
};
/**
* Make an API call [GET /models/{modelId}/info/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-models-modelId-info-v1}
*
* @method getModelInfo
* @memberof client
* @instance client
*
* @param {number} modelId - the model to fetch information for
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getModelInfo = function(modelId, callback){
this.get(this.url("/models/" + modelId + "/info/"), callback);
};
/**
* Make an API call [DELETE /models/{modelId}/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#DELETE_-models-modelId-v1}
*
* @method deleteModel
* @memberof client
* @instance client
*
* @param {number} modelId - the model to delete
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.deleteModel = function(modelId, callback){
this.delete(this.url("/models/" + modelId), callback);
};
/**
* Make an API call [PUT /models/{modelId}/info/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#PUT_-models-modelId-info-v1}
*
* @method updateModelInfo
* @memberof client
* @instance client
*
* @param {number} modelId - the model to update
* @param {object} params - model data to update with
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.updateModelInfo = function(modelId, params, callback){
this.put(this.url("/models/" + modelId), JSON.stringify(params), callback);
};
/**
* Make an API call [GET /models/{modelId}/files/{fileVersion}/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-models-modelId-files-fileVersion-v1}
*
* @method getModelFile
* @memberof client
* @instance client
*
* @param {number} modelId - the model the file belongs to
* @param {int} fileVersion - the version of the file to get
* @param {boolean} [includeFile=false] - whether or not to include the raw file data with the response
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getModelFile = function(modelId, fileVersion, includeFile, callback){
if(typeof(includeFile) == "function"){
callback = includeFile;
includeFile = 0;
}
var params = {
file: +includeFile,
};
this.get(this.url("/models/" + modelId + "/files/" + fileVersion, params), callback);
};
/**
* Make an API call [POST /models/{modelId}/photos/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#POST_-models-modelId-photos-v1}
*
* The `file` parameter is base64 + URI encoded before sending to server
*
* @method addModelPhoto
* @memberof client
* @instance client
*
* @param {number} modelId - the model the photo belongs to
* @param {object} params - the photo information
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.addModelPhoto = function(modelId, params, callback){
if(!("file" in params)){
return callback("error, params missing required property: file");
}
params.file = this.connection._encodeData(new Buffer(params.file).toString("base64"));
this.post(this.url("/models/" + modelId + "/photos/"), JSON.stringify(params), callback);
};
/**
* Make an API call [POST /models/{modelId}/files/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#POST_-models-modelId-files-v1}
*
* The `file` parameter is base64 + URI encoded before sending to server
*
* @method addModelFile
* @memberof client
* @instance client
*
* @param {number} modelId - the model the file belongs to
* @param {object} params - the file information
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.addModelFile = function(modelId, params, callback){
var required = ["file", "fileName", "hasRightsToModel", "acceptTermsAndConditions"];
var missing = [];
required.forEach(function(property){
if(!(property in params)){
missing.push(property);
}
});
if(missing.length){
return callback("error, params missing required parameters: " + missing);
}
params.file = this.connection._encodeData(new Buffer(params.file).toString("base64"));
this.post(this.url("/models/" + modelId + "/files/"), JSON.stringify(params), callback);
};
/**
* Make an API call [GET /printers/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-printers-v1}
*
* @method getPrinters
* @memberof client
* @instance client
*
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getPrinters = function(callback){
this.get(this.url("/printers/"), callback);
};
/**
* Make an API call [GET /printers/{printerId}/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-printers-printerId-v1}
*
* @method getPrinters
* @memberof client
* @instance client
*
* @param {number} printerId - the printer id to get information for
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getPrinter = function(printerId, callback){
this.get(this.url("/printers/" + printerId), callback);
};
/**
* Make an API call [GET /categories/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-categories-v1}
*
* @method getCategories
* @memberof client
* @instance client
*
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getCategories = function(callback){
this.get(this.url("/categories/"), callback);
};
/**
* Make an API call [GET /categories/{categoryId}/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#GET_-categories-categoryId-v1}
*
* @method getCategory
* @memberof client
* @instance client
*
* @param {number} catId - the category id to fetch
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getCategory = function(catId, callback){
this.get(this.url("/categories/" + catId), callback);
};
/**
* Make an API call [POST /price/v1]{@link https://developers.shapeways.com/docs?li=dh_docs#POST_-price-v1}
*
* @method gePrice
* @memberof client
* @instance client
*
* @param {object} params - the pricing information to get pricing for
* @param {client~resultsCallback} [callback] - function to call when finished
**/
client.prototype.getPrice = function(params, callback){
var required = ["volume", "area", "xBoundMin", "xBoundMax", "yBoundMin", "yBoundMax", "zBoundMin", "zBoundMax"];
var missing = [];
required.forEach(function(property){
if(!(property in params)){
missing.push(property);
}
});
if(missing.length){
return callback("error, params missing required properties: " + missing);
}
this.post(this.url("/price/"), JSON.stringify(params), callback);
};
module.exports = client;