Skip to content

Commit da082cc

Browse files
authored
feat: Add retrieve classroom course aliases snippet (#368)
* feat: Add retrieve classroom course aliases snippet Incorrect snippet in https://developers.google.com/classroom/guides/manage-aliases#retrieve_course_aliases will be replaced by this * Format and sort imports
1 parent 44f307f commit da082cc

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Copyright 2018 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
"""
17+
18+
# [START classroom_list_course_aliases]
19+
20+
from __future__ import print_function
21+
22+
import google.auth
23+
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
25+
26+
27+
def classroom_list_course_aliases(course_id):
28+
"""
29+
Prints the list of the aliases of a specified course the user has access to.
30+
Load pre-authorized user credentials from the environment.
31+
TODO(developer) - See https://developers.google.com/identity
32+
for guides on implementing OAuth2 for the application.
33+
"""
34+
35+
creds, _ = google.auth.default()
36+
try:
37+
service = build('classroom', 'v1', credentials=creds)
38+
course_aliases = []
39+
page_token = None
40+
41+
while True:
42+
response = service.courses().aliases().list(
43+
pageToken=page_token,
44+
courseId=course_id).execute()
45+
course_aliases.extend(response.get('aliases', []))
46+
page_token = response.get('nextPageToken', None)
47+
if not page_token:
48+
break
49+
50+
if not course_aliases:
51+
print('No course aliases found.')
52+
53+
print("Course aliases:")
54+
for course_alias in course_aliases:
55+
print(f"{course_alias.get('alias')}")
56+
return course_aliases
57+
except HttpError as error:
58+
print(f"An error occurred: {error}")
59+
return error
60+
61+
62+
if __name__ == '__main__':
63+
classroom_list_course_aliases('course_id')
64+
65+
# [END classroom_list_course_aliases]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
"""
13+
import unittest
14+
15+
from base_test import BaseTest
16+
from classroom_create_course import classroom_create_course
17+
from classroom_list_course_aliases import classroom_list_course_aliases
18+
19+
20+
class TestClassroomListCourseAliases(BaseTest):
21+
"""Unit test class for List course aliases snippet"""
22+
23+
def test_classroom_list_course_aliases(self):
24+
"""Unit test method for List course snippet"""
25+
course = classroom_create_course()
26+
self.assertIsNotNone(course)
27+
self.delete_course_on_cleanup(course.get('id'))
28+
course_aliases = classroom_list_course_aliases(course.get('id'))
29+
self.assertIsNotNone(course_aliases)
30+
31+
32+
if __name__ == "__main__":
33+
unittest.main()

0 commit comments

Comments
 (0)