forked from cullenjett/quickbase-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
186 lines (148 loc) · 4.81 KB
/
api.js
File metadata and controls
186 lines (148 loc) · 4.81 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
const https = require('https');
const URL = require('url');
class ApiClient {
constructor(config) {
this.config = config;
this.config.password = this.config.password || process.env.QUICKBASE_CLI_PASSWORD;
this.config.username = this.config.username || process.env.QUICKBASE_CLI_USERNAME;
this.config.appToken = this.config.appToken || process.env.QUICKBASE_CLI_APPTOKEN;
this.config.userToken = this.config.userToken || process.env.QUICKBASE_CLI_USERTOKEN;
this.authData = null;
}
uploadPage(pageName, pageText) {
const xmlData = `
<pagebody>${this._handleXMLChars(pageText)}</pagebody>
<pagetype>1</pagetype>
<pagename>${pageName}</pagename>
`;
return new Promise((resolve, reject) => {
this.sendQbRequest('API_AddReplaceDBPage', xmlData).then((response) => {
resolve(response)
}).catch((errorDesc, err) => {
reject(errorDesc, err)
});
});
}
// Private-ish
_handleXMLChars(string) {
if (!string) {
return;
}
return string.replace(/[<>&'"]/g, char => {
switch (char) {
case '<':
return '<';
case '>':
return '>';
case '&':
return '&';
case "'":
return ''';
case '"':
return '"';
}
});
}
authenticateIfNeeded() {
return new Promise((resolve, reject) => {
//Decide here which type of authentication should be done
if (this.config.userToken) {
//Use usertoken
this.authData = `<usertoken>${this.config.userToken}</usertoken>`;
resolve()
} else if (this.config.username && this.config.password) {
//regenerate ticket first, then Use ticket
const dbid = 'main';
const action = "API_Authenticate";
const url = URL.parse(
`https://${this.config.realm}.quickbase.com/db/${dbid}?a=${action}`
);
const options = {
hostname: url.hostname,
path: url.pathname + url.search,
method: 'POST',
headers: {
'Content-Type': 'application/xml',
'QUICKBASE-ACTION': action
}
};
const postData = `
<qdbapi>
<username>${this.config.username}</username>
<password>${this.config.password}</password>
<hours>${this.config.authenticate_hours}</hours>
</qdbapi>`;
const req = https.request(options, res => {
let response = '';
res.setEncoding('utf8');
res.on('data', chunk => (response += chunk));
res.on('end', () => {
const errCode = +response.match(/<errcode>(.*)<\/errcode>/)[1];
if (errCode != 0) {
const errtext = response.match(/<errtext>(.*)<\/errtext>/)[1];
reject(errtext);
} else {
const ticket = response.match(/<ticket>(.*)<\/ticket>/)[1];
this.authData = `<ticket>${ticket}</ticket>`;
if (this.config.appToken) {
this.authData += `<apptoken>${this.config.appToken}</apptoken>`;
}
//Suggest to use ticket now, just validated
resolve();
}
});
});
req.on('error', err => reject('Could not send Authentication request', err));
req.write(postData);
req.end();
} else {
//Error: not enough auth credentials
reject("There are not enough authentication credentials in the config or environment. Please setup a valid username and password.")
}
});
};
async sendQbRequest(action, data, mainAPICall) {
const dbid = mainAPICall ? 'main' : this.config.dbid;
const url = URL.parse(
`https://${this.config.realm}.quickbase.com/db/${dbid}?a=${action}`
);
if (!this.authData) {
reject("You must call `authenticateIfNeeded()` before calling `sendQbRequest`");
return;
}
return new Promise((resolve, reject) => {
const postData = `
<qdbapi>
${this.authData}
${data}
</qdbapi>`;
const options = {
hostname: url.hostname,
path: url.pathname + url.search,
method: 'POST',
headers: {
'Content-Type': 'application/xml',
'QUICKBASE-ACTION': action
}
};
const req = https.request(options, res => {
let response = '';
res.setEncoding('utf8');
res.on('data', chunk => (response += chunk));
res.on('end', () => {
const errCode = +response.match(/<errcode>(.*)<\/errcode>/)[1];
if (errCode != 0) {
reject(response);
} else {
resolve(response);
}
resolve(response);
});
});
req.on('error', err => reject('ERROR:', err));
req.write(postData);
req.end();
});
}
}
module.exports = ApiClient;