Exciting News!
Our transition to a new website is complete. Click here to seamlessly access the latest version of this article.
Kindly update any bookmarked URLs accordingly. The Development Center will no longer be accessible after April 1, 2024. Thank you for your attention to this matter.
GitHub
Setup
Insert oneroster.py into your project and install the requests module.
Usage
First, import OneRoster.
from oneroster import OneRoster
Then, create a new instance of the OneRoster object, which takes in two strings,
the client_id and client_secret.
oneRoster = OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX')
You are now able to make requests to a specified URL. This is done using the method make_roster_request(url), which takes in the URL and returns a dictionary.
oneRoster = OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX')
response = oneRoster.make_roster_request("https://example.com/users")
The response is made up of "status_code", which contains the status code, and "response", which contains the JSON response as a string.
print(response["status_code"]); // The status code
print(response["response"]); // The JSON response
Example
Example
Print all students' names
import json
# Import OneRoster
from oneroster import OneRoster
def get_users(url):
# Create new OneRoster with the client_id and client_secret
oneRoster = OneRoster('XXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXX')
# Make the request to the given url and store the dictionary with status_code and response
response = oneRoster.make_roster_request(url)
# Decode the response
decoded = json.loads(response["response"])
# If status_code is 200, create array of users from response, otherwise print message and return None
if response["status_code"] == 200:
return decoded["users"]
elif response["status_code"] == 401:
print("Unauthorized Request\n" + response["response"])
elif response["status_code"] == 404:
print("Not Found\n" + response["response"])
elif response["status_code"] == 500:
print("Server Error\n" + response["response"])
else:
print("Something Went Wrong, status code " + response["status_code"]
+ "\n" + response["response"])
return None
users = get_users("https://example.com/users?fields=givenName%2CfamilyName&filter=role%3D'student'")
if users is not None:
for user in users:
print(user["givenName"] + " " + user["familyName"])