Skip to content

Commit 216736e

Browse files
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.
1 parent 275a497 commit 216736e

8 files changed

Lines changed: 93 additions & 114 deletions

File tree

classroom/quickstart/quickstart.gs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,24 @@
1818
* Lists 10 course names and ids.
1919
*/
2020
function listCourses() {
21-
/** here pass pageSize Query parameter as argument to get maximum number of result
21+
if (!Classroom || !Classroom.Courses) {
22+
throw new Error('Enable the Classroom API advanced service.');
23+
}
24+
/**
2225
* @see https://developers.google.com/classroom/reference/rest/v1/courses/list
2326
*/
2427
const optionalArgs = {
25-
pageSize: 10
26-
// Use other parameter here if needed
28+
pageSize: 10,
2729
};
28-
try {
29-
// call courses.list() method to list the courses in classroom
30-
const response = Classroom.Courses.list(optionalArgs);
31-
const courses = response.courses;
32-
if (!courses || courses.length === 0) {
33-
console.log('No courses found.');
34-
return;
35-
}
36-
// Print the course names and IDs of the courses
37-
for (const course of courses) {
38-
console.log('%s (%s)', course.name, course.id);
39-
}
40-
} catch (err) {
41-
// TODO (developer)- Handle Courses.list() exception from Classroom API
42-
// get errors like PERMISSION_DENIED/INVALID_ARGUMENT/NOT_FOUND
43-
console.log('Failed with error %s', err.message);
30+
const response = Classroom.Courses.list(optionalArgs);
31+
const courses = response.courses;
32+
if (!courses || courses.length === 0) {
33+
console.log('No courses found.');
34+
return;
35+
}
36+
// Print the course names and IDs of the courses
37+
for (const course of courses) {
38+
console.log('%s (%s)', course.name, course.id);
4439
}
4540
}
4641
// [END classroom_quickstart]

classroom/snippets/addAlias.gs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@
1515
*/
1616
// [START classroom_add_alias]
1717
/**
18-
* Updates the section and room of Google Classroom.
19-
* @param {string} course_id
18+
* Adds an alias to a course.
19+
* @param {string} courseId The course ID.
2020
* @see https://developers.google.com/classroom/reference/rest/v1/courses.aliases/create
2121
*/
22-
function addAlias(course_id) {
22+
function addAlias(courseId) {
23+
if (!Classroom || !Classroom.Courses || !Classroom.Courses.Aliases) {
24+
throw new Error('Enable the Classroom API advanced service.');
25+
}
2326
const alias = {
24-
'alias': 'p:bio_101'
27+
alias: 'p:bio_101',
2528
};
26-
try {
27-
const course_alias = Classroom.Courses.Aliases.create(resource=alias, courseId=course_id);
28-
console.log('%s successfully added as an alias!', course_alias.alias);
29-
} catch (err) {
30-
// TODO (developer) - Handle exception
31-
console.log('Request to add alias %s failed with error %s.', alias.alias, err.message);
32-
}
29+
const courseAlias = Classroom.Courses.Aliases.create(alias, courseId);
30+
console.log('%s successfully added as an alias!', courseAlias.alias);
3331
}
3432
// [END classroom_add_alias]

classroom/snippets/courseUpdate.gs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515
*/
1616
// [START classroom_update_course]
1717
/**
18-
* Updates the section and room of Google Classroom.
19-
* @param {string} courseId
18+
* Updates the section and room of a Google Classroom course.
19+
* @param {string} courseId The course ID.
2020
* @see https://developers.google.com/classroom/reference/rest/v1/courses/update
2121
*/
2222
function courseUpdate(courseId) {
23-
try {
24-
// Get the course using course ID
25-
let course = Classroom.Courses.get(courseId);
26-
course.section = 'Period 3';
27-
course.room = '302';
28-
// Update the course
29-
course = Classroom.Courses.update(course, courseId);
30-
console.log('Course "%s" updated.', course.name);
31-
} catch (e) {
32-
// TODO (developer) - Handle exception
33-
console.log('Failed to update the course with error %s', e.message);
23+
if (!Classroom || !Classroom.Courses) {
24+
throw new Error('Enable the Classroom API advanced service.');
3425
}
26+
let course = Classroom.Courses.get(courseId);
27+
course.section = 'Period 3';
28+
course.room = '302';
29+
// Update the course
30+
course = Classroom.Courses.update(course, courseId);
31+
console.log('Course "%s" updated.', course.name);
3532
}
3633
// [END classroom_update_course]

classroom/snippets/createAlias.gs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,26 @@
1515
*/
1616
// [START classroom_create_alias]
1717
/**
18-
* Creates Course with an alias specified
18+
* Creates a course with an alias.
1919
*/
2020
function createAlias() {
21-
let course = {
21+
if (!Classroom || !Classroom.Courses) {
22+
throw new Error('Enable the Classroom API advanced service.');
23+
}
24+
const course = {
2225
id: 'p:bio_101',
2326
name: '10th Grade Biology',
2427
section: 'Period 2',
2528
descriptionHeading: 'Welcome to 10th Grade Biology',
26-
description: 'We\'ll be learning about the structure of living creatures from a combination ' +
27-
'of textbooks, guest lectures, and lab work. Expect to be excited!',
29+
description:
30+
'We\'ll be learning about the structure of living creatures from a ' +
31+
'combination of textbooks, guest lectures, and lab work. Expect to be ' +
32+
'excited!',
2833
room: '301',
2934
ownerId: 'me',
30-
courseState: 'PROVISIONED'
35+
courseState: 'PROVISIONED',
3136
};
32-
try {
33-
// Create the course using course details.
34-
course = Classroom.Courses.create(course);
35-
console.log('Course created: %s (%s)', course.name, course.id);
36-
} catch (err) {
37-
// TODO (developer) - Handle Courses.create() exception
38-
console.log('Failed to create course %s with an error %s', course.name, err.message);
39-
}
37+
const newCourse = Classroom.Courses.create(course);
38+
console.log('Course created: %s (%s)', newCourse.name, newCourse.id);
4039
}
4140
// [END classroom_create_alias]

classroom/snippets/createCourse.gs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,31 @@
1515
*/
1616
// [START classroom_create_course]
1717
/**
18-
* Creates 10th Grade Biology Course.
18+
* Creates a 10th Grade Biology course.
19+
* @return {string} The ID of the created course.
1920
* @see https://developers.google.com/classroom/reference/rest/v1/courses/create
20-
* return {string} Id of created course
2121
*/
2222
function createCourse() {
23-
let course = {
23+
if (!Classroom || !Classroom.Courses) {
24+
throw new Error('Enable the Classroom API advanced service.');
25+
}
26+
const course = {
2427
name: '10th Grade Biology',
2528
section: 'Period 2',
2629
descriptionHeading: 'Welcome to 10th Grade Biology',
27-
description: 'We\'ll be learning about the structure of living creatures from a combination ' +
28-
'of textbooks, guest lectures, and lab work. Expect to be excited!',
30+
description:
31+
'We\'ll be learning about the structure of living creatures from a ' +
32+
'combination of textbooks, guest lectures, and lab work. Expect to be ' +
33+
'excited!',
2934
room: '301',
3035
ownerId: 'me',
31-
courseState: 'PROVISIONED'
36+
courseState: 'PROVISIONED',
3237
};
33-
try {
34-
// Create the course using course details.
35-
course = Classroom.Courses.create(course);
36-
console.log('Course created: %s (%s)', course.name, course.id);
37-
return course.id;
38-
} catch (err) {
39-
// TODO (developer) - Handle Courses.create() exception
40-
console.log('Failed to create course %s with an error %s', course.name, err.message);
38+
const newCourse = Classroom.Courses.create(course);
39+
console.log('Course created: %s (%s)', newCourse.name, newCourse.id);
40+
if (!newCourse.id) {
41+
throw new Error('Course created but no ID was returned.');
4142
}
43+
return newCourse.id;
4244
}
4345
// [END classroom_create_course]

classroom/snippets/getCourse.gs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
*/
1616
// [START classroom_get_course]
1717
/**
18-
* Retrieves course by id.
19-
* @param {string} courseId
18+
* Retrieves a course by its ID.
19+
* @param {string} courseId The course ID.
2020
* @see https://developers.google.com/classroom/reference/rest/v1/courses/get
2121
*/
2222
function getCourse(courseId) {
23-
try {
24-
// Get the course details using course id
25-
const course = Classroom.Courses.get(courseId);
26-
console.log('Course "%s" found. ', course.name);
27-
} catch (err) {
28-
// TODO (developer) - Handle Courses.get() exception of Handle Classroom API
29-
console.log('Failed to found course %s with error %s ', courseId, err.message);
23+
if (!Classroom || !Classroom.Courses) {
24+
throw new Error('Enable the Classroom API advanced service.');
3025
}
26+
const course = Classroom.Courses.get(courseId);
27+
console.log('Course "%s" found. ', course.name);
3128
}
3229
// [END classroom_get_course]

classroom/snippets/listCourses.gs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,25 @@
1515
*/
1616
// [START classroom_list_courses]
1717
/**
18-
* Lists all course names and ids.
18+
* Lists all course names and IDs.
1919
* @see https://developers.google.com/classroom/reference/rest/v1/courses/list
2020
*/
2121
function listCourses() {
22-
let courses = [];
23-
const pageToken = null;
22+
if (!Classroom || !Classroom.Courses) {
23+
throw new Error('Enable the Classroom API advanced service.');
24+
}
2425
const optionalArgs = {
25-
pageToken: pageToken,
26-
pageSize: 100
26+
pageSize: 100,
2727
};
28-
try {
29-
const response = Classroom.Courses.list(optionalArgs);
30-
courses = response.courses;
31-
if (courses.length === 0) {
32-
console.log('No courses found.');
33-
return;
34-
}
35-
// Print the courses available in classroom
36-
console.log('Courses:');
37-
for ( const course in courses) {
38-
console.log('%s (%s)', courses[course].name, courses[course].id);
39-
}
40-
} catch (err) {
41-
// TODO (developer) - Handle exception
42-
console.log('Failed with error %s', err.message);
28+
const response = Classroom.Courses.list(optionalArgs);
29+
const courses = response.courses;
30+
if (!courses || courses.length === 0) {
31+
console.log('No courses found.');
32+
return;
33+
}
34+
console.log('Courses:');
35+
for (const course of courses) {
36+
console.log('%s (%s)', course.name, course.id);
4337
}
4438
}
4539
// [END classroom_list_courses]

classroom/snippets/patchCourse.gs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,22 @@
1515
*/
1616
// [START classroom_patch_course]
1717
/**
18-
* Updates the section and room of Google Classroom.
19-
* @param {string} courseId
18+
* Updates the section and room of a Google Classroom course.
19+
* @param {string} courseId The course ID.
2020
* @see https://developers.google.com/classroom/reference/rest/v1/courses/patch
2121
*/
2222
function coursePatch(courseId) {
23-
let course = {
24-
'section': 'Period 3',
25-
'room': '302'
23+
if (!Classroom || !Classroom.Courses) {
24+
throw new Error('Enable the Classroom API advanced service.');
25+
}
26+
const course = {
27+
section: 'Period 3',
28+
room: '302',
2629
};
27-
const mask = {
28-
updateMask: 'section,room'
30+
const optionalArgs = {
31+
updateMask: 'section,room',
2932
};
30-
try {
31-
// Update section and room in course.
32-
course = Classroom.Courses.patch(body=course, id=courseId, updateMask=mask);
33-
console.log('Course "%s" updated.', course.name);
34-
} catch (err) {
35-
// TODO (developer) - Handle Courses.patch() exception
36-
console.log('Failed to update the course. Error message: %s', err.message);
37-
}
33+
const newCourse = Classroom.Courses.patch(course, courseId, optionalArgs);
34+
console.log('Course "%s" updated.', newCourse.name);
3835
}
3936
// [END classroom_patch_course]

0 commit comments

Comments
 (0)