Skip to content

Commit

Permalink
Merge pull request #4 from DavidM42/master
Browse files Browse the repository at this point in the history
Adding of option for other untis subdomain servers and customTimetable method
  • Loading branch information
tuesd4y authored Aug 20, 2017
2 parents 22bedb4 + bdf0256 commit d311cff
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 35 deletions.
54 changes: 49 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ A simple interface for accessing the webuntis API
`npm init`
* install the api

`npm install --save untis-api`
`npm install --save https://github.com/DavidM42/node-webuntis.git`

* write something like this in your main.js

```js
const w = require("untis-api");
const e = w.entities;

w.connect("username", "password", "school").then(() => {
w.connect("username", "password", "school", "borys").then(() => {
w.getTeachers().then(teachers => {
console.log(teachers);
});
Expand All @@ -39,16 +39,17 @@ A __Promise__ that is either resolved if the connection was successful or reject

#### Usage
```js
w.connect("username", "password", "school");
w.connect("username", "password", "school", "servername");
```
#### Parameters
* Username, Password and Schoolname as Strings
* Username, Password, Schoolname and Servername as Strings
* An object containing the login information in the following structure
```json
{
"schoolName": "school",
"username": "username",
"password": "password"
"password": "password",
"servername": "borys",
}
```

Expand Down Expand Up @@ -341,6 +342,49 @@ A lot of data, better see for yourself...

***

## getCustomTimetable

#### Usage

```js
TimeTableEntity = require("untis-api").Entities.TimeTableEntity;

w.getTimetable(new TimeTableEntity(1, 5), {
showSubstText: true,
showLsText: true,
klasseFields: ["name"],
roomFields: ["name"],
subjectFields: ["name"]
}).then(data => console.log(data));
```
#### Parameters
`getTimetable(tte, paramsObject)`
* tte: a TimeTableEntity containing the person whose timetable you want to watch's id and personType
* paramsObject: a Object with all custom parameters as properties of this object with their respective value

##### Valid properties:
- startDate: number, format: YYYYMMDD, optional (default: monday of this week)
- endDate: number, format: YYYYMMDD, optional (default: friday of this week)
- showBooking: boolean, returns the period's booking info if available (default: false)
- showInfo: boolean, returns the period information if available (default: false)
- showSubstText: boolean, returns the Untis substitution text if available (default: false)
- showLsText: boolean, returns the text of the period's lesson (default: false)
- showLsNumber: boolean, returns the number of the period's lesson (default: false)
- showStudentgroup: boolean, returns the name(s) of the studentgroup(s) (default: false)
- klasseFields: array, optional, values: „id“, „name“, „longname“, „externalkey“
- roomFields: array, optional, values: „id“, „name“, „longname“, „externalkey“
- subjectFields: array, optional, values: „id“, „name“, „longname“, „externalkey“
- teacherFields: array, optional, values: „id“, „name“, „longname“, „externalkey“


If a Fields parameter (e.g. „teacherFields“) is not set explicitly, the return object will contain the
internal id („keyType“: „id“) of the references (e.g. the teachers of a period) by default.

#### Returns
A lot of data, better see for yourself...

***

## searchPerson

#### Usage
Expand Down
2 changes: 1 addition & 1 deletion entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 12.02.2017
*
* ---------------------------------------------------------------------------- */

"use strict";
const isNumber = require("util").isNumber;

class TimeTableEntity {
Expand Down
20 changes: 0 additions & 20 deletions index.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "untis-api",
"version": "1.0.0",
"description": "a simple api for webuntis",
"main": "index.js",
"main": "webuntis.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
13 changes: 8 additions & 5 deletions rpcWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 12.02.2017 (tuesd4y) :: updated
*
* ---------------------------------------------------------------------------- */

"use strict";
const _ = require("lodash"),
https = require("https"),
extend = require("util")._extend;
Expand Down Expand Up @@ -54,6 +54,7 @@ let config = {
url: null,
userName: null,
password: null,
servername: null,
};

let info = {};
Expand All @@ -76,18 +77,19 @@ function url(schoolname) {


function setupWithObject(options) {
if(options.servername) config.servername = options.servername;
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) {
function setup(username, password, schoolName, servername) {
config.userName = username;
config.password = password;
config.url = url(schoolName);

config.servername = servername;
return connect();
}

Expand All @@ -111,9 +113,11 @@ function rpc(method, params, cb) {
}


let concatHostname = config.servername + ".webuntis.com";

let options = {
method: "POST",
hostname: "mese.webuntis.com",
hostname: concatHostname,
path: u,
headers: headers,
};
Expand Down Expand Up @@ -170,4 +174,3 @@ module.exports = {
loggedIn: _loggedIn,
logOut: disconnect,
};

71 changes: 68 additions & 3 deletions webuntis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 15.02.2017 (tuesd4y) :: edited syntax and export statements
*
* ---------------------------------------------------------------------------- */

"use strict";
const isDate = require("util").isDate,
isUndefined = require("util").isUndefined,
moment = require("moment"),
Expand All @@ -21,12 +21,12 @@ const TIME_FORMAT = "hhmm";

let connectPromise = null;

function connect(creds, password, schoolname) {
function connect(creds, password, schoolname, servername) {
if(isObject(creds)) {
return connectPromise = rpc.setupWithObject(creds)
}

else return connectPromise = rpc.setup(creds, password, schoolname)
else return connectPromise = rpc.setup(creds, password, schoolname, servername)
}

function getTeachers() {
Expand Down Expand Up @@ -188,6 +188,70 @@ function getTimetable(timeTableEntity, dateInWeek) {
});
}

function getCustomTimetable(timeTableEntity, paramsObject,dateInWeek) {
return new Promise((resolve, reject) => {
if (isUndefined(timeTableEntity)) {
throw Error("please pass a timeTableEntity");
}

if (isUndefined(dateInWeek) || !isDate(dateInWeek)) {
dateInWeek = moment();
}

let startDate = dateInWeek.subtract(dateInWeek.day(), 'days').add(1, 'days');

let endDate = startDate.clone().add(5, 'days');

let optionsObject = {
element: {
id: timeTableEntity.id,
type: timeTableEntity.type
},
startDate: parseInt(startDate.format(DATE_FORMAT)),
endDate: parseInt(endDate.format(DATE_FORMAT)),
};

let validOptions = [
"startDate",
"endDate",
"showBooking",
"showInfo",
"showSubstText",
"showLsText",
"showLsNumber",
"showStudentgroup",
"klasseFields",
"roomFields",
"subjectFields",
"teacherFields",
];

for (var property in paramsObject) {
if (paramsObject.hasOwnProperty(property)) {

if (validOptions.indexOf(property) > -1){
optionsObject[property] = paramsObject[property];
}
else {
throw Error("Option: " + property + " is not a valid option for this method");
}

}
}

connectPromise.then(rpc.rpc("getTimetable", {
options: optionsObject,

}, data => {
if (data.error) {
throw Error(data.error);
} else {
resolve(data.result);
}
}))
});
}

function getClassRegisterForPeriod(timeTableId) {
return new Promise((resolve, reject) => {
if(!isNumber(timeTableId)){
Expand Down Expand Up @@ -280,6 +344,7 @@ module.exports = {
getCurrentSchoolYear,
getSchoolYears,
getTimetable,
getCustomTimetable,
getClassRegisterForPeriod,
getLastImportTime,
searchPerson,
Expand Down

0 comments on commit d311cff

Please sign in to comment.