-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
68 lines (63 loc) · 3.23 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
const soap = require("strong-soap").soap;
const PowerSchoolUser = require("./models/PowerSchoolUser");
/** The main PowerSchool API wrapper, for logging into user accounts and caching of retrieved info. */
class PowerSchoolAPI {
/**
* Create an API wrapper.
* @param {string} url - The main URL of the PowerSchool installation, such as "http://sales.powerschool.com".
* @param {string} [apiUsername] - The API username to use for logging in, if your installation has a different one. For most installations, the default provided value should work.
* @param {string} [apiPassword] - The API password to use for logging in, if your installation has a different one. For most installations, the default provided value should work.
*/
constructor(url, apiUsername = "pearson", apiPassword = "m0bApP5") {
this.url = url;
this.apiUsername = apiUsername;
this.apiPassword = apiPassword;
this.ready = false;
this.errored = false;
this.requestOptions = { auth: { user: apiUsername, pass: apiPassword, sendImmediately: false } };
this._cachedInfo = {};
}
/**
* Setup the API wrapper for usage (required for any interaction).
* @return {Promise.<PowerSchoolAPI, Error>} - A promise that returns the API again if resolved, or an Error if rejected.
*/
setup() {
const publicPortalServiceURL = this.url + "/pearson-rest/services/PublicPortalServiceJSON";
return new Promise((resolve, reject) => {
soap.createClient(publicPortalServiceURL + "?wsdl", { wsdl_options: this.requestOptions }, (err, client) => {
if(!client) {
this.errored = true;
return reject(err);
}
this.ready = true;
client.setEndpoint(publicPortalServiceURL);
this.client = client;
resolve(this);
});
})
}
/**
* Log into a user's account and get their user object.
* @param {string} username - The username of the account to log in to.
* @param {string} password - The password of the account to log in to.
* @return {Promise.<PowerSchoolUser, Error>} - A promise that resolves with the user if login was successful, resolves to null if invalid credentials were provided, and rejects with an Error if another error occurred during login.
*/
login(username, password) {
return new Promise((resolve, reject) => {
if(!this.ready) reject(null);
this.client.loginToPublicPortal({username: username, password: password}, this.requestOptions, (err, result) => {
if(!result || !result.return) return reject(err);
if(!result.return.userSessionVO) return resolve(null);
var user = new PowerSchoolUser(result.return.userSessionVO, this);
resolve(user);
});
});
}
// - Cached Info
storeCacheInfo(dataArray, dataType, idKey = "id") {
if(!Array.isArray(dataArray)) dataArray = [dataArray];
if(!this._cachedInfo[dataType]) this._cachedInfo[dataType] = {};
dataArray.forEach((item) => this._cachedInfo[dataType][item[idKey]] = item);
}
}
module.exports = PowerSchoolAPI;