Skip to content

Remove default xml parser, allow users to use their own libraries. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Node.js Library for interacting with StudentVue portals.

**Important: 1.0.0 release removes built-in xml to javascript object parsing**. Use the library of your choice to parse xml, see notes below

## Installation

Install with `npm install studentvue.js`
Expand All @@ -11,29 +13,61 @@ Install with `npm install studentvue.js`
Logging in and getting messages:

```javascript
const StudentVue = require('studentvue.js');
StudentVue.login('district url', 'username', 'password')
const { login } = require('studentvue.js');
login('district url', 'username', 'password')
.then(client => client.getMessages())
.then(console.log);
```

Getting districts near a zip code:

```javascript
const StudentVue = require('studentvue.js');
StudentVue.getDistrictUrls('zip code').then(console.log);
{
"DistrictLists": {
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
"DistrictInfos": {
"DistrictInfo": [
{"DistrictID":"","Name":"San Francisco Unified School District","Address":"San Francisco CA 94102","PvueURL":"https://portal.sfusd.edu/"}
...
]
}
}
}
const { getDistrictUrls } = require('studentvue.js');
getDistrictUrls('zip code').then(console.log);
```
```xml
<DistrictLists xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DistrictInfos>
<DistrictInfo DistrictID="" Name="Jefferson Elementary School District" Address="Daly City CA 94015" PvueURL="https://ca-jsd.edupoint.com" />
<DistrictInfo DistrictID="" Name="Jefferson Union High School District" Address="Daly City CA 94015" PvueURL="https://genesis.juhsd.net/pxp" />
<DistrictInfo DistrictID="" Name="Key Academy Charter School" Address="Hayward CA 94541" PvueURL="https://ca-kac.edupoint.com/" />
<DistrictInfo DistrictID="" Name="Millbrae School District" Address="Millbrae CA 94030" PvueURL="https://ca-mesd-pvue.edupoint.com" />
<DistrictInfo DistrictID="" Name="Newark Unified School District" Address="Newark CA 94560" PvueURL="https://vue.newarkunified.org/PXP" />
<DistrictInfo DistrictID="" Name="Pacifica School District" Address="Pacifica CA 94044-3042" PvueURL="https://synergy.pacificasd.org/" />
<DistrictInfo DistrictID="" Name="Pleasanton Unified School District" Address="Pleasanton CA 94566" PvueURL="https://ca-pleas-psv.edupoint.com" />
<DistrictInfo DistrictID="" Name="San Francisco Unified School District" Address="San Francisco CA 94102" PvueURL="https://ca-sfu-psv.edupoint.com" />
<DistrictInfo DistrictID="" Name="Tamalpais Union High School District" Address="Larkspur CA 94939" PvueURL="https://ca-tamal-psv.edupoint.com/" />
</DistrictInfos>
</DistrictLists>
```

## New in 1.0.0 - Getting JSON output

install a package such as the reccomended `xml2js`, and utilize the serialize field in `getDistrictUrls` and `login`:

```js
const { getDistrictUrls } = require("./index");
const { parseStringPromise } = require("xml2js");

getDistrictUrls('zip code', parseStringPromise).then(val => {
console.log(val);
})
```
```js
const { login } = require('studentvue.js');
login('district url', 'username', 'password', {}, parseStringPromise)
.then(client => client.getMessages())
.then(console.log);
```
Pass in a function that accepts a string of XML and returns a promise into the serialize field.

**Before 1.0.0** the default xml parser was `xml2json`. The following code should restore previous functionality:

```js
const { toJson } = require("xml2json");
login('district url', 'username', 'password', (xml) => {
return Promise.resolve(toJson(xml));
});
```

## Methods
Expand Down
53 changes: 31 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,82 @@
const soap = require('soap');
const xml2json = require('xml2json');

class StudentVueClient {
constructor(username, password, client) {
// serialize should be a function that takes XML input and returns a promise.
constructor(username, password, client, serialize) {
this.username = username;
this.password = password;

this.client = client;

if (serialize) {
this.serialize = serialize;
} else {
this.serialize = function (data) {
return Promise.resolve(data);
};
}
}

getMessages() {
return this._xmlJsonSerialize(this._makeServiceRequest('GetPXPMessages'));
return this._serialize(this._makeServiceRequest('GetPXPMessages'));
}

getCalendar() {
return this._xmlJsonSerialize(this._makeServiceRequest('StudentCalendar'));
return this._serialize(this._makeServiceRequest('StudentCalendar'));
}

getAttendance() {
return this._xmlJsonSerialize(this._makeServiceRequest('Attendance'));
return this._serialize(this._makeServiceRequest('Attendance'));
}

getGradebook(reportPeriod) {
let params = {};
if (typeof reportPeriod !== 'undefined') {
params.ReportPeriod = reportPeriod;
}
return this._xmlJsonSerialize(this._makeServiceRequest('Gradebook', params));
return this._serialize(this._makeServiceRequest('Gradebook', params));
}

getClassNotes() {
return this._xmlJsonSerialize(this._makeServiceRequest('StudentHWNotes'));
return this._serialize(this._makeServiceRequest('StudentHWNotes'));
}

getStudentInfo() {
return this._xmlJsonSerialize(this._makeServiceRequest('StudentInfo'));
return this._serialize(this._makeServiceRequest('StudentInfo'));
}

getSchedule(termIndex) {
let params = {};
if (typeof termIndex !== 'undefined') {
params.TermIndex = termIndex;
}
return this._xmlJsonSerialize(this._makeServiceRequest('StudentClassList', params));
return this._serialize(this._makeServiceRequest('StudentClassList', params));
}

getSchoolInfo() {
return this._xmlJsonSerialize(this._makeServiceRequest('StudentSchoolInfo'));
return this._serialize(this._makeServiceRequest('StudentSchoolInfo'));
}

listReportCards() {
return this._xmlJsonSerialize(this._makeServiceRequest('GetReportCardInitialData'));
return this._serialize(this._makeServiceRequest('GetReportCardInitialData'));
}

getReportCard(documentGuid) {
return this._xmlJsonSerialize(this._makeServiceRequest('GetReportCardDocumentData', { DocumentGU: documentGuid }));
return this._serialize(this._makeServiceRequest('GetReportCardDocumentData', { DocumentGU: documentGuid }));
}

listDocuments() {
return this._xmlJsonSerialize(this._makeServiceRequest('GetStudentDocumentInitialData'));
return this._serialize(this._makeServiceRequest('GetStudentDocumentInitialData'));
}

getDocument(documentGuid) {
return this._xmlJsonSerialize(this._makeServiceRequest('GetContentOfAttachedDoc', { DocumentGU: documentGuid }));
return this.serialize(this._makeServiceRequest('GetContentOfAttachedDoc', { DocumentGU: documentGuid }));
}

_xmlJsonSerialize(servicePromise) {
return servicePromise.then(result => xml2json.toJson(result[0].ProcessWebServiceRequestResult));
_serialize(servicePromise) {
return servicePromise.then((result) => {
return this.serialize(result[0].ProcessWebServiceRequestResult)
});
}

_makeServiceRequest(methodName, params = {}, serviceHandle = 'PXPWebServices') {
Expand All @@ -90,7 +100,7 @@ class StudentVueClient {
}
}

function login(url, username, password, soapOptions = {}) {
function login(url, username, password, soapOptions = {}, serialize) {
const host = new URL(url).host;
const endpoint = `https://${ host }/Service/PXPCommunication.asmx`;

Expand All @@ -100,19 +110,18 @@ function login(url, username, password, soapOptions = {}) {
}, soapOptions);

const wsdlURL = endpoint + '?WSDL';

return soap.createClientAsync(wsdlURL, resolvedOptions)
.then(client => new StudentVueClient(username, password, client));
.then(client => new StudentVueClient(username, password, client, serialize));
}

function getDistrictUrls(zipCode) {
function getDistrictUrls(zipCode, serialize) {
return soap.createClientAsync('https://support.edupoint.com/Service/HDInfoCommunication.asmx?WSDL', {
endpoint: 'https://support.edupoint.com/Service/HDInfoCommunication.asmx',
escapeXML: false
})
.then(client => {
const supportClient = new StudentVueClient('EdupointDistrictInfo', 'Edup01nt', client);
return supportClient._xmlJsonSerialize(supportClient._makeServiceRequest('GetMatchingDistrictList', {
const supportClient = new StudentVueClient('EdupointDistrictInfo', 'Edup01nt', client, serialize);
return supportClient._serialize(supportClient._makeServiceRequest('GetMatchingDistrictList', {
MatchToDistrictZipCode: zipCode,
Key: '5E4B7859-B805-474B-A833-FDB15D205D40' // idk how safe this is
}, 'HDInfoServices'));
Expand Down
Loading