【问题标题】:is it possible to batch requests to google classroom in AppScript是否可以在 AppScript 中批量请求谷歌教室
【发布时间】:2020-01-14 10:21:15
【问题描述】:

我正在开发一个使用 AppScript 将数据上传到 Google Classroom 的应用程序。我经常使用两种方法:

/*
 *@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)
}

它们都可以工作,但我想提高性能和批量请求以添加链接或评分,这在 Google Sheets API 中是可能的,但我找不到这样做的方法。

另外,是否可以一次批量更新多个文档中的文本?

【问题讨论】:

    标签: google-apps-script google-classroom


    【解决方案1】:

    Classroom Advanced Service 不支持批处理请求。出于这个原因,您必须在不支持的情况下实现创建批处理请求主体的代码,然后使用UrlFetchApp 发送它。见下文:

    示例

    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);
    }
    

    在这种情况下,将创建一个批处理请求,其中包含对 API 的两个不同的 PATCH 调用。获得的响应将如下所示:

    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:12930812@classroom.example.com>
    
    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:12930812@classroom.example.com>
    
    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--
    

    参考

    【讨论】:

    • 感谢您的回复!我会尝试实现它,它通常是使用应用脚本时非常有用的示例!
    • 很高兴您喜欢我的回答@Jagiellonczyk,请考虑Accepting my answer,以防它解决您最初的问题。此外,如果您对答案有任何疑问,请不要犹豫发表其他评论或问题。干杯!
    • 老实说,我在实施此方法时遇到了问题,我首先尝试将其应用于批量发送标记,但没有成功下面的方法链接和错误消息我真的很感谢您的评论docs.google.com/document/d/…
    • 还有一个我之前没有发现的问题,上面提到的批处理服务将因为谷歌迁移而无法使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多