Skip to content

Commit 2fa7089

Browse files
feat: added Java code snippets for Aliases, CourseWork, and Topics  (#464)
* add alias snippet and unit test * java snippets for topics, create coursework, create alias * updates based on team review * renamed alias methods, added devsite comments * added missing license header to TestDeleteTopic.java
1 parent 3d4bca3 commit 2fa7089

17 files changed

+954
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2022 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+
// https://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+
16+
// [START classroom_add_alias_to_course_class]
17+
18+
import com.google.api.client.googleapis.json.GoogleJsonError;
19+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20+
import com.google.api.client.http.HttpRequestInitializer;
21+
import com.google.api.client.http.javanet.NetHttpTransport;
22+
import com.google.api.client.json.gson.GsonFactory;
23+
import com.google.api.services.classroom.Classroom;
24+
import com.google.api.services.classroom.ClassroomScopes;
25+
import com.google.api.services.classroom.model.CourseAlias;
26+
import com.google.auth.http.HttpCredentialsAdapter;
27+
import com.google.auth.oauth2.GoogleCredentials;
28+
import java.io.IOException;
29+
import java.util.Collections;
30+
31+
/* Class to demonstrate the use of Classroom Create Alias API. */
32+
public class AddAliasToCourse {
33+
/**
34+
* Add an alias on an existing course.
35+
*
36+
* @param courseId - id of the course to add an alias to.
37+
* @return - newly created course alias.
38+
* @throws IOException - if credentials file not found.
39+
*/
40+
public static CourseAlias addAliasToCourse(String courseId) throws IOException {
41+
/* Load pre-authorized user credentials from the environment.
42+
TODO(developer) - See https://developers.google.com/identity for
43+
guides on implementing OAuth2 for your application. */
44+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
45+
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES));
46+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
47+
credentials);
48+
49+
// Create the classroom API client.
50+
Classroom service = new Classroom.Builder(new NetHttpTransport(),
51+
GsonFactory.getDefaultInstance(),
52+
requestInitializer)
53+
.setApplicationName("Classroom samples")
54+
.build();
55+
56+
// [START classroom_add_alias_to_course_code_snippet]
57+
58+
/* Create a new CourseAlias object with a project-wide alias. Project-wide aliases use a prefix
59+
of "p:" and can only be seen and used by the application that created them. */
60+
CourseAlias content = new CourseAlias()
61+
.setAlias("p:biology_10");
62+
CourseAlias courseAlias = null;
63+
64+
try {
65+
courseAlias = service.courses().aliases().create(courseId, content)
66+
.execute();
67+
System.out.printf("Course alias created: %s \n", courseAlias.getAlias());
68+
} catch (GoogleJsonResponseException e) {
69+
//TODO (developer) - handle error appropriately
70+
GoogleJsonError error = e.getDetails();
71+
if (error.getCode() == 409) {
72+
System.out.printf("The course alias already exists: %s.\n", content);
73+
} else {
74+
throw e;
75+
}
76+
} catch (Exception e) {
77+
throw e;
78+
}
79+
return courseAlias;
80+
81+
// [END classroom_add_alias_to_course_code_snippet]
82+
83+
}
84+
}
85+
// [END classroom_add_alias_to_course_class]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2022 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+
// https://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+
16+
// [START classroom_create_course_with_alias_class]
17+
import com.google.api.client.googleapis.json.GoogleJsonError;
18+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
19+
import com.google.api.client.http.HttpRequestInitializer;
20+
import com.google.api.client.http.javanet.NetHttpTransport;
21+
import com.google.api.client.json.gson.GsonFactory;
22+
import com.google.api.services.classroom.Classroom;
23+
import com.google.api.services.classroom.ClassroomScopes;
24+
import com.google.api.services.classroom.model.Course;
25+
import com.google.auth.http.HttpCredentialsAdapter;
26+
import com.google.auth.oauth2.GoogleCredentials;
27+
import java.io.IOException;
28+
import java.util.Collections;
29+
30+
/* Class to demonstrate how to create a course with an alias. */
31+
public class CreateCourseWithAlias {
32+
/**
33+
* Create a new course with an alias. Set the new course id to the desired alias.
34+
*
35+
* @return - newly created course.
36+
* @throws IOException - if credentials file not found.
37+
*/
38+
public static Course createCourseWithAlias() throws IOException {
39+
/* Load pre-authorized user credentials from the environment.
40+
TODO(developer) - See https://developers.google.com/identity for
41+
guides on implementing OAuth2 for your application. */
42+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
43+
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES));
44+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
45+
credentials);
46+
47+
// Create the classroom API client.
48+
Classroom service = new Classroom.Builder(new NetHttpTransport(),
49+
GsonFactory.getDefaultInstance(),
50+
requestInitializer)
51+
.setApplicationName("Classroom samples")
52+
.build();
53+
54+
// [START classroom_create_course_with_alias_code_snippet]
55+
56+
Course course = null;
57+
58+
/* Create a new Course with the alias set as the id field. Project-wide aliases use a prefix
59+
of "p:" and can only be seen and used by the application that created them. */
60+
Course content = new Course()
61+
.setId("p:history_4_2022")
62+
.setName("9th Grade History")
63+
.setSection("Period 4")
64+
.setDescriptionHeading("Welcome to 9th Grade History.")
65+
.setOwnerId("me")
66+
.setCourseState("PROVISIONED");
67+
68+
try {
69+
course = service.courses().create(content).execute();
70+
// Prints the new created course id and name
71+
System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId());
72+
} catch (GoogleJsonResponseException e) {
73+
//TODO (developer) - handle error appropriately
74+
GoogleJsonError error = e.getDetails();
75+
if (error.getCode() == 409) {
76+
System.out.printf("The course alias already exists: %s.\n", content.getId());
77+
} else {
78+
throw e;
79+
}
80+
} catch (Exception e) {
81+
throw e;
82+
}
83+
return course;
84+
85+
// [END classroom_create_course_with_alias_code_snippet]
86+
87+
}
88+
}
89+
// [END classroom_create_course_with_alias_class]
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2022 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+
// https://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+
16+
// [START classroom_create_coursework_class]
17+
import com.google.api.client.googleapis.json.GoogleJsonError;
18+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
19+
import com.google.api.client.http.HttpRequestInitializer;
20+
import com.google.api.client.http.javanet.NetHttpTransport;
21+
import com.google.api.client.json.gson.GsonFactory;
22+
import com.google.api.services.classroom.Classroom;
23+
import com.google.api.services.classroom.ClassroomScopes;
24+
import com.google.api.services.classroom.model.CourseWork;
25+
import com.google.api.services.classroom.model.Date;
26+
import com.google.api.services.classroom.model.Link;
27+
import com.google.api.services.classroom.model.Material;
28+
import com.google.api.services.classroom.model.TimeOfDay;
29+
import com.google.auth.http.HttpCredentialsAdapter;
30+
import com.google.auth.oauth2.GoogleCredentials;
31+
import java.io.IOException;
32+
import java.util.Arrays;
33+
import java.util.Collections;
34+
import java.util.List;
35+
36+
/* Class to demonstrate the use of Classroom Create CourseWork API. */
37+
public class CreateCourseWork {
38+
/**
39+
* Creates course work.
40+
*
41+
* @param courseId - id of the course to create coursework in.
42+
* @return - newly created CourseWork object.
43+
* @throws IOException - if credentials file not found.
44+
*/
45+
public static CourseWork createCourseWork(String courseId) throws IOException {
46+
/* Load pre-authorized user credentials from the environment.
47+
TODO(developer) - See https://developers.google.com/identity for
48+
guides on implementing OAuth2 for your application. */
49+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
50+
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS));
51+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
52+
credentials);
53+
54+
// Create the classroom API client.
55+
Classroom service = new Classroom.Builder(new NetHttpTransport(),
56+
GsonFactory.getDefaultInstance(),
57+
requestInitializer)
58+
.setApplicationName("Classroom samples")
59+
.build();
60+
61+
// [START classroom_create_coursework_code_snippet]
62+
63+
CourseWork courseWork = null;
64+
try {
65+
// Create a link to add as a material on course work.
66+
Link articleLink = new Link()
67+
.setTitle("SR-71 Blackbird")
68+
.setUrl("https://www.lockheedmartin.com/en-us/news/features/history/blackbird.html");
69+
70+
// Create a list of Materials to add to course work.
71+
List<Material> materials = Arrays.asList(new Material().setLink(articleLink));
72+
73+
/* Create new CourseWork object with the material attached.
74+
Set workType to `ASSIGNMENT`. Possible values of workType can be found here:
75+
https://developers.google.com/classroom/reference/rest/v1/CourseWorkType
76+
Set state to `PUBLISHED`. Possible values of state can be found here:
77+
https://developers.google.com/classroom/reference/rest/v1/courses.courseWork#courseworkstate */
78+
CourseWork content = new CourseWork()
79+
.setTitle("Supersonic aviation")
80+
.setDescription("Read about how the SR-71 Blackbird, the world’s fastest and "
81+
+ "highest-flying manned aircraft, was built.")
82+
.setMaterials(materials)
83+
.setDueDate(new Date().setMonth(12).setDay(10).setYear(2022))
84+
.setDueTime(new TimeOfDay().setHours(15).setMinutes(0))
85+
.setWorkType("ASSIGNMENT")
86+
.setState("PUBLISHED");
87+
88+
courseWork = service.courses().courseWork().create(courseId, content)
89+
.execute();
90+
} catch (GoogleJsonResponseException e) {
91+
//TODO (developer) - handle error appropriately
92+
GoogleJsonError error = e.getDetails();
93+
if (error.getCode() == 404) {
94+
System.out.printf("The courseId does not exist: %s.\n", courseId);
95+
} else {
96+
throw e;
97+
}
98+
throw e;
99+
} catch (Exception e) {
100+
throw e;
101+
}
102+
return courseWork;
103+
104+
// [END classroom_create_coursework_code_snippet]
105+
}
106+
}
107+
// [END classroom_create_coursework_class]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2022 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+
// https://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+
16+
// [START classroom_create_topic_class]
17+
18+
import com.google.api.client.googleapis.json.GoogleJsonError;
19+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20+
import com.google.api.client.http.HttpRequestInitializer;
21+
import com.google.api.client.http.javanet.NetHttpTransport;
22+
import com.google.api.client.json.gson.GsonFactory;
23+
import com.google.api.services.classroom.Classroom;
24+
import com.google.api.services.classroom.ClassroomScopes;
25+
import com.google.api.services.classroom.model.Topic;
26+
import com.google.auth.http.HttpCredentialsAdapter;
27+
import com.google.auth.oauth2.GoogleCredentials;
28+
import java.io.IOException;
29+
import java.util.Collections;
30+
31+
/* Class to demonstrate how to create a topic. */
32+
public class CreateTopic {
33+
/**
34+
* Create a new topic in a course.
35+
*
36+
* @param courseId - the id of the course to create a topic in.
37+
* @return - newly created topic.
38+
* @throws IOException - if credentials file not found.
39+
*/
40+
public static Topic createTopic(String courseId) throws IOException {
41+
/* Load pre-authorized user credentials from the environment.
42+
TODO(developer) - See https://developers.google.com/identity for
43+
guides on implementing OAuth2 for your application. */
44+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
45+
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS));
46+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
47+
credentials);
48+
49+
// Create the classroom API client.
50+
Classroom service = new Classroom.Builder(new NetHttpTransport(),
51+
GsonFactory.getDefaultInstance(),
52+
requestInitializer)
53+
.setApplicationName("Classroom samples")
54+
.build();
55+
56+
// [START classroom_create_topic_code_snippet]
57+
58+
Topic topic = null;
59+
try {
60+
// Create the new Topic.
61+
Topic content = new Topic().setName("Semester 1");
62+
topic = service.courses().topics().create(courseId, content).execute();
63+
System.out.println("Topic id: " + topic.getTopicId() + "\n" + "Course id: " + courseId);
64+
} catch (GoogleJsonResponseException e) {
65+
//TODO (developer) - handle error appropriately
66+
GoogleJsonError error = e.getDetails();
67+
if (error.getCode() == 404) {
68+
System.out.printf("The courseId does not exist: %s.\n", courseId);
69+
} else {
70+
throw e;
71+
}
72+
} catch (Exception e) {
73+
throw e;
74+
}
75+
return topic;
76+
77+
// [END classroom_create_topic_code_snippet]
78+
79+
}
80+
}
81+
// [END classroom_create_topic_class]

0 commit comments

Comments
 (0)