Skip to content

Commit ffe694c

Browse files
author
Michael Townsend
committed
Initial import of client - scales not tested
0 parents  commit ffe694c

14 files changed

+630
-0
lines changed

.gitignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directories
27+
node_modules
28+
jspm_packages
29+
30+
# Optional npm cache directory
31+
.npm
32+
33+
# Optional REPL history
34+
.node_repl_history

README.md

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# PrintNode-NodeJS
2+
3+
Cloud Printing wrapper for the [PrintNode](https://www.printnode.com) API.
4+
5+
## Installation
6+
7+
Install dependencies
8+
```
9+
npm install printnode-client
10+
```
11+
12+
Require and instantiate a client:
13+
14+
```
15+
var PrintNodeClient = require('printnode');
16+
var client = new PrintNodeClient({ api_key: "DUMMY_API_KEY", default_printer_id: 123456 });
17+
```
18+
19+
## Usage
20+
### [Account Information](https://www.printnode.com/docs/api/curl/#account_information)
21+
22+
```
23+
// fetch metadata about account
24+
client.whoAmI().then(console.log);
25+
26+
// fetch account credits
27+
client.credits().then(console.log);
28+
```
29+
30+
### [Computers](https://www.printnode.com/docs/api/curl/#computers)
31+
32+
```
33+
// fetch computers
34+
client.fetchComputers().then(...);
35+
```
36+
37+
### [Printers](https://www.printnode.com/docs/api/curl/#printers)
38+
39+
```
40+
// Fetch printers
41+
client.fetchPrinters().then(...);
42+
43+
// Fetch printers for a specific computer
44+
client.fetchPrinters(computer_id).then(...);
45+
46+
// Fetch a specific printer (returns array of one)
47+
client.fetchPrinters(printer_id).then(...);
48+
49+
// Fetch a specific printer from a computer
50+
client.fetchPrinters(computer_id, printer_id).then(...);
51+
```
52+
53+
### [PrintJobs](https://www.printnode.com/docs/api/curl/#printjobs)
54+
55+
#### Fetching:
56+
```
57+
// Fetch all print jobs
58+
client.fetchPrintJob().then(...);
59+
60+
// Fetch a specific print jobs (returns array of one)
61+
client.fetchPrintJob(printjob_id).then(...);
62+
63+
// Fetch all print jobs for a specific printer
64+
client.fetchPrintJobsForPrinter(printer_id).then(...);
65+
66+
// Fetch a specific printer from a printer (returns array of one)
67+
client.fetchPrintJobForPrinter(printer_id, printjob_id).then(...);
68+
```
69+
70+
#### Creating
71+
[API documentation has full options for PrintJob creation](https://www.printnode.com/docs/api/curl/#printjobs)
72+
```
73+
// Create a print job
74+
var options = {
75+
title: "Printing example 1",
76+
source: "PrintNode-NodeJS", // defaults to this
77+
content: "https://app.printnode.com/testpdfs/4x6_combo_vertical_ol829.pdf",
78+
contentType: "pdf_uri"
79+
};
80+
client.createPrintJob(options).then(console.log); // returns printjob id only
81+
82+
// Create a print job from a local PDF file
83+
var options = {
84+
title: "Printing example 2",
85+
filename: "./test/examples/label.pdf"
86+
};
87+
client.createPrintJobFromPDF().then(...);
88+
89+
90+
// Create a print job from a local raw print file
91+
var options = {
92+
title: "Printing example 2",
93+
filename: "./test/examples/label.pdf"
94+
};
95+
client.createPrintJobFromRaw().then(...);
96+
97+
```
98+
99+
### [Scales](https://www.printnode.com/docs/api/curl/#scales)
100+
101+
```
102+
// Fetch all print jobs
103+
client.fetchPrintJob().then(...);
104+
105+
// Fetch a specific print jobs (returns array of one)
106+
client.fetchPrintJob(printjob_id).then(...);
107+
108+
// Fetch all print jobs for a specific printer
109+
client.fetchPrintJobsForPrinter(printer_id).then(...);
110+
111+
// Fetch a specific printer from a printer (returns array of one)
112+
client.fetchPrintJobForPrinter(printer_id, printjob_id).then(...);
113+
```
114+
115+
## Testing
116+
117+
The tests in place at the moment are simply testing for positive responses, they are not looking at the detail of the response.
118+
119+
Requirements:
120+
- a live account. This should be setup on [printnode.com](https://printnode.com)
121+
- the printnode client to be installed and logged in on that computer
122+
- at least one active printer to be available on that computer, you can use a pdf printer to avoid printing during testing [CutePDF](http://cutepdf.com/)
123+
124+
Running the tests
125+
126+
1. Change the API_KEY and ACTIVE_PRINTER_ID
127+
2. Run `npm test`

index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var _ = require('lodash');
2+
3+
var client = require('./lib/client');
4+
5+
_.assign(client.prototype, require('./lib/account-information'));
6+
// _.assign(client.prototype, require('./lib/account-management'));
7+
_.assign(client.prototype, require('./lib/computers'));
8+
_.assign(client.prototype, require('./lib/printers'));
9+
_.assign(client.prototype, require('./lib/printjobs'));
10+
_.assign(client.prototype, require('./lib/scales'));
11+
12+
module.exports = client;

lib/account-information.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** @mixin Account */
2+
var Account = {
3+
4+
/** Fetch metadata about PrintNode account */
5+
whoami: function () {
6+
return this._getJSON('whoami');
7+
},
8+
9+
/** Fetch print credits available to PrintNode account */
10+
credits: function () {
11+
return this._getJSON('credits');
12+
}
13+
14+
};
15+
16+
module.exports = Account;
17+

lib/client.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var request = require('request');
2+
var _ = require('lodash');
3+
4+
/**
5+
* Instantiate a client
6+
*
7+
* @class PrintNodeClient
8+
* @mixes Account
9+
* @mixes Computers
10+
* @mixes Printers
11+
* @mixes PrintJobs
12+
* @mixes Scales
13+
*/
14+
var PrintNodeClient = function (options) {
15+
if( !options || !options.api_key ) {
16+
throw new Error('Must instantiate PrintNodeClient with minimum of: { api_key: "#API-KEY#" }');
17+
}
18+
19+
this.api_key = options.api_key;
20+
this.default_printer_id = options.default_printer_id;
21+
this.version = "~3";
22+
this.base_url = "https://" + this.api_key + ":@api.printnode.com/";
23+
};
24+
25+
PrintNodeClient.prototype._getJSON = function (path) {
26+
return this._makeRequest(path);
27+
};
28+
29+
PrintNodeClient.prototype._postJSON = function (path, body) {
30+
return this._makeRequest(path, {
31+
method: "POST",
32+
body: body
33+
});
34+
};
35+
36+
PrintNodeClient.prototype._makeRequest = function (path, options) {
37+
var defaults = {
38+
url: this.base_url + path,
39+
json: true
40+
};
41+
42+
var xhr_options = _.defaults(options || {}, defaults);
43+
44+
return new Promise(function (resolve, reject) {
45+
request(xhr_options, function (err, response, body) {
46+
if (err) {
47+
reject(err);
48+
} else if (!(/^2/.test('' + response.statusCode))) { // Status Codes other than 2xx
49+
var message = body && body.message ? body.message : body;
50+
reject(new Error(response.statusCode + " - " + message));
51+
} else {
52+
resolve(body);
53+
}
54+
});
55+
});
56+
};
57+
58+
module.exports = PrintNodeClient;

lib/computers.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/** @mixin Computers */
2+
var Computers = {
3+
/**
4+
* Retrieves an array of computers associated with this client / account.
5+
*
6+
* ```
7+
* @for PrintNodeClient
8+
* @return {promise} response - promise which fulfils with an array of computers:
9+
*
10+
* [{
11+
* "id": 11,
12+
* "name": "AnalyticalEngine",
13+
* "inet": null,
14+
* "inet6": null,
15+
* "hostname": null,
16+
* "version": null,
17+
* "jre": null,
18+
* "createTimestamp": "2015-06-28T18:29:19.871Z",
19+
* "state": "disconnected"
20+
* }]
21+
* ```
22+
*/
23+
fetchComputers: function () {
24+
return this._getJSON('computers');
25+
},
26+
};
27+
28+
module.exports = Computers;

lib/printers.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var _ = require('lodash');
2+
3+
/** @mixin Printers */
4+
var Printers = {
5+
6+
/**
7+
* Retrieves an array of computers associated with this client / account.
8+
*/
9+
fetchPrinters: function () {
10+
return this._getJSON('printers');
11+
},
12+
13+
/**
14+
* Retrieves the printer for the specified id.
15+
*/
16+
fetchPrinter: function (printer_id) {
17+
return this._getJSON('printers/' + printer_id);
18+
},
19+
20+
/**
21+
* Retrieves all printers for a single computer.
22+
*/
23+
fetchPrintersForComputer: function (computer_id) {
24+
return this._getJSON('computers/' + computer_id + '/printers');
25+
},
26+
27+
/**
28+
* Retrieves a single printer from the specified computer.
29+
*/
30+
fetchPrinterForComputer: function (computer_id, printer_id) {
31+
return this._getJSON('computers/' + computer_id + '/printers/' + printer_id);
32+
}
33+
};
34+
35+
module.exports = Printers;

0 commit comments

Comments
 (0)