-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpassdock.js
More file actions
130 lines (107 loc) · 3.29 KB
/
passdock.js
File metadata and controls
130 lines (107 loc) · 3.29 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
/*
Name : passdock
Source : https://github.com/fvdm/nodejs-passdock
Feedback : https://github.com/fvdm/nodejs-passdock/issues
License : Unlicense (Public Domain, see LICENSE file)
*/
const { request } = require( 'https' );
const { stringify } = require( 'querystring' );
var app = {
api: {
host: 'api.passdock.com',
path: '/api/v1/',
timeout: 30,
token: '',
},
};
// Communication error
app.buildError = ( reason, response, requestBody ) => {
return {
reason: reason,
response: {
headers: response.headers,
statusCode: response.statusCode,
complete: response.complete,
},
request: {
method: response.req.method,
path: response.req.path,
headers: response.req._headers,
body: requestBody,
finished: response.req.finished,
},
};
};
// Communicate
app.talk = function ( method, path, fields, cb ) {
if ( !cb && typeof fields === 'function' ) {
let cb = fields;
let fields = {};
}
// build request
fields.api_token = app.api.token;
fields = stringify( fields );
const options = {
host: app.api.host,
port: 443,
path: app.api.path + path + ( method === 'GET' ? '?' + fields : '' ),
method: method,
agent: false,
headers: {
Accept: 'application/json',
'User-Agent': 'fvdm/nodejs-passdock',
},
};
if ( method !== 'GET' ) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.headers['Content-Length'] = fields.length;
}
// do request
const req = request( options, response => {
let data = '';
response.on( 'data', d => { data += d; } );
// process response
response.on( 'end', () => {
data = data.toString( 'utf8' ).trim();
if ( response.statusCode >= 500 ) {
// server trouble
cb( data, app.buildError( 'server error', response, fields ) );
} else if ( response.statusCode >= 200 && response.statusCode < 300 ) {
// all good
if ( data.match( /^(\{.*\}|\[.*\])$/ ) ) {
cb( JSON.parse( data ), false );
} else {
cb( data, app.buildError( 'invalid data', response, fields ) );
}
} else {
// API error
cb( data, app.buildError( 'error', response, fields ) );
}
} );
// request cut off
response.on( 'close', err => {
cb( data.toString( 'utf8' ), app.buildError( 'early disconnect', response, fields ) );
} );
} );
req.setTimeout( app.api.timeout * 1000 );
if ( method !== 'GET' ) {
req.write( fields );
}
req.end();
}
// Templates
app.templates = {
list: ( cb ) => app.talk( 'GET', 'templates', cb ),
show: ( id, cb ) => app.talk( 'GET', `templates/${id}`, cb ),
delete: ( id, cb ) => app.talk( 'DELETE', `templates/${id}`, cb ),
};
// Passes
app.passes = {
list: ( tid, cb ) => app.talk( 'GET', `templates/${tid}/passes`, cb ),
show: ( tid, id, cb ) => app.talk( 'GET', `templates/${tid}/passes/${id}`, cb ),
delete: ( tid, id, cb ) => app.talk( 'DELETE', `templates/${tid}/passes/${id}`, cb ),
create: ( tid, pass, cb ) => app.talk( 'POST', `templates/${tid}/passes`, { pass }, cb ),
update: ( tid, pid, pass, cb ) => app.talk( 'PUT', `templates/${tid}/passes/${pid}`, { pass }, cb ),
};
// module magic
module.exports = app;