GitHub
Setup
Insert the file OneRoster.js into your project.
RosterServer uses crypto-js and requests, so you will need to install them if they are not being used in your project already.
In the directory containing your package.json:
npm install request --save
npm install crypto-js --save
Usage
First, import OneRoster.
const OneRoster = require('./OneRoster')
Next, create a new instance of the OneRoster object, which takes in two strings,
the client_id and client_secret.
let oneRoster = new OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX');
You are now able to make requests to a specified URL. This is done using the method makeRosterRequest(url, callback), which takes in the URL and a callback, which takes in error, the status code, and the JSON response.
let oneRoster = new OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX');
oneRoster.makeRosterRequest("https://example.com/users", function(error, statusCode, response) {
if (error) throw new Error(error);
console.log(statusCode); // The status code
console.log(response); // The JSON response
});
Example
Example
Print all students' names
// Import the OneRoster module
const OneRoster = require('./OneRoster');
function getUsers(url) {
// Create new OneRoster object with the client_id and client_secret
let oneRoster = new OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX');
// Make the request to the given url with the callback that takes in error, the status code, and the response body
oneRoster.makeRosterRequest(url, function (error, statusCode, response) {
if (error) throw new Error(error);
// Decode the resopnse
let decoded = JSON.parse(response);
// If statusCode is 200, create array of users from response, otherwise print message
switch(statusCode) {
case 200:
printUsers(decoded.users);
break;
case 401:
console.log("Unauthorized Request\n" + response);
break;
case 404:
console.log("Not Found\n" + response);
break;
case 500:
console.log("Server Error\n" + response);
break;
default:
console.log("Something Went Wrong, status code " + response.statusCode + "\n" + response);
break;
}
});
}
// Print the users
function printUsers(users) {
for (let i = 0; i < users.length; i++) {
console.log(users[i].givenName + " " + users[i].familyName);
}
}
getUsers("https://example.com/users?fields=givenName%2CfamilyName&filter=role%3D'student'");