Reputation: 167
I am working on an app that is uploading data to Google Classroom using AppScript. I use two methods a lot:
/*
*@description adding grade to the submission
*@param courseId {string} id of clasroom in which there is coursework that we are intrested in
*@param courseWorkId {string} id of coursework in which there is submission that we are intrested in
*@param submId {string} submission id of submission to which we want to set mark*/
function setMark(courseId, courseWorkId, submIdd, mark) {
var maark = parseFloat(mark) || 0
if (maark < 0) {
maark = 0
}
var studentSubmissionRes = {
'assignedGrade': maark,
'draftGrade': maark
}
var extra = {
'updateMask': 'assignedGrade,draftGrade'
};
Classroom.Courses.CourseWork.StudentSubmissions.patch(studentSubmissionRes,
courseId, courseWorkId, submIdd, extra)
}
/**
*@description addind link to the submission
*@param fileUrl {string} url of file we want to add to submission
*@param courseId {string} id of clasroom in which there is coursework that we are intrested in
*@param courseWorkId {string} id of coursework in which there is submission that we are intrested in
*@param submId {string} submission id of submission to which we want to add the link
*/
function setLinkTosubmission(fileUrl, courseId, courseWorkId, submId) {
var request = ModifyAttachmentsRequest = {
'addAttachments': [{
'link': {
'url': fileUrl
}
}]
}
Classroom.Courses.CourseWork.StudentSubmissions.modifyAttachments(request, courseId, courseWorkId, submId)
}
Both of them work, but i would like to improve performance and batch requests to add link or grade, as is possible in Google Sheets API, but I cannot find a way to do this.
Also, is there a possibility to batch update text in many documents in at once?
Upvotes: 0
Views: 364
Reputation: 4440
The Classroom Advanced Service does not support batching requests. For that reason, you will have to implement the code that creates the body of the batch request without its support, and send it afterwards using UrlFetchApp. See below:
function main() {
var body = [
{
method: "PATCH",
endpoint: "/v1/courses/134529639?updateMask=name",
requestBody: {"name": "Course 1"}
},
{
method: "PATCH",
endpoint: "/v1/courses/134529901?updateMask=section",
requestBody: {"section": "Section 2"}
}
];
var url = "https://classroom.googleapis.com/batch";
var boundary = "batch_foobarbaz";
var contentId = 0;
var data = "--" + boundary + "\r\n";
for (var i in body) {
data += "Content-Type: application/http\r\n";
data += "Content-ID: " + ++contentId + "\r\n\r\n";
data += body[i].method + " " + body[i].endpoint + "\r\n";
data += body[i].requestBody ? "Content-Type: application/json; charset=utf-8\r\n\r\n" : "\r\n";
data += body[i].requestBody ? JSON.stringify(body[i].requestBody) + "\r\n" : "";
data += "--" + boundary + "\r\n";
}
var payload = Utilities.newBlob(data).getBytes();
var options = {
method: "post",
contentType: "multipart/mixed; boundary=" + boundary,
payload: payload,
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
muteHttpExceptions: true,
};
var res = UrlFetchApp.fetch(url, options).getContentText();
Logger.log(res);
}
In this case, a batch request is created containing two different PATCH
calls to the API. The obtained response will look somewhat like the following:
HTTP/1.1 200
Content-Length: response_total_content_length
Content-Type: multipart/mixed; boundary=batch_foobarbaz
--batch_foobarbaz
Content-Type: application/http
Content-ID: <response-item1:[email protected]>
HTTP/1.1 200 OK
Content-Type application/json
Content-Length: response_part_1_content_length
{
"id": "134529639",
"name": "Course 1",
"section": "Section 1",
"ownerId": "116269102540619633451",
"creationTime": "2015-06-25T14:23:56.535Z",
"updateTime": "2015-06-25T14:33:06.583Z",
"enrollmentCode": "6paeflo",
"courseState": "PROVISIONED",
"alternateLink": "http://classroom.google.com/c/MTM0NTI5NjM5"
}
--batch_foobarbaz
Content-Type: application/http
Content-ID: <response-item2:[email protected]>
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: response_part_2_content_length
{
"id": "134529901",
"name": "Course 1",
"section": "Section 2",
"ownerId": "116269102540619633451",
"creationTime": "2015-06-25T14:23:08.761Z",
"updateTime": "2015-06-25T14:33:06.490Z",
"enrollmentCode": "so75ha5",
"courseState": "PROVISIONED",
"alternateLink": "http://classroom.google.com/c/MTM0NTI5OTAx"
}
--batch_foobarbaz--
Upvotes: 2