Skip to content

Commit f88b5cd

Browse files
updated python snippets structure
1 parent 1dcc702 commit f88b5cd

File tree

3 files changed

+127
-43
lines changed

3 files changed

+127
-43
lines changed
Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,54 @@
1-
from __future__ import print_function
2-
from googleapiclient.discovery import build
3-
from httplib2 import Http
4-
from oauth2client import file, client, tools
1+
# Copyright 2018 Google LLC
2+
#
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+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
514

6-
# If modifying these scopes, delete the file token.json.
7-
SCOPES = 'https://www.googleapis.com/auth/classroom.courses'
15+
from __future__ import print_function
816

9-
def main():
10-
"""Shows basic usage of the Classroom API.
11-
Prints the names of the first 10 courses the user has access to.
12-
"""
13-
# The file token.json stores the user's access and refresh tokens, and is
14-
# created automatically when the authorization flow completes for the first
15-
# time.
16-
store = file.Storage('token.json')
17-
print(store)
18-
creds = None
19-
if not creds or creds.invalid:
20-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
21-
creds = tools.run_flow(flow, store)
22-
# [START classroom_quickstart]
23-
service = build('classroom', 'v1', http=creds.authorize(Http()))
17+
class ClassroomSnippets(object):
18+
def __init__(self, service):
19+
self.service = service
2420

25-
alias = 'd:school_math_101'
26-
course = {
27-
'id': alias,
28-
'name': 'Math 101',
29-
'section': 'Period 2',
30-
'description': 'Course Description',
31-
'room': '301',
32-
'ownerId': 'teacherId'
33-
}
34-
try:
35-
course = service.courses().create(
36-
body=course).execute()
37-
except:
38-
print('Course Creation Failed')
39-
# Handle error
40-
# [END classroom_quickstart
21+
def add_alias_existing(self):
22+
"""Shows basic usage of the Classroom API.
23+
Creates a course with alias specification.
24+
"""
25+
service = self.service
26+
# [START classroom_quickstart]
27+
alias = 'd:school_math_101'
28+
course = {
29+
'id': alias,
30+
'name': 'Math 101',
31+
'section': 'Period 2',
32+
'description': 'Course Description',
33+
'room': '301',
34+
'ownerId': 'teacherId'
35+
}
36+
try:
37+
course = service.courses().create(
38+
body=course).execute()
39+
except:
40+
print('Course Creation Failed')
41+
# [END classroom_quickstart]
4142

42-
if __name__ == '__main__':
43-
main()
43+
def add_alias_new(self):
44+
# [START classroom_quickstart]
45+
service = self.service
46+
classroomCourseId = '123456'
47+
alias = 'd:school_math_101'
48+
courseAlias = {
49+
'alias': alias
50+
}
51+
courseAlias = service.courses().aliases().create(
52+
courseId=classroomCourseId,
53+
body=courseAlias).execute()
54+
# [END classroom_quickstart

classroom/snippets/manage_existing_aliases.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2018 Google LLC
2+
#
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+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from __future__ import print_function
216
from googleapiclient.discovery import build
317
from httplib2 import Http
@@ -7,15 +21,14 @@
721
SCOPES = 'https://www.googleapis.com/auth/classroom.courses'
822

923
def main():
10-
"""Shows basic usage of the Classroom API.
11-
Prints the names of the first 10 courses the user has access to.
24+
"""
25+
Adds an alias to an existing course.
1226
"""
1327
# The file token.json stores the user's access and refresh tokens, and is
1428
# created automatically when the authorization flow completes for the first
1529
# time.
1630
store = file.Storage('token.json')
17-
print(store)
18-
creds = None
31+
creds = store.get()
1932
if not creds or creds.invalid:
2033
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
2134
creds = tools.run_flow(flow, store)

classroom/snippets/snippets.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2018 Google LLC
2+
#
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+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
class ClassroomSnippets(object):
18+
def __init__(self, service):
19+
self.service = service
20+
21+
def add_alias_existing(self):
22+
"""
23+
Creates a course with alias specification.
24+
"""
25+
service = self.service
26+
# [START classroom_quickstart]
27+
alias = 'd:school_math_101'
28+
course = {
29+
'id': alias,
30+
'name': 'Math 101',
31+
'section': 'Period 2',
32+
'description': 'Course Description',
33+
'room': '301',
34+
'ownerId': 'teacherId'
35+
}
36+
try:
37+
course = service.courses().create(
38+
body=course).execute()
39+
except:
40+
print('Course Creation Failed')
41+
# [END classroom_quickstart]
42+
43+
def add_alias_new(self):
44+
"""
45+
Adds alias to existing course.
46+
"""
47+
# [START classroom_quickstart]
48+
service = self.service
49+
classroomCourseId = '123456'
50+
alias = 'd:school_math_101'
51+
courseAlias = {
52+
'alias': alias
53+
}
54+
try:
55+
courseAlias = service.courses().aliases().create(
56+
courseId=classroomCourseId,
57+
body=courseAlias).execute()
58+
except:
59+
print('Alias Creation Failed')
60+
# [END classroom_quickstart

0 commit comments

Comments
 (0)