From d94f7d1b2ef6c56fc7bf39011c3221f5b63d08ba Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 1 Dec 2022 12:41:15 -0500 Subject: [PATCH 01/11] add alias snippet and unit test --- .../snippets/src/main/java/AddAlias.java | 72 +++++++++++++++++++ .../snippets/src/test/java/BaseTest.java | 4 ++ .../snippets/src/test/java/TestAddAlias.java | 28 ++++++++ 3 files changed, 104 insertions(+) create mode 100644 classroom/snippets/src/main/java/AddAlias.java create mode 100644 classroom/snippets/src/test/java/TestAddAlias.java diff --git a/classroom/snippets/src/main/java/AddAlias.java b/classroom/snippets/src/main/java/AddAlias.java new file mode 100644 index 00000000..1a81ba9d --- /dev/null +++ b/classroom/snippets/src/main/java/AddAlias.java @@ -0,0 +1,72 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_add_alias] + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.CourseAlias; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate the use of Classroom Create Alias API */ +public class AddAlias { + /** + * Create an aliases for an existing course. + * + * @param courseId - id of the course to add an alias to. + * @return - newly created course alias. + * @throws IOException - if credentials file not found. + */ + public static CourseAlias addCourseAlias(String courseId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + CourseAlias courseAlias = null; + try { + // Create new CourseAlias object + CourseAlias content = new CourseAlias() + .setAlias("p:test_alias_2"); + courseAlias = service.courses().aliases().create(courseId, content) + .execute(); + System.out.printf("Course alias created: %s \n", courseAlias.getAlias()); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + return courseAlias; + } +} +// [END classroom_add_alias] diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index c1e46717..cc3996de 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -80,6 +80,10 @@ public CourseAlias createAlias(String courseId) throws IOException { } public void deleteCourse(String courseId) throws IOException { + // updating the course state to be archived so the course can be deleted. + Course course = service.courses().get(courseId).execute(); + course.setCourseState("ARCHIVED"); + this.service.courses().update(courseId, course).execute(); this.service.courses().delete(courseId).execute(); } } diff --git a/classroom/snippets/src/test/java/TestAddAlias.java b/classroom/snippets/src/test/java/TestAddAlias.java new file mode 100644 index 00000000..3551c638 --- /dev/null +++ b/classroom/snippets/src/test/java/TestAddAlias.java @@ -0,0 +1,28 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.services.classroom.model.CourseAlias; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Add Alias classroom snippet +public class TestAddAlias extends BaseTest { + + @Test + public void testAddAlias() throws IOException { + CourseAlias courseAlias = AddAlias.addCourseAlias(testCourse.getId()); + Assert.assertNotNull("Course alias not returned.", courseAlias); + } +} \ No newline at end of file From abd08eca60aad6409d0ac91a9047ee1e8d9f1772 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 1 Dec 2022 18:04:14 -0500 Subject: [PATCH 02/11] java snippets for topics, create coursework, create alias --- .../snippets/src/main/java/CreateAlias.java | 75 ++++++++++++++++ .../src/main/java/CreateCourseWork.java | 72 +++++++++++++++ .../snippets/src/main/java/CreateTopic.java | 69 +++++++++++++++ .../snippets/src/main/java/DeleteTopic.java | 64 ++++++++++++++ .../snippets/src/main/java/GetTopic.java | 68 +++++++++++++++ .../snippets/src/main/java/ListTopics.java | 87 +++++++++++++++++++ .../snippets/src/main/java/UpdateTopic.java | 71 +++++++++++++++ .../src/test/java/TestCreateAlias.java | 29 +++++++ .../src/test/java/TestCreateCourseWork.java | 28 ++++++ .../src/test/java/TestCreateTopic.java | 28 ++++++ .../src/test/java/TestDeleteTopic.java | 15 ++++ .../snippets/src/test/java/TestGetTopic.java | 31 +++++++ .../src/test/java/TestListTopics.java | 31 +++++++ .../src/test/java/TestUpdateTopic.java | 28 ++++++ 14 files changed, 696 insertions(+) create mode 100644 classroom/snippets/src/main/java/CreateAlias.java create mode 100644 classroom/snippets/src/main/java/CreateCourseWork.java create mode 100644 classroom/snippets/src/main/java/CreateTopic.java create mode 100644 classroom/snippets/src/main/java/DeleteTopic.java create mode 100644 classroom/snippets/src/main/java/GetTopic.java create mode 100644 classroom/snippets/src/main/java/ListTopics.java create mode 100644 classroom/snippets/src/main/java/UpdateTopic.java create mode 100644 classroom/snippets/src/test/java/TestCreateAlias.java create mode 100644 classroom/snippets/src/test/java/TestCreateCourseWork.java create mode 100644 classroom/snippets/src/test/java/TestCreateTopic.java create mode 100644 classroom/snippets/src/test/java/TestDeleteTopic.java create mode 100644 classroom/snippets/src/test/java/TestGetTopic.java create mode 100644 classroom/snippets/src/test/java/TestListTopics.java create mode 100644 classroom/snippets/src/test/java/TestUpdateTopic.java diff --git a/classroom/snippets/src/main/java/CreateAlias.java b/classroom/snippets/src/main/java/CreateAlias.java new file mode 100644 index 00000000..3ccd7660 --- /dev/null +++ b/classroom/snippets/src/main/java/CreateAlias.java @@ -0,0 +1,75 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_create_alias] +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.Course; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate how to create a course with an alias. */ +public class CreateAlias { + /** + * Create a new course with an alias. Set the new course id to the desired alias. + * + * @return - newly created course. + * @throws IOException - if credentials file not found. + */ + public static Course createAlias() throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + Course course = null; + try { + // Create the new Course + Course content = new Course() + .setId("p:history_4_2022") + .setName("9th Grade History") + .setSection("Period 4") + .setDescriptionHeading("Welcome to 9th Grade History.") + .setOwnerId("me") + .setCourseState("PROVISIONED"); + course = service.courses().create(content).execute(); + // Prints the new created course id and name + System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId()); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + return course; + } +} +// [END classroom_create_alias] diff --git a/classroom/snippets/src/main/java/CreateCourseWork.java b/classroom/snippets/src/main/java/CreateCourseWork.java new file mode 100644 index 00000000..94291230 --- /dev/null +++ b/classroom/snippets/src/main/java/CreateCourseWork.java @@ -0,0 +1,72 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_create_coursework] +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.CourseWork; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate the use of Classroom Create CourseWork API. */ +public class CreateCourseWork { + /** + * Creates course work. + * + * @param courseId - id of the course to create coursework in. + * @return - newly created CourseWork object + * @throws IOException - if credentials file not found. + */ + public static CourseWork createCourseWork(String courseId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + CourseWork courseWork = null; + try { + // Create new CourseWork object + CourseWork content = new CourseWork() + .setTitle("Midterm Quiz") + .setWorkType("ASSIGNMENT") + .setState("PUBLISHED"); + courseWork = service.courses().courseWork().create(courseId, content) + .execute(); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + return courseWork; + } +} +// [END classroom_create_coursework] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/CreateTopic.java b/classroom/snippets/src/main/java/CreateTopic.java new file mode 100644 index 00000000..a7bf7e68 --- /dev/null +++ b/classroom/snippets/src/main/java/CreateTopic.java @@ -0,0 +1,69 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_create_topic] + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.Topic; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate how to create a topic. */ +public class CreateTopic { + /** + * Create a new topic in a course. + * + * @param courseId - the id of the course to create a topic in. + * @return - newly created topic. + * @throws IOException - if credentials file not found. + */ + public static Topic createTopic(String courseId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + Topic topic = null; + try { + // Create the new Topic + Topic content = new Topic().setName("Semester 1"); + topic = service.courses().topics().create(courseId, content).execute(); + System.out.println("Topic id: " + topic.getTopicId() + "\n" + "Course id: " + courseId); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + return topic; + } +} diff --git a/classroom/snippets/src/main/java/DeleteTopic.java b/classroom/snippets/src/main/java/DeleteTopic.java new file mode 100644 index 00000000..eddd2f5c --- /dev/null +++ b/classroom/snippets/src/main/java/DeleteTopic.java @@ -0,0 +1,64 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_delete_topic] + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate how to delete a topic. */ +public class DeleteTopic { + /** + * Delete a topic in a course. + * + * @param courseId - the id of the course where the topic belongs. + * @param topicId - the id of the topic to delete. + * @return - updated topic. + * @throws IOException - if credentials file not found. + */ + public static void deleteTopic(String courseId, String topicId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + try { + service.courses().topics().delete(courseId, topicId).execute(); + } catch (GoogleJsonResponseException e) { + // TODO(developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + } +} diff --git a/classroom/snippets/src/main/java/GetTopic.java b/classroom/snippets/src/main/java/GetTopic.java new file mode 100644 index 00000000..d40eb533 --- /dev/null +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -0,0 +1,68 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_get_topic] + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.Topic; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate how to get a topic. */ +public class GetTopic { + /** + * Get a topic in a course. + * + * @param courseId - the id of the course the topic belongs to. + * @param topicId - the id of the topic to retrieve. + * @return - the topic to retrieve. + * @throws IOException - if credentials file not found. + */ + public static Topic getTopic(String courseId, String topicId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + Topic topic = null; + try { + // Get the topic + topic = service.courses().topics().get(courseId, topicId).execute(); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + return topic; + } +} diff --git a/classroom/snippets/src/main/java/ListTopics.java b/classroom/snippets/src/main/java/ListTopics.java new file mode 100644 index 00000000..8dd7efd1 --- /dev/null +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -0,0 +1,87 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_list_topic] + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.ListTopicResponse; +import com.google.api.services.classroom.model.Topic; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/* Class to demonstrate how to list topics in a course. */ +public class ListTopics { + /** + * List topics in a course. + * + * @param courseId - the id of the course to retrieve topics for. + * @return - the list of topics in the course that the caller is permitted to view. + * @throws IOException - if credentials file not found. + */ + public static List listTopics(String courseId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + List topics = new ArrayList<>(); + String pageToken = null; + + try { + do { + ListTopicResponse response = service.courses().topics().list(courseId) + .setPageSize(100) + .setPageToken(pageToken) + .execute(); + topics.addAll(response.getTopic()); + pageToken = response.getNextPageToken(); + } while (pageToken != null); + + if (topics.isEmpty()) { + System.out.println("No topics found."); + } else { + for (Topic topic : topics) { + System.out.printf("%s (%s)\n", topic.getName(), topic.getTopicId()); + } + } + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + return topics; + } + +} diff --git a/classroom/snippets/src/main/java/UpdateTopic.java b/classroom/snippets/src/main/java/UpdateTopic.java new file mode 100644 index 00000000..8278c76d --- /dev/null +++ b/classroom/snippets/src/main/java/UpdateTopic.java @@ -0,0 +1,71 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_update_topic] + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.Topic; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate how to update one or more fields in a topic. */ +public class UpdateTopic { + /** + * Update one or more fields in a topic in a course. + * + * @param courseId - the id of the course where the topic belongs. + * @param topicId - the id of the topic to update. + * @return - updated topic. + * @throws IOException - if credentials file not found. + */ + public static Topic updateTopic(String courseId, String topicId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + Topic topic = null; + try { + Topic topicToUpdate = service.courses().topics().get(courseId, topicId).execute(); + topicToUpdate.setName("Semester 2"); + topic = service.courses().topics().patch(courseId, topicId, topicToUpdate) + .set("updateMask", "name") + .execute(); + } catch(GoogleJsonResponseException e) { + // TODO(developer) - handle error appropriately + throw e; + } catch (Exception e) { + throw e; + } + return topic; + } +} diff --git a/classroom/snippets/src/test/java/TestCreateAlias.java b/classroom/snippets/src/test/java/TestCreateAlias.java new file mode 100644 index 00000000..e1aa8813 --- /dev/null +++ b/classroom/snippets/src/test/java/TestCreateAlias.java @@ -0,0 +1,29 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Create Alias classroom snippet +public class TestCreateAlias extends BaseTest { + + @Test + public void testCreateAlias() throws IOException { + Course course = CreateAlias.createAlias(); + Assert.assertNotNull("Course not returned.", course); + deleteCourse(course.getId()); + } +} \ No newline at end of file diff --git a/classroom/snippets/src/test/java/TestCreateCourseWork.java b/classroom/snippets/src/test/java/TestCreateCourseWork.java new file mode 100644 index 00000000..e3a25b6b --- /dev/null +++ b/classroom/snippets/src/test/java/TestCreateCourseWork.java @@ -0,0 +1,28 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.services.classroom.model.CourseWork; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Create Coursework classroom snippet +public class TestCreateCourseWork extends BaseTest { + + @Test + public void testCreateCoursework() throws IOException { + CourseWork courseWork = CreateCourseWork.createCourseWork(testCourse.getId()); + Assert.assertNotNull("Coursework not returned.", courseWork); + } +} diff --git a/classroom/snippets/src/test/java/TestCreateTopic.java b/classroom/snippets/src/test/java/TestCreateTopic.java new file mode 100644 index 00000000..e2d77586 --- /dev/null +++ b/classroom/snippets/src/test/java/TestCreateTopic.java @@ -0,0 +1,28 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.services.classroom.model.Topic; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Create Topic classroom snippet +public class TestCreateTopic extends BaseTest { + + @Test + public void testCreateTopic() throws IOException { + Topic topic = CreateTopic.createTopic(testCourse.getId()); + Assert.assertNotNull("Topic not returned.", topic); + } +} diff --git a/classroom/snippets/src/test/java/TestDeleteTopic.java b/classroom/snippets/src/test/java/TestDeleteTopic.java new file mode 100644 index 00000000..03cb77f4 --- /dev/null +++ b/classroom/snippets/src/test/java/TestDeleteTopic.java @@ -0,0 +1,15 @@ +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.services.classroom.model.Topic; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +public class TestDeleteTopic extends BaseTest { + @Test + public void testDeleteTopic() throws IOException { + Topic topic = CreateTopic.createTopic(testCourse.getId()); + DeleteTopic.deleteTopic(testCourse.getId(), topic.getTopicId()); + Assert.assertThrows(GoogleJsonResponseException.class, + () -> GetTopic.getTopic(testCourse.getId(), topic.getTopicId())); + } +} diff --git a/classroom/snippets/src/test/java/TestGetTopic.java b/classroom/snippets/src/test/java/TestGetTopic.java new file mode 100644 index 00000000..b7cb7840 --- /dev/null +++ b/classroom/snippets/src/test/java/TestGetTopic.java @@ -0,0 +1,31 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.services.classroom.model.Topic; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Get Topic classroom snippet +public class TestGetTopic extends BaseTest { + + @Test + public void testGetTopic() throws IOException { + Topic topic = CreateTopic.createTopic(testCourse.getId()); + Topic getTopic = GetTopic.getTopic(testCourse.getId(), topic.getTopicId()); + Assert.assertNotNull("Topic could not be created.", topic); + Assert.assertNotNull("Topic could not be retrieved.", getTopic); + Assert.assertEquals("Wrong topic was retrieved.", topic, getTopic); + } +} diff --git a/classroom/snippets/src/test/java/TestListTopics.java b/classroom/snippets/src/test/java/TestListTopics.java new file mode 100644 index 00000000..081be57b --- /dev/null +++ b/classroom/snippets/src/test/java/TestListTopics.java @@ -0,0 +1,31 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.services.classroom.model.Topic; +import java.io.IOException; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for List Topics classroom snippet +public class TestListTopics extends BaseTest { + + @Test + public void testListTopics() throws IOException { + CreateTopic.createTopic(testCourse.getId()); + List listTopics = ListTopics.listTopics(testCourse.getId()); + Assert.assertNotNull("Topics could not be retrieved.", listTopics); + Assert.assertFalse("No topics were retrieved.", listTopics.size() == 0); + } +} diff --git a/classroom/snippets/src/test/java/TestUpdateTopic.java b/classroom/snippets/src/test/java/TestUpdateTopic.java new file mode 100644 index 00000000..5b1b9eec --- /dev/null +++ b/classroom/snippets/src/test/java/TestUpdateTopic.java @@ -0,0 +1,28 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +import com.google.api.services.classroom.model.Topic; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Update Topic classroom snippet +public class TestUpdateTopic extends BaseTest { + @Test + public void testUpdateTopic() throws IOException { + Topic topic = CreateTopic.createTopic(testCourse.getId()); + System.out.println("New topic created: " + topic.getName()); + Topic updatedTopic = UpdateTopic.updateTopic(testCourse.getId(), topic.getTopicId()); + Assert.assertNotEquals("Topic name was not updated.", topic.getName(), updatedTopic.getName()); + } +} From ac9b45805ca6caa318a4337d49b6299d2977264a Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 6 Dec 2022 14:43:30 -0500 Subject: [PATCH 03/11] updates based on team review --- .../snippets/src/main/java/AddAlias.java | 22 +++++++---- .../snippets/src/main/java/CreateAlias.java | 29 ++++++++++----- .../src/main/java/CreateCourseWork.java | 37 +++++++++++++++++-- .../snippets/src/main/java/CreateTopic.java | 12 ++++-- .../snippets/src/main/java/DeleteTopic.java | 10 ++++- .../snippets/src/main/java/GetTopic.java | 12 ++++-- .../snippets/src/main/java/ListTopics.java | 10 ++++- .../snippets/src/main/java/UpdateTopic.java | 16 +++++++- 8 files changed, 116 insertions(+), 32 deletions(-) diff --git a/classroom/snippets/src/main/java/AddAlias.java b/classroom/snippets/src/main/java/AddAlias.java index 1a81ba9d..42c8d883 100644 --- a/classroom/snippets/src/main/java/AddAlias.java +++ b/classroom/snippets/src/main/java/AddAlias.java @@ -15,6 +15,7 @@ // [START classroom_add_alias] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -27,10 +28,10 @@ import java.io.IOException; import java.util.Collections; -/* Class to demonstrate the use of Classroom Create Alias API */ +/* Class to demonstrate the use of Classroom Create Alias API. */ public class AddAlias { /** - * Create an aliases for an existing course. + * Add an alias on an existing course. * * @param courseId - id of the course to add an alias to. * @return - newly created course alias. @@ -45,24 +46,31 @@ public static CourseAlias addCourseAlias(String courseId) throws IOException { HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) .setApplicationName("Classroom samples") .build(); + /* Create a new CourseAlias object with a project-wide alias. Project-wide aliases use a prefix + of "p:" and can only be seen and used by the application that created them. */ + CourseAlias content = new CourseAlias() + .setAlias("p:biology_10"); CourseAlias courseAlias = null; + try { - // Create new CourseAlias object - CourseAlias content = new CourseAlias() - .setAlias("p:test_alias_2"); courseAlias = service.courses().aliases().create(courseId, content) .execute(); System.out.printf("Course alias created: %s \n", courseAlias.getAlias()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately - throw e; + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 409) { + System.out.printf("The course alias already exists: %s.\n", content); + } else { + throw e; + } } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/CreateAlias.java b/classroom/snippets/src/main/java/CreateAlias.java index 3ccd7660..bc50f0f8 100644 --- a/classroom/snippets/src/main/java/CreateAlias.java +++ b/classroom/snippets/src/main/java/CreateAlias.java @@ -14,6 +14,7 @@ // [START classroom_create_alias] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -43,7 +44,7 @@ public static Course createAlias() throws IOException { HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) @@ -51,21 +52,29 @@ public static Course createAlias() throws IOException { .build(); Course course = null; + + /* Create a new Course with the alias set as the id field. Project-wide aliases use a prefix + of "p:" and can only be seen and used by the application that created them. */ + Course content = new Course() + .setId("p:history_4_2022") + .setName("9th Grade History") + .setSection("Period 4") + .setDescriptionHeading("Welcome to 9th Grade History.") + .setOwnerId("me") + .setCourseState("PROVISIONED"); + try { - // Create the new Course - Course content = new Course() - .setId("p:history_4_2022") - .setName("9th Grade History") - .setSection("Period 4") - .setDescriptionHeading("Welcome to 9th Grade History.") - .setOwnerId("me") - .setCourseState("PROVISIONED"); course = service.courses().create(content).execute(); // Prints the new created course id and name System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately - throw e; + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 409) { + System.out.printf("The course alias already exists: %s.\n", content.getId()); + } else { + throw e; + } } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/CreateCourseWork.java b/classroom/snippets/src/main/java/CreateCourseWork.java index 94291230..11dfab7f 100644 --- a/classroom/snippets/src/main/java/CreateCourseWork.java +++ b/classroom/snippets/src/main/java/CreateCourseWork.java @@ -14,6 +14,7 @@ // [START classroom_create_coursework] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -21,10 +22,16 @@ import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.CourseWork; +import com.google.api.services.classroom.model.Date; +import com.google.api.services.classroom.model.Link; +import com.google.api.services.classroom.model.Material; +import com.google.api.services.classroom.model.TimeOfDay; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; +import java.util.List; /* Class to demonstrate the use of Classroom Create CourseWork API. */ public class CreateCourseWork { @@ -44,7 +51,7 @@ public static CourseWork createCourseWork(String courseId) throws IOException { HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) @@ -53,15 +60,39 @@ public static CourseWork createCourseWork(String courseId) throws IOException { CourseWork courseWork = null; try { - // Create new CourseWork object + // Create a link to add as a material on course work. + Link articleLink = new Link() + .setTitle("SR-71 Blackbird") + .setUrl("https://www.lockheedmartin.com/en-us/news/features/history/blackbird.html"); + + // Create a list of Materials to add to course work. + List materials = Arrays.asList(new Material().setLink(articleLink)); + + /* Create new CourseWork object with the material attached. + Set workType to `ASSIGNMENT`. Possible values of workType can be found here: + https://developers.google.com/classroom/reference/rest/v1/CourseWorkType + Set state to `PUBLISHED`. Possible values of state can be found here: + https://developers.google.com/classroom/reference/rest/v1/courses.courseWork#courseworkstate */ CourseWork content = new CourseWork() - .setTitle("Midterm Quiz") + .setTitle("Supersonic aviation") + .setDescription("Read about how the SR-71 Blackbird, the world’s fastest and " + + "highest-flying manned aircraft, was built.") + .setMaterials(materials) + .setDueDate(new Date().setMonth(12).setDay(10).setYear(2022)) + .setDueTime(new TimeOfDay().setHours(15).setMinutes(0)) .setWorkType("ASSIGNMENT") .setState("PUBLISHED"); + courseWork = service.courses().courseWork().create(courseId, content) .execute(); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId does not exist: %s.\n", courseId); + } else { + throw e; + } throw e; } catch (Exception e) { throw e; diff --git a/classroom/snippets/src/main/java/CreateTopic.java b/classroom/snippets/src/main/java/CreateTopic.java index a7bf7e68..ce8e61f7 100644 --- a/classroom/snippets/src/main/java/CreateTopic.java +++ b/classroom/snippets/src/main/java/CreateTopic.java @@ -15,6 +15,7 @@ // [START classroom_create_topic] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -45,7 +46,7 @@ public static Topic createTopic(String courseId) throws IOException { HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) @@ -54,13 +55,18 @@ public static Topic createTopic(String courseId) throws IOException { Topic topic = null; try { - // Create the new Topic + // Create the new Topic. Topic content = new Topic().setName("Semester 1"); topic = service.courses().topics().create(courseId, content).execute(); System.out.println("Topic id: " + topic.getTopicId() + "\n" + "Course id: " + courseId); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately - throw e; + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId does not exist: %s.\n", courseId); + } else { + throw e; + } } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/DeleteTopic.java b/classroom/snippets/src/main/java/DeleteTopic.java index eddd2f5c..fab242c9 100644 --- a/classroom/snippets/src/main/java/DeleteTopic.java +++ b/classroom/snippets/src/main/java/DeleteTopic.java @@ -15,6 +15,7 @@ // [START classroom_delete_topic] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -45,7 +46,7 @@ public static void deleteTopic(String courseId, String topicId) throws IOExcepti HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) @@ -56,7 +57,12 @@ public static void deleteTopic(String courseId, String topicId) throws IOExcepti service.courses().topics().delete(courseId, topicId).execute(); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately - throw e; + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId); + } else { + throw e; + } } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/GetTopic.java b/classroom/snippets/src/main/java/GetTopic.java index d40eb533..5ffe87b4 100644 --- a/classroom/snippets/src/main/java/GetTopic.java +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -15,6 +15,7 @@ // [START classroom_get_topic] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -46,7 +47,7 @@ public static Topic getTopic(String courseId, String topicId) throws IOException HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) @@ -55,11 +56,16 @@ public static Topic getTopic(String courseId, String topicId) throws IOException Topic topic = null; try { - // Get the topic + // Get the topic. topic = service.courses().topics().get(courseId, topicId).execute(); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately - throw e; + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId); + } else { + throw e; + } } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/ListTopics.java b/classroom/snippets/src/main/java/ListTopics.java index 8dd7efd1..ffed9712 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -15,6 +15,7 @@ // [START classroom_list_topic] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -48,7 +49,7 @@ public static List listTopics(String courseId) throws IOException { HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) @@ -77,7 +78,12 @@ public static List listTopics(String courseId) throws IOException { } } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately - throw e; + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId does not exist: %s.\n", courseId); + } else { + throw e; + } } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/UpdateTopic.java b/classroom/snippets/src/main/java/UpdateTopic.java index 8278c76d..49bde0c0 100644 --- a/classroom/snippets/src/main/java/UpdateTopic.java +++ b/classroom/snippets/src/main/java/UpdateTopic.java @@ -15,6 +15,7 @@ // [START classroom_update_topic] +import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -46,7 +47,7 @@ public static Topic updateTopic(String courseId, String topicId) throws IOExcept HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); - // Create the classroom API client + // Create the classroom API client. Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) @@ -55,14 +56,25 @@ public static Topic updateTopic(String courseId, String topicId) throws IOExcept Topic topic = null; try { + // Retrieve the topic to update. Topic topicToUpdate = service.courses().topics().get(courseId, topicId).execute(); + + // Update the name field for the topic retrieved. topicToUpdate.setName("Semester 2"); + + /* Call the patch endpoint and set the updateMask query parameter to the field that needs to + be updated. */ topic = service.courses().topics().patch(courseId, topicId, topicToUpdate) .set("updateMask", "name") .execute(); } catch(GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately - throw e; + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId); + } else { + throw e; + } } catch (Exception e) { throw e; } From a5cbd302d230e45b72d45e5af108b5f531b63b53 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 9 Dec 2022 13:38:10 -0500 Subject: [PATCH 04/11] renamed alias methods, added devsite comments --- .../java/{AddAlias.java => AddAliasToCourse.java} | 13 +++++++++---- ...{CreateAlias.java => CreateCourseWithAlias.java} | 13 +++++++++---- .../snippets/src/main/java/CreateCourseWork.java | 10 +++++++--- classroom/snippets/src/main/java/CreateTopic.java | 8 +++++++- classroom/snippets/src/main/java/DeleteTopic.java | 8 +++++++- classroom/snippets/src/main/java/GetTopic.java | 8 +++++++- classroom/snippets/src/main/java/ListTopics.java | 9 +++++++-- classroom/snippets/src/main/java/UpdateTopic.java | 8 +++++++- ...{TestAddAlias.java => TestAddAliasToCourse.java} | 12 +++++++++--- ...ateAlias.java => TestCreateCourseWithAlias.java} | 13 ++++++++++--- 10 files changed, 79 insertions(+), 23 deletions(-) rename classroom/snippets/src/main/java/{AddAlias.java => AddAliasToCourse.java} (90%) rename classroom/snippets/src/main/java/{CreateAlias.java => CreateCourseWithAlias.java} (90%) rename classroom/snippets/src/test/java/{TestAddAlias.java => TestAddAliasToCourse.java} (66%) rename classroom/snippets/src/test/java/{TestCreateAlias.java => TestCreateCourseWithAlias.java} (64%) diff --git a/classroom/snippets/src/main/java/AddAlias.java b/classroom/snippets/src/main/java/AddAliasToCourse.java similarity index 90% rename from classroom/snippets/src/main/java/AddAlias.java rename to classroom/snippets/src/main/java/AddAliasToCourse.java index 42c8d883..10434391 100644 --- a/classroom/snippets/src/main/java/AddAlias.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_add_alias] +// [START classroom_add_alias_to_course_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; @@ -29,7 +29,7 @@ import java.util.Collections; /* Class to demonstrate the use of Classroom Create Alias API. */ -public class AddAlias { +public class AddAliasToCourse { /** * Add an alias on an existing course. * @@ -37,7 +37,7 @@ public class AddAlias { * @return - newly created course alias. * @throws IOException - if credentials file not found. */ - public static CourseAlias addCourseAlias(String courseId) throws IOException { + public static CourseAlias addAliasToCourse(String courseId) throws IOException { /* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ @@ -53,6 +53,8 @@ public static CourseAlias addCourseAlias(String courseId) throws IOException { .setApplicationName("Classroom samples") .build(); + // [START classroom_add_alias_to_course_code_snippet] + /* Create a new CourseAlias object with a project-wide alias. Project-wide aliases use a prefix of "p:" and can only be seen and used by the application that created them. */ CourseAlias content = new CourseAlias() @@ -75,6 +77,9 @@ public static CourseAlias addCourseAlias(String courseId) throws IOException { throw e; } return courseAlias; + + // [END classroom_add_alias_to_course_code_snippet] + } } -// [END classroom_add_alias] +// [END classroom_add_alias_to_course_class] diff --git a/classroom/snippets/src/main/java/CreateAlias.java b/classroom/snippets/src/main/java/CreateCourseWithAlias.java similarity index 90% rename from classroom/snippets/src/main/java/CreateAlias.java rename to classroom/snippets/src/main/java/CreateCourseWithAlias.java index bc50f0f8..77a6521b 100644 --- a/classroom/snippets/src/main/java/CreateAlias.java +++ b/classroom/snippets/src/main/java/CreateCourseWithAlias.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_create_alias] +// [START classroom_create_course_with_alias_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; @@ -28,14 +28,14 @@ import java.util.Collections; /* Class to demonstrate how to create a course with an alias. */ -public class CreateAlias { +public class CreateCourseWithAlias { /** * Create a new course with an alias. Set the new course id to the desired alias. * * @return - newly created course. * @throws IOException - if credentials file not found. */ - public static Course createAlias() throws IOException { + public static Course createCourseWithAlias() throws IOException { /* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ @@ -51,6 +51,8 @@ public static Course createAlias() throws IOException { .setApplicationName("Classroom samples") .build(); + // [START classroom_create_course_with_alias_code_snippet] + Course course = null; /* Create a new Course with the alias set as the id field. Project-wide aliases use a prefix @@ -79,6 +81,9 @@ public static Course createAlias() throws IOException { throw e; } return course; + + // [END classroom_create_course_with_alias_code_snippet] + } } -// [END classroom_create_alias] +// [END classroom_create_course_with_alias_class] diff --git a/classroom/snippets/src/main/java/CreateCourseWork.java b/classroom/snippets/src/main/java/CreateCourseWork.java index 11dfab7f..631ced38 100644 --- a/classroom/snippets/src/main/java/CreateCourseWork.java +++ b/classroom/snippets/src/main/java/CreateCourseWork.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_create_coursework] +// [START classroom_create_coursework_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; @@ -39,7 +39,7 @@ public class CreateCourseWork { * Creates course work. * * @param courseId - id of the course to create coursework in. - * @return - newly created CourseWork object + * @return - newly created CourseWork object. * @throws IOException - if credentials file not found. */ public static CourseWork createCourseWork(String courseId) throws IOException { @@ -58,6 +58,8 @@ public static CourseWork createCourseWork(String courseId) throws IOException { .setApplicationName("Classroom samples") .build(); + // [START classroom_create_coursework_code_snippet] + CourseWork courseWork = null; try { // Create a link to add as a material on course work. @@ -98,6 +100,8 @@ public static CourseWork createCourseWork(String courseId) throws IOException { throw e; } return courseWork; + + // [END classroom_create_coursework_code_snippet] } } -// [END classroom_create_coursework] \ No newline at end of file +// [END classroom_create_coursework_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/CreateTopic.java b/classroom/snippets/src/main/java/CreateTopic.java index ce8e61f7..a46e573f 100644 --- a/classroom/snippets/src/main/java/CreateTopic.java +++ b/classroom/snippets/src/main/java/CreateTopic.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_create_topic] +// [START classroom_create_topic_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; @@ -53,6 +53,8 @@ public static Topic createTopic(String courseId) throws IOException { .setApplicationName("Classroom samples") .build(); + // [START classroom_create_topic_code_snippet] + Topic topic = null; try { // Create the new Topic. @@ -71,5 +73,9 @@ public static Topic createTopic(String courseId) throws IOException { throw e; } return topic; + + // [END classroom_create_topic_code_snippet] + } } +// [END classroom_create_topic_class] diff --git a/classroom/snippets/src/main/java/DeleteTopic.java b/classroom/snippets/src/main/java/DeleteTopic.java index fab242c9..b8ee8f73 100644 --- a/classroom/snippets/src/main/java/DeleteTopic.java +++ b/classroom/snippets/src/main/java/DeleteTopic.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_delete_topic] +// [START classroom_delete_topic_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; @@ -53,6 +53,8 @@ public static void deleteTopic(String courseId, String topicId) throws IOExcepti .setApplicationName("Classroom samples") .build(); + // [START classroom_delete_topic_code_snippet] + try { service.courses().topics().delete(courseId, topicId).execute(); } catch (GoogleJsonResponseException e) { @@ -66,5 +68,9 @@ public static void deleteTopic(String courseId, String topicId) throws IOExcepti } catch (Exception e) { throw e; } + + // [END classroom_delete_topic_code_snippet] + } } +// [END classroom_delete_topic_class] diff --git a/classroom/snippets/src/main/java/GetTopic.java b/classroom/snippets/src/main/java/GetTopic.java index 5ffe87b4..5332bd5c 100644 --- a/classroom/snippets/src/main/java/GetTopic.java +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_get_topic] +// [START classroom_get_topic_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; @@ -54,6 +54,8 @@ public static Topic getTopic(String courseId, String topicId) throws IOException .setApplicationName("Classroom samples") .build(); + // [START classroom_get_topic_code_snippet] + Topic topic = null; try { // Get the topic. @@ -70,5 +72,9 @@ public static Topic getTopic(String courseId, String topicId) throws IOException throw e; } return topic; + + // [END classroom_get_topic_code_snippet] + } } +// [END classroom_get_topic_class] diff --git a/classroom/snippets/src/main/java/ListTopics.java b/classroom/snippets/src/main/java/ListTopics.java index ffed9712..61ea9b89 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_list_topic] +// [START classroom_list_topic_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; @@ -56,6 +56,8 @@ public static List listTopics(String courseId) throws IOException { .setApplicationName("Classroom samples") .build(); + // [START classroom_list_topic_code_snippet] + List topics = new ArrayList<>(); String pageToken = null; @@ -88,6 +90,9 @@ public static List listTopics(String courseId) throws IOException { throw e; } return topics; - } + // [END classroom_list_topic_code_snippet] + + } } +// [END classroom_list_topic_class] diff --git a/classroom/snippets/src/main/java/UpdateTopic.java b/classroom/snippets/src/main/java/UpdateTopic.java index 49bde0c0..770bee30 100644 --- a/classroom/snippets/src/main/java/UpdateTopic.java +++ b/classroom/snippets/src/main/java/UpdateTopic.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_update_topic] +// [START classroom_update_topic_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; @@ -54,6 +54,8 @@ public static Topic updateTopic(String courseId, String topicId) throws IOExcept .setApplicationName("Classroom samples") .build(); + // [START classroom_update_topic_code_snippet] + Topic topic = null; try { // Retrieve the topic to update. @@ -79,5 +81,9 @@ public static Topic updateTopic(String courseId, String topicId) throws IOExcept throw e; } return topic; + + // [END classroom_update_topic_code_snippet] + } } +// [END classroom_update_topic_class] diff --git a/classroom/snippets/src/test/java/TestAddAlias.java b/classroom/snippets/src/test/java/TestAddAliasToCourse.java similarity index 66% rename from classroom/snippets/src/test/java/TestAddAlias.java rename to classroom/snippets/src/test/java/TestAddAliasToCourse.java index 3551c638..c5b85777 100644 --- a/classroom/snippets/src/test/java/TestAddAlias.java +++ b/classroom/snippets/src/test/java/TestAddAliasToCourse.java @@ -14,15 +14,21 @@ import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; +import java.util.List; import org.junit.Assert; import org.junit.Test; // Unit test class for Add Alias classroom snippet -public class TestAddAlias extends BaseTest { +public class TestAddAliasToCourse extends BaseTest { @Test - public void testAddAlias() throws IOException { - CourseAlias courseAlias = AddAlias.addCourseAlias(testCourse.getId()); + public void testAddCourseAlias() throws IOException { + CourseAlias courseAlias = AddAliasToCourse.addAliasToCourse(testCourse.getId()); + List courseAliases = service.courses().aliases() + .list(testCourse.getId() + ).execute() + .getAliases(); Assert.assertNotNull("Course alias not returned.", courseAlias); + Assert.assertTrue("No course aliases exist.", courseAliases.size() > 0); } } \ No newline at end of file diff --git a/classroom/snippets/src/test/java/TestCreateAlias.java b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java similarity index 64% rename from classroom/snippets/src/test/java/TestCreateAlias.java rename to classroom/snippets/src/test/java/TestCreateCourseWithAlias.java index e1aa8813..5152b984 100644 --- a/classroom/snippets/src/test/java/TestCreateAlias.java +++ b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java @@ -13,17 +13,24 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; +import java.util.List; import org.junit.Assert; import org.junit.Test; // Unit test class for Create Alias classroom snippet -public class TestCreateAlias extends BaseTest { +public class TestCreateCourseWithAlias extends BaseTest { @Test - public void testCreateAlias() throws IOException { - Course course = CreateAlias.createAlias(); + public void testCreateCourseWithAlias() throws IOException { + Course course = CreateCourseWithAlias.createCourseWithAlias(); + List courseAliases = service.courses().aliases() + .list(course.getId() + ).execute() + .getAliases(); Assert.assertNotNull("Course not returned.", course); + Assert.assertTrue("No course aliases exist.", courseAliases.size() > 0); deleteCourse(course.getId()); } } \ No newline at end of file From f567ffe1e642774418507e602f9372dcdfb0f243 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 9 Dec 2022 13:59:42 -0500 Subject: [PATCH 05/11] added missing license header to TestDeleteTopic.java --- .../snippets/src/test/java/TestDeleteTopic.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/classroom/snippets/src/test/java/TestDeleteTopic.java b/classroom/snippets/src/test/java/TestDeleteTopic.java index 03cb77f4..b093865c 100644 --- a/classroom/snippets/src/test/java/TestDeleteTopic.java +++ b/classroom/snippets/src/test/java/TestDeleteTopic.java @@ -1,3 +1,17 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.services.classroom.model.Topic; import java.io.IOException; From e9d825d00c61d59b154cbb4c2727e89b7370a28e Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 9 Dec 2022 16:22:29 -0500 Subject: [PATCH 06/11] Adding List, ModifyAttachments, Patch, Return StudentSubmission methods --- .../src/main/java/ListStudentSubmissions.java | 89 ++++++++++++++++++ .../main/java/ListStudentsSubmissions.java | 88 ++++++++++++++++++ .../ModifyAttachmentsStudentSubmission.java | 92 +++++++++++++++++++ .../src/main/java/PatchStudentSubmission.java | 89 ++++++++++++++++++ .../main/java/ReturnStudentSubmission.java | 79 ++++++++++++++++ .../java/TestListStudentsSubmissions.java | 31 +++++++ 6 files changed, 468 insertions(+) create mode 100644 classroom/snippets/src/main/java/ListStudentSubmissions.java create mode 100644 classroom/snippets/src/main/java/ListStudentsSubmissions.java create mode 100644 classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java create mode 100644 classroom/snippets/src/main/java/PatchStudentSubmission.java create mode 100644 classroom/snippets/src/main/java/ReturnStudentSubmission.java create mode 100644 classroom/snippets/src/test/java/TestListStudentsSubmissions.java diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java new file mode 100644 index 00000000..ca9ec5d9 --- /dev/null +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -0,0 +1,89 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_list_student_submissions_class] + +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.ListStudentSubmissionsResponse; +import com.google.api.services.classroom.model.StudentSubmission; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/* Class to demonstrate the use of Classroom List StudentSubmissions API. */ +public class ListStudentSubmissions { + /** + * Lists student submissions based on courseId, courseWorkId, and userId. + * + * @param courseId - identifier of the course. + * @param courseWorkId - identifier of the course work. + * @param userId - identifier of the student whose work to return. + * @return - list of student submission. + * @throws IOException - if credentials file not found. + */ + public static List listStudentSubmissions(String courseId, String courseWorkId, + String userId) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client. + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_list_student_submissions_code_snippet] + + List studentSubmissions = new ArrayList<>(); + try { + // Set the userId as a query parameter on the request. + ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() + .list(courseId, courseWorkId) + .set("userId", userId) + .execute(); + studentSubmissions.addAll(response.getStudentSubmissions()); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId (%s), courseWorkId (%s), or userId (%s) does " + + "not exist.\n", courseId, courseWorkId, userId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + return studentSubmissions; + + // [END classroom_list_student_submissions_code_snippet] + + } +} +// [END classroom_list_student_submissions_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ListStudentsSubmissions.java b/classroom/snippets/src/main/java/ListStudentsSubmissions.java new file mode 100644 index 00000000..2e79ab44 --- /dev/null +++ b/classroom/snippets/src/main/java/ListStudentsSubmissions.java @@ -0,0 +1,88 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_list_submissions_class] + +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.ListStudentSubmissionsResponse; +import com.google.api.services.classroom.model.StudentSubmission; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/* Class to demonstrate the use of Classroom List StudentSubmissions API. */ +public class ListStudentsSubmissions { + /** + * Lists students submissions based on courseId and courseWorkId. + * + * @param courseId - identifier of the course. + * @param courseWorkId - identifier of the course work. + * @return - list of student submissions. + * @throws IOException - if credentials file not found. + */ + public static List listStudentsSubmissions(String courseId, String courseWorkId) + throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client. + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_list_submissions_code_snippet] + + List studentSubmissions = new ArrayList<>(); + try { + ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() + .list(courseId, courseWorkId) + .execute(); + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + } + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId (%s) or courseWorkId (%s) (%s) does not exist.\n", courseId, + courseWorkId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + return studentSubmissions; + + // [END classroom_list_submissions_code_snippet] + + } +} +// [END classroom_list_submissions_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java new file mode 100644 index 00000000..df3f17da --- /dev/null +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -0,0 +1,92 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_modify_attachments_student_submissions_class] + +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.Attachment; +import com.google.api.services.classroom.model.Link; +import com.google.api.services.classroom.model.ModifyAttachmentsRequest; +import com.google.api.services.classroom.model.StudentSubmission; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; + +/* Class to demonstrate the use of Classroom Modify Attachments StudentSubmissions API. */ +public class ModifyAttachmentsStudentSubmission { + /** + * Lists student submissions based on courseId, courseWorkId, and userId. + * + * @param courseId - identifier of the course. + * @param courseWorkId - identifier of the course work. + * @param id - identifier of the student submission. + * @return - the modified student submissions. + * @throws IOException - if credentials file not found. + */ + public static StudentSubmission modifyAttachments(String courseId, String courseWorkId, String id) + throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client. + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_modify_attachments_student_submissions_code_snippet] + + StudentSubmission studentSubmission = null; + try { + // Create ModifyAttachmentRequest object that includes a new attachment with a link. + Link link = new Link().setUrl("https://en.wikipedia.org/wiki/Irrational_number"); + Attachment attachment = new Attachment().setLink(link); + ModifyAttachmentsRequest modifyAttachmentsRequest = new ModifyAttachmentsRequest() + .setAddAttachments(Arrays.asList(attachment)); + studentSubmission = service.courses().courseWork().studentSubmissions().modifyAttachments( + courseId, courseWorkId, id, modifyAttachmentsRequest) + .execute(); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", courseId, courseWorkId, id); + } else { + throw e; + } + } catch(Exception e) { + throw e; + } + return studentSubmission; + + // [END classroom_modify_attachments_student_submissions_code_snippet] + + } +} +// [END classroom_modify_attachments_student_submissions_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java new file mode 100644 index 00000000..25ee96c1 --- /dev/null +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -0,0 +1,89 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_patch_student_submissions_class] + +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.StudentSubmission; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate the use of Classroom Patch StudentSubmissions API. */ +public class PatchStudentSubmission { + /** + * Updates one or more fields of a student submission. + * + * @param courseId - identifier of the course. + * @param courseWorkId - identifier of the course work. + * @param id - identifier of the student submission. + * @return - updated StudentSubmission instance. + * @throws IOException - if credentials file not found. + */ + public static StudentSubmission patchStudentSubmission(String courseId, String courseWorkId, + String id) throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client. + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_patch_student_submissions_code_snippet] + + StudentSubmission studentSubmission = null; + try { + // + StudentSubmission content = service.courses().courseWork().studentSubmissions() + .get(courseId, courseWorkId, id) + .execute(); + content.setAssignedGrade(80.00); + content.setDraftGrade(90.00); + studentSubmission = service.courses().courseWork().studentSubmissions() + .patch(courseId, courseWorkId, id, content) + .execute(); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", courseId, courseWorkId, id); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + return studentSubmission; + + // [END classroom_patch_student_submissions_code_snippet] + + } +} +// [END classroom_patch_student_submissions_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ReturnStudentSubmission.java b/classroom/snippets/src/main/java/ReturnStudentSubmission.java new file mode 100644 index 00000000..38bfcf45 --- /dev/null +++ b/classroom/snippets/src/main/java/ReturnStudentSubmission.java @@ -0,0 +1,79 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// [START classroom_return_student_submissions_class] + +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; + +/* Class to demonstrate the use of Classroom Return StudentSubmissions API. */ +public class ReturnStudentSubmission { + /** + * Return student submission. + * + * @param courseId - identifier of the course. + * @param courseWorkId - identifier of the course work. + * @param id - identifier of the student submission. + * @throws IOException - if credentials file not found. + */ + public static void returnSubmission(String courseId, String courseWorkId, String id) + throws IOException { + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( + credentials); + + // Create the classroom API client. + Classroom service = new Classroom.Builder(new NetHttpTransport(), + GsonFactory.getDefaultInstance(), + requestInitializer) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_return_student_submissions_code_snippet] + + try { + service.courses().courseWork().studentSubmissions() + .classroomReturn(courseId, courseWorkId, id, null) + .execute(); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", courseId, courseWorkId, id); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + + // [END classroom_return_student_submissions_code_snippet] + + } +} +// [END classroom_return_student_submissions_class] diff --git a/classroom/snippets/src/test/java/TestListStudentsSubmissions.java b/classroom/snippets/src/test/java/TestListStudentsSubmissions.java new file mode 100644 index 00000000..d92a77ca --- /dev/null +++ b/classroom/snippets/src/test/java/TestListStudentsSubmissions.java @@ -0,0 +1,31 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.services.classroom.model.StudentSubmission; +import java.io.IOException; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for ListStudentsSubmissions classroom snippet +public class TestListStudentsSubmissions extends BaseTest { + + @Test + public void testListStudentsSubmissions() throws IOException { + List studentSubmissions = ListStudentsSubmissions.listStudentsSubmissions( + testCourse.getId(), + "-"); + Assert.assertNotNull("Student Submissions not returned.", studentSubmissions); + } +} From 0cfab7864c785ac49b42ce55f9a03c42907eeeea Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 9 Dec 2022 17:25:15 -0500 Subject: [PATCH 07/11] minor fixes --- classroom/snippets/src/main/java/ListStudentSubmissions.java | 2 +- .../src/main/java/ModifyAttachmentsStudentSubmission.java | 2 +- classroom/snippets/src/main/java/PatchStudentSubmission.java | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index ca9ec5d9..cfcf14ab 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -39,7 +39,7 @@ public class ListStudentSubmissions { * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. * @param userId - identifier of the student whose work to return. - * @return - list of student submission. + * @return - list of student submissions. * @throws IOException - if credentials file not found. */ public static List listStudentSubmissions(String courseId, String courseWorkId, diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index df3f17da..50aeb711 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -32,7 +32,7 @@ import java.util.Arrays; import java.util.Collections; -/* Class to demonstrate the use of Classroom Modify Attachments StudentSubmissions API. */ +/* Class to demonstrate the use of Classroom ModifyAttachments StudentSubmissions API. */ public class ModifyAttachmentsStudentSubmission { /** * Lists student submissions based on courseId, courseWorkId, and userId. diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 25ee96c1..6248c9a5 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -68,6 +68,7 @@ public static StudentSubmission patchStudentSubmission(String courseId, String c content.setDraftGrade(90.00); studentSubmission = service.courses().courseWork().studentSubmissions() .patch(courseId, courseWorkId, id, content) + .set("updateMask", "draftGrade,assignedGrade") .execute(); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); From bd46dd42b4c8884d3d3efc3f042ef362150ef8f7 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 9 Dec 2022 17:45:53 -0500 Subject: [PATCH 08/11] adding comments for developer --- classroom/snippets/src/main/java/ListStudentSubmissions.java | 1 + classroom/snippets/src/main/java/ListStudentsSubmissions.java | 1 + .../src/main/java/ModifyAttachmentsStudentSubmission.java | 1 + classroom/snippets/src/main/java/PatchStudentSubmission.java | 1 + classroom/snippets/src/main/java/ReturnStudentSubmission.java | 1 + 5 files changed, 5 insertions(+) diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index cfcf14ab..f9e451b6 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -70,6 +70,7 @@ public static List listStudentSubmissions(String courseId, St .execute(); studentSubmissions.addAll(response.getStudentSubmissions()); } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId (%s), courseWorkId (%s), or userId (%s) does " diff --git a/classroom/snippets/src/main/java/ListStudentsSubmissions.java b/classroom/snippets/src/main/java/ListStudentsSubmissions.java index 2e79ab44..50a4d91c 100644 --- a/classroom/snippets/src/main/java/ListStudentsSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentsSubmissions.java @@ -69,6 +69,7 @@ public static List listStudentsSubmissions(String courseId, S studentSubmissions.addAll(response.getStudentSubmissions()); } } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId (%s) or courseWorkId (%s) (%s) does not exist.\n", courseId, diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index 50aeb711..0a27bb3c 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -73,6 +73,7 @@ public static StudentSubmission modifyAttachments(String courseId, String course courseId, courseWorkId, id, modifyAttachmentsRequest) .execute(); } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 6248c9a5..847c46d5 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -71,6 +71,7 @@ public static StudentSubmission patchStudentSubmission(String courseId, String c .set("updateMask", "draftGrade,assignedGrade") .execute(); } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " diff --git a/classroom/snippets/src/main/java/ReturnStudentSubmission.java b/classroom/snippets/src/main/java/ReturnStudentSubmission.java index 38bfcf45..14211c6a 100644 --- a/classroom/snippets/src/main/java/ReturnStudentSubmission.java +++ b/classroom/snippets/src/main/java/ReturnStudentSubmission.java @@ -61,6 +61,7 @@ public static void returnSubmission(String courseId, String courseWorkId, String .classroomReturn(courseId, courseWorkId, id, null) .execute(); } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " From 59b07b361c415f1904447db8d72804bfa2d12210 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 13 Dec 2022 13:00:29 -0500 Subject: [PATCH 09/11] modifications based on team review --- .../snippets/src/main/java/ListStudentSubmissions.java | 2 +- ...stStudentsSubmissions.java => ListSubmissions.java} | 6 +++--- .../main/java/ModifyAttachmentsStudentSubmission.java | 4 +++- .../snippets/src/main/java/PatchStudentSubmission.java | 6 ++++-- .../src/main/java/ReturnStudentSubmission.java | 2 +- ...udentsSubmissions.java => TestListSubmissions.java} | 10 +++++----- 6 files changed, 17 insertions(+), 13 deletions(-) rename classroom/snippets/src/main/java/{ListStudentsSubmissions.java => ListSubmissions.java} (94%) rename classroom/snippets/src/test/java/{TestListStudentsSubmissions.java => TestListSubmissions.java} (68%) diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index f9e451b6..636a24ab 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -34,7 +34,7 @@ /* Class to demonstrate the use of Classroom List StudentSubmissions API. */ public class ListStudentSubmissions { /** - * Lists student submissions based on courseId, courseWorkId, and userId. + * Retrieves a specific student's submissions for the specified course work. * * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. diff --git a/classroom/snippets/src/main/java/ListStudentsSubmissions.java b/classroom/snippets/src/main/java/ListSubmissions.java similarity index 94% rename from classroom/snippets/src/main/java/ListStudentsSubmissions.java rename to classroom/snippets/src/main/java/ListSubmissions.java index 50a4d91c..aafafc8d 100644 --- a/classroom/snippets/src/main/java/ListStudentsSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -32,16 +32,16 @@ import java.util.List; /* Class to demonstrate the use of Classroom List StudentSubmissions API. */ -public class ListStudentsSubmissions { +public class ListSubmissions { /** - * Lists students submissions based on courseId and courseWorkId. + * Retrieves submissions for all students for the specified course work in a course. * * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. * @return - list of student submissions. * @throws IOException - if credentials file not found. */ - public static List listStudentsSubmissions(String courseId, String courseWorkId) + public static List listSubmissions(String courseId, String courseWorkId) throws IOException { /* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index 0a27bb3c..b573e55b 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -35,7 +35,7 @@ /* Class to demonstrate the use of Classroom ModifyAttachments StudentSubmissions API. */ public class ModifyAttachmentsStudentSubmission { /** - * Lists student submissions based on courseId, courseWorkId, and userId. + * Modify attachments on a student submission. * * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. @@ -69,6 +69,8 @@ public static StudentSubmission modifyAttachments(String courseId, String course Attachment attachment = new Attachment().setLink(link); ModifyAttachmentsRequest modifyAttachmentsRequest = new ModifyAttachmentsRequest() .setAddAttachments(Arrays.asList(attachment)); + + // The modified studentSubmission object is returned with the new attachment added to it. studentSubmission = service.courses().courseWork().studentSubmissions().modifyAttachments( courseId, courseWorkId, id, modifyAttachmentsRequest) .execute(); diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 847c46d5..83a29b47 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -31,7 +31,7 @@ /* Class to demonstrate the use of Classroom Patch StudentSubmissions API. */ public class PatchStudentSubmission { /** - * Updates one or more fields of a student submission. + * Updates the draft grade and/or assigned grade of a student submission. * * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. @@ -60,12 +60,14 @@ public static StudentSubmission patchStudentSubmission(String courseId, String c StudentSubmission studentSubmission = null; try { - // + // Updating the draftGrade and assignedGrade fields for the specific student submission. StudentSubmission content = service.courses().courseWork().studentSubmissions() .get(courseId, courseWorkId, id) .execute(); content.setAssignedGrade(80.00); content.setDraftGrade(90.00); + + // The updated studentSubmission object is returned with the new draftGrade and assignedGrade. studentSubmission = service.courses().courseWork().studentSubmissions() .patch(courseId, courseWorkId, id, content) .set("updateMask", "draftGrade,assignedGrade") diff --git a/classroom/snippets/src/main/java/ReturnStudentSubmission.java b/classroom/snippets/src/main/java/ReturnStudentSubmission.java index 14211c6a..6b40fc96 100644 --- a/classroom/snippets/src/main/java/ReturnStudentSubmission.java +++ b/classroom/snippets/src/main/java/ReturnStudentSubmission.java @@ -30,7 +30,7 @@ /* Class to demonstrate the use of Classroom Return StudentSubmissions API. */ public class ReturnStudentSubmission { /** - * Return student submission. + * Return a student submission back to the student which updates the submission state to `RETURNED`. * * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. diff --git a/classroom/snippets/src/test/java/TestListStudentsSubmissions.java b/classroom/snippets/src/test/java/TestListSubmissions.java similarity index 68% rename from classroom/snippets/src/test/java/TestListStudentsSubmissions.java rename to classroom/snippets/src/test/java/TestListSubmissions.java index d92a77ca..b4bafd26 100644 --- a/classroom/snippets/src/test/java/TestListStudentsSubmissions.java +++ b/classroom/snippets/src/test/java/TestListSubmissions.java @@ -18,14 +18,14 @@ import org.junit.Assert; import org.junit.Test; -// Unit test class for ListStudentsSubmissions classroom snippet -public class TestListStudentsSubmissions extends BaseTest { +// Unit test class for ListSubmissions classroom snippet +public class TestListSubmissions extends BaseTest { @Test - public void testListStudentsSubmissions() throws IOException { - List studentSubmissions = ListStudentsSubmissions.listStudentsSubmissions( + public void testListSubmissions() throws IOException { + List submissions = ListSubmissions.listSubmissions( testCourse.getId(), "-"); - Assert.assertNotNull("Student Submissions not returned.", studentSubmissions); + Assert.assertNotNull("Submissions not returned.", submissions); } } From 9968e729ba17cf52cb721d9eb48085a05c6f1ff4 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 13 Dec 2022 14:50:36 -0500 Subject: [PATCH 10/11] modifications based on team review --- classroom/snippets/src/main/java/ListStudentSubmissions.java | 4 +++- classroom/snippets/src/main/java/ListSubmissions.java | 2 +- .../src/main/java/ModifyAttachmentsStudentSubmission.java | 2 +- classroom/snippets/src/main/java/PatchStudentSubmission.java | 2 +- classroom/snippets/src/test/java/TestListSubmissions.java | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index 636a24ab..2243ec3c 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -68,7 +68,9 @@ public static List listStudentSubmissions(String courseId, St .list(courseId, courseWorkId) .set("userId", userId) .execute(); - studentSubmissions.addAll(response.getStudentSubmissions()); + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + } } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); diff --git a/classroom/snippets/src/main/java/ListSubmissions.java b/classroom/snippets/src/main/java/ListSubmissions.java index aafafc8d..dd0d8201 100644 --- a/classroom/snippets/src/main/java/ListSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -72,7 +72,7 @@ public static List listSubmissions(String courseId, String co //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { - System.out.printf("The courseId (%s) or courseWorkId (%s) (%s) does not exist.\n", courseId, + System.out.printf("The courseId (%s) or courseWorkId (%s) does not exist.\n", courseId, courseWorkId); } else { throw e; diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index b573e55b..8f209966 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -40,7 +40,7 @@ public class ModifyAttachmentsStudentSubmission { * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. * @param id - identifier of the student submission. - * @return - the modified student submissions. + * @return - the modified student submission. * @throws IOException - if credentials file not found. */ public static StudentSubmission modifyAttachments(String courseId, String courseWorkId, String id) diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 83a29b47..92fd39dd 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -36,7 +36,7 @@ public class PatchStudentSubmission { * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. * @param id - identifier of the student submission. - * @return - updated StudentSubmission instance. + * @return - the updated student submission. * @throws IOException - if credentials file not found. */ public static StudentSubmission patchStudentSubmission(String courseId, String courseWorkId, diff --git a/classroom/snippets/src/test/java/TestListSubmissions.java b/classroom/snippets/src/test/java/TestListSubmissions.java index b4bafd26..148e9529 100644 --- a/classroom/snippets/src/test/java/TestListSubmissions.java +++ b/classroom/snippets/src/test/java/TestListSubmissions.java @@ -26,6 +26,6 @@ public void testListSubmissions() throws IOException { List submissions = ListSubmissions.listSubmissions( testCourse.getId(), "-"); - Assert.assertNotNull("Submissions not returned.", submissions); + Assert.assertNotNull("No submissions returned.", submissions); } } From dbf1401f19ea40ee69b05255125a4b6a8e073c6b Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 16 Dec 2022 14:57:41 -0600 Subject: [PATCH 11/11] Adding print statements and devsite tag to ListCourseAliases --- .../src/main/java/CreateCourseWork.java | 5 ++-- .../snippets/src/main/java/GetTopic.java | 1 + .../src/main/java/ListCourseAliases.java | 8 ++++-- .../src/main/java/ListStudentSubmissions.java | 27 ++++++++++++++----- .../src/main/java/ListSubmissions.java | 23 ++++++++++++---- .../snippets/src/main/java/ListTopics.java | 6 +++-- .../ModifyAttachmentsStudentSubmission.java | 5 ++++ .../src/main/java/PatchStudentSubmission.java | 9 +++++-- .../snippets/src/main/java/UpdateTopic.java | 5 +++- 9 files changed, 68 insertions(+), 21 deletions(-) diff --git a/classroom/snippets/src/main/java/CreateCourseWork.java b/classroom/snippets/src/main/java/CreateCourseWork.java index 631ced38..7f9973ca 100644 --- a/classroom/snippets/src/main/java/CreateCourseWork.java +++ b/classroom/snippets/src/main/java/CreateCourseWork.java @@ -80,13 +80,14 @@ public static CourseWork createCourseWork(String courseId) throws IOException { .setDescription("Read about how the SR-71 Blackbird, the world’s fastest and " + "highest-flying manned aircraft, was built.") .setMaterials(materials) - .setDueDate(new Date().setMonth(12).setDay(10).setYear(2022)) - .setDueTime(new TimeOfDay().setHours(15).setMinutes(0)) .setWorkType("ASSIGNMENT") .setState("PUBLISHED"); courseWork = service.courses().courseWork().create(courseId, content) .execute(); + + /* Prints the created courseWork. */ + System.out.printf("CourseWork created: %s\n", courseWork.getTitle()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); diff --git a/classroom/snippets/src/main/java/GetTopic.java b/classroom/snippets/src/main/java/GetTopic.java index 5332bd5c..140a0407 100644 --- a/classroom/snippets/src/main/java/GetTopic.java +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -60,6 +60,7 @@ public static Topic getTopic(String courseId, String topicId) throws IOException try { // Get the topic. topic = service.courses().topics().get(courseId, topicId).execute(); + System.out.printf("Topic '%s' found.\n", topic.getName()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); diff --git a/classroom/snippets/src/main/java/ListCourseAliases.java b/classroom/snippets/src/main/java/ListCourseAliases.java index 171f4fa6..c3141cf1 100644 --- a/classroom/snippets/src/main/java/ListCourseAliases.java +++ b/classroom/snippets/src/main/java/ListCourseAliases.java @@ -13,7 +13,7 @@ // limitations under the License. -// [START classroom_list_aliases] +// [START classroom_list_aliases_class] import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; @@ -57,6 +57,8 @@ public static List listCourseAliases(String courseId) .setApplicationName("Classroom samples") .build(); + // [START classroom_list_aliases_code_snippet] + String pageToken = null; List courseAliases = new ArrayList<>(); @@ -89,6 +91,8 @@ public static List listCourseAliases(String courseId) } } return courseAliases; + + // [END classroom_list_aliases_code_snippet] } } -// [END classroom_list_aliases] \ No newline at end of file +// [END classroom_list_aliases_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index 2243ec3c..acc7b898 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -62,14 +62,27 @@ public static List listStudentSubmissions(String courseId, St // [START classroom_list_student_submissions_code_snippet] List studentSubmissions = new ArrayList<>(); + String pageToken = null; + try { - // Set the userId as a query parameter on the request. - ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() - .list(courseId, courseWorkId) - .set("userId", userId) - .execute(); - if (response.getStudentSubmissions() != null) { - studentSubmissions.addAll(response.getStudentSubmissions()); + do { + // Set the userId as a query parameter on the request. + ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() + .list(courseId, courseWorkId) + .set("userId", userId) + .execute(); + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (studentSubmissions.isEmpty()) { + System.out.println("No student submission found."); + } else { + for (StudentSubmission submission : studentSubmissions) { + System.out.printf("Student submission: %s.\n", submission.getId()); + } } } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately diff --git a/classroom/snippets/src/main/java/ListSubmissions.java b/classroom/snippets/src/main/java/ListSubmissions.java index dd0d8201..ed5898de 100644 --- a/classroom/snippets/src/main/java/ListSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -61,12 +61,25 @@ public static List listSubmissions(String courseId, String co // [START classroom_list_submissions_code_snippet] List studentSubmissions = new ArrayList<>(); + String pageToken = null; + try { - ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() - .list(courseId, courseWorkId) - .execute(); - if (response.getStudentSubmissions() != null) { - studentSubmissions.addAll(response.getStudentSubmissions()); + do { + ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() + .list(courseId, courseWorkId) + .execute(); + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + } + } while (pageToken != null); + + if (studentSubmissions.isEmpty()) { + System.out.println("No student submission found."); + } else { + for (StudentSubmission submission : studentSubmissions) { + System.out.printf("Student id (%s), student submission id (%s)\n", submission.getUserId(), + submission.getId()); + } } } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately diff --git a/classroom/snippets/src/main/java/ListTopics.java b/classroom/snippets/src/main/java/ListTopics.java index 61ea9b89..e754f82b 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -67,8 +67,10 @@ public static List listTopics(String courseId) throws IOException { .setPageSize(100) .setPageToken(pageToken) .execute(); - topics.addAll(response.getTopic()); - pageToken = response.getNextPageToken(); + if (response.getTopic() != null) { + topics.addAll(response.getTopic()); + pageToken = response.getNextPageToken(); + } } while (pageToken != null); if (topics.isEmpty()) { diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index 8f209966..252f51f5 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -74,6 +74,11 @@ public static StudentSubmission modifyAttachments(String courseId, String course studentSubmission = service.courses().courseWork().studentSubmissions().modifyAttachments( courseId, courseWorkId, id, modifyAttachmentsRequest) .execute(); + + /* Prints the modified student submission. */ + System.out.printf("Modified student submission attachments: '%s'.\n", studentSubmission + .getAssignmentSubmission() + .getAttachments()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 92fd39dd..1690ef8e 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -64,14 +64,19 @@ public static StudentSubmission patchStudentSubmission(String courseId, String c StudentSubmission content = service.courses().courseWork().studentSubmissions() .get(courseId, courseWorkId, id) .execute(); - content.setAssignedGrade(80.00); - content.setDraftGrade(90.00); + content.setAssignedGrade(90.00); + content.setDraftGrade(80.00); // The updated studentSubmission object is returned with the new draftGrade and assignedGrade. studentSubmission = service.courses().courseWork().studentSubmissions() .patch(courseId, courseWorkId, id, content) .set("updateMask", "draftGrade,assignedGrade") .execute(); + + /* Prints the updated student submission. */ + System.out.printf("Updated student submission draft grade (%s) and assigned grade (%s).\n", + studentSubmission.getDraftGrade(), + studentSubmission.getAssignedGrade()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); diff --git a/classroom/snippets/src/main/java/UpdateTopic.java b/classroom/snippets/src/main/java/UpdateTopic.java index 770bee30..f45ffeca 100644 --- a/classroom/snippets/src/main/java/UpdateTopic.java +++ b/classroom/snippets/src/main/java/UpdateTopic.java @@ -69,7 +69,10 @@ public static Topic updateTopic(String courseId, String topicId) throws IOExcept topic = service.courses().topics().patch(courseId, topicId, topicToUpdate) .set("updateMask", "name") .execute(); - } catch(GoogleJsonResponseException e) { + + /* Prints the updated topic. */ + System.out.printf("Topic '%s' updated.\n", topic.getName()); + } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) {