Skip to content

Commit

Permalink
✨ a few syntax changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stoez committed Feb 14, 2017
1 parent 6aa2229 commit 11f7e48
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
index.js
credentials.json
22 changes: 3 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,13 @@
* ---------------------------------------------------------------------------- */

const w = require("./webuntis");
const e = require("./entities.js");
const e = w.entities;

w.connectPromise.then(() => {
w.connect(require('./credentials.json')).then(() => {
w.getTimetable(new e.TimeTableEntity(w.info.personId, w.info.personType))
.then(data => console.log(data));

w.getClasses()
.then(data => console.log(data));

w.getRooms()
.then(data => console.log(data));

w.getSubjects()
.then(data => console.log(data));

w.getStatusData()
.then(data => console.log(data));

w.getHolidays()
.then(data => console.log(data));
.then(d => console.log(d));

w.logOut();
});



6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "untis-api",
"version": "1.0.0",
"description": "a sime",
"description": "a simple api for webuntis",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "tuesd4y",
"author": "tuesd4y <[email protected]>",
"license": "ISC",
"dependencies": {
"enumify": "^1.0.4",
"lodash": "^4.17.4",
"moment": "^2.17.1",
"rxjs": "^5.1.0"°
"rxjs": "^5.1.0"
}
}
101 changes: 59 additions & 42 deletions rpcWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,69 +79,86 @@ function setupWithObject(options) {
if(options.schoolName) config.url = url(options.schoolName);
if(options.username) config.userName = options.username;
if(options.password) config.password = options.password;

return connect();
}

function setup(username, password, schoolName) {
config.userName = username;
config.password = password;
config.url = url(schoolName)
config.url = url(schoolName);

return connect();
}


function rpc(method, params, cb) {
if(_loggedIn || method === authMethod) {
return new Promise((resolve, reject) => {
if(_loggedIn || method === authMethod) {

let body = JSON.stringify(generateParams(method, params));
let body = JSON.stringify(generateParams(method, params));

let headers = {
"Content-Type": "application/json",
"Content-Length": `${body.length}`
};
let headers = {
"Content-Type": "application/json",
"Content-Length": `${body.length}`
};

let u = config.url;
let u = config.url;

if (info.sessionId && info.sessionId !== null) {
headers["Cookie"] = `JSESSIONID=${info.sessionId}`;
u = baseUrl;
}
if (info.sessionId && info.sessionId !== null) {
headers["Cookie"] = `JSESSIONID=${info.sessionId}`;
u = baseUrl;
}


let options = {
method: "POST",
hostname: "mese.webuntis.com",
path: u,
headers: headers,
};
let options = {
method: "POST",
hostname: "mese.webuntis.com",
path: u,
headers: headers,
};

return httpRequest(options, body)
.then(cb)
} else {
return Promise.reject();
}
return httpRequest(options, body)
.then(cb)
} else {
return reject("not authenticated");
}
});
}

function connect() {
return rpc(authMethod, {
user: config.userName,
client:"MY CLIENT",
password: config.password
},
(data) => {
let result = data.result;

info = extend(info, result);
if(info.sessionId) {
_loggedIn = true;
console.log("connected!");
}
});
return new Promise((resolve, reject) => {
rpc(authMethod, {
user: config.userName,
client: "MY CLIENT",
password: config.password
},
(data) => {
try {
let result = data.result;

info = extend(info, result);
if (info.sessionId) {
_loggedIn = true;
console.log("connected!");
resolve(info)
}
else reject("no sessionId returned")
}
catch(error) {
reject(error);
}
});
});
}

function disconnect() {
return rpc("logout", {}, data => {
_loggedIn = false;
console.log("disconnected!");
});
return new Promise((resolve, reject) => {rpc("logout", {}, data => {
_loggedIn = false;
console.log("disconnected!");
resolve(data)
});
})
}

module.exports = {
Expand All @@ -151,6 +168,6 @@ module.exports = {
setup: setup,
setupWithObject: setupWithObject,
loggedIn: _loggedIn,
logOut: disconnect(),
logOut: disconnect,
};

Loading

0 comments on commit 11f7e48

Please sign in to comment.