Last active
April 5, 2021 17:51
-
-
Save JEverhart383/7548c8ba59dffa4614f2cd1acf298ca1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CanvasLMSClient(baseURL, accessToken) { | |
this.baseURL = baseURL; | |
this.accessToken = accessToken | |
this.getAllResources = function(resourceUrl, nextLink = null, resources = [] ) { | |
const url = nextLink ? nextLink + '&access_token=' + this.accessToken : resourceUrl; | |
const params = { | |
method: 'get', | |
muteHttpExceptions: true | |
} | |
const response = UrlFetchApp.fetch(url, params ) | |
const responseText = JSON.parse(response.getContentText()) | |
if (responseText.errors && responseText.errors.length > 0) throw new Error(responseText.errors[0].message) | |
const headers = response.getAllHeaders() | |
const links = headers['Link'] | |
? headers['Link'].split(',').filter(link => link.includes('next')) | |
: null | |
if (Array.isArray(responseText)) { | |
responseText.forEach(resource => { | |
resources.push(resource) | |
}) | |
} else { | |
resources.push(responseText) | |
} | |
if (links && links.length > 0) { | |
const nextLink = links[0].split(';')[0].replace("<", "").replace(">", "") | |
return this.getAllResources(url, nextLink, resources) | |
} | |
return resources | |
} | |
this.postResource = function(resourceUrl, payload){ | |
Logger.log(`POSTing to Canvas API at ${resourceUrl}`) | |
const params = { | |
method: 'post', | |
muteHttpExceptions: true, | |
payload: JSON.stringify(payload), | |
contentType: 'application/json' | |
}; | |
const response = UrlFetchApp.fetch(resourceUrl, params); | |
Logger.log(response.getResponseCode()) | |
Logger.log(response) | |
return JSON.parse(response); | |
} | |
this.deleteResource = function(resourceUrl, payload){ | |
Logger.log(`Deleting from Canvas API at ${resourceUrl}`) | |
const params = { | |
method: 'delete', | |
muteHttpExceptions: true, | |
payload: JSON.stringify(payload), | |
contentType: 'application/json' | |
}; | |
const response = UrlFetchApp.fetch(resourceUrl, params); | |
Logger.log(response.getResponseCode()) | |
Logger.log(response) | |
return JSON.parse(response); | |
} | |
this.getUser = function(userId) { | |
const requestUrl = `https://${this.baseURL}/api/v1/users/${userId}?access_token=${this.accessToken}`; | |
const response = this.getAllResources(requestUrl); | |
if (response.length === 0) new Error('No user found with that id') | |
const user = response[0]; | |
return user; | |
} | |
this.getCourseEnrollments = function(courseId){ | |
const requestUrl = `https://${this.baseURL}/api/v1/courses/${courseId}/enrollments?&per_page=100&access_token=${this.accessToken}`; | |
const enrollments = this.getAllResources(requestUrl); | |
return enrollments; | |
} | |
this.createEnrollmentInCourse = function(courseId, userId, enrollmentType = 'TeacherEnrollment') { | |
const requestUrl = `https://${this.baseURL}/api/v1/courses/${courseId}/enrollments?access_token=${this.accessToken}`; | |
const enrollment = {user_id:userId, type: enrollmentType} | |
const newCourse = this.postResource(requestUrl, {enrollment: enrollment}); | |
return newCourse; | |
} | |
this.deleteEnrollmentFromCourse = function(courseId, enrollmentId, task = 'delete'){ | |
const requestUrl = `https://${this.baseURL}/api/v1/courses/${courseId}/enrollments/${enrollmentId}?access_token=${this.accessToken}`; | |
const deletedEnrollment = this.deleteResource(requestUrl, {task: task}); | |
return deletedEnrollment; | |
} | |
this.getTerms = function(accountId){ | |
const requestUrl = `https://${this.baseURL}/api/v1/accounts/${accountId}/terms?&per_page=100&access_token=${this.accessToken}`; | |
const terms = this.getAllResources(requestUrl); | |
return terms; | |
} | |
this.getUserByEmail = function(email = '[email protected]', accountId = 1){ | |
const requestUrl = `https://${this.baseURL}/api/v1/accounts/${accountId}/users?search_term=${email}&access_token=${this.accessToken}`; | |
const response = this.getAllResources(requestUrl); | |
Logger.log(response) | |
if (response.length === 0) new Error('No user found with that email address') | |
const user = response[0]; | |
return user; | |
} | |
this.createCourse = function(accountId = 1, course){ | |
const requestUrl = `https://${this.baseURL}/api/v1/accounts/${accountId}/courses?access_token=${this.accessToken}`; | |
const newCourse = this.postResource(requestUrl, {course: course}); | |
return newCourse; | |
} | |
this.getCourseDetails = function(courseId) { | |
const requestUrl = `https://virginiacommonwealth.instructure.com/api/v1/courses/${courseId}?access_token=${this.accessToken}` | |
const course = this.getAllResources(requestUrl); | |
return course[0] | |
} | |
return this | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment