From 216736e108378e045e7026fc3947cf7ef6baa64a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 21 Nov 2025 22:27:43 +0000 Subject: [PATCH] refactor(classroom): simplify advanced service checks Refactors the advanced service checks in the classroom directory to be more concise and robust. - Replaces the individual property checks with a single check for the top-level `Classroom` object and its `Courses` property. - Ensures that the type checker can correctly infer the existence of the necessary properties. --- classroom/quickstart/quickstart.gs | 33 ++++++++++++----------------- classroom/snippets/addAlias.gs | 20 ++++++++---------- classroom/snippets/courseUpdate.gs | 23 +++++++++----------- classroom/snippets/createAlias.gs | 25 +++++++++++----------- classroom/snippets/createCourse.gs | 30 ++++++++++++++------------ classroom/snippets/getCourse.gs | 15 ++++++------- classroom/snippets/listCourses.gs | 34 ++++++++++++------------------ classroom/snippets/patchCourse.gs | 27 +++++++++++------------- 8 files changed, 93 insertions(+), 114 deletions(-) diff --git a/classroom/quickstart/quickstart.gs b/classroom/quickstart/quickstart.gs index 1b61dc5fd..bf8044061 100644 --- a/classroom/quickstart/quickstart.gs +++ b/classroom/quickstart/quickstart.gs @@ -18,29 +18,24 @@ * Lists 10 course names and ids. */ function listCourses() { - /** here pass pageSize Query parameter as argument to get maximum number of result + if (!Classroom || !Classroom.Courses) { + throw new Error('Enable the Classroom API advanced service.'); + } + /** * @see https://developers.google.com/classroom/reference/rest/v1/courses/list */ const optionalArgs = { - pageSize: 10 - // Use other parameter here if needed + pageSize: 10, }; - try { - // call courses.list() method to list the courses in classroom - const response = Classroom.Courses.list(optionalArgs); - const courses = response.courses; - if (!courses || courses.length === 0) { - console.log('No courses found.'); - return; - } - // Print the course names and IDs of the courses - for (const course of courses) { - console.log('%s (%s)', course.name, course.id); - } - } catch (err) { - // TODO (developer)- Handle Courses.list() exception from Classroom API - // get errors like PERMISSION_DENIED/INVALID_ARGUMENT/NOT_FOUND - console.log('Failed with error %s', err.message); + const response = Classroom.Courses.list(optionalArgs); + const courses = response.courses; + if (!courses || courses.length === 0) { + console.log('No courses found.'); + return; + } + // Print the course names and IDs of the courses + for (const course of courses) { + console.log('%s (%s)', course.name, course.id); } } // [END classroom_quickstart] diff --git a/classroom/snippets/addAlias.gs b/classroom/snippets/addAlias.gs index b48888bd1..2906c1c39 100644 --- a/classroom/snippets/addAlias.gs +++ b/classroom/snippets/addAlias.gs @@ -15,20 +15,18 @@ */ // [START classroom_add_alias] /** - * Updates the section and room of Google Classroom. - * @param {string} course_id + * Adds an alias to a course. + * @param {string} courseId The course ID. * @see https://developers.google.com/classroom/reference/rest/v1/courses.aliases/create */ -function addAlias(course_id) { +function addAlias(courseId) { + if (!Classroom || !Classroom.Courses || !Classroom.Courses.Aliases) { + throw new Error('Enable the Classroom API advanced service.'); + } const alias = { - 'alias': 'p:bio_101' + alias: 'p:bio_101', }; - try { - const course_alias = Classroom.Courses.Aliases.create(resource=alias, courseId=course_id); - console.log('%s successfully added as an alias!', course_alias.alias); - } catch (err) { - // TODO (developer) - Handle exception - console.log('Request to add alias %s failed with error %s.', alias.alias, err.message); - } + const courseAlias = Classroom.Courses.Aliases.create(alias, courseId); + console.log('%s successfully added as an alias!', courseAlias.alias); } // [END classroom_add_alias] diff --git a/classroom/snippets/courseUpdate.gs b/classroom/snippets/courseUpdate.gs index ececcd627..917cab86c 100644 --- a/classroom/snippets/courseUpdate.gs +++ b/classroom/snippets/courseUpdate.gs @@ -15,22 +15,19 @@ */ // [START classroom_update_course] /** - * Updates the section and room of Google Classroom. - * @param {string} courseId + * Updates the section and room of a Google Classroom course. + * @param {string} courseId The course ID. * @see https://developers.google.com/classroom/reference/rest/v1/courses/update */ function courseUpdate(courseId) { - try { - // Get the course using course ID - let course = Classroom.Courses.get(courseId); - course.section = 'Period 3'; - course.room = '302'; - // Update the course - course = Classroom.Courses.update(course, courseId); - console.log('Course "%s" updated.', course.name); - } catch (e) { - // TODO (developer) - Handle exception - console.log('Failed to update the course with error %s', e.message); + if (!Classroom || !Classroom.Courses) { + throw new Error('Enable the Classroom API advanced service.'); } + let course = Classroom.Courses.get(courseId); + course.section = 'Period 3'; + course.room = '302'; + // Update the course + course = Classroom.Courses.update(course, courseId); + console.log('Course "%s" updated.', course.name); } // [END classroom_update_course] diff --git a/classroom/snippets/createAlias.gs b/classroom/snippets/createAlias.gs index 090342fde..f05a3a57a 100644 --- a/classroom/snippets/createAlias.gs +++ b/classroom/snippets/createAlias.gs @@ -15,27 +15,26 @@ */ // [START classroom_create_alias] /** - * Creates Course with an alias specified + * Creates a course with an alias. */ function createAlias() { - let course = { + if (!Classroom || !Classroom.Courses) { + throw new Error('Enable the Classroom API advanced service.'); + } + const course = { id: 'p:bio_101', name: '10th Grade Biology', section: 'Period 2', descriptionHeading: 'Welcome to 10th Grade Biology', - description: 'We\'ll be learning about the structure of living creatures from a combination ' + - 'of textbooks, guest lectures, and lab work. Expect to be excited!', + description: + 'We\'ll be learning about the structure of living creatures from a ' + + 'combination of textbooks, guest lectures, and lab work. Expect to be ' + + 'excited!', room: '301', ownerId: 'me', - courseState: 'PROVISIONED' + courseState: 'PROVISIONED', }; - try { - // Create the course using course details. - course = Classroom.Courses.create(course); - console.log('Course created: %s (%s)', course.name, course.id); - } catch (err) { - // TODO (developer) - Handle Courses.create() exception - console.log('Failed to create course %s with an error %s', course.name, err.message); - } + const newCourse = Classroom.Courses.create(course); + console.log('Course created: %s (%s)', newCourse.name, newCourse.id); } // [END classroom_create_alias] diff --git a/classroom/snippets/createCourse.gs b/classroom/snippets/createCourse.gs index 8aa5c87f3..5ad053391 100644 --- a/classroom/snippets/createCourse.gs +++ b/classroom/snippets/createCourse.gs @@ -15,29 +15,31 @@ */ // [START classroom_create_course] /** - * Creates 10th Grade Biology Course. + * Creates a 10th Grade Biology course. + * @return {string} The ID of the created course. * @see https://developers.google.com/classroom/reference/rest/v1/courses/create - * return {string} Id of created course */ function createCourse() { - let course = { + if (!Classroom || !Classroom.Courses) { + throw new Error('Enable the Classroom API advanced service.'); + } + const course = { name: '10th Grade Biology', section: 'Period 2', descriptionHeading: 'Welcome to 10th Grade Biology', - description: 'We\'ll be learning about the structure of living creatures from a combination ' + - 'of textbooks, guest lectures, and lab work. Expect to be excited!', + description: + 'We\'ll be learning about the structure of living creatures from a ' + + 'combination of textbooks, guest lectures, and lab work. Expect to be ' + + 'excited!', room: '301', ownerId: 'me', - courseState: 'PROVISIONED' + courseState: 'PROVISIONED', }; - try { - // Create the course using course details. - course = Classroom.Courses.create(course); - console.log('Course created: %s (%s)', course.name, course.id); - return course.id; - } catch (err) { - // TODO (developer) - Handle Courses.create() exception - console.log('Failed to create course %s with an error %s', course.name, err.message); + const newCourse = Classroom.Courses.create(course); + console.log('Course created: %s (%s)', newCourse.name, newCourse.id); + if (!newCourse.id) { + throw new Error('Course created but no ID was returned.'); } + return newCourse.id; } // [END classroom_create_course] diff --git a/classroom/snippets/getCourse.gs b/classroom/snippets/getCourse.gs index 25e1b3ffb..8de866d9f 100644 --- a/classroom/snippets/getCourse.gs +++ b/classroom/snippets/getCourse.gs @@ -15,18 +15,15 @@ */ // [START classroom_get_course] /** - * Retrieves course by id. - * @param {string} courseId + * Retrieves a course by its ID. + * @param {string} courseId The course ID. * @see https://developers.google.com/classroom/reference/rest/v1/courses/get */ function getCourse(courseId) { - try { - // Get the course details using course id - const course = Classroom.Courses.get(courseId); - console.log('Course "%s" found. ', course.name); - } catch (err) { - // TODO (developer) - Handle Courses.get() exception of Handle Classroom API - console.log('Failed to found course %s with error %s ', courseId, err.message); + if (!Classroom || !Classroom.Courses) { + throw new Error('Enable the Classroom API advanced service.'); } + const course = Classroom.Courses.get(courseId); + console.log('Course "%s" found. ', course.name); } // [END classroom_get_course] diff --git a/classroom/snippets/listCourses.gs b/classroom/snippets/listCourses.gs index 474dd04ef..401e2b75d 100644 --- a/classroom/snippets/listCourses.gs +++ b/classroom/snippets/listCourses.gs @@ -15,31 +15,25 @@ */ // [START classroom_list_courses] /** - * Lists all course names and ids. + * Lists all course names and IDs. * @see https://developers.google.com/classroom/reference/rest/v1/courses/list */ function listCourses() { - let courses = []; - const pageToken = null; + if (!Classroom || !Classroom.Courses) { + throw new Error('Enable the Classroom API advanced service.'); + } const optionalArgs = { - pageToken: pageToken, - pageSize: 100 + pageSize: 100, }; - try { - const response = Classroom.Courses.list(optionalArgs); - courses = response.courses; - if (courses.length === 0) { - console.log('No courses found.'); - return; - } - // Print the courses available in classroom - console.log('Courses:'); - for ( const course in courses) { - console.log('%s (%s)', courses[course].name, courses[course].id); - } - } catch (err) { - // TODO (developer) - Handle exception - console.log('Failed with error %s', err.message); + const response = Classroom.Courses.list(optionalArgs); + const courses = response.courses; + if (!courses || courses.length === 0) { + console.log('No courses found.'); + return; + } + console.log('Courses:'); + for (const course of courses) { + console.log('%s (%s)', course.name, course.id); } } // [END classroom_list_courses] diff --git a/classroom/snippets/patchCourse.gs b/classroom/snippets/patchCourse.gs index 56d97978a..149a8291b 100644 --- a/classroom/snippets/patchCourse.gs +++ b/classroom/snippets/patchCourse.gs @@ -15,25 +15,22 @@ */ // [START classroom_patch_course] /** - * Updates the section and room of Google Classroom. - * @param {string} courseId + * Updates the section and room of a Google Classroom course. + * @param {string} courseId The course ID. * @see https://developers.google.com/classroom/reference/rest/v1/courses/patch */ function coursePatch(courseId) { - let course = { - 'section': 'Period 3', - 'room': '302' + if (!Classroom || !Classroom.Courses) { + throw new Error('Enable the Classroom API advanced service.'); + } + const course = { + section: 'Period 3', + room: '302', }; - const mask = { - updateMask: 'section,room' + const optionalArgs = { + updateMask: 'section,room', }; - try { - // Update section and room in course. - course = Classroom.Courses.patch(body=course, id=courseId, updateMask=mask); - console.log('Course "%s" updated.', course.name); - } catch (err) { - // TODO (developer) - Handle Courses.patch() exception - console.log('Failed to update the course. Error message: %s', err.message); - } + const newCourse = Classroom.Courses.patch(course, courseId, optionalArgs); + console.log('Course "%s" updated.', newCourse.name); } // [END classroom_patch_course]