|
14 | 14 |
|
15 | 15 | from __future__ import print_function |
16 | 16 | from googleapiclient import errors |
17 | | - |
| 17 | +import json |
18 | 18 |
|
19 | 19 | class ClassroomSnippets(object): |
20 | 20 | def __init__(self, service): |
21 | 21 | self.service = service |
22 | 22 |
|
| 23 | + def create_course(self): |
| 24 | + """ |
| 25 | + Creates a 10th Grade Biology course. |
| 26 | + """ |
| 27 | + service = self.service |
| 28 | + # [START classroom_create_course] |
| 29 | + course = { |
| 30 | + 'name': '10th Grade Biology', |
| 31 | + 'section': 'Period 2', |
| 32 | + 'descriptionHeading': 'Welcome to 10th Grade Biology', |
| 33 | + 'description': """We'll be learning about about the structure of living |
| 34 | + creatures from a combination of textbooks, guest |
| 35 | + lectures, and lab work. Expect to be excited!""", |
| 36 | + 'room': '301', |
| 37 | + 'ownerId': 'me', |
| 38 | + 'courseState': 'PROVISIONED' |
| 39 | + } |
| 40 | + course = service.courses().create(body=course).execute() |
| 41 | + print('Course created:', course.get('name'), course.get('id')) |
| 42 | + # [END classroom_create_course] |
| 43 | + |
| 44 | + def get_course(self): |
| 45 | + """ |
| 46 | + Retrieves a classroom course by its id. |
| 47 | + """ |
| 48 | + service = self.service |
| 49 | + # [START classroom_get_course] |
| 50 | + course_id = '123456' |
| 51 | + try: |
| 52 | + course = service.courses().get(id=course_id).execute() |
| 53 | + print('Course "{%s}" found.', course.get('name')) |
| 54 | + except errors.HttpError: |
| 55 | + error = json.loads(e.content).get('error') |
| 56 | + if(error.get('code') == 404): |
| 57 | + print('Course with ID "{%s}" not found.', course_id) |
| 58 | + else: |
| 59 | + raise |
| 60 | + # [END classroom_get_course] |
| 61 | + |
| 62 | + def list_courses(self): |
| 63 | + """ |
| 64 | + Lists all classroom courses. |
| 65 | + """ |
| 66 | + service = self.service |
| 67 | + # [START classroom_list_courses] |
| 68 | + courses = [] |
| 69 | + page_token = None |
| 70 | + |
| 71 | + while True: |
| 72 | + response = service.courses().list(pageToken=page_token, |
| 73 | + pageSize=100).execute() |
| 74 | + courses.extend(response.get('courses', [])) |
| 75 | + page_token = response.get('nextPageToken', None) |
| 76 | + if not page_token: |
| 77 | + break |
| 78 | + |
| 79 | + if not courses: |
| 80 | + print('No courses found.') |
| 81 | + else: |
| 82 | + print('Courses:') |
| 83 | + for course in courses: |
| 84 | + print(course.get('name'), course.get('id')) |
| 85 | + # [END classroom_list_courses] |
| 86 | + |
23 | 87 | def add_alias_new(self): |
24 | 88 | """ |
25 | 89 | Creates a course with alias specification. |
|
0 commit comments