From d94f7d1b2ef6c56fc7bf39011c3221f5b63d08ba Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 1 Dec 2022 12:41:15 -0500 Subject: [PATCH 01/50] 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/50] 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/50] 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/50] 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/50] 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/50] 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/50] 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/50] 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/50] 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/50] 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/50] 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) { From 2391573ef5835dfbd0ea01c1eefcd3cda18a60de Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 16 Dec 2022 16:21:50 -0600 Subject: [PATCH 12/50] added class templates for guardians and guardianInvitations --- .../main/java/CancelGuardianInvitation.java | 21 +++++++++++++++++++ .../main/java/CreateGuardianInvitation.java | 21 +++++++++++++++++++ .../src/main/java/DeleteGuardian.java | 21 +++++++++++++++++++ .../ListGuardianInvitationsByStudent.java | 21 +++++++++++++++++++ .../snippets/src/main/java/ListGuardians.java | 21 +++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 classroom/snippets/src/main/java/CancelGuardianInvitation.java create mode 100644 classroom/snippets/src/main/java/CreateGuardianInvitation.java create mode 100644 classroom/snippets/src/main/java/DeleteGuardian.java create mode 100644 classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java create mode 100644 classroom/snippets/src/main/java/ListGuardians.java diff --git a/classroom/snippets/src/main/java/CancelGuardianInvitation.java b/classroom/snippets/src/main/java/CancelGuardianInvitation.java new file mode 100644 index 00000000..1bd0bce1 --- /dev/null +++ b/classroom/snippets/src/main/java/CancelGuardianInvitation.java @@ -0,0 +1,21 @@ +// 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_cancel_guardian_invitation_class] + +public class CancelGuardianInvitation { + +} +// [END classroom_cancel_guardian_invitation_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/CreateGuardianInvitation.java b/classroom/snippets/src/main/java/CreateGuardianInvitation.java new file mode 100644 index 00000000..6fcd45d7 --- /dev/null +++ b/classroom/snippets/src/main/java/CreateGuardianInvitation.java @@ -0,0 +1,21 @@ +// 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_guardian_invitation_class] + +public class CreateGuardianInvitation { + +} +// [END classroom_create_guardian_invitation_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/DeleteGuardian.java b/classroom/snippets/src/main/java/DeleteGuardian.java new file mode 100644 index 00000000..3f564779 --- /dev/null +++ b/classroom/snippets/src/main/java/DeleteGuardian.java @@ -0,0 +1,21 @@ +// 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_guardian_class] + +public class DeleteGuardian { + +} +// [END classroom_delete_guardian_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java new file mode 100644 index 00000000..0063578d --- /dev/null +++ b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java @@ -0,0 +1,21 @@ +// 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_guardian_invitations_class] + +public class ListGuardianInvitationsByStudent { + +} +// [END classroom_list_guardian_invitations_class] diff --git a/classroom/snippets/src/main/java/ListGuardians.java b/classroom/snippets/src/main/java/ListGuardians.java new file mode 100644 index 00000000..dbc8030f --- /dev/null +++ b/classroom/snippets/src/main/java/ListGuardians.java @@ -0,0 +1,21 @@ +// 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_guardians_class] + +public class ListGuardians { + +} +// [END classroom_list_guardians_class] From fa1c6b0dee3397a0f13eaf922f07fc002323fae5 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 16 Dec 2022 17:00:20 -0600 Subject: [PATCH 13/50] guardian and guardian invitation implementation --- .../main/java/CancelGuardianInvitation.java | 75 +++++++++++++++++- .../main/java/CreateGuardianInvitation.java | 68 ++++++++++++++++ .../src/main/java/DeleteGuardian.java | 47 +++++++++++ .../ListGuardianInvitationsByStudent.java | 77 +++++++++++++++++++ .../snippets/src/main/java/ListGuardians.java | 77 +++++++++++++++++++ .../snippets/src/main/java/ListTopics.java | 6 +- 6 files changed, 347 insertions(+), 3 deletions(-) diff --git a/classroom/snippets/src/main/java/CancelGuardianInvitation.java b/classroom/snippets/src/main/java/CancelGuardianInvitation.java index 1bd0bce1..7f3a07f7 100644 --- a/classroom/snippets/src/main/java/CancelGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CancelGuardianInvitation.java @@ -12,10 +12,83 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_cancel_guardian_invitation_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.GuardianInvitation; +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 Guardian Invitation API. */ public class CancelGuardianInvitation { + /** + * Cancel a guardian invitation by modifying the state of the invite. + * + * @param studentId - the id of the student. + * @param invitationId - the id of the guardian invitation to modify. + * @return - the modified guardian invitation. + * @throws IOException - if credentials file not found. + */ + public static GuardianInvitation cancelGuardianInvitation(String studentId, String invitationId) + 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_GUARDIANLINKS_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_cancel_guardian_invitation_code_snippet] + + GuardianInvitation guardianInvitation = null; + + try { + /* Change the state of the GuardianInvitation from PENDING to COMPLETE. See + https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate + for other possible states of guardian invitations. */ + GuardianInvitation content = service.userProfiles().guardianInvitations() + .get(studentId, invitationId) + .execute(); + content.setState("COMPLETE"); + + guardianInvitation = service.userProfiles().guardianInvitations() + .patch(studentId, invitationId, content) + .set("updateMask", "state") + .execute(); + + System.out.printf("Invitation (%s) state set to %s", guardianInvitation.getInvitationId(), + guardianInvitation.getState()); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("There is no record of studentId (%s) or invitationId (%s).", studentId, + invitationId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + return guardianInvitation; + // [END classroom_cancel_guardian_invitation_code_snippet] + } } // [END classroom_cancel_guardian_invitation_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/CreateGuardianInvitation.java b/classroom/snippets/src/main/java/CreateGuardianInvitation.java index 6fcd45d7..f4cd1927 100644 --- a/classroom/snippets/src/main/java/CreateGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CreateGuardianInvitation.java @@ -15,7 +15,75 @@ // [START classroom_create_guardian_invitation_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.GuardianInvitation; +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 Guardian Invitation API. */ public class CreateGuardianInvitation { + /** + * Creates a guardian invitation by sending an email to the guardian for confirmation. + * + * @param studentId - the id of the student. + * @return - the newly created guardian invitation. + * @throws IOException - if credentials file not found. + */ + public static GuardianInvitation createGuardianInvitation(String studentId) 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_GUARDIANLINKS_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_create_guardian_invitation_code_snippet] + + GuardianInvitation guardianInvitation = null; + + /* Create a GuardianInvitation object with state set to PENDING. See + https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate + for other possible states of guardian invitations. */ + GuardianInvitation content = new GuardianInvitation() + .setStudentId(studentId) + .setInvitationId("guardian@gmail.com") + .setState("PENDING"); + try { + guardianInvitation = service.userProfiles().guardianInvitations() + .create(studentId, content) + .execute(); + + System.out.printf("Invitation created: %s", guardianInvitation.getInvitationId()); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("There is no record of studentId: %s", studentId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + return guardianInvitation; + // [END classroom_create_guardian_invitation_code_snippet] + } } // [END classroom_create_guardian_invitation_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/DeleteGuardian.java b/classroom/snippets/src/main/java/DeleteGuardian.java index 3f564779..3cdd1f95 100644 --- a/classroom/snippets/src/main/java/DeleteGuardian.java +++ b/classroom/snippets/src/main/java/DeleteGuardian.java @@ -15,7 +15,54 @@ // [START classroom_delete_guardian_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 Delete Guardian API. */ public class DeleteGuardian { + /** + * Delete a guardian for a specific student. + * + * @param studentId - the id of the student the guardian belongs to. + * @param guardianId - the id of the guardian to delete. + * @throws IOException - if credentials file not found. + */ + public static void deleteGuardian(String studentId, String guardianId) 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_GUARDIANLINKS_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_delete_guardian_code_snippet] + try { + service.userProfiles().guardians().delete(studentId, guardianId) + .execute(); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("There is no record of guardianId (%s).", guardianId); + } + } + // [END classroom_delete_guardian_code_snippet] + } } // [END classroom_delete_guardian_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java index 0063578d..312010f6 100644 --- a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java @@ -15,7 +15,84 @@ // [START classroom_list_guardian_invitations_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.GuardianInvitation; +import com.google.api.services.classroom.model.ListGuardianInvitationsResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +/* Class to demonstrate the use of Classroom List Guardian Invitations API. */ public class ListGuardianInvitationsByStudent { + /** + * Retrieves guardian invitations by student. + * + * @param studentId - the id of the student. + * @return a list of guardian invitations that were sent for a specific student. + * @throws IOException - if credentials file not found. + */ + public static List listGuardianInvitationsByStudent(String studentId) + 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_GUARDIANLINKS_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_guardian_invitations_code_snippet] + + List guardianInvitations = null; + String pageToken = null; + + try { + do { + ListGuardianInvitationsResponse response = service.userProfiles().guardianInvitations() + .list(studentId) + .execute(); + + if (response.getGuardianInvitations() != null) { + guardianInvitations.addAll(response.getGuardianInvitations()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (guardianInvitations.isEmpty()) { + System.out.println("No guardian invitations found."); + } else { + for (GuardianInvitation invitation : guardianInvitations) { + System.out.printf("Guardian invitation sent to %s", invitation.getInvitedEmailAddress()); + } + } + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("There is no record of studentId (%s).", studentId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + return guardianInvitations; + // [END classroom_list_guardian_invitations_code_snippet] + } } // [END classroom_list_guardian_invitations_class] diff --git a/classroom/snippets/src/main/java/ListGuardians.java b/classroom/snippets/src/main/java/ListGuardians.java index dbc8030f..4705a34e 100644 --- a/classroom/snippets/src/main/java/ListGuardians.java +++ b/classroom/snippets/src/main/java/ListGuardians.java @@ -15,7 +15,84 @@ // [START classroom_list_guardians_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.Guardian; +import com.google.api.services.classroom.model.ListGuardiansResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +/* Class to demonstrate the use of Classroom List Guardians API. */ public class ListGuardians { + /** + * Retrieves guardians for a specific student. + * + * @param studentId - the id of the student. + * @return a list of guardians for a specific student. + * @throws IOException - if credentials file not found. + */ + public static List listGuardians(String studentId) 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_GUARDIANLINKS_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_guardians_code_snippet] + + List guardians = null; + String pageToken = null; + + try { + do { + ListGuardiansResponse response = service.userProfiles().guardians() + .list(studentId) + .execute(); + + if (response.getGuardians() != null) { + guardians.addAll(response.getGuardians()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (guardians.isEmpty()) { + System.out.println("No guardians found."); + } else { + for (Guardian guardian : guardians) { + System.out.printf("Guardian: %s", guardian.getGuardianProfile().getName().getFullName()); + } + } + + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("There is no record of studentId (%s).", studentId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + return guardians; + // [END classroom_list_guardians_code_snippet] + } } // [END classroom_list_guardians_class] 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()) { From d8a0421621ad016a751fcc3766c2c07355ea8ce9 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 20 Dec 2022 13:47:54 -0600 Subject: [PATCH 14/50] tests for list guardians and list guardian invitations --- .../ListGuardianInvitationsByStudent.java | 7 ++-- .../snippets/src/main/java/ListGuardians.java | 13 +++++--- .../TestListGuardianInvitationsByStudent.java | 32 +++++++++++++++++++ .../src/test/java/TestListGuardians.java | 32 +++++++++++++++++++ 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java create mode 100644 classroom/snippets/src/test/java/TestListGuardians.java diff --git a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java index 312010f6..d7a3d91e 100644 --- a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java @@ -27,6 +27,7 @@ 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; @@ -45,7 +46,7 @@ public static List listGuardianInvitationsByStudent(String s 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_GUARDIANLINKS_STUDENTS)); + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS_READONLY)); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); @@ -58,7 +59,7 @@ public static List listGuardianInvitationsByStudent(String s // [START classroom_list_guardian_invitations_code_snippet] - List guardianInvitations = null; + List guardianInvitations = new ArrayList<>(); String pageToken = null; try { @@ -77,7 +78,7 @@ public static List listGuardianInvitationsByStudent(String s System.out.println("No guardian invitations found."); } else { for (GuardianInvitation invitation : guardianInvitations) { - System.out.printf("Guardian invitation sent to %s", invitation.getInvitedEmailAddress()); + System.out.printf("Guardian invitation: %s\n", invitation.getInvitedEmailAddress()); } } } catch (GoogleJsonResponseException e) { diff --git a/classroom/snippets/src/main/java/ListGuardians.java b/classroom/snippets/src/main/java/ListGuardians.java index 4705a34e..a11d993c 100644 --- a/classroom/snippets/src/main/java/ListGuardians.java +++ b/classroom/snippets/src/main/java/ListGuardians.java @@ -27,16 +27,17 @@ 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 Guardians API. */ public class ListGuardians { /** - * Retrieves guardians for a specific student. + * Retrieves active guardians for a specific student. * * @param studentId - the id of the student. - * @return a list of guardians for a specific student. + * @return a list of active guardians for a specific student. * @throws IOException - if credentials file not found. */ public static List listGuardians(String studentId) throws IOException { @@ -44,7 +45,7 @@ public static List listGuardians(String studentId) throws IOException 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_GUARDIANLINKS_STUDENTS)); + .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS_READONLY)); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); @@ -57,7 +58,7 @@ public static List listGuardians(String studentId) throws IOException // [START classroom_list_guardians_code_snippet] - List guardians = null; + List guardians = new ArrayList<>(); String pageToken = null; try { @@ -76,7 +77,9 @@ public static List listGuardians(String studentId) throws IOException System.out.println("No guardians found."); } else { for (Guardian guardian : guardians) { - System.out.printf("Guardian: %s", guardian.getGuardianProfile().getName().getFullName()); + System.out.printf("Guardian name: %s, guardian email: %s\n", + guardian.getGuardianProfile().getName().getFullName(), + guardian.getInvitedEmailAddress()); } } diff --git a/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java new file mode 100644 index 00000000..af5e64cb --- /dev/null +++ b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java @@ -0,0 +1,32 @@ +// 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.GuardianInvitation; +import java.io.IOException; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for List Guardian Invitations classroom snippet +public class TestListGuardianInvitationsByStudent { + + @Test + public void testGuardianInvitationsByStudent() throws IOException { + String studentId = "insert_student_id"; + List invitationList = ListGuardianInvitationsByStudent + .listGuardianInvitationsByStudent(studentId); + + Assert.assertTrue("No guardian invitations returned.", invitationList.size() > 0); + } +} diff --git a/classroom/snippets/src/test/java/TestListGuardians.java b/classroom/snippets/src/test/java/TestListGuardians.java new file mode 100644 index 00000000..86b4222d --- /dev/null +++ b/classroom/snippets/src/test/java/TestListGuardians.java @@ -0,0 +1,32 @@ +// 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.Guardian; +import java.io.IOException; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for List Guardians classroom snippet +public class TestListGuardians { + + @Test + public void testListGuardians() throws IOException { + String studentId = "insert_student_id"; + List guardianList = ListGuardians.listGuardians(studentId); + + Assert.assertTrue("No guardians returned.", guardianList.size() > 0); + } + +} From d7fc3ecc0e8102f576bd3c865815049eaecad31f Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 21 Dec 2022 15:42:34 -0600 Subject: [PATCH 15/50] adding tests for guardians and guardian invites. removing use of ADC for auth. --- classroom/snippets/build.gradle | 2 +- .../main/java/CancelGuardianInvitation.java | 29 ++++--- .../src/main/java/ClassroomCredentials.java | 52 +++++++++++++ .../main/java/CreateGuardianInvitation.java | 33 ++++---- .../src/main/java/DeleteGuardian.java | 26 +++---- .../snippets/src/main/java/GetGuardian.java | 75 +++++++++++++++++++ .../ListGuardianInvitationsByStudent.java | 28 ++++--- .../snippets/src/main/java/ListGuardians.java | 26 +++---- .../java/TestCancelGuardianInvitation.java | 31 ++++++++ .../java/TestCreateGuardianInvitation.java | 31 ++++++++ .../src/test/java/TestDeleteGuardian.java | 31 ++++++++ .../TestListGuardianInvitationsByStudent.java | 3 +- .../src/test/java/TestListGuardians.java | 3 +- 13 files changed, 290 insertions(+), 80 deletions(-) create mode 100644 classroom/snippets/src/main/java/ClassroomCredentials.java create mode 100644 classroom/snippets/src/main/java/GetGuardian.java create mode 100644 classroom/snippets/src/test/java/TestCancelGuardianInvitation.java create mode 100644 classroom/snippets/src/test/java/TestCreateGuardianInvitation.java create mode 100644 classroom/snippets/src/test/java/TestDeleteGuardian.java diff --git a/classroom/snippets/build.gradle b/classroom/snippets/build.gradle index 6971123c..7e19291b 100644 --- a/classroom/snippets/build.gradle +++ b/classroom/snippets/build.gradle @@ -7,7 +7,7 @@ repositories { dependencies { implementation 'com.google.api-client:google-api-client:2.0.0' - implementation 'com.google.auth:google-auth-library-oauth2-http:1.11.0' + implementation 'com.google.oauth-client:google-oauth-client-jetty:1.34.1' implementation 'com.google.apis:google-api-services-classroom:v1-rev20220323-2.0.0' testImplementation 'junit:junit:4.13.2' } diff --git a/classroom/snippets/src/main/java/CancelGuardianInvitation.java b/classroom/snippets/src/main/java/CancelGuardianInvitation.java index 7f3a07f7..f6342eef 100644 --- a/classroom/snippets/src/main/java/CancelGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CancelGuardianInvitation.java @@ -14,18 +14,17 @@ // [START classroom_cancel_guardian_invitation_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.GuardianInvitation; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; +import java.util.List; /* Class to demonstrate the use of Classroom Patch Guardian Invitation API. */ public class CancelGuardianInvitation { @@ -38,19 +37,19 @@ public class CancelGuardianInvitation { * @throws IOException - if credentials file not found. */ public static GuardianInvitation cancelGuardianInvitation(String studentId, String invitationId) - 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_GUARDIANLINKS_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws Exception { - // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = + Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); + + // Create the classroom API client + ClassroomCredentials classroomCredentials = new ClassroomCredentials(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - requestInitializer) + classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -72,7 +71,7 @@ public static GuardianInvitation cancelGuardianInvitation(String studentId, Stri .set("updateMask", "state") .execute(); - System.out.printf("Invitation (%s) state set to %s", guardianInvitation.getInvitationId(), + System.out.printf("Invitation (%s) state set to %s\n.", guardianInvitation.getInvitationId(), guardianInvitation.getState()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately diff --git a/classroom/snippets/src/main/java/ClassroomCredentials.java b/classroom/snippets/src/main/java/ClassroomCredentials.java new file mode 100644 index 00000000..0818d90e --- /dev/null +++ b/classroom/snippets/src/main/java/ClassroomCredentials.java @@ -0,0 +1,52 @@ +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Collection; + +public class ClassroomCredentials { + private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); + private static final String TOKENS_DIRECTORY_PATH = "tokens"; + + private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; + + /** + * Creates an authorized Credential object. + * + * @param HTTP_TRANSPORT The network HTTP Transport. + * @param SCOPES The scopes required to make the API call. + * @return An authorized Credential object. + * @throws IOException If the credentials.json file cannot be found. + */ + static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT, + Collection SCOPES) + throws IOException { + // Load client secrets. + InputStream in = ClassroomCredentials.class.getResourceAsStream(CREDENTIALS_FILE_PATH); + if (in == null) { + throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH); + } + + GoogleClientSecrets clientSecrets = + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); + + // Build flow and trigger user authorization request. + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( + HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) + .setAccessType("offline") + .build(); + + LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); + return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + } +} \ No newline at end of file diff --git a/classroom/snippets/src/main/java/CreateGuardianInvitation.java b/classroom/snippets/src/main/java/CreateGuardianInvitation.java index f4cd1927..5a79e487 100644 --- a/classroom/snippets/src/main/java/CreateGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CreateGuardianInvitation.java @@ -15,18 +15,17 @@ // [START classroom_create_guardian_invitation_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.GuardianInvitation; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; +import java.util.List; /* Class to demonstrate the use of Classroom Create Guardian Invitation API. */ public class CreateGuardianInvitation { @@ -34,22 +33,24 @@ public class CreateGuardianInvitation { * Creates a guardian invitation by sending an email to the guardian for confirmation. * * @param studentId - the id of the student. + * @param guardianEmail - email to send the guardian invitation to. * @return - the newly created guardian invitation. * @throws IOException - if credentials file not found. */ - public static GuardianInvitation createGuardianInvitation(String studentId) 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_GUARDIANLINKS_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static GuardianInvitation createGuardianInvitation(String studentId, String guardianEmail) + throws Exception { - // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = + Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); + + // Create the classroom API client + ClassroomCredentials classroomCredentials = new ClassroomCredentials(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - requestInitializer) + classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -62,14 +63,14 @@ public static GuardianInvitation createGuardianInvitation(String studentId) thro for other possible states of guardian invitations. */ GuardianInvitation content = new GuardianInvitation() .setStudentId(studentId) - .setInvitationId("guardian@gmail.com") + .setInvitedEmailAddress(guardianEmail) .setState("PENDING"); try { guardianInvitation = service.userProfiles().guardianInvitations() .create(studentId, content) .execute(); - System.out.printf("Invitation created: %s", guardianInvitation.getInvitationId()); + System.out.printf("Invitation created: %s\n", guardianInvitation.getInvitationId()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); diff --git a/classroom/snippets/src/main/java/DeleteGuardian.java b/classroom/snippets/src/main/java/DeleteGuardian.java index 3cdd1f95..6d2ab1e5 100644 --- a/classroom/snippets/src/main/java/DeleteGuardian.java +++ b/classroom/snippets/src/main/java/DeleteGuardian.java @@ -15,17 +15,16 @@ // [START classroom_delete_guardian_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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; +import java.util.List; /* Class to demonstrate the use of Classroom Delete Guardian API. */ public class DeleteGuardian { @@ -36,19 +35,18 @@ public class DeleteGuardian { * @param guardianId - the id of the guardian to delete. * @throws IOException - if credentials file not found. */ - public static void deleteGuardian(String studentId, String guardianId) 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_GUARDIANLINKS_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static void deleteGuardian(String studentId, String guardianId) throws Exception { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = + Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); - // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), + // Create the classroom API client + ClassroomCredentials classroomCredentials = new ClassroomCredentials(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - requestInitializer) + classroomCredentials.getCredentials(HTTP_TRANSPORT,SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/GetGuardian.java b/classroom/snippets/src/main/java/GetGuardian.java new file mode 100644 index 00000000..2617799d --- /dev/null +++ b/classroom/snippets/src/main/java/GetGuardian.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_get_guardian_class] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +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.Guardian; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +/* Class to demonstrate the use of Classroom Get Guardian API. */ +public class GetGuardian { + /** + * Retrieve a guardian for a specific student. + * + * @param studentId - the id of the student the guardian belongs to. + * @param guardianId - the id of the guardian to delete. + * @throws IOException - if credentials file not found. + */ + public static void getGuardian(String studentId, String guardianId) throws Exception { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = + Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); + + // Create the classroom API client + ClassroomCredentials classroomCredentials = new ClassroomCredentials(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = new Classroom.Builder(HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_get_guardian_code_snippet] + Guardian guardian = null; + + try { + guardian = service.userProfiles().guardians().get(studentId, guardianId) + .execute(); + System.out.printf("Guardian retrieved: %s", guardian.getInvitedEmailAddress()); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.err.printf("There is no record of guardianId (%s).\n", guardianId); + throw e; + } else { + throw e; + } + } catch (Exception e) { + throw e; + } + // [END classroom_get_guardian_code_snippet] + } +} +// [END classroom_get_guardian_class] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java index d7a3d91e..999ec3a5 100644 --- a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java @@ -15,17 +15,15 @@ // [START classroom_list_guardian_invitations_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.GuardianInvitation; import com.google.api.services.classroom.model.ListGuardianInvitationsResponse; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -41,19 +39,19 @@ public class ListGuardianInvitationsByStudent { * @throws IOException - if credentials file not found. */ public static List listGuardianInvitationsByStudent(String studentId) - 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_GUARDIANLINKS_STUDENTS_READONLY)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws Exception { - // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = + Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); + + // Create the classroom API client + ClassroomCredentials classroomCredentials = new ClassroomCredentials(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - requestInitializer) + classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -78,7 +76,7 @@ public static List listGuardianInvitationsByStudent(String s System.out.println("No guardian invitations found."); } else { for (GuardianInvitation invitation : guardianInvitations) { - System.out.printf("Guardian invitation: %s\n", invitation.getInvitedEmailAddress()); + System.out.printf("Guardian invitation id: %s\n", invitation.getInvitationId()); } } } catch (GoogleJsonResponseException e) { diff --git a/classroom/snippets/src/main/java/ListGuardians.java b/classroom/snippets/src/main/java/ListGuardians.java index a11d993c..ebc16b41 100644 --- a/classroom/snippets/src/main/java/ListGuardians.java +++ b/classroom/snippets/src/main/java/ListGuardians.java @@ -15,17 +15,15 @@ // [START classroom_list_guardians_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Guardian; import com.google.api.services.classroom.model.ListGuardiansResponse; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -40,19 +38,16 @@ public class ListGuardians { * @return a list of active guardians for a specific student. * @throws IOException - if credentials file not found. */ - public static List listGuardians(String studentId) 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_GUARDIANLINKS_STUDENTS_READONLY)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static List listGuardians(String studentId) throws Exception { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = + Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); - // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - requestInitializer) + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -77,8 +72,9 @@ public static List listGuardians(String studentId) throws IOException System.out.println("No guardians found."); } else { for (Guardian guardian : guardians) { - System.out.printf("Guardian name: %s, guardian email: %s\n", + System.out.printf("Guardian name: %s, guardian id: %s, guardian email: %s\n", guardian.getGuardianProfile().getName().getFullName(), + guardian.getGuardianId(), guardian.getInvitedEmailAddress()); } } diff --git a/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java new file mode 100644 index 00000000..20bd36f6 --- /dev/null +++ b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.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.GuardianInvitation; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Cancel Guardian Invitation classroom snippet +public class TestCancelGuardianInvitation { + + @Test + public void testCancelGuardianInvitation() throws Exception { + String studentId = "insert_student_id"; + String invitationId = "insert_invitation_id"; + GuardianInvitation guardianInvitation = CancelGuardianInvitation.cancelGuardianInvitation(studentId, + invitationId); + + Assert.assertTrue("Guardian invitation not canceled.", guardianInvitation != null); + } +} diff --git a/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java b/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java new file mode 100644 index 00000000..a87c3fb1 --- /dev/null +++ b/classroom/snippets/src/test/java/TestCreateGuardianInvitation.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.GuardianInvitation; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Create Guardian Invitation classroom snippet +public class TestCreateGuardianInvitation { + + @Test + public void testCreateGuardianInvitation() throws Exception { + String studentId = "insert_student_id"; + String guardianEmail = "insert_guardian_email"; + GuardianInvitation guardianInvitation = CreateGuardianInvitation.createGuardianInvitation(studentId, + guardianEmail); + + Assert.assertTrue("Guardian invitation not created.", guardianInvitation != null); + } +} diff --git a/classroom/snippets/src/test/java/TestDeleteGuardian.java b/classroom/snippets/src/test/java/TestDeleteGuardian.java new file mode 100644 index 00000000..ba5a5105 --- /dev/null +++ b/classroom/snippets/src/test/java/TestDeleteGuardian.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.client.googleapis.json.GoogleJsonResponseException; +import org.junit.Assert; +import org.junit.Test; + +// Unit test class for Delete Guardian classroom snippet +public class TestDeleteGuardian { + + @Test + public void testDeleteGuardian() throws Exception { + String studentId = "insert_student_id"; + String guardianId = "insert_guardian_id"; + DeleteGuardian.deleteGuardian(studentId, guardianId); + + Assert.assertThrows("Not found", GoogleJsonResponseException.class, () -> + GetGuardian.getGuardian(studentId, guardianId)); + } +} diff --git a/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java index af5e64cb..1cc45dd3 100644 --- a/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java @@ -13,7 +13,6 @@ // limitations under the License. import com.google.api.services.classroom.model.GuardianInvitation; -import java.io.IOException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,7 +21,7 @@ public class TestListGuardianInvitationsByStudent { @Test - public void testGuardianInvitationsByStudent() throws IOException { + public void testListGuardianInvitationsByStudent() throws Exception { String studentId = "insert_student_id"; List invitationList = ListGuardianInvitationsByStudent .listGuardianInvitationsByStudent(studentId); diff --git a/classroom/snippets/src/test/java/TestListGuardians.java b/classroom/snippets/src/test/java/TestListGuardians.java index 86b4222d..3d6c008c 100644 --- a/classroom/snippets/src/test/java/TestListGuardians.java +++ b/classroom/snippets/src/test/java/TestListGuardians.java @@ -13,7 +13,6 @@ // limitations under the License. import com.google.api.services.classroom.model.Guardian; -import java.io.IOException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,7 +21,7 @@ public class TestListGuardians { @Test - public void testListGuardians() throws IOException { + public void testListGuardians() throws Exception { String studentId = "insert_student_id"; List guardianList = ListGuardians.listGuardians(studentId); From 448fe9a96bfd72e3ffb84b4b05b0e5ac97c005fc Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 21 Dec 2022 15:47:15 -0600 Subject: [PATCH 16/50] base test updates --- .../snippets/src/test/java/BaseTest.java | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index cc3996de..878d5015 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -14,15 +14,15 @@ * limitations under the License. */ -import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.api.services.classroom.model.CourseAlias; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; +import java.util.Collections; +import java.util.List; import org.junit.After; import org.junit.Before; @@ -40,27 +40,26 @@ public class BaseTest { * @return an authorized Classroom client service * @throws IOException - if credentials file not found. */ - protected Classroom buildService() 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(ClassroomScopes.CLASSROOM_ROSTERS); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + protected Classroom buildService() throws Exception { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = + Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), + ClassroomCredentials classroomCredentials = new ClassroomCredentials(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom Snippets") + classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") .build(); return service; } @Before - public void setup() throws IOException { + public void setup() throws Exception { this.service = buildService(); this.testCourse = CreateCourse.createCourse(); createAlias(this.testCourse.getId()); From 25efd520ec03bd0f581c24aebed1ae831d1bc0c7 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Mon, 9 Jan 2023 13:25:32 -0500 Subject: [PATCH 17/50] updates based on team feedback --- classroom/snippets/build.gradle | 4 +++ .../main/java/CancelGuardianInvitation.java | 3 +- .../src/main/java/ClassroomCredentials.java | 15 ++++++++- .../main/java/CreateGuardianInvitation.java | 3 +- .../src/main/java/DeleteGuardian.java | 4 +-- .../snippets/src/main/java/GetGuardian.java | 3 +- .../ListGuardianInvitationsByStudent.java | 5 +-- .../snippets/src/main/java/ListGuardians.java | 2 ++ .../src/main/java/ListStudentSubmissions.java | 31 ++++++++++++++----- .../src/main/java/ListSubmissions.java | 28 ++++++++++++++--- .../snippets/src/main/java/ListTopics.java | 2 ++ .../java/TestCancelGuardianInvitation.java | 10 ++++-- .../src/test/java/TestDeleteGuardian.java | 2 +- 13 files changed, 86 insertions(+), 26 deletions(-) diff --git a/classroom/snippets/build.gradle b/classroom/snippets/build.gradle index 7e19291b..8def0b0f 100644 --- a/classroom/snippets/build.gradle +++ b/classroom/snippets/build.gradle @@ -10,6 +10,10 @@ dependencies { implementation 'com.google.oauth-client:google-oauth-client-jetty:1.34.1' implementation 'com.google.apis:google-api-services-classroom:v1-rev20220323-2.0.0' testImplementation 'junit:junit:4.13.2' + + /** This will be removed once all the classes have been updated to use the + * ClassroomCredentials class. */ + implementation 'com.google.auth:google-auth-library-oauth2-http:1.11.0' } test { diff --git a/classroom/snippets/src/main/java/CancelGuardianInvitation.java b/classroom/snippets/src/main/java/CancelGuardianInvitation.java index f6342eef..618c9f89 100644 --- a/classroom/snippets/src/main/java/CancelGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CancelGuardianInvitation.java @@ -45,11 +45,10 @@ public static GuardianInvitation cancelGuardianInvitation(String studentId, Stri Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); // Create the classroom API client - ClassroomCredentials classroomCredentials = new ClassroomCredentials(); final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ClassroomCredentials.java b/classroom/snippets/src/main/java/ClassroomCredentials.java index 0818d90e..4f4f6c64 100644 --- a/classroom/snippets/src/main/java/ClassroomCredentials.java +++ b/classroom/snippets/src/main/java/ClassroomCredentials.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.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; @@ -16,7 +30,6 @@ public class ClassroomCredentials { private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); private static final String TOKENS_DIRECTORY_PATH = "tokens"; - private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; /** diff --git a/classroom/snippets/src/main/java/CreateGuardianInvitation.java b/classroom/snippets/src/main/java/CreateGuardianInvitation.java index 5a79e487..23f6b367 100644 --- a/classroom/snippets/src/main/java/CreateGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CreateGuardianInvitation.java @@ -46,11 +46,10 @@ public static GuardianInvitation createGuardianInvitation(String studentId, Stri Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); // Create the classroom API client - ClassroomCredentials classroomCredentials = new ClassroomCredentials(); final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/DeleteGuardian.java b/classroom/snippets/src/main/java/DeleteGuardian.java index 6d2ab1e5..1196b7ef 100644 --- a/classroom/snippets/src/main/java/DeleteGuardian.java +++ b/classroom/snippets/src/main/java/DeleteGuardian.java @@ -42,11 +42,10 @@ public static void deleteGuardian(String studentId, String guardianId) throws Ex Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); // Create the classroom API client - ClassroomCredentials classroomCredentials = new ClassroomCredentials(); final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - classroomCredentials.getCredentials(HTTP_TRANSPORT,SCOPES)) + ClassroomCredentials.getCredentials(HTTP_TRANSPORT,SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -54,6 +53,7 @@ public static void deleteGuardian(String studentId, String guardianId) throws Ex try { service.userProfiles().guardians().delete(studentId, guardianId) .execute(); + System.out.printf("The guardian with id %s was deleted.\n", guardianId); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { diff --git a/classroom/snippets/src/main/java/GetGuardian.java b/classroom/snippets/src/main/java/GetGuardian.java index 2617799d..7fcd1792 100644 --- a/classroom/snippets/src/main/java/GetGuardian.java +++ b/classroom/snippets/src/main/java/GetGuardian.java @@ -43,11 +43,10 @@ public static void getGuardian(String studentId, String guardianId) throws Excep Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); // Create the classroom API client - ClassroomCredentials classroomCredentials = new ClassroomCredentials(); final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java index 999ec3a5..150d453c 100644 --- a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java @@ -47,11 +47,10 @@ public static List listGuardianInvitationsByStudent(String s Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); // Create the classroom API client - ClassroomCredentials classroomCredentials = new ClassroomCredentials(); final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -64,8 +63,10 @@ public static List listGuardianInvitationsByStudent(String s do { ListGuardianInvitationsResponse response = service.userProfiles().guardianInvitations() .list(studentId) + .setPageToken(pageToken) .execute(); + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getGuardianInvitations() != null) { guardianInvitations.addAll(response.getGuardianInvitations()); pageToken = response.getNextPageToken(); diff --git a/classroom/snippets/src/main/java/ListGuardians.java b/classroom/snippets/src/main/java/ListGuardians.java index ebc16b41..bd2f1a13 100644 --- a/classroom/snippets/src/main/java/ListGuardians.java +++ b/classroom/snippets/src/main/java/ListGuardians.java @@ -60,8 +60,10 @@ public static List listGuardians(String studentId) throws Exception { do { ListGuardiansResponse response = service.userProfiles().guardians() .list(studentId) + .setPageToken(pageToken) .execute(); + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getGuardians() != null) { guardians.addAll(response.getGuardians()); pageToken = response.getNextPageToken(); diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index 2243ec3c..0989945c 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -62,14 +62,31 @@ 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) + .setPageToken(pageToken) + .set("userId", userId) + .execute(); + + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (studentSubmissions.isEmpty()) { + System.out.println("No submissions found."); + } else { + System.out.println("Student submissions:"); + for (StudentSubmission submission : studentSubmissions) { + System.out.printf("Submission id (%s)", 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..955726e0 100644 --- a/classroom/snippets/src/main/java/ListSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -61,12 +61,30 @@ 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) + .setPageToken(pageToken) + .execute(); + + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (studentSubmissions.isEmpty()) { + System.out.println("No submissions found."); + } else { + System.out.println("Submissions:"); + for (StudentSubmission submission : studentSubmissions) { + System.out.printf("Submission id (%s) for user (%s)", submission.getId(), + submission.getUserId()); + } } } 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 e754f82b..251880cb 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -67,6 +67,8 @@ public static List listTopics(String courseId) throws IOException { .setPageSize(100) .setPageToken(pageToken) .execute(); + + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getTopic() != null) { topics.addAll(response.getTopic()); pageToken = response.getNextPageToken(); diff --git a/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java index 20bd36f6..a2caa7b5 100644 --- a/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java +++ b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java @@ -22,10 +22,16 @@ public class TestCancelGuardianInvitation { @Test public void testCancelGuardianInvitation() throws Exception { String studentId = "insert_student_id"; - String invitationId = "insert_invitation_id"; + String guardianEmail = "insert_guardian_email"; + + GuardianInvitation invitation = CreateGuardianInvitation.createGuardianInvitation(studentId, + guardianEmail); + GuardianInvitation guardianInvitation = CancelGuardianInvitation.cancelGuardianInvitation(studentId, - invitationId); + invitation.getInvitationId()); Assert.assertTrue("Guardian invitation not canceled.", guardianInvitation != null); + Assert.assertTrue("Guardian invitation state not updated.", guardianInvitation.getState() + .equals("COMPLETE")); } } diff --git a/classroom/snippets/src/test/java/TestDeleteGuardian.java b/classroom/snippets/src/test/java/TestDeleteGuardian.java index ba5a5105..371ec1f2 100644 --- a/classroom/snippets/src/test/java/TestDeleteGuardian.java +++ b/classroom/snippets/src/test/java/TestDeleteGuardian.java @@ -25,7 +25,7 @@ public void testDeleteGuardian() throws Exception { String guardianId = "insert_guardian_id"; DeleteGuardian.deleteGuardian(studentId, guardianId); - Assert.assertThrows("Not found", GoogleJsonResponseException.class, () -> + Assert.assertThrows(GoogleJsonResponseException.class, () -> GetGuardian.getGuardian(studentId, guardianId)); } } From c0b8c1bb9d2bead4ce3ea51618150e4b32c65182 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 11 Jan 2023 15:45:48 -0500 Subject: [PATCH 18/50] fixing BaseTest.java --- classroom/snippets/src/test/java/BaseTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index 878d5015..8aa6749d 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -47,11 +47,10 @@ protected Classroom buildService() throws Exception { Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client - ClassroomCredentials classroomCredentials = new ClassroomCredentials(); final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder(HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), - classroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); From 0b209ee9e41c836022c76edffbbca8e6084fd11a Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 17 Jan 2023 10:54:40 -0500 Subject: [PATCH 19/50] updating copyright year to 2023 for new files --- classroom/snippets/src/main/java/CancelGuardianInvitation.java | 2 +- classroom/snippets/src/main/java/ClassroomCredentials.java | 2 +- classroom/snippets/src/main/java/CreateGuardianInvitation.java | 2 +- classroom/snippets/src/main/java/DeleteGuardian.java | 2 +- classroom/snippets/src/main/java/GetGuardian.java | 2 +- .../src/main/java/ListGuardianInvitationsByStudent.java | 2 +- classroom/snippets/src/main/java/ListGuardians.java | 2 +- .../snippets/src/test/java/TestCancelGuardianInvitation.java | 2 +- .../snippets/src/test/java/TestCreateGuardianInvitation.java | 2 +- classroom/snippets/src/test/java/TestDeleteGuardian.java | 2 +- .../src/test/java/TestListGuardianInvitationsByStudent.java | 2 +- classroom/snippets/src/test/java/TestListGuardians.java | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/classroom/snippets/src/main/java/CancelGuardianInvitation.java b/classroom/snippets/src/main/java/CancelGuardianInvitation.java index 618c9f89..02fd6732 100644 --- a/classroom/snippets/src/main/java/CancelGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CancelGuardianInvitation.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/main/java/ClassroomCredentials.java b/classroom/snippets/src/main/java/ClassroomCredentials.java index 4f4f6c64..ac132fcf 100644 --- a/classroom/snippets/src/main/java/ClassroomCredentials.java +++ b/classroom/snippets/src/main/java/ClassroomCredentials.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/main/java/CreateGuardianInvitation.java b/classroom/snippets/src/main/java/CreateGuardianInvitation.java index 23f6b367..004a6661 100644 --- a/classroom/snippets/src/main/java/CreateGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CreateGuardianInvitation.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/main/java/DeleteGuardian.java b/classroom/snippets/src/main/java/DeleteGuardian.java index 1196b7ef..78a05684 100644 --- a/classroom/snippets/src/main/java/DeleteGuardian.java +++ b/classroom/snippets/src/main/java/DeleteGuardian.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/main/java/GetGuardian.java b/classroom/snippets/src/main/java/GetGuardian.java index 7fcd1792..e820a109 100644 --- a/classroom/snippets/src/main/java/GetGuardian.java +++ b/classroom/snippets/src/main/java/GetGuardian.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java index 150d453c..e6b61d36 100644 --- a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/main/java/ListGuardians.java b/classroom/snippets/src/main/java/ListGuardians.java index bd2f1a13..90206e1b 100644 --- a/classroom/snippets/src/main/java/ListGuardians.java +++ b/classroom/snippets/src/main/java/ListGuardians.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java index a2caa7b5..95c5a801 100644 --- a/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java +++ b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java b/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java index a87c3fb1..6e2bbe0d 100644 --- a/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java +++ b/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/test/java/TestDeleteGuardian.java b/classroom/snippets/src/test/java/TestDeleteGuardian.java index 371ec1f2..61c4ef15 100644 --- a/classroom/snippets/src/test/java/TestDeleteGuardian.java +++ b/classroom/snippets/src/test/java/TestDeleteGuardian.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java index 1cc45dd3..157f3608 100644 --- a/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/classroom/snippets/src/test/java/TestListGuardians.java b/classroom/snippets/src/test/java/TestListGuardians.java index 3d6c008c..3d389359 100644 --- a/classroom/snippets/src/test/java/TestListGuardians.java +++ b/classroom/snippets/src/test/java/TestListGuardians.java @@ -1,4 +1,4 @@ -// Copyright 2022 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 1cfb9391a2df8dac33e070fcc1d327a5dc174f3d Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 17 Jan 2023 11:23:56 -0500 Subject: [PATCH 20/50] testing google-java-format plugin to fix lint errors --- .../main/java/CancelGuardianInvitation.java | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/classroom/snippets/src/main/java/CancelGuardianInvitation.java b/classroom/snippets/src/main/java/CancelGuardianInvitation.java index 02fd6732..33585400 100644 --- a/classroom/snippets/src/main/java/CancelGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CancelGuardianInvitation.java @@ -46,11 +46,13 @@ public static GuardianInvitation cancelGuardianInvitation(String studentId, Stri // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = new Classroom.Builder(HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) - .setApplicationName("Classroom samples") - .build(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_cancel_guardian_invitation_code_snippet] @@ -58,26 +60,29 @@ public static GuardianInvitation cancelGuardianInvitation(String studentId, Stri try { /* Change the state of the GuardianInvitation from PENDING to COMPLETE. See - https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate - for other possible states of guardian invitations. */ - GuardianInvitation content = service.userProfiles().guardianInvitations() - .get(studentId, invitationId) - .execute(); + https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate + for other possible states of guardian invitations. */ + GuardianInvitation content = + service.userProfiles().guardianInvitations().get(studentId, invitationId).execute(); content.setState("COMPLETE"); - guardianInvitation = service.userProfiles().guardianInvitations() - .patch(studentId, invitationId, content) - .set("updateMask", "state") - .execute(); + guardianInvitation = + service + .userProfiles() + .guardianInvitations() + .patch(studentId, invitationId, content) + .set("updateMask", "state") + .execute(); - System.out.printf("Invitation (%s) state set to %s\n.", guardianInvitation.getInvitationId(), - guardianInvitation.getState()); + System.out.printf( + "Invitation (%s) state set to %s\n.", + guardianInvitation.getInvitationId(), guardianInvitation.getState()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { - System.out.printf("There is no record of studentId (%s) or invitationId (%s).", studentId, - invitationId); + System.out.printf( + "There is no record of studentId (%s) or invitationId (%s).", studentId, invitationId); } else { throw e; } @@ -89,4 +94,4 @@ public static GuardianInvitation cancelGuardianInvitation(String studentId, Stri // [END classroom_cancel_guardian_invitation_code_snippet] } } -// [END classroom_cancel_guardian_invitation_class] \ No newline at end of file +// [END classroom_cancel_guardian_invitation_class] From 0e91d79b5132288c0f2b1d66e1da18aeb0d5ecb0 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 17 Jan 2023 11:41:18 -0500 Subject: [PATCH 21/50] updated files after running google-java-format plugin --- .../src/main/java/ClassroomCredentials.java | 15 +++--- .../main/java/CreateGuardianInvitation.java | 31 ++++++------ .../src/main/java/DeleteGuardian.java | 18 +++---- .../snippets/src/main/java/GetGuardian.java | 18 +++---- .../ListGuardianInvitationsByStudent.java | 24 +++++---- .../snippets/src/main/java/ListGuardians.java | 22 ++++---- .../src/main/java/ListStudentSubmissions.java | 50 ++++++++++--------- .../src/main/java/ListSubmissions.java | 48 ++++++++++-------- .../snippets/src/main/java/ListTopics.java | 37 +++++++------- .../snippets/src/test/java/BaseTest.java | 15 +++--- .../java/TestCancelGuardianInvitation.java | 12 ++--- .../java/TestCreateGuardianInvitation.java | 4 +- .../src/test/java/TestDeleteGuardian.java | 4 +- .../TestListGuardianInvitationsByStudent.java | 4 +- .../src/test/java/TestListGuardians.java | 1 - 15 files changed, 159 insertions(+), 144 deletions(-) diff --git a/classroom/snippets/src/main/java/ClassroomCredentials.java b/classroom/snippets/src/main/java/ClassroomCredentials.java index ac132fcf..70cd5d94 100644 --- a/classroom/snippets/src/main/java/ClassroomCredentials.java +++ b/classroom/snippets/src/main/java/ClassroomCredentials.java @@ -40,8 +40,7 @@ public class ClassroomCredentials { * @return An authorized Credential object. * @throws IOException If the credentials.json file cannot be found. */ - static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT, - Collection SCOPES) + static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT, Collection SCOPES) throws IOException { // Load client secrets. InputStream in = ClassroomCredentials.class.getResourceAsStream(CREDENTIALS_FILE_PATH); @@ -53,13 +52,13 @@ static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT, GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); // Build flow and trigger user authorization request. - GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( - HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) - .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) - .setAccessType("offline") - .build(); + GoogleAuthorizationCodeFlow flow = + new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) + .setAccessType("offline") + .build(); LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/main/java/CreateGuardianInvitation.java b/classroom/snippets/src/main/java/CreateGuardianInvitation.java index 004a6661..cb3fe374 100644 --- a/classroom/snippets/src/main/java/CreateGuardianInvitation.java +++ b/classroom/snippets/src/main/java/CreateGuardianInvitation.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_guardian_invitation_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -47,11 +46,13 @@ public static GuardianInvitation createGuardianInvitation(String studentId, Stri // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = new Classroom.Builder(HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) - .setApplicationName("Classroom samples") - .build(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_create_guardian_invitation_code_snippet] @@ -60,18 +61,18 @@ public static GuardianInvitation createGuardianInvitation(String studentId, Stri /* Create a GuardianInvitation object with state set to PENDING. See https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate for other possible states of guardian invitations. */ - GuardianInvitation content = new GuardianInvitation() - .setStudentId(studentId) - .setInvitedEmailAddress(guardianEmail) - .setState("PENDING"); + GuardianInvitation content = + new GuardianInvitation() + .setStudentId(studentId) + .setInvitedEmailAddress(guardianEmail) + .setState("PENDING"); try { - guardianInvitation = service.userProfiles().guardianInvitations() - .create(studentId, content) - .execute(); + guardianInvitation = + service.userProfiles().guardianInvitations().create(studentId, content).execute(); System.out.printf("Invitation created: %s\n", guardianInvitation.getInvitationId()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("There is no record of studentId: %s", studentId); @@ -86,4 +87,4 @@ public static GuardianInvitation createGuardianInvitation(String studentId, Stri // [END classroom_create_guardian_invitation_code_snippet] } } -// [END classroom_create_guardian_invitation_class] \ No newline at end of file +// [END classroom_create_guardian_invitation_class] diff --git a/classroom/snippets/src/main/java/DeleteGuardian.java b/classroom/snippets/src/main/java/DeleteGuardian.java index 78a05684..7cc67a23 100644 --- a/classroom/snippets/src/main/java/DeleteGuardian.java +++ b/classroom/snippets/src/main/java/DeleteGuardian.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_delete_guardian_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -43,16 +42,17 @@ public static void deleteGuardian(String studentId, String guardianId) throws Ex // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = new Classroom.Builder(HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT,SCOPES)) - .setApplicationName("Classroom samples") - .build(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_delete_guardian_code_snippet] try { - service.userProfiles().guardians().delete(studentId, guardianId) - .execute(); + service.userProfiles().guardians().delete(studentId, guardianId).execute(); System.out.printf("The guardian with id %s was deleted.\n", guardianId); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); @@ -63,4 +63,4 @@ public static void deleteGuardian(String studentId, String guardianId) throws Ex // [END classroom_delete_guardian_code_snippet] } } -// [END classroom_delete_guardian_class] \ No newline at end of file +// [END classroom_delete_guardian_class] diff --git a/classroom/snippets/src/main/java/GetGuardian.java b/classroom/snippets/src/main/java/GetGuardian.java index e820a109..781b6728 100644 --- a/classroom/snippets/src/main/java/GetGuardian.java +++ b/classroom/snippets/src/main/java/GetGuardian.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_get_guardian_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -44,18 +43,19 @@ public static void getGuardian(String studentId, String guardianId) throws Excep // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = new Classroom.Builder(HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) - .setApplicationName("Classroom samples") - .build(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_get_guardian_code_snippet] Guardian guardian = null; try { - guardian = service.userProfiles().guardians().get(studentId, guardianId) - .execute(); + guardian = service.userProfiles().guardians().get(studentId, guardianId).execute(); System.out.printf("Guardian retrieved: %s", guardian.getInvitedEmailAddress()); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); @@ -71,4 +71,4 @@ public static void getGuardian(String studentId, String guardianId) throws Excep // [END classroom_get_guardian_code_snippet] } } -// [END classroom_get_guardian_class] \ No newline at end of file +// [END classroom_get_guardian_class] diff --git a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java index e6b61d36..b565f44d 100644 --- a/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_list_guardian_invitations_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -48,11 +47,13 @@ public static List listGuardianInvitationsByStudent(String s // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = new Classroom.Builder(HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) - .setApplicationName("Classroom samples") - .build(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_list_guardian_invitations_code_snippet] @@ -61,10 +62,13 @@ public static List listGuardianInvitationsByStudent(String s try { do { - ListGuardianInvitationsResponse response = service.userProfiles().guardianInvitations() - .list(studentId) - .setPageToken(pageToken) - .execute(); + ListGuardianInvitationsResponse response = + service + .userProfiles() + .guardianInvitations() + .list(studentId) + .setPageToken(pageToken) + .execute(); /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getGuardianInvitations() != null) { diff --git a/classroom/snippets/src/main/java/ListGuardians.java b/classroom/snippets/src/main/java/ListGuardians.java index 90206e1b..1cb87014 100644 --- a/classroom/snippets/src/main/java/ListGuardians.java +++ b/classroom/snippets/src/main/java/ListGuardians.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_list_guardians_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -45,11 +44,13 @@ public static List listGuardians(String studentId) throws Exception { Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = new Classroom.Builder(HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) - .setApplicationName("Classroom samples") - .build(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_list_guardians_code_snippet] @@ -58,10 +59,8 @@ public static List listGuardians(String studentId) throws Exception { try { do { - ListGuardiansResponse response = service.userProfiles().guardians() - .list(studentId) - .setPageToken(pageToken) - .execute(); + ListGuardiansResponse response = + service.userProfiles().guardians().list(studentId).setPageToken(pageToken).execute(); /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getGuardians() != null) { @@ -74,7 +73,8 @@ public static List listGuardians(String studentId) throws Exception { System.out.println("No guardians found."); } else { for (Guardian guardian : guardians) { - System.out.printf("Guardian name: %s, guardian id: %s, guardian email: %s\n", + System.out.printf( + "Guardian name: %s, guardian id: %s, guardian email: %s\n", guardian.getGuardianProfile().getName().getFullName(), guardian.getGuardianId(), guardian.getInvitedEmailAddress()); diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index 639e8c7b..c5d8b29f 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -12,7 +12,6 @@ // 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; @@ -42,22 +41,22 @@ public class ListStudentSubmissions { * @return - list of student submissions. * @throws IOException - if credentials file not found. */ - public static List listStudentSubmissions(String courseId, String courseWorkId, - String userId) throws IOException { + 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); + 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(); + Classroom service = + new Classroom.Builder( + new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + .setApplicationName("Classroom samples") + .build(); // [START classroom_list_student_submissions_code_snippet] @@ -67,11 +66,15 @@ public static List listStudentSubmissions(String courseId, St try { do { // Set the userId as a query parameter on the request. - ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() - .list(courseId, courseWorkId) - .setPageToken(pageToken) - .set("userId", userId) - .execute(); + ListStudentSubmissionsResponse response = + service + .courses() + .courseWork() + .studentSubmissions() + .list(courseId, courseWorkId) + .setPageToken(pageToken) + .set("userId", userId) + .execute(); /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getStudentSubmissions() != null) { @@ -88,11 +91,12 @@ public static List listStudentSubmissions(String courseId, St } } } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // 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 " - + "not exist.\n", courseId, courseWorkId, userId); + System.out.printf( + "The courseId (%s), courseWorkId (%s), or userId (%s) does " + "not exist.\n", + courseId, courseWorkId, userId); } else { throw e; } @@ -105,4 +109,4 @@ public static List listStudentSubmissions(String courseId, St } } -// [END classroom_list_student_submissions_class] \ No newline at end of file +// [END classroom_list_student_submissions_class] diff --git a/classroom/snippets/src/main/java/ListSubmissions.java b/classroom/snippets/src/main/java/ListSubmissions.java index 031eb5a1..d5356d70 100644 --- a/classroom/snippets/src/main/java/ListSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -12,7 +12,6 @@ // 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; @@ -44,19 +43,19 @@ public class ListSubmissions { 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 - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + 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(); + Classroom service = + new Classroom.Builder( + new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + .setApplicationName("Classroom samples") + .build(); // [START classroom_list_submissions_code_snippet] @@ -65,10 +64,14 @@ public static List listSubmissions(String courseId, String co try { do { - ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions() - .list(courseId, courseWorkId) - .setPageToken(pageToken) - .execute(); + ListStudentSubmissionsResponse response = + service + .courses() + .courseWork() + .studentSubmissions() + .list(courseId, courseWorkId) + .setPageToken(pageToken) + .execute(); /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getStudentSubmissions() != null) { @@ -81,16 +84,17 @@ public static List listSubmissions(String courseId, String co 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()); + System.out.printf( + "Student id (%s), student submission id (%s)\n", + submission.getUserId(), submission.getId()); } } } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { - System.out.printf("The courseId (%s) or courseWorkId (%s) does not exist.\n", courseId, - courseWorkId); + System.out.printf( + "The courseId (%s) or courseWorkId (%s) does not exist.\n", courseId, courseWorkId); } else { throw e; } @@ -103,4 +107,4 @@ public static List listSubmissions(String courseId, String co } } -// [END classroom_list_submissions_class] \ No newline at end of file +// [END classroom_list_submissions_class] diff --git a/classroom/snippets/src/main/java/ListTopics.java b/classroom/snippets/src/main/java/ListTopics.java index 251880cb..6412a1cb 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_list_topic_class] import com.google.api.client.googleapis.json.GoogleJsonError; @@ -42,19 +41,19 @@ public class ListTopics { */ 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); + 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(); + Classroom service = + new Classroom.Builder( + new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + .setApplicationName("Classroom samples") + .build(); // [START classroom_list_topic_code_snippet] @@ -63,10 +62,14 @@ public static List listTopics(String courseId) throws IOException { try { do { - ListTopicResponse response = service.courses().topics().list(courseId) - .setPageSize(100) - .setPageToken(pageToken) - .execute(); + ListTopicResponse response = + service + .courses() + .topics() + .list(courseId) + .setPageSize(100) + .setPageToken(pageToken) + .execute(); /* Ensure that the response is not null before retrieving data from it to avoid errors. */ if (response.getTopic() != null) { @@ -83,7 +86,7 @@ public static List listTopics(String courseId) throws IOException { } } } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId does not exist: %s.\n", courseId); diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index 8aa6749d..3a9758bb 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -43,16 +43,17 @@ public class BaseTest { protected Classroom buildService() throws Exception { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - final List SCOPES = - Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); + final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = new Classroom.Builder(HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) - .setApplicationName("Classroom samples") - .build(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); return service; } diff --git a/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java index 95c5a801..d5ad35b4 100644 --- a/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java +++ b/classroom/snippets/src/test/java/TestCancelGuardianInvitation.java @@ -24,14 +24,14 @@ public void testCancelGuardianInvitation() throws Exception { String studentId = "insert_student_id"; String guardianEmail = "insert_guardian_email"; - GuardianInvitation invitation = CreateGuardianInvitation.createGuardianInvitation(studentId, - guardianEmail); + GuardianInvitation invitation = + CreateGuardianInvitation.createGuardianInvitation(studentId, guardianEmail); - GuardianInvitation guardianInvitation = CancelGuardianInvitation.cancelGuardianInvitation(studentId, - invitation.getInvitationId()); + GuardianInvitation guardianInvitation = + CancelGuardianInvitation.cancelGuardianInvitation(studentId, invitation.getInvitationId()); Assert.assertTrue("Guardian invitation not canceled.", guardianInvitation != null); - Assert.assertTrue("Guardian invitation state not updated.", guardianInvitation.getState() - .equals("COMPLETE")); + Assert.assertTrue( + "Guardian invitation state not updated.", guardianInvitation.getState().equals("COMPLETE")); } } diff --git a/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java b/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java index 6e2bbe0d..bc98e2b9 100644 --- a/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java +++ b/classroom/snippets/src/test/java/TestCreateGuardianInvitation.java @@ -23,8 +23,8 @@ public class TestCreateGuardianInvitation { public void testCreateGuardianInvitation() throws Exception { String studentId = "insert_student_id"; String guardianEmail = "insert_guardian_email"; - GuardianInvitation guardianInvitation = CreateGuardianInvitation.createGuardianInvitation(studentId, - guardianEmail); + GuardianInvitation guardianInvitation = + CreateGuardianInvitation.createGuardianInvitation(studentId, guardianEmail); Assert.assertTrue("Guardian invitation not created.", guardianInvitation != null); } diff --git a/classroom/snippets/src/test/java/TestDeleteGuardian.java b/classroom/snippets/src/test/java/TestDeleteGuardian.java index 61c4ef15..fc70bd08 100644 --- a/classroom/snippets/src/test/java/TestDeleteGuardian.java +++ b/classroom/snippets/src/test/java/TestDeleteGuardian.java @@ -25,7 +25,7 @@ public void testDeleteGuardian() throws Exception { String guardianId = "insert_guardian_id"; DeleteGuardian.deleteGuardian(studentId, guardianId); - Assert.assertThrows(GoogleJsonResponseException.class, () -> - GetGuardian.getGuardian(studentId, guardianId)); + Assert.assertThrows( + GoogleJsonResponseException.class, () -> GetGuardian.getGuardian(studentId, guardianId)); } } diff --git a/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java index 157f3608..0ee3390f 100644 --- a/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java +++ b/classroom/snippets/src/test/java/TestListGuardianInvitationsByStudent.java @@ -23,8 +23,8 @@ public class TestListGuardianInvitationsByStudent { @Test public void testListGuardianInvitationsByStudent() throws Exception { String studentId = "insert_student_id"; - List invitationList = ListGuardianInvitationsByStudent - .listGuardianInvitationsByStudent(studentId); + List invitationList = + ListGuardianInvitationsByStudent.listGuardianInvitationsByStudent(studentId); Assert.assertTrue("No guardian invitations returned.", invitationList.size() > 0); } diff --git a/classroom/snippets/src/test/java/TestListGuardians.java b/classroom/snippets/src/test/java/TestListGuardians.java index 3d389359..b1b5eb03 100644 --- a/classroom/snippets/src/test/java/TestListGuardians.java +++ b/classroom/snippets/src/test/java/TestListGuardians.java @@ -27,5 +27,4 @@ public void testListGuardians() throws Exception { Assert.assertTrue("No guardians returned.", guardianList.size() > 0); } - } From 09c634c4d09ae2c25a4ee35ed9d03d79f36145be Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 17 Jan 2023 11:47:11 -0500 Subject: [PATCH 22/50] updated BaseTest --- classroom/snippets/src/test/java/BaseTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index 3a9758bb..4fd032f1 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -47,15 +47,13 @@ protected Classroom buildService() throws Exception { // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = + return service = new Classroom.Builder( HTTP_TRANSPORT, GsonFactory.getDefaultInstance(), ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); - - return service; } @Before @@ -71,11 +69,10 @@ public void tearDown() throws IOException { this.testCourse = null; } - public CourseAlias createAlias(String courseId) throws IOException { + public void createAlias(String courseId) throws IOException { String alias = "p:" + UUID.randomUUID(); CourseAlias courseAlias = new CourseAlias().setAlias(alias); - courseAlias = this.service.courses().aliases().create(courseId, courseAlias).execute(); - return courseAlias; + this.service.courses().aliases().create(courseId, courseAlias).execute(); } public void deleteCourse(String courseId) throws IOException { From fde753dbe34441b2ce7e54870af03552ff0c76f5 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 17 Jan 2023 11:56:59 -0500 Subject: [PATCH 23/50] fixing copyright header for BaseTest --- .../snippets/src/test/java/BaseTest.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index 4fd032f1..b0c5286e 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -1,18 +1,16 @@ -/* - * 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 - * - * http://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. - */ +// 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.javanet.GoogleNetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; From f69ff9f7b3a9f20c56aa4f98f6b39a9786092e89 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Mon, 23 Jan 2023 16:18:09 -0500 Subject: [PATCH 24/50] updated auth for AddAlias and CreateCourse classes --- .../src/main/java/AddAliasToCourse.java | 29 ++++++++++--------- .../snippets/src/main/java/CreateCourse.java | 27 ++++++++--------- .../src/test/java/TestAddAliasToCourse.java | 2 +- .../src/test/java/TestAddStudent.java | 2 +- .../src/test/java/TestCreateCourse.java | 2 +- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index 10434391..c0c4d530 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -15,6 +15,7 @@ // [START classroom_add_alias_to_course_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; @@ -27,6 +28,7 @@ import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; +import java.util.List; /* Class to demonstrate the use of Classroom Create Alias API. */ public class AddAliasToCourse { @@ -37,21 +39,20 @@ public class AddAliasToCourse { * @return - newly created course alias. * @throws IOException - if credentials file not found. */ - 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. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static CourseAlias addAliasToCourse(String courseId) throws Exception { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); - // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_add_alias_to_course_code_snippet] diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index ce33dd4d..b3862bed 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -15,6 +15,7 @@ // [START classroom_create_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; @@ -27,6 +28,7 @@ import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; +import java.util.List; /* Class to demonstrate the use of Classroom Create Course API */ public class CreateCourse { @@ -36,21 +38,20 @@ public class CreateCourse { * @return newly created course * @throws IOException - if credentials file not found. */ - public static Course createCourse() 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); + public static Course createCourse() throws Exception { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Course course = null; try { diff --git a/classroom/snippets/src/test/java/TestAddAliasToCourse.java b/classroom/snippets/src/test/java/TestAddAliasToCourse.java index c5b85777..4085c25f 100644 --- a/classroom/snippets/src/test/java/TestAddAliasToCourse.java +++ b/classroom/snippets/src/test/java/TestAddAliasToCourse.java @@ -22,7 +22,7 @@ public class TestAddAliasToCourse extends BaseTest { @Test - public void testAddCourseAlias() throws IOException { + public void testAddCourseAlias() throws Exception { CourseAlias courseAlias = AddAliasToCourse.addAliasToCourse(testCourse.getId()); List courseAliases = service.courses().aliases() .list(testCourse.getId() diff --git a/classroom/snippets/src/test/java/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index f023989a..57fc2b45 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -22,7 +22,7 @@ public class TestAddStudent extends BaseTest { @Test - public void testAddStudent() throws IOException { + public void testAddStudent() throws Exception { Course course = CreateCourse.createCourse(); Student student = AddStudent.addStudent(course.getId(), course.getEnrollmentCode()); deleteCourse(course.getId()); diff --git a/classroom/snippets/src/test/java/TestCreateCourse.java b/classroom/snippets/src/test/java/TestCreateCourse.java index 381f2c2c..e09bd55f 100644 --- a/classroom/snippets/src/test/java/TestCreateCourse.java +++ b/classroom/snippets/src/test/java/TestCreateCourse.java @@ -21,7 +21,7 @@ public class TestCreateCourse extends BaseTest { @Test - public void testCreateCourse() throws IOException { + public void testCreateCourse() throws Exception { Course course = CreateCourse.createCourse(); Assert.assertNotNull("Course not returned.", course); deleteCourse(course.getId()); From ab81119b68e4d362015ae0bc8b9ef47370d4fad8 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Mon, 23 Jan 2023 16:30:57 -0500 Subject: [PATCH 25/50] removing unused imports --- classroom/snippets/src/main/java/AddAliasToCourse.java | 3 --- classroom/snippets/src/main/java/CreateCourse.java | 3 --- 2 files changed, 6 deletions(-) diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index c0c4d530..dd7fddec 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -18,14 +18,11 @@ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.CourseAlias; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; import java.util.List; diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index b3862bed..d8fcd78b 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -18,14 +18,11 @@ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; import java.util.List; From 1cb5583bba3656097437cf0702b43626c7b37e49 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 24 Jan 2023 11:09:34 -0500 Subject: [PATCH 26/50] Fixing comment. --- classroom/snippets/src/main/java/AddAliasToCourse.java | 2 +- classroom/snippets/src/main/java/CreateCourse.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index dd7fddec..8d23709c 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -41,7 +41,7 @@ public static CourseAlias addAliasToCourse(String courseId) throws Exception { tokens/ folder. */ final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); - // Create the classroom API client + // Create the classroom API client. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index d8fcd78b..b08ba7a1 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -40,7 +40,7 @@ public static Course createCourse() throws Exception { tokens/ folder. */ final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); - // Create the classroom API client + // Create the classroom API client. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( From ef20f9f5dc7e28bd63eae2d9e4a1c110cde4661d Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 25 Jan 2023 11:09:13 -0500 Subject: [PATCH 27/50] specifying errors in method signatures --- classroom/snippets/src/main/java/AddAliasToCourse.java | 6 ++++-- classroom/snippets/src/main/java/CreateCourse.java | 5 +++-- classroom/snippets/src/test/java/TestAddAliasToCourse.java | 3 ++- classroom/snippets/src/test/java/TestAddStudent.java | 3 ++- classroom/snippets/src/test/java/TestCreateCourse.java | 3 ++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index 8d23709c..95832161 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_add_alias_to_course_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -24,11 +23,13 @@ import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; /* Class to demonstrate the use of Classroom Create Alias API. */ public class AddAliasToCourse { + /** * Add an alias on an existing course. * @@ -36,7 +37,8 @@ public class AddAliasToCourse { * @return - newly created course alias. * @throws IOException - if credentials file not found. */ - public static CourseAlias addAliasToCourse(String courseId) throws Exception { + public static CourseAlias addAliasToCourse(String courseId) + throws GeneralSecurityException, IOException { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index b08ba7a1..a7589c4b 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_course] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -24,18 +23,20 @@ import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; /* Class to demonstrate the use of Classroom Create Course API */ public class CreateCourse { + /** * Creates a course * * @return newly created course * @throws IOException - if credentials file not found. */ - public static Course createCourse() throws Exception { + public static Course createCourse() throws GeneralSecurityException, IOException { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); diff --git a/classroom/snippets/src/test/java/TestAddAliasToCourse.java b/classroom/snippets/src/test/java/TestAddAliasToCourse.java index 4085c25f..ddae9187 100644 --- a/classroom/snippets/src/test/java/TestAddAliasToCourse.java +++ b/classroom/snippets/src/test/java/TestAddAliasToCourse.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,7 +23,7 @@ public class TestAddAliasToCourse extends BaseTest { @Test - public void testAddCourseAlias() throws Exception { + public void testAddCourseAlias() throws GeneralSecurityException, IOException { CourseAlias courseAlias = AddAliasToCourse.addAliasToCourse(testCourse.getId()); List courseAliases = service.courses().aliases() .list(testCourse.getId() diff --git a/classroom/snippets/src/test/java/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index 57fc2b45..25bb5e39 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.Student; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; import java.io.IOException; @@ -22,7 +23,7 @@ public class TestAddStudent extends BaseTest { @Test - public void testAddStudent() throws Exception { + public void testAddStudent() throws GeneralSecurityException, IOException { Course course = CreateCourse.createCourse(); Student student = AddStudent.addStudent(course.getId(), course.getEnrollmentCode()); deleteCourse(course.getId()); diff --git a/classroom/snippets/src/test/java/TestCreateCourse.java b/classroom/snippets/src/test/java/TestCreateCourse.java index e09bd55f..316b1355 100644 --- a/classroom/snippets/src/test/java/TestCreateCourse.java +++ b/classroom/snippets/src/test/java/TestCreateCourse.java @@ -13,6 +13,7 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; import java.io.IOException; @@ -21,7 +22,7 @@ public class TestCreateCourse extends BaseTest { @Test - public void testCreateCourse() throws Exception { + public void testCreateCourse() throws GeneralSecurityException, IOException { Course course = CreateCourse.createCourse(); Assert.assertNotNull("Course not returned.", course); deleteCourse(course.getId()); From 676cffaaefd8c12917fe8a33733c4ff0fc4a993f Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 25 Jan 2023 15:21:51 -0500 Subject: [PATCH 28/50] updated BaseTest and AddStudent --- .../src/main/java/AddAliasToCourse.java | 10 +++--- .../snippets/src/main/java/AddStudent.java | 36 +++++++++---------- .../snippets/src/main/java/CreateCourse.java | 12 +++---- .../snippets/src/test/java/BaseTest.java | 15 ++++---- .../src/test/java/TestAddAliasToCourse.java | 2 ++ .../src/test/java/TestAddStudent.java | 10 +++--- .../src/test/java/TestCreateCourse.java | 2 ++ 7 files changed, 47 insertions(+), 40 deletions(-) diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index 95832161..4b6898c9 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -24,11 +24,14 @@ import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; import java.security.GeneralSecurityException; -import java.util.Collections; -import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Create Alias API. */ public class AddAliasToCourse { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Add an alias on an existing course. @@ -39,9 +42,6 @@ public class AddAliasToCourse { */ public static CourseAlias addAliasToCourse(String courseId) throws GeneralSecurityException, IOException { - /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); diff --git a/classroom/snippets/src/main/java/AddStudent.java b/classroom/snippets/src/main/java/AddStudent.java index c04c38fb..c8690c8c 100644 --- a/classroom/snippets/src/main/java/AddStudent.java +++ b/classroom/snippets/src/main/java/AddStudent.java @@ -15,21 +15,25 @@ // [START classroom_add_student] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Student; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Add Student API */ public class AddStudent { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + /** * Add a student in a specified course. * @@ -39,21 +43,17 @@ public class AddStudent { * @throws IOException - if credentials file not found. */ public static Student addStudent(String courseId, String enrollmentCode) - 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_ROSTERS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Student student = new Student().setUserId("gduser1@workspacesamples.dev"); try { diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index a7589c4b..44e267df 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -24,11 +24,14 @@ import com.google.api.services.classroom.model.Course; import java.io.IOException; import java.security.GeneralSecurityException; -import java.util.Collections; -import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Create Course API */ public class CreateCourse { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Creates a course @@ -37,9 +40,6 @@ public class CreateCourse { * @throws IOException - if credentials file not found. */ public static Course createCourse() throws GeneralSecurityException, IOException { - /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); @@ -63,7 +63,7 @@ public static Course createCourse() throws GeneralSecurityException, IOException + "to be excited!") .setRoom("301") .setOwnerId("me") - .setCourseState("PROVISIONED"); + .setCourseState("ACTIVE"); course = service.courses().create(course).execute(); // Prints the new created course Id and name System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId()); diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index b0c5286e..cdad6755 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -19,6 +19,8 @@ import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.CourseAlias; +import java.security.GeneralSecurityException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.junit.After; @@ -38,10 +40,10 @@ public class BaseTest { * @return an authorized Classroom client service * @throws IOException - if credentials file not found. */ - protected Classroom buildService() throws Exception { + protected Classroom buildService(ArrayList SCOPES) throws GeneralSecurityException, IOException { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); + SCOPES.add(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); @@ -54,16 +56,17 @@ protected Classroom buildService() throws Exception { .build(); } - @Before - public void setup() throws Exception { - this.service = buildService(); + public void setup(ArrayList scopes) throws GeneralSecurityException, IOException { + this.service = buildService(scopes); this.testCourse = CreateCourse.createCourse(); createAlias(this.testCourse.getId()); } @After public void tearDown() throws IOException { - deleteCourse(this.testCourse.getId()); + if (this.testCourse != null) { + deleteCourse(this.testCourse.getId()); + } this.testCourse = null; } diff --git a/classroom/snippets/src/test/java/TestAddAliasToCourse.java b/classroom/snippets/src/test/java/TestAddAliasToCourse.java index ddae9187..8bbfb289 100644 --- a/classroom/snippets/src/test/java/TestAddAliasToCourse.java +++ b/classroom/snippets/src/test/java/TestAddAliasToCourse.java @@ -24,6 +24,8 @@ public class TestAddAliasToCourse extends BaseTest { @Test public void testAddCourseAlias() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(AddAliasToCourse.SCOPES); CourseAlias courseAlias = AddAliasToCourse.addAliasToCourse(testCourse.getId()); List courseAliases = service.courses().aliases() .list(testCourse.getId() diff --git a/classroom/snippets/src/test/java/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index 25bb5e39..77485e52 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.Student; import java.security.GeneralSecurityException; import org.junit.Assert; @@ -24,11 +23,12 @@ public class TestAddStudent extends BaseTest { @Test public void testAddStudent() throws GeneralSecurityException, IOException { - Course course = CreateCourse.createCourse(); - Student student = AddStudent.addStudent(course.getId(), course.getEnrollmentCode()); - deleteCourse(course.getId()); + // Include the scopes required to run the code example for testing purposes. + setup(AddStudent.SCOPES); + Student student = AddStudent.addStudent(testCourse.getId(), testCourse.getEnrollmentCode()); Assert.assertNotNull("Student not returned.", student); - Assert.assertEquals("Student added to wrong course.", course.getId(), + Assert.assertNotNull("Course not returned.", student.getCourseId()); + Assert.assertEquals("Student added to wrong course.", testCourse.getId(), student.getCourseId()); } } \ No newline at end of file diff --git a/classroom/snippets/src/test/java/TestCreateCourse.java b/classroom/snippets/src/test/java/TestCreateCourse.java index 316b1355..86200500 100644 --- a/classroom/snippets/src/test/java/TestCreateCourse.java +++ b/classroom/snippets/src/test/java/TestCreateCourse.java @@ -23,6 +23,8 @@ public class TestCreateCourse extends BaseTest { @Test public void testCreateCourse() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(CreateCourse.SCOPES); Course course = CreateCourse.createCourse(); Assert.assertNotNull("Course not returned.", course); deleteCourse(course.getId()); From 130240f186864447d9bdbc04ef1f81f9e2609ecd Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 25 Jan 2023 15:34:32 -0500 Subject: [PATCH 29/50] Adding annotation for GeneralSecurityException --- classroom/snippets/src/main/java/AddAliasToCourse.java | 1 + classroom/snippets/src/main/java/AddStudent.java | 3 ++- classroom/snippets/src/main/java/CreateCourse.java | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index 4b6898c9..8187e44f 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -39,6 +39,7 @@ public class AddAliasToCourse { * @param courseId - id of the course to add an alias to. * @return - newly created course alias. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static CourseAlias addAliasToCourse(String courseId) throws GeneralSecurityException, IOException { diff --git a/classroom/snippets/src/main/java/AddStudent.java b/classroom/snippets/src/main/java/AddStudent.java index c8690c8c..06bd7d36 100644 --- a/classroom/snippets/src/main/java/AddStudent.java +++ b/classroom/snippets/src/main/java/AddStudent.java @@ -37,10 +37,11 @@ public class AddStudent { /** * Add a student in a specified course. * - * @param courseId - Id of the course. + * @param courseId - Id of the course. * @param enrollmentCode - Code of the course to enroll. * @return newly added student * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static Student addStudent(String courseId, String enrollmentCode) throws GeneralSecurityException, IOException { diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index 44e267df..b54de6a0 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -38,6 +38,7 @@ public class CreateCourse { * * @return newly created course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static Course createCourse() throws GeneralSecurityException, IOException { From b2cbc21f93beba97e8079b693717681448b9eb45 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 26 Jan 2023 14:39:03 -0500 Subject: [PATCH 30/50] updating AddStudent method signature and test --- classroom/snippets/src/main/java/AddStudent.java | 9 +++++---- classroom/snippets/src/test/java/TestAddStudent.java | 5 ++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/classroom/snippets/src/main/java/AddStudent.java b/classroom/snippets/src/main/java/AddStudent.java index 06bd7d36..e87ac1ff 100644 --- a/classroom/snippets/src/main/java/AddStudent.java +++ b/classroom/snippets/src/main/java/AddStudent.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_add_student] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -30,9 +29,11 @@ /* Class to demonstrate the use of Classroom Add Student API */ public class AddStudent { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Add a student in a specified course. @@ -43,7 +44,7 @@ public class AddStudent { * @throws IOException - if credentials file not found. * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Student addStudent(String courseId, String enrollmentCode) + public static Student addStudent(String courseId, String enrollmentCode, String studentId) throws GeneralSecurityException, IOException { // Create the classroom API client. @@ -56,7 +57,7 @@ public static Student addStudent(String courseId, String enrollmentCode) .setApplicationName("Classroom samples") .build(); - Student student = new Student().setUserId("gduser1@workspacesamples.dev"); + Student student = new Student().setUserId(studentId); try { // Enrolling a student to a specified course student = service.courses().students().create(courseId, student) diff --git a/classroom/snippets/src/test/java/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index 77485e52..51d9d22a 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -21,11 +21,14 @@ // Unit test class for Add Student classroom snippet public class TestAddStudent extends BaseTest { + private String studentId = "insert_student_id"; + @Test public void testAddStudent() throws GeneralSecurityException, IOException { // Include the scopes required to run the code example for testing purposes. setup(AddStudent.SCOPES); - Student student = AddStudent.addStudent(testCourse.getId(), testCourse.getEnrollmentCode()); + Student student = AddStudent.addStudent(testCourse.getId(), testCourse.getEnrollmentCode(), + studentId); Assert.assertNotNull("Student not returned.", student); Assert.assertNotNull("Course not returned.", student.getCourseId()); Assert.assertEquals("Student added to wrong course.", testCourse.getId(), From c32006ac1ac0eec2588fe0d756ce09e34dd0d8b5 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 26 Jan 2023 14:53:57 -0500 Subject: [PATCH 31/50] updated AddTeacher and AddStudent test --- .../snippets/src/main/java/AddTeacher.java | 42 ++++++++++--------- .../src/test/java/TestAddStudent.java | 2 +- .../src/test/java/TestAddTeacher.java | 10 +++-- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/classroom/snippets/src/main/java/AddTeacher.java b/classroom/snippets/src/main/java/AddTeacher.java index 9f5652e3..7f07a661 100644 --- a/classroom/snippets/src/main/java/AddTeacher.java +++ b/classroom/snippets/src/main/java/AddTeacher.java @@ -12,48 +12,50 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_add_teacher] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Teacher; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Add Teacher API */ public class AddTeacher { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + /** * Add teacher to a specific course. * - * @param courseId - Id of the course. + * @param courseId - Id of the course. * @param teacherEmail - Email address of the teacher. * @return newly created teacher * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static Teacher addTeacher(String courseId, String teacherEmail) - 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_ROSTERS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Teacher teacher = new Teacher().setUserId(teacherEmail); try { diff --git a/classroom/snippets/src/test/java/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index 51d9d22a..c1cc6e6f 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -28,7 +28,7 @@ public void testAddStudent() throws GeneralSecurityException, IOException { // Include the scopes required to run the code example for testing purposes. setup(AddStudent.SCOPES); Student student = AddStudent.addStudent(testCourse.getId(), testCourse.getEnrollmentCode(), - studentId); + this.studentId); Assert.assertNotNull("Student not returned.", student); Assert.assertNotNull("Course not returned.", student.getCourseId()); Assert.assertEquals("Student added to wrong course.", testCourse.getId(), diff --git a/classroom/snippets/src/test/java/TestAddTeacher.java b/classroom/snippets/src/test/java/TestAddTeacher.java index f2846ddf..efa1cafc 100644 --- a/classroom/snippets/src/test/java/TestAddTeacher.java +++ b/classroom/snippets/src/test/java/TestAddTeacher.java @@ -13,17 +13,21 @@ // limitations under the License. import com.google.api.services.classroom.model.Teacher; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; import java.io.IOException; // Unit test class for Add Teacher classroom snippet public class TestAddTeacher extends BaseTest { - private String otherUser = "gduser1@workspacesamples.dev"; + + private String teacherEmail = "insert_teacher_email"; @Test - public void testAddTeacher() throws IOException { - Teacher teacher = AddTeacher.addTeacher(testCourse.getId(), this.otherUser); + public void testAddTeacher() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(AddTeacher.SCOPES); + Teacher teacher = AddTeacher.addTeacher(testCourse.getId(), this.teacherEmail); Assert.assertNotNull("Teacher not returned.", teacher); Assert.assertEquals("Teacher added to wrong course.", testCourse.getId(), teacher.getCourseId()); From a55e9ec493026ae72254d6265d81058a63545560 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 26 Jan 2023 15:06:40 -0500 Subject: [PATCH 32/50] updated auth for BatchAddStudents --- .../src/main/java/BatchAddStudents.java | 39 ++++++++++--------- .../src/test/java/TestBatchAddStudents.java | 8 ++-- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/classroom/snippets/src/main/java/BatchAddStudents.java b/classroom/snippets/src/main/java/BatchAddStudents.java index 9588ec3d..9e2cf6e0 100644 --- a/classroom/snippets/src/main/java/BatchAddStudents.java +++ b/classroom/snippets/src/main/java/BatchAddStudents.java @@ -17,45 +17,48 @@ import com.google.api.client.googleapis.batch.BatchRequest; import com.google.api.client.googleapis.batch.json.JsonBatchCallback; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.http.HttpHeaders; -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.Student; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom Batch Add Students API */ public class BatchAddStudents { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + /** * Add multiple students in a specified course. * * @param courseId - Id of the course to add students. * @param studentEmails - Email address of the students. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static void batchAddStudents(String courseId, List studentEmails) - 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_ROSTERS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); BatchRequest batch = service.batch(); JsonBatchCallback callback = new JsonBatchCallback<>() { diff --git a/classroom/snippets/src/test/java/TestBatchAddStudents.java b/classroom/snippets/src/test/java/TestBatchAddStudents.java index 04ee930e..cfa1f4c7 100644 --- a/classroom/snippets/src/test/java/TestBatchAddStudents.java +++ b/classroom/snippets/src/test/java/TestBatchAddStudents.java @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import java.security.GeneralSecurityException; import org.junit.Test; import java.io.IOException; import java.util.Arrays; @@ -21,9 +22,10 @@ public class TestBatchAddStudents extends BaseTest { @Test - public void testBatchAddStudents() throws IOException { - List studentEmails = Arrays.asList("gduser2@workpsacesamples.dev", - "gduser3@workpsacesamples.dev"); + public void testBatchAddStudents() throws GeneralSecurityException, IOException { + setup(BatchAddStudents.SCOPES); + List studentEmails = Arrays.asList("insert_student_1_email", + "insert_student_2_email"); BatchAddStudents.batchAddStudents(testCourse.getId(), studentEmails); } } \ No newline at end of file From 86b7655bf53568766d2f96473148368d594c5ccd Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 26 Jan 2023 15:28:05 -0500 Subject: [PATCH 33/50] updated auth for CreateCourseWork --- .../src/main/java/CreateCourseWork.java | 39 ++++++++++--------- .../src/test/java/TestCreateCourseWork.java | 5 ++- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/classroom/snippets/src/main/java/CreateCourseWork.java b/classroom/snippets/src/main/java/CreateCourseWork.java index 7f9973ca..32afb896 100644 --- a/classroom/snippets/src/main/java/CreateCourseWork.java +++ b/classroom/snippets/src/main/java/CreateCourseWork.java @@ -14,49 +14,50 @@ // [START classroom_create_coursework_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.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.security.GeneralSecurityException; +import java.util.ArrayList; 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 { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + /** * 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. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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); + public static CourseWork createCourseWork(String courseId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_create_coursework_code_snippet] diff --git a/classroom/snippets/src/test/java/TestCreateCourseWork.java b/classroom/snippets/src/test/java/TestCreateCourseWork.java index e3a25b6b..5a160af0 100644 --- a/classroom/snippets/src/test/java/TestCreateCourseWork.java +++ b/classroom/snippets/src/test/java/TestCreateCourseWork.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.CourseWork; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; @@ -21,7 +22,9 @@ public class TestCreateCourseWork extends BaseTest { @Test - public void testCreateCoursework() throws IOException { + public void testCreateCoursework() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(CreateCourseWork.SCOPES); CourseWork courseWork = CreateCourseWork.createCourseWork(testCourse.getId()); Assert.assertNotNull("Coursework not returned.", courseWork); } From 4c3b0e13b26c190b03942fe22790303266abc53a Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 26 Jan 2023 15:57:43 -0500 Subject: [PATCH 34/50] updated auth for Topic related classes and tests --- .../snippets/src/main/java/CreateTopic.java | 38 +++++++-------- .../snippets/src/main/java/DeleteTopic.java | 46 +++++++++---------- .../snippets/src/main/java/GetTopic.java | 46 +++++++++---------- .../snippets/src/main/java/ListTopics.java | 29 ++++++------ .../snippets/src/main/java/UpdateTopic.java | 40 ++++++++-------- .../src/test/java/TestCreateTopic.java | 4 +- .../src/test/java/TestDeleteTopic.java | 5 +- .../snippets/src/test/java/TestGetTopic.java | 4 +- .../src/test/java/TestListTopics.java | 4 +- .../src/test/java/TestUpdateTopic.java | 6 ++- 10 files changed, 119 insertions(+), 103 deletions(-) diff --git a/classroom/snippets/src/main/java/CreateTopic.java b/classroom/snippets/src/main/java/CreateTopic.java index a46e573f..f09aa875 100644 --- a/classroom/snippets/src/main/java/CreateTopic.java +++ b/classroom/snippets/src/main/java/CreateTopic.java @@ -12,46 +12,48 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Topic; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to create a topic. */ public class CreateTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * 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. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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); + public static Topic createTopic(String courseId) throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_create_topic_code_snippet] diff --git a/classroom/snippets/src/main/java/DeleteTopic.java b/classroom/snippets/src/main/java/DeleteTopic.java index b8ee8f73..b94ce294 100644 --- a/classroom/snippets/src/main/java/DeleteTopic.java +++ b/classroom/snippets/src/main/java/DeleteTopic.java @@ -12,23 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_delete_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to delete a topic. */ public class DeleteTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * Delete a topic in a course. * @@ -36,22 +41,20 @@ public class DeleteTopic { * @param topicId - the id of the topic to delete. * @return - updated topic. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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); + public static void deleteTopic(String courseId, String topicId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_delete_topic_code_snippet] @@ -59,12 +62,7 @@ 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 - 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; - } + 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 140a0407..3570636e 100644 --- a/classroom/snippets/src/main/java/GetTopic.java +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -12,24 +12,29 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_get_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Topic; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to get a topic. */ public class GetTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * Get a topic in a course. * @@ -37,22 +42,20 @@ public class GetTopic { * @param topicId - the id of the topic to retrieve. * @return - the topic to retrieve. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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); + public static Topic getTopic(String courseId, String topicId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_get_topic_code_snippet] @@ -63,12 +66,7 @@ public static Topic getTopic(String courseId, String topicId) throws IOException System.out.printf("Topic '%s' found.\n", topic.getName()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately - 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; - } + 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 6412a1cb..f2e2dd0c 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -14,44 +14,47 @@ // [START classroom_list_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.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.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate how to list topics in a course. */ public class ListTopics { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * 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. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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); + public static List listTopics(String courseId) + throws GeneralSecurityException, IOException { // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/UpdateTopic.java b/classroom/snippets/src/main/java/UpdateTopic.java index f45ffeca..62546e22 100644 --- a/classroom/snippets/src/main/java/UpdateTopic.java +++ b/classroom/snippets/src/main/java/UpdateTopic.java @@ -12,24 +12,29 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_update_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Topic; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to update one or more fields in a topic. */ public class UpdateTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * Update one or more fields in a topic in a course. * @@ -37,22 +42,19 @@ public class UpdateTopic { * @param topicId - the id of the topic to update. * @return - updated topic. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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); - + public static Topic updateTopic(String courseId, String topicId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_update_topic_code_snippet] diff --git a/classroom/snippets/src/test/java/TestCreateTopic.java b/classroom/snippets/src/test/java/TestCreateTopic.java index e2d77586..381921d9 100644 --- a/classroom/snippets/src/test/java/TestCreateTopic.java +++ b/classroom/snippets/src/test/java/TestCreateTopic.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; @@ -21,7 +22,8 @@ public class TestCreateTopic extends BaseTest { @Test - public void testCreateTopic() throws IOException { + public void testCreateTopic() throws GeneralSecurityException, IOException { + setup(CreateTopic.SCOPES); 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 index b093865c..a77a2b91 100644 --- a/classroom/snippets/src/test/java/TestDeleteTopic.java +++ b/classroom/snippets/src/test/java/TestDeleteTopic.java @@ -15,12 +15,15 @@ import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; public class TestDeleteTopic extends BaseTest { + @Test - public void testDeleteTopic() throws IOException { + public void testDeleteTopic() throws GeneralSecurityException, IOException { + setup(DeleteTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); DeleteTopic.deleteTopic(testCourse.getId(), topic.getTopicId()); Assert.assertThrows(GoogleJsonResponseException.class, diff --git a/classroom/snippets/src/test/java/TestGetTopic.java b/classroom/snippets/src/test/java/TestGetTopic.java index b7cb7840..39775ff2 100644 --- a/classroom/snippets/src/test/java/TestGetTopic.java +++ b/classroom/snippets/src/test/java/TestGetTopic.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; @@ -21,7 +22,8 @@ public class TestGetTopic extends BaseTest { @Test - public void testGetTopic() throws IOException { + public void testGetTopic() throws GeneralSecurityException, IOException { + setup(GetTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); Topic getTopic = GetTopic.getTopic(testCourse.getId(), topic.getTopicId()); Assert.assertNotNull("Topic could not be created.", topic); diff --git a/classroom/snippets/src/test/java/TestListTopics.java b/classroom/snippets/src/test/java/TestListTopics.java index 081be57b..f0b0bc85 100644 --- a/classroom/snippets/src/test/java/TestListTopics.java +++ b/classroom/snippets/src/test/java/TestListTopics.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,7 +23,8 @@ public class TestListTopics extends BaseTest { @Test - public void testListTopics() throws IOException { + public void testListTopics() throws GeneralSecurityException, IOException { + setup(ListTopics.SCOPES); CreateTopic.createTopic(testCourse.getId()); List listTopics = ListTopics.listTopics(testCourse.getId()); Assert.assertNotNull("Topics could not be retrieved.", listTopics); diff --git a/classroom/snippets/src/test/java/TestUpdateTopic.java b/classroom/snippets/src/test/java/TestUpdateTopic.java index 5b1b9eec..3144ff2d 100644 --- a/classroom/snippets/src/test/java/TestUpdateTopic.java +++ b/classroom/snippets/src/test/java/TestUpdateTopic.java @@ -11,15 +11,19 @@ // 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.security.GeneralSecurityException; 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 { + public void testUpdateTopic() throws GeneralSecurityException, IOException { + setup(UpdateTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); System.out.println("New topic created: " + topic.getName()); Topic updatedTopic = UpdateTopic.updateTopic(testCourse.getId(), topic.getTopicId()); From 4c92d15ca7506c5b7a61a6ebacab35979590cc5a Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Mon, 6 Feb 2023 12:24:15 -0500 Subject: [PATCH 35/50] added link to CourseState info and updated GetTopic error handling --- classroom/snippets/src/main/java/CreateCourse.java | 3 ++- classroom/snippets/src/main/java/GetTopic.java | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index b54de6a0..73f49e1f 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -54,7 +54,8 @@ public static Course createCourse() throws GeneralSecurityException, IOException Course course = null; try { - // Adding a new course with description + // Adding a new course with description. Set CourseState to `ACTIVE`. Possible values of + // CourseState can be found here: https://developers.google.com/classroom/reference/rest/v1/courses#coursestate course = new Course() .setName("10th Grade Biology") .setSection("Period 2") diff --git a/classroom/snippets/src/main/java/GetTopic.java b/classroom/snippets/src/main/java/GetTopic.java index 3570636e..facf7ea5 100644 --- a/classroom/snippets/src/main/java/GetTopic.java +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -66,6 +66,10 @@ public static Topic getTopic(String courseId, String topicId) System.out.printf("Topic '%s' found.\n", topic.getName()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId); + } throw e; } catch (Exception e) { throw e; From 4342b9f1d0c05723264b6372b6734e8c0cc8963e Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 7 Feb 2023 12:33:19 -0500 Subject: [PATCH 36/50] updated auth for remaining classes --- .../snippets/src/main/java/GetCourse.java | 38 +++++++++--------- .../src/main/java/ListCourseAliases.java | 37 +++++++++--------- .../snippets/src/main/java/ListCourses.java | 38 +++++++++--------- .../src/main/java/ListStudentSubmissions.java | 29 +++++++------- .../src/main/java/ListSubmissions.java | 26 ++++++------- .../ModifyAttachmentsStudentSubmission.java | 37 ++++++++++-------- .../snippets/src/main/java/PatchCourse.java | 39 ++++++++++--------- .../src/main/java/PatchStudentSubmission.java | 36 +++++++++-------- .../main/java/ReturnStudentSubmission.java | 36 +++++++++-------- .../snippets/src/main/java/UpdateCourse.java | 38 +++++++++--------- .../snippets/src/test/java/TestGetCourse.java | 6 ++- .../src/test/java/TestListCourseAliases.java | 8 ++-- .../src/test/java/TestListCourses.java | 7 ++-- .../src/test/java/TestListSubmissions.java | 4 +- .../src/test/java/TestPatchCourse.java | 6 ++- .../src/test/java/TestUpdateCourse.java | 6 ++- 16 files changed, 209 insertions(+), 182 deletions(-) diff --git a/classroom/snippets/src/main/java/GetCourse.java b/classroom/snippets/src/main/java/GetCourse.java index 7955f1da..1efea231 100644 --- a/classroom/snippets/src/main/java/GetCourse.java +++ b/classroom/snippets/src/main/java/GetCourse.java @@ -15,43 +15,45 @@ // [START classroom_get_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Get Course API */ public class GetCourse { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Retrieve a single course's metadata. * * @param courseId - Id of the course to return. * @return a course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course getCourse(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); + public static Course getCourse(String courseId) throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Course course = null; try { diff --git a/classroom/snippets/src/main/java/ListCourseAliases.java b/classroom/snippets/src/main/java/ListCourseAliases.java index c3141cf1..3a6a93cc 100644 --- a/classroom/snippets/src/main/java/ListCourseAliases.java +++ b/classroom/snippets/src/main/java/ListCourseAliases.java @@ -15,47 +15,48 @@ // [START classroom_list_aliases_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.CourseAlias; import com.google.api.services.classroom.model.ListCourseAliasesResponse; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List Alias API */ public class ListCourseAliases { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Retrieve the aliases for a course. * * @param courseId - id of the course. * @return list of course aliases * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static List listCourseAliases(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); + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_list_aliases_code_snippet] diff --git a/classroom/snippets/src/main/java/ListCourses.java b/classroom/snippets/src/main/java/ListCourses.java index 5b7bd58f..3ba78a13 100644 --- a/classroom/snippets/src/main/java/ListCourses.java +++ b/classroom/snippets/src/main/java/ListCourses.java @@ -15,44 +15,44 @@ // [START classroom_list_courses] -import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.api.services.classroom.model.ListCoursesResponse; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; - import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List Course API */ public class ListCourses { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Retrieves all courses with metadata * * @return list of courses with its metadata * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static List listCourses() 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); + public static List listCourses() throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); String pageToken = null; List courses = new ArrayList<>(); diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index c5d8b29f..a0d6dc9e 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -14,24 +14,28 @@ // [START classroom_list_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List StudentSubmissions API. */ public class ListStudentSubmissions { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + /** * Retrieves a specific student's submissions for the specified course work. * @@ -40,21 +44,18 @@ public class ListStudentSubmissions { * @param userId - identifier of the student whose work to return. * @return - list of student submissions. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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); + public static List listStudentSubmissions(String courseId, String courseWorkId, String userId) + throws GeneralSecurityException, IOException { // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ListSubmissions.java b/classroom/snippets/src/main/java/ListSubmissions.java index d5356d70..84bf5fa6 100644 --- a/classroom/snippets/src/main/java/ListSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -14,24 +14,27 @@ // [START classroom_list_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List StudentSubmissions API. */ public class ListSubmissions { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Retrieves submissions for all students for the specified course work in a course. * @@ -39,21 +42,18 @@ public class ListSubmissions { * @param courseWorkId - identifier of the course work. * @return - list of student submissions. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ 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 - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = - GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials); + throws GeneralSecurityException, IOException { // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index 252f51f5..13d439dd 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -15,9 +15,9 @@ // [START classroom_modify_attachments_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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; @@ -26,14 +26,20 @@ 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.security.GeneralSecurityException; +import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; /* Class to demonstrate the use of Classroom ModifyAttachments StudentSubmissions API. */ public class ModifyAttachmentsStudentSubmission { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + + /** * Modify attachments on a student submission. * @@ -42,23 +48,20 @@ public class ModifyAttachmentsStudentSubmission { * @param id - identifier of the student submission. * @return - the modified student submission. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ 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); + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_modify_attachments_student_submissions_code_snippet] diff --git a/classroom/snippets/src/main/java/PatchCourse.java b/classroom/snippets/src/main/java/PatchCourse.java index e8c5d31b..afd85ba1 100644 --- a/classroom/snippets/src/main/java/PatchCourse.java +++ b/classroom/snippets/src/main/java/PatchCourse.java @@ -15,43 +15,46 @@ // [START classroom_patch_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Patch Course API */ public class PatchCourse { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Updates one or more fields in a course. * * @param courseId - Id of the course to update. * @return updated course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course patchCourse(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); + public static Course patchCourse(String courseId) throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); Course course = null; try { course = new Course() diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 1690ef8e..7931179b 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -15,21 +15,26 @@ // [START classroom_patch_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Patch StudentSubmissions API. */ public class PatchStudentSubmission { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Updates the draft grade and/or assigned grade of a student submission. * @@ -38,23 +43,20 @@ public class PatchStudentSubmission { * @param id - identifier of the student submission. * @return - the updated student submission. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ 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); + String id) throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_patch_student_submissions_code_snippet] diff --git a/classroom/snippets/src/main/java/ReturnStudentSubmission.java b/classroom/snippets/src/main/java/ReturnStudentSubmission.java index 6b40fc96..dcf8e7bd 100644 --- a/classroom/snippets/src/main/java/ReturnStudentSubmission.java +++ b/classroom/snippets/src/main/java/ReturnStudentSubmission.java @@ -15,20 +15,25 @@ // [START classroom_return_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Return StudentSubmissions API. */ public class ReturnStudentSubmission { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Return a student submission back to the student which updates the submission state to `RETURNED`. * @@ -36,23 +41,20 @@ public class ReturnStudentSubmission { * @param courseWorkId - identifier of the course work. * @param id - identifier of the student submission. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ 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); + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_return_student_submissions_code_snippet] diff --git a/classroom/snippets/src/main/java/UpdateCourse.java b/classroom/snippets/src/main/java/UpdateCourse.java index 9e0d63ed..431640c7 100644 --- a/classroom/snippets/src/main/java/UpdateCourse.java +++ b/classroom/snippets/src/main/java/UpdateCourse.java @@ -15,43 +15,45 @@ // [START classroom_update_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Update Course API */ public class UpdateCourse { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Updates a course's metadata. * * @param courseId - Id of the course to update. * @return updated course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course updateCourse(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); + public static Course updateCourse(String courseId) throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Course course = null; try { diff --git a/classroom/snippets/src/test/java/TestGetCourse.java b/classroom/snippets/src/test/java/TestGetCourse.java index c43059f7..ac95bda5 100644 --- a/classroom/snippets/src/test/java/TestGetCourse.java +++ b/classroom/snippets/src/test/java/TestGetCourse.java @@ -13,15 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Get Course classroom snippet public class TestGetCourse extends BaseTest { @Test - public void testGetCourse() throws IOException { + public void testGetCourse() throws GeneralSecurityException, IOException { + setup(GetCourse.SCOPES); Course course = GetCourse.getCourse(testCourse.getId()); Assert.assertNotNull("Course not returned.", course); Assert.assertEquals("Wrong course returned.", testCourse.getId(), course.getId()); diff --git a/classroom/snippets/src/test/java/TestListCourseAliases.java b/classroom/snippets/src/test/java/TestListCourseAliases.java index f18c1cce..3543f217 100644 --- a/classroom/snippets/src/test/java/TestListCourseAliases.java +++ b/classroom/snippets/src/test/java/TestListCourseAliases.java @@ -13,16 +13,18 @@ // limitations under the License. import com.google.api.services.classroom.model.CourseAlias; -import org.junit.Assert; -import org.junit.Test; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; +import org.junit.Assert; +import org.junit.Test; // Unit test class for List Course Aliases classroom snippet public class TestListCourseAliases extends BaseTest { @Test - public void testListCourseAliases() throws IOException { + public void testListCourseAliases() throws GeneralSecurityException, IOException { + setup(ListCourseAliases.SCOPES); List courseAliases = ListCourseAliases.listCourseAliases(testCourse.getId()); Assert.assertTrue("Incorrect number of course aliases returned.", courseAliases.size() == 1); } diff --git a/classroom/snippets/src/test/java/TestListCourses.java b/classroom/snippets/src/test/java/TestListCourses.java index fe254b76..7a4dd553 100644 --- a/classroom/snippets/src/test/java/TestListCourses.java +++ b/classroom/snippets/src/test/java/TestListCourses.java @@ -13,16 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; -import org.junit.Assert; -import org.junit.Test; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; +import org.junit.Assert; +import org.junit.Test; // Unit test class for List Course classroom snippet public class TestListCourses { @Test - public void testListCourses() throws IOException { + public void testListCourses() throws GeneralSecurityException, IOException { List courses = ListCourses.listCourses(); Assert.assertTrue("No courses returned.", courses.size() > 0); } diff --git a/classroom/snippets/src/test/java/TestListSubmissions.java b/classroom/snippets/src/test/java/TestListSubmissions.java index 148e9529..77d8c63e 100644 --- a/classroom/snippets/src/test/java/TestListSubmissions.java +++ b/classroom/snippets/src/test/java/TestListSubmissions.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.StudentSubmission; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,7 +23,8 @@ public class TestListSubmissions extends BaseTest { @Test - public void testListSubmissions() throws IOException { + public void testListSubmissions() throws GeneralSecurityException, IOException { + setup(ListSubmissions.SCOPES); List submissions = ListSubmissions.listSubmissions( testCourse.getId(), "-"); diff --git a/classroom/snippets/src/test/java/TestPatchCourse.java b/classroom/snippets/src/test/java/TestPatchCourse.java index 51e05a43..a7d0d92f 100644 --- a/classroom/snippets/src/test/java/TestPatchCourse.java +++ b/classroom/snippets/src/test/java/TestPatchCourse.java @@ -13,15 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Patch Course classroom snippet public class TestPatchCourse extends BaseTest { @Test - public void testPatchCourse() throws IOException { + public void testPatchCourse() throws GeneralSecurityException, IOException { + setup(PatchCourse.SCOPES); Course course = PatchCourse.patchCourse(testCourse.getId()); Assert.assertNotNull("Course not returned.", course); Assert.assertEquals("Wrong course returned.", testCourse.getId(), course.getId()); diff --git a/classroom/snippets/src/test/java/TestUpdateCourse.java b/classroom/snippets/src/test/java/TestUpdateCourse.java index f5e0080d..3c7cbda2 100644 --- a/classroom/snippets/src/test/java/TestUpdateCourse.java +++ b/classroom/snippets/src/test/java/TestUpdateCourse.java @@ -13,15 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Update Course classroom snippet public class TestUpdateCourse extends BaseTest { @Test - public void testUpdateCourse() throws IOException { + public void testUpdateCourse() throws GeneralSecurityException, IOException { + setup(UpdateCourse.SCOPES); Course course = UpdateCourse.updateCourse(testCourse.getId()); Assert.assertNotNull("Course not returned.", course); Assert.assertEquals("Wrong course returned.", testCourse.getId(), course.getId()); From a351fd5048a418186b04bd4aeb1504f8f03df8ec Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 7 Feb 2023 13:01:17 -0500 Subject: [PATCH 37/50] updated auth for CreateCourseWithAlias --- .../src/main/java/CreateCourseWithAlias.java | 38 ++++++++++--------- .../test/java/TestCreateCourseWithAlias.java | 4 +- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/classroom/snippets/src/main/java/CreateCourseWithAlias.java b/classroom/snippets/src/main/java/CreateCourseWithAlias.java index 77a6521b..c6b9ba0e 100644 --- a/classroom/snippets/src/main/java/CreateCourseWithAlias.java +++ b/classroom/snippets/src/main/java/CreateCourseWithAlias.java @@ -14,42 +14,46 @@ // [START classroom_create_course_with_alias_class] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 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.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to create a course with an alias. */ public class CreateCourseWithAlias { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * 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. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - 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. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Course createCourseWithAlias() throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_create_course_with_alias_code_snippet] diff --git a/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java index 5152b984..13b704e7 100644 --- a/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java +++ b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java @@ -15,6 +15,7 @@ import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -23,7 +24,8 @@ public class TestCreateCourseWithAlias extends BaseTest { @Test - public void testCreateCourseWithAlias() throws IOException { + public void testCreateCourseWithAlias() throws GeneralSecurityException, IOException { + setup(CreateCourseWithAlias.SCOPES); Course course = CreateCourseWithAlias.createCourseWithAlias(); List courseAliases = service.courses().aliases() .list(course.getId() From f31c151a95c484a48ad73115ec401091e1ba4f14 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 7 Feb 2023 16:02:17 -0500 Subject: [PATCH 38/50] CreateInvitation code snippet --- .../src/main/java/CreateInvitation.java | 91 +++++++++++++++++++ .../src/test/java/TestCreateInvitation.java | 34 +++++++ 2 files changed, 125 insertions(+) create mode 100644 classroom/snippets/src/main/java/CreateInvitation.java create mode 100644 classroom/snippets/src/test/java/TestCreateInvitation.java diff --git a/classroom/snippets/src/main/java/CreateInvitation.java b/classroom/snippets/src/main/java/CreateInvitation.java new file mode 100644 index 00000000..bf04633d --- /dev/null +++ b/classroom/snippets/src/main/java/CreateInvitation.java @@ -0,0 +1,91 @@ +// Copyright 2023 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_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +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.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Create Invitation API */ +public class CreateInvitation { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Create an invitation to allow a user to join a course. + * + * @param courseId - the course to invite the user to. + * @param userId - the user to be invited to the course. + * @return the created invitation. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static Invitation createInvitation(String courseId, String userId) + throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_create_invitation_code_snippet] + + Invitation invitation = null; + try { + /* Set the role the user is invited to have in the course. Possible values of CourseRole can be + found here: https://developers.google.com/classroom/reference/rest/v1/invitations#courserole.*/ + Invitation content = new Invitation() + .setCourseId(courseId) + .setUserId(userId) + .setRole("TEACHER"); + + invitation = service.invitations().create(content).execute(); + + System.out.printf("User (%s) has been invited to course (%s)", invitation.getUserId(), + invitation.getCourseId()); + } catch (GoogleJsonResponseException e) { + //TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The course or user does not exist.\n"); + } + throw e; + } catch (Exception e) { + throw e; + } + return invitation; + + // [END classroom_create_invitation_code_snippet] + } + +} +// [END classroom_create_invitation] \ No newline at end of file diff --git a/classroom/snippets/src/test/java/TestCreateInvitation.java b/classroom/snippets/src/test/java/TestCreateInvitation.java new file mode 100644 index 00000000..c6daeebc --- /dev/null +++ b/classroom/snippets/src/test/java/TestCreateInvitation.java @@ -0,0 +1,34 @@ +// Copyright 2023 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.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import org.junit.Assert; +import org.junit.Test; + +public class TestCreateInvitation extends BaseTest { + + private String userId = "insert_user_id"; + + @Test + public void testCreateInvitation() throws GeneralSecurityException, IOException { + setup(CreateInvitation.SCOPES); + Invitation invitation = CreateInvitation.createInvitation(testCourse.getId(), userId); + Assert.assertNotNull("Invitation not created", invitation); + Assert.assertEquals(invitation.getCourseId(), testCourse.getId()); + Assert.assertEquals(invitation.getUserId(), userId); + } + +} From fd73ed368b3fcde78482933842e906fb7a96ff36 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 8 Feb 2023 16:21:09 -0500 Subject: [PATCH 39/50] accept, create, delete invitation classes --- .../src/main/java/AcceptInvitation.java | 72 ++++++++++++++++++ .../src/main/java/CreateInvitation.java | 2 +- .../src/main/java/DeleteInvitation.java | 72 ++++++++++++++++++ .../snippets/src/main/java/GetInvitation.java | 75 +++++++++++++++++++ .../src/main/java/ListInvitations.java | 2 + .../src/test/java/TestAcceptInvitation.java | 31 ++++++++ .../src/test/java/TestDeleteInvitation.java | 34 +++++++++ .../src/test/java/TestListInvitations.java | 2 + 8 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 classroom/snippets/src/main/java/AcceptInvitation.java create mode 100644 classroom/snippets/src/main/java/DeleteInvitation.java create mode 100644 classroom/snippets/src/main/java/GetInvitation.java create mode 100644 classroom/snippets/src/main/java/ListInvitations.java create mode 100644 classroom/snippets/src/test/java/TestAcceptInvitation.java create mode 100644 classroom/snippets/src/test/java/TestDeleteInvitation.java create mode 100644 classroom/snippets/src/test/java/TestListInvitations.java diff --git a/classroom/snippets/src/main/java/AcceptInvitation.java b/classroom/snippets/src/main/java/AcceptInvitation.java new file mode 100644 index 00000000..7dd141f6 --- /dev/null +++ b/classroom/snippets/src/main/java/AcceptInvitation.java @@ -0,0 +1,72 @@ +// Copyright 2023 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_accept_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +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 java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Accept Invitation API. */ +public class AcceptInvitation { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Accepts an invitation to a course by adding the user as the role specified in the invitation. + * + * @param id - the identifier of the invitation to accept. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static void acceptInvitation(String id) throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_accept_invitation_code_snippet] + try { + service.invitations().accept(id).execute(); + System.out.printf("Invitation (%s) was accepted.\n", id); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The invitation id (%s) does not exist.\n", id); + } + throw e; + } catch (Exception e) { + throw e; + } + // [END classroom_accept_invitation_code_snippet] + } +} +// [END classroom_accept_invitation] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/CreateInvitation.java b/classroom/snippets/src/main/java/CreateInvitation.java index bf04633d..7e06b468 100644 --- a/classroom/snippets/src/main/java/CreateInvitation.java +++ b/classroom/snippets/src/main/java/CreateInvitation.java @@ -70,7 +70,7 @@ public static Invitation createInvitation(String courseId, String userId) invitation = service.invitations().create(content).execute(); - System.out.printf("User (%s) has been invited to course (%s)", invitation.getUserId(), + System.out.printf("User (%s) has been invited to course (%s).\n", invitation.getUserId(), invitation.getCourseId()); } catch (GoogleJsonResponseException e) { //TODO (developer) - handle error appropriately diff --git a/classroom/snippets/src/main/java/DeleteInvitation.java b/classroom/snippets/src/main/java/DeleteInvitation.java new file mode 100644 index 00000000..d11fee03 --- /dev/null +++ b/classroom/snippets/src/main/java/DeleteInvitation.java @@ -0,0 +1,72 @@ +// Copyright 2023 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_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +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 java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Delete Invitation API. */ +public class DeleteInvitation { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Deletes an invitation. + * + * @param id - the identifier of the invitation to delete. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static void deleteInvitation(String id) throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_delete_invitation_code_snippet] + try { + service.invitations().delete(id).execute(); + System.out.printf("Invitation (%s) was deleted.\n", id); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The invitation id (%s) does not exist.\n", id); + } + throw e; + } catch (Exception e) { + throw e; + } + // [END classroom_delete_invitation_code_snippet] + } +} +// [END classroom_delete_invitation] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/GetInvitation.java b/classroom/snippets/src/main/java/GetInvitation.java new file mode 100644 index 00000000..1b921b7f --- /dev/null +++ b/classroom/snippets/src/main/java/GetInvitation.java @@ -0,0 +1,75 @@ +// Copyright 2023 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_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +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.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Get Invitation API */ +public class GetInvitation { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Retrieves an invitation. + * + * @param id - the identifier of the invitation to retrieve. + * @return the specified invitation. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static Invitation getInvitation(String id) throws GeneralSecurityException, IOException { + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_get_invitation_code_snippet] + Invitation invitation = null; + try { + invitation = service.invitations().get(id).execute(); + System.out.printf("Invitation (%s) for user (%s) in course (%s) retrieved.\n", + invitation.getId(), invitation.getUserId(), invitation.getCourseId()); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The invitation id (%s) does not exist.\n", id); + } + throw e; + } catch (Exception e) { + throw e; + } + return invitation; + // [END classroom_get_invitation_code_snippet] + } +} +// [END classroom_get_invitation] \ No newline at end of file diff --git a/classroom/snippets/src/main/java/ListInvitations.java b/classroom/snippets/src/main/java/ListInvitations.java new file mode 100644 index 00000000..64ea3585 --- /dev/null +++ b/classroom/snippets/src/main/java/ListInvitations.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class ListInvitations { +} diff --git a/classroom/snippets/src/test/java/TestAcceptInvitation.java b/classroom/snippets/src/test/java/TestAcceptInvitation.java new file mode 100644 index 00000000..427c1b98 --- /dev/null +++ b/classroom/snippets/src/test/java/TestAcceptInvitation.java @@ -0,0 +1,31 @@ +// Copyright 2023 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 java.io.IOException; +import java.security.GeneralSecurityException; +import org.junit.Assert; +import org.junit.Test; + +public class TestAcceptInvitation { + + private String invitationId = "insert_invitation_id"; + + @Test + public void testAcceptInvitation() throws GeneralSecurityException, IOException { + AcceptInvitation.acceptInvitation(invitationId); + Assert.assertThrows(GoogleJsonResponseException.class, + () -> GetInvitation.getInvitation(invitationId)); + } +} diff --git a/classroom/snippets/src/test/java/TestDeleteInvitation.java b/classroom/snippets/src/test/java/TestDeleteInvitation.java new file mode 100644 index 00000000..9d53a751 --- /dev/null +++ b/classroom/snippets/src/test/java/TestDeleteInvitation.java @@ -0,0 +1,34 @@ +// Copyright 2023 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.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import org.junit.Assert; +import org.junit.Test; + +public class TestDeleteInvitation extends BaseTest { + + private String userId = "insert_user_id"; + + @Test + public void testDeleteInvitation() throws GeneralSecurityException, IOException { + setup(DeleteInvitation.SCOPES); + Invitation invitation = CreateInvitation.createInvitation(testCourse.getId(), userId); + DeleteInvitation.deleteInvitation(invitation.getId()); + Assert.assertThrows(GoogleJsonResponseException.class, + () -> GetInvitation.getInvitation(invitation.getId())); + } +} diff --git a/classroom/snippets/src/test/java/TestListInvitations.java b/classroom/snippets/src/test/java/TestListInvitations.java new file mode 100644 index 00000000..3e41c4a9 --- /dev/null +++ b/classroom/snippets/src/test/java/TestListInvitations.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class TestListInvitations { +} From c3965e2d31eccd21b9d32be5406660e68a978435 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 8 Feb 2023 16:23:32 -0500 Subject: [PATCH 40/50] list invitations class --- .../src/main/java/ListInvitations.java | 102 +++++++++++++++++- .../src/test/java/TestListInvitations.java | 33 +++++- 2 files changed, 133 insertions(+), 2 deletions(-) diff --git a/classroom/snippets/src/main/java/ListInvitations.java b/classroom/snippets/src/main/java/ListInvitations.java index 64ea3585..fec0182e 100644 --- a/classroom/snippets/src/main/java/ListInvitations.java +++ b/classroom/snippets/src/main/java/ListInvitations.java @@ -1,2 +1,102 @@ -package PACKAGE_NAME;public class ListInvitations { +// Copyright 2023 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_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +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.Invitation; +import com.google.api.services.classroom.model.ListInvitationsResponse; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/* Class to demonstrate the use of Classroom List Invitation API. */ +public class ListInvitations { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = new ArrayList<>( + Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Returns a list of invitations that the requesting user is permitted to view, + * restricted to those that match the specified courseId and/or userId. + * + * @param courseId - a specified course. + * @param userId - a specified userId. + * @return list of invitations for the specified courseId and/or userId. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static List listInvitations(String courseId, String userId) + throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_list_invitation_code_snippet] + List invitations = new ArrayList<>(); + String pageToken = null; + try { + do { + ListInvitationsResponse response = service.invitations().list() + .setCourseId(courseId) + .setUserId(userId) + .setPageToken(pageToken) + .execute(); + + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ + if (response.getInvitations() != null) { + invitations.addAll(response.getInvitations()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (invitations.isEmpty()) { + System.out.println("No invitations found."); + } else { + for (Invitation invitation : invitations) { + System.out.printf("Invitation id (%s).\n", invitation.getId()); + } + } + } catch (GoogleJsonResponseException e) { + // TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The courseId or userId does not exist.\n"); + } + throw e; + } catch (Exception e) { + throw e; + } + return invitations; + // [END classroom_list_invitation_code_snippet] + } } +// [END classroom_list_invitation] \ No newline at end of file diff --git a/classroom/snippets/src/test/java/TestListInvitations.java b/classroom/snippets/src/test/java/TestListInvitations.java index 3e41c4a9..c601427f 100644 --- a/classroom/snippets/src/test/java/TestListInvitations.java +++ b/classroom/snippets/src/test/java/TestListInvitations.java @@ -1,2 +1,33 @@ -package PACKAGE_NAME;public class TestListInvitations { +// Copyright 2023 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.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +public class TestListInvitations extends BaseTest { + + private String userId = "insert_user_id"; + + @Test + public void testListInvitations() throws GeneralSecurityException, IOException { + setup(ListInvitations.SCOPES); + CreateInvitation.createInvitation(testCourse.getId(), userId); + List invitationList = ListInvitations.listInvitations(testCourse.getId(), userId); + Assert.assertTrue("No invitations returned.", invitationList.size() > 0); + } } From 6bf7c4affe8b3303adaa37f75c6a131617d656bf Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 8 Feb 2023 16:43:54 -0500 Subject: [PATCH 41/50] listAssignedGrades method in ListStudentSubmissions class --- .../src/main/java/ListStudentSubmissions.java | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index a0d6dc9e..a5389823 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -105,9 +105,74 @@ public static List listStudentSubmissions(String courseId, St throw e; } return studentSubmissions; - // [END classroom_list_student_submissions_code_snippet] + } + + /** + * Lists all assigned grades for a particular coursework item. + * + * @param courseId - identifier of the course. + * @param courseWorkId - identifier of the course work. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static void listAssignedGrades(String courseId, String courseWorkId) + throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + List studentSubmissions = new ArrayList<>(); + String pageToken = null; + + try { + do { + // [START classroom_list_assigned_grades_code_snippet] + ListStudentSubmissionsResponse response = + service + .courses() + .courseWork() + .studentSubmissions() + .list(courseId, courseWorkId) + .setPageToken(pageToken) + .execute(); + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (studentSubmissions.isEmpty()) { + System.out.println("No student submissions found."); + } else { + for (StudentSubmission submission : studentSubmissions) { + System.out.printf("User ID %s, Assigned grade: %s\n", submission.getUserId(), + submission.getAssignedGrade()); + } + } + // [END classroom_list_assigned_grades_code_snippet] + } 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) does not exist.\n", + courseId, courseWorkId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } } } // [END classroom_list_student_submissions_class] From 439948608b0ab6a08f254f5510735b81f4446e0f Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Wed, 8 Feb 2023 16:50:37 -0500 Subject: [PATCH 42/50] deleting ListInvitations classes until bug is fixed --- .../src/main/java/ListInvitations.java | 102 ------------------ .../src/test/java/TestListInvitations.java | 33 ------ 2 files changed, 135 deletions(-) delete mode 100644 classroom/snippets/src/main/java/ListInvitations.java delete mode 100644 classroom/snippets/src/test/java/TestListInvitations.java diff --git a/classroom/snippets/src/main/java/ListInvitations.java b/classroom/snippets/src/main/java/ListInvitations.java deleted file mode 100644 index fec0182e..00000000 --- a/classroom/snippets/src/main/java/ListInvitations.java +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright 2023 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_invitation] - -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.googleapis.json.GoogleJsonError; -import com.google.api.client.googleapis.json.GoogleJsonResponseException; -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.Invitation; -import com.google.api.services.classroom.model.ListInvitationsResponse; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/* Class to demonstrate the use of Classroom List Invitation API. */ -public class ListInvitations { - - /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); - - /** - * Returns a list of invitations that the requesting user is permitted to view, - * restricted to those that match the specified courseId and/or userId. - * - * @param courseId - a specified course. - * @param userId - a specified userId. - * @return list of invitations for the specified courseId and/or userId. - * @throws IOException - if credentials file not found. - * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. - */ - public static List listInvitations(String courseId, String userId) - throws GeneralSecurityException, IOException { - - // Create the classroom API client. - final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Classroom service = - new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) - .setApplicationName("Classroom samples") - .build(); - - // [START classroom_list_invitation_code_snippet] - List invitations = new ArrayList<>(); - String pageToken = null; - try { - do { - ListInvitationsResponse response = service.invitations().list() - .setCourseId(courseId) - .setUserId(userId) - .setPageToken(pageToken) - .execute(); - - /* Ensure that the response is not null before retrieving data from it to avoid errors. */ - if (response.getInvitations() != null) { - invitations.addAll(response.getInvitations()); - pageToken = response.getNextPageToken(); - } - } while (pageToken != null); - - if (invitations.isEmpty()) { - System.out.println("No invitations found."); - } else { - for (Invitation invitation : invitations) { - System.out.printf("Invitation id (%s).\n", invitation.getId()); - } - } - } catch (GoogleJsonResponseException e) { - // TODO (developer) - handle error appropriately - GoogleJsonError error = e.getDetails(); - if (error.getCode() == 404) { - System.out.printf("The courseId or userId does not exist.\n"); - } - throw e; - } catch (Exception e) { - throw e; - } - return invitations; - // [END classroom_list_invitation_code_snippet] - } -} -// [END classroom_list_invitation] \ No newline at end of file diff --git a/classroom/snippets/src/test/java/TestListInvitations.java b/classroom/snippets/src/test/java/TestListInvitations.java deleted file mode 100644 index c601427f..00000000 --- a/classroom/snippets/src/test/java/TestListInvitations.java +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2023 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.Invitation; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.List; -import org.junit.Assert; -import org.junit.Test; - -public class TestListInvitations extends BaseTest { - - private String userId = "insert_user_id"; - - @Test - public void testListInvitations() throws GeneralSecurityException, IOException { - setup(ListInvitations.SCOPES); - CreateInvitation.createInvitation(testCourse.getId(), userId); - List invitationList = ListInvitations.listInvitations(testCourse.getId(), userId); - Assert.assertTrue("No invitations returned.", invitationList.size() > 0); - } -} From 17325241b892262352614428cc408d747c68f4ce Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 9 Feb 2023 11:25:20 -0500 Subject: [PATCH 43/50] team review updates --- classroom/snippets/src/main/java/AcceptInvitation.java | 2 +- classroom/snippets/src/main/java/CreateInvitation.java | 2 +- classroom/snippets/src/main/java/GetInvitation.java | 2 +- classroom/snippets/src/test/java/TestAcceptInvitation.java | 6 ++++++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/classroom/snippets/src/main/java/AcceptInvitation.java b/classroom/snippets/src/main/java/AcceptInvitation.java index 7dd141f6..241a80c9 100644 --- a/classroom/snippets/src/main/java/AcceptInvitation.java +++ b/classroom/snippets/src/main/java/AcceptInvitation.java @@ -35,7 +35,7 @@ public class AcceptInvitation { Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** - * Accepts an invitation to a course by adding the user as the role specified in the invitation. + * Accepts an invitation to a course. * * @param id - the identifier of the invitation to accept. * @throws IOException - if credentials file not found. diff --git a/classroom/snippets/src/main/java/CreateInvitation.java b/classroom/snippets/src/main/java/CreateInvitation.java index 7e06b468..b6a01588 100644 --- a/classroom/snippets/src/main/java/CreateInvitation.java +++ b/classroom/snippets/src/main/java/CreateInvitation.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.Arrays; -/* Class to demonstrate the use of Classroom Create Invitation API */ +/* Class to demonstrate the use of Classroom Create Invitation API. */ public class CreateInvitation { /* Scopes required by this API call. If modifying these scopes, delete your previously saved diff --git a/classroom/snippets/src/main/java/GetInvitation.java b/classroom/snippets/src/main/java/GetInvitation.java index 1b921b7f..b689d851 100644 --- a/classroom/snippets/src/main/java/GetInvitation.java +++ b/classroom/snippets/src/main/java/GetInvitation.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.Arrays; -/* Class to demonstrate the use of Classroom Get Invitation API */ +/* Class to demonstrate the use of Classroom Get Invitation API. */ public class GetInvitation { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ diff --git a/classroom/snippets/src/test/java/TestAcceptInvitation.java b/classroom/snippets/src/test/java/TestAcceptInvitation.java index 427c1b98..d34c2d43 100644 --- a/classroom/snippets/src/test/java/TestAcceptInvitation.java +++ b/classroom/snippets/src/test/java/TestAcceptInvitation.java @@ -28,4 +28,10 @@ public void testAcceptInvitation() throws GeneralSecurityException, IOException Assert.assertThrows(GoogleJsonResponseException.class, () -> GetInvitation.getInvitation(invitationId)); } + + @Test + public void testAcceptInvitationWithInvalidId() { + Assert.assertThrows(GoogleJsonResponseException.class, + () -> AcceptInvitation.acceptInvitation("invalid-id")); + } } From a1503fea556464576bdceeeb5973a7a867d6d917 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 9 Feb 2023 12:44:29 -0500 Subject: [PATCH 44/50] google java format for tests --- classroom/snippets/src/test/java/BaseTest.java | 6 ++---- .../snippets/src/test/java/TestAddAliasToCourse.java | 10 ++++------ classroom/snippets/src/test/java/TestAddStudent.java | 10 +++++----- classroom/snippets/src/test/java/TestAddTeacher.java | 6 +++--- .../snippets/src/test/java/TestBatchAddStudents.java | 5 ++--- classroom/snippets/src/test/java/TestCreateCourse.java | 2 +- .../src/test/java/TestCreateCourseWithAlias.java | 8 +++----- classroom/snippets/src/test/java/TestDeleteTopic.java | 3 ++- .../snippets/src/test/java/TestListCourseAliases.java | 2 +- .../snippets/src/test/java/TestListSubmissions.java | 4 +--- 10 files changed, 24 insertions(+), 32 deletions(-) diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index cdad6755..d004570e 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -21,10 +21,7 @@ import com.google.api.services.classroom.model.CourseAlias; import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import org.junit.After; -import org.junit.Before; import java.io.IOException; import java.util.UUID; @@ -40,7 +37,8 @@ public class BaseTest { * @return an authorized Classroom client service * @throws IOException - if credentials file not found. */ - protected Classroom buildService(ArrayList SCOPES) throws GeneralSecurityException, IOException { + protected Classroom buildService(ArrayList SCOPES) + throws GeneralSecurityException, IOException { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ SCOPES.add(ClassroomScopes.CLASSROOM_COURSES); diff --git a/classroom/snippets/src/test/java/TestAddAliasToCourse.java b/classroom/snippets/src/test/java/TestAddAliasToCourse.java index 8bbfb289..4eb659eb 100644 --- a/classroom/snippets/src/test/java/TestAddAliasToCourse.java +++ b/classroom/snippets/src/test/java/TestAddAliasToCourse.java @@ -24,14 +24,12 @@ public class TestAddAliasToCourse extends BaseTest { @Test public void testAddCourseAlias() throws GeneralSecurityException, IOException { - // Include the scopes required to run the code example for testing purposes. + // Include the scopes required to run the code example for testing purposes. setup(AddAliasToCourse.SCOPES); CourseAlias courseAlias = AddAliasToCourse.addAliasToCourse(testCourse.getId()); - List courseAliases = service.courses().aliases() - .list(testCourse.getId() - ).execute() - .getAliases(); + 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/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index c1cc6e6f..980ef941 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -27,11 +27,11 @@ public class TestAddStudent extends BaseTest { public void testAddStudent() throws GeneralSecurityException, IOException { // Include the scopes required to run the code example for testing purposes. setup(AddStudent.SCOPES); - Student student = AddStudent.addStudent(testCourse.getId(), testCourse.getEnrollmentCode(), - this.studentId); + Student student = + AddStudent.addStudent(testCourse.getId(), testCourse.getEnrollmentCode(), this.studentId); Assert.assertNotNull("Student not returned.", student); Assert.assertNotNull("Course not returned.", student.getCourseId()); - Assert.assertEquals("Student added to wrong course.", testCourse.getId(), - student.getCourseId()); + Assert.assertEquals( + "Student added to wrong course.", testCourse.getId(), student.getCourseId()); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestAddTeacher.java b/classroom/snippets/src/test/java/TestAddTeacher.java index efa1cafc..09c86a29 100644 --- a/classroom/snippets/src/test/java/TestAddTeacher.java +++ b/classroom/snippets/src/test/java/TestAddTeacher.java @@ -29,7 +29,7 @@ public void testAddTeacher() throws GeneralSecurityException, IOException { setup(AddTeacher.SCOPES); Teacher teacher = AddTeacher.addTeacher(testCourse.getId(), this.teacherEmail); Assert.assertNotNull("Teacher not returned.", teacher); - Assert.assertEquals("Teacher added to wrong course.", testCourse.getId(), - teacher.getCourseId()); + Assert.assertEquals( + "Teacher added to wrong course.", testCourse.getId(), teacher.getCourseId()); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestBatchAddStudents.java b/classroom/snippets/src/test/java/TestBatchAddStudents.java index cfa1f4c7..b185f28b 100644 --- a/classroom/snippets/src/test/java/TestBatchAddStudents.java +++ b/classroom/snippets/src/test/java/TestBatchAddStudents.java @@ -24,8 +24,7 @@ public class TestBatchAddStudents extends BaseTest { @Test public void testBatchAddStudents() throws GeneralSecurityException, IOException { setup(BatchAddStudents.SCOPES); - List studentEmails = Arrays.asList("insert_student_1_email", - "insert_student_2_email"); + List studentEmails = Arrays.asList("insert_student_1_email", "insert_student_2_email"); BatchAddStudents.batchAddStudents(testCourse.getId(), studentEmails); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestCreateCourse.java b/classroom/snippets/src/test/java/TestCreateCourse.java index 86200500..5606b101 100644 --- a/classroom/snippets/src/test/java/TestCreateCourse.java +++ b/classroom/snippets/src/test/java/TestCreateCourse.java @@ -23,7 +23,7 @@ public class TestCreateCourse extends BaseTest { @Test public void testCreateCourse() throws GeneralSecurityException, IOException { - // Include the scopes required to run the code example for testing purposes. + // Include the scopes required to run the code example for testing purposes. setup(CreateCourse.SCOPES); Course course = CreateCourse.createCourse(); Assert.assertNotNull("Course not returned.", course); diff --git a/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java index 13b704e7..2d906fbf 100644 --- a/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java +++ b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java @@ -27,12 +27,10 @@ public class TestCreateCourseWithAlias extends BaseTest { public void testCreateCourseWithAlias() throws GeneralSecurityException, IOException { setup(CreateCourseWithAlias.SCOPES); Course course = CreateCourseWithAlias.createCourseWithAlias(); - List courseAliases = service.courses().aliases() - .list(course.getId() - ).execute() - .getAliases(); + 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 +} diff --git a/classroom/snippets/src/test/java/TestDeleteTopic.java b/classroom/snippets/src/test/java/TestDeleteTopic.java index a77a2b91..bffff20d 100644 --- a/classroom/snippets/src/test/java/TestDeleteTopic.java +++ b/classroom/snippets/src/test/java/TestDeleteTopic.java @@ -26,7 +26,8 @@ public void testDeleteTopic() throws GeneralSecurityException, IOException { setup(DeleteTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); DeleteTopic.deleteTopic(testCourse.getId(), topic.getTopicId()); - Assert.assertThrows(GoogleJsonResponseException.class, + Assert.assertThrows( + GoogleJsonResponseException.class, () -> GetTopic.getTopic(testCourse.getId(), topic.getTopicId())); } } diff --git a/classroom/snippets/src/test/java/TestListCourseAliases.java b/classroom/snippets/src/test/java/TestListCourseAliases.java index 3543f217..d3e357b5 100644 --- a/classroom/snippets/src/test/java/TestListCourseAliases.java +++ b/classroom/snippets/src/test/java/TestListCourseAliases.java @@ -28,4 +28,4 @@ public void testListCourseAliases() throws GeneralSecurityException, IOException List courseAliases = ListCourseAliases.listCourseAliases(testCourse.getId()); Assert.assertTrue("Incorrect number of course aliases returned.", courseAliases.size() == 1); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestListSubmissions.java b/classroom/snippets/src/test/java/TestListSubmissions.java index 77d8c63e..1649e926 100644 --- a/classroom/snippets/src/test/java/TestListSubmissions.java +++ b/classroom/snippets/src/test/java/TestListSubmissions.java @@ -25,9 +25,7 @@ public class TestListSubmissions extends BaseTest { @Test public void testListSubmissions() throws GeneralSecurityException, IOException { setup(ListSubmissions.SCOPES); - List submissions = ListSubmissions.listSubmissions( - testCourse.getId(), - "-"); + List submissions = ListSubmissions.listSubmissions(testCourse.getId(), "-"); Assert.assertNotNull("No submissions returned.", submissions); } } From 8ae450a1a1880d6a2f1359621586aec024db2288 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 9 Feb 2023 12:57:06 -0500 Subject: [PATCH 45/50] google-java-format on classes --- .../src/main/java/AddAliasToCourse.java | 17 +++--- .../snippets/src/main/java/AddStudent.java | 27 +++++---- .../snippets/src/main/java/AddTeacher.java | 17 +++--- .../src/main/java/BatchAddStudents.java | 35 ++++++------ .../snippets/src/main/java/CreateCourse.java | 38 +++++++------ .../src/main/java/CreateCourseWithAlias.java | 30 +++++----- .../src/main/java/CreateCourseWork.java | 43 +++++++------- .../snippets/src/main/java/CreateTopic.java | 14 ++--- .../snippets/src/main/java/DeleteTopic.java | 1 - .../snippets/src/main/java/GetCourse.java | 12 ++-- .../snippets/src/main/java/GetTopic.java | 12 ++-- .../src/main/java/ListCourseAliases.java | 24 ++++---- .../snippets/src/main/java/ListCourses.java | 18 +++--- .../src/main/java/ListStudentSubmissions.java | 12 ++-- .../src/main/java/ListSubmissions.java | 9 +-- .../snippets/src/main/java/ListTopics.java | 10 ++-- .../ModifyAttachmentsStudentSubmission.java | 46 ++++++++------- .../snippets/src/main/java/PatchCourse.java | 20 +++---- .../src/main/java/PatchStudentSubmission.java | 56 +++++++++++-------- .../main/java/ReturnStudentSubmission.java | 27 +++++---- .../snippets/src/main/java/UpdateCourse.java | 15 +++-- .../snippets/src/main/java/UpdateTopic.java | 22 +++++--- .../snippets/src/test/java/BaseTest.java | 5 +- .../src/test/java/TestAddStudent.java | 2 +- .../src/test/java/TestAddTeacher.java | 2 +- .../src/test/java/TestBatchAddStudents.java | 4 +- .../src/test/java/TestCreateCourse.java | 2 +- 27 files changed, 276 insertions(+), 244 deletions(-) diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index 8187e44f..720dccec 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -31,7 +31,8 @@ public class AddAliasToCourse { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Add an alias on an existing course. @@ -48,9 +49,9 @@ public static CourseAlias addAliasToCourse(String courseId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -58,16 +59,14 @@ public static CourseAlias addAliasToCourse(String courseId) /* 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 content = new CourseAlias().setAlias("p:biology_10"); CourseAlias courseAlias = null; try { - courseAlias = service.courses().aliases().create(courseId, content) - .execute(); + 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 + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 409) { System.out.printf("The course alias already exists: %s.\n", content); diff --git a/classroom/snippets/src/main/java/AddStudent.java b/classroom/snippets/src/main/java/AddStudent.java index e87ac1ff..6331f3ed 100644 --- a/classroom/snippets/src/main/java/AddStudent.java +++ b/classroom/snippets/src/main/java/AddStudent.java @@ -31,9 +31,9 @@ public class AddStudent { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Add a student in a specified course. @@ -51,20 +51,25 @@ public static Student addStudent(String courseId, String enrollmentCode, String final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); Student student = new Student().setUserId(studentId); try { // Enrolling a student to a specified course - student = service.courses().students().create(courseId, student) - .setEnrollmentCode(enrollmentCode) - .execute(); + student = + service + .courses() + .students() + .create(courseId, student) + .setEnrollmentCode(enrollmentCode) + .execute(); // Prints the course id with the Student name - System.out.printf("User '%s' was enrolled as a student in the course with ID '%s'.\n", + System.out.printf( + "User '%s' was enrolled as a student in the course with ID '%s'.\n", student.getProfile().getName().getFullName(), courseId); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately @@ -80,4 +85,4 @@ public static Student addStudent(String courseId, String enrollmentCode, String return student; } } -// [END classroom_add_student] \ No newline at end of file +// [END classroom_add_student] diff --git a/classroom/snippets/src/main/java/AddTeacher.java b/classroom/snippets/src/main/java/AddTeacher.java index 7f07a661..4c843721 100644 --- a/classroom/snippets/src/main/java/AddTeacher.java +++ b/classroom/snippets/src/main/java/AddTeacher.java @@ -31,9 +31,9 @@ public class AddTeacher { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Add teacher to a specific course. @@ -51,9 +51,9 @@ public static Teacher addTeacher(String courseId, String teacherEmail) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -62,7 +62,8 @@ public static Teacher addTeacher(String courseId, String teacherEmail) // Add a teacher to a specified course teacher = service.courses().teachers().create(courseId, teacher).execute(); // Prints the course id with the teacher name - System.out.printf("User '%s' was added as a teacher to the course with ID '%s'.\n", + System.out.printf( + "User '%s' was added as a teacher to the course with ID '%s'.\n", teacher.getProfile().getName().getFullName(), courseId); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately @@ -78,4 +79,4 @@ public static Teacher addTeacher(String courseId, String teacherEmail) return teacher; } } -// [END classroom_add_teacher] \ No newline at end of file +// [END classroom_add_teacher] diff --git a/classroom/snippets/src/main/java/BatchAddStudents.java b/classroom/snippets/src/main/java/BatchAddStudents.java index 9e2cf6e0..d4a54c2e 100644 --- a/classroom/snippets/src/main/java/BatchAddStudents.java +++ b/classroom/snippets/src/main/java/BatchAddStudents.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_batch_add_students] import com.google.api.client.googleapis.batch.BatchRequest; @@ -36,13 +35,13 @@ public class BatchAddStudents { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Add multiple students in a specified course. * - * @param courseId - Id of the course to add students. + * @param courseId - Id of the course to add students. * @param studentEmails - Email address of the students. * @throws IOException - if credentials file not found. * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. @@ -54,23 +53,25 @@ public static void batchAddStudents(String courseId, List studentEmails) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); BatchRequest batch = service.batch(); - JsonBatchCallback callback = new JsonBatchCallback<>() { - public void onSuccess(Student student, HttpHeaders responseHeaders) { - System.out.printf("User '%s' was added as a student to the course.\n", - student.getProfile().getName().getFullName()); - } + JsonBatchCallback callback = + new JsonBatchCallback<>() { + public void onSuccess(Student student, HttpHeaders responseHeaders) { + System.out.printf( + "User '%s' was added as a student to the course.\n", + student.getProfile().getName().getFullName()); + } - public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) { - System.out.printf("Error adding student to the course: %s\n", error.getMessage()); - } - }; + public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) { + System.out.printf("Error adding student to the course: %s\n", error.getMessage()); + } + }; for (String studentEmail : studentEmails) { Student student = new Student().setUserId(studentEmail); service.courses().students().create(courseId, student).queue(batch, callback); @@ -78,4 +79,4 @@ public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) { batch.execute(); } } -// [END classroom_batch_add_students] \ No newline at end of file +// [END classroom_batch_add_students] diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index 73f49e1f..95f7353c 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -30,8 +30,9 @@ /* Class to demonstrate the use of Classroom Create Course API */ public class CreateCourse { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Creates a course @@ -46,26 +47,29 @@ public static Course createCourse() throws GeneralSecurityException, IOException final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); Course course = null; try { // Adding a new course with description. Set CourseState to `ACTIVE`. Possible values of - // CourseState can be found here: https://developers.google.com/classroom/reference/rest/v1/courses#coursestate - course = new Course() - .setName("10th Grade Biology") - .setSection("Period 2") - .setDescriptionHeading("Welcome to 10th Grade Biology") - .setDescription("We'll be learning about about the structure of living creatures " - + "from a combination of textbooks, guest lectures, and lab work. Expect " - + "to be excited!") - .setRoom("301") - .setOwnerId("me") - .setCourseState("ACTIVE"); + // CourseState can be found here: + // https://developers.google.com/classroom/reference/rest/v1/courses#coursestate + course = + new Course() + .setName("10th Grade Biology") + .setSection("Period 2") + .setDescriptionHeading("Welcome to 10th Grade Biology") + .setDescription( + "We'll be learning about about the structure of living creatures " + + "from a combination of textbooks, guest lectures, and lab work. Expect " + + "to be excited!") + .setRoom("301") + .setOwnerId("me") + .setCourseState("ACTIVE"); course = service.courses().create(course).execute(); // Prints the new created course Id and name System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId()); @@ -80,4 +84,4 @@ public static Course createCourse() throws GeneralSecurityException, IOException return course; } } -// [END classroom_create_course] \ No newline at end of file +// [END classroom_create_course] diff --git a/classroom/snippets/src/main/java/CreateCourseWithAlias.java b/classroom/snippets/src/main/java/CreateCourseWithAlias.java index c6b9ba0e..c676b973 100644 --- a/classroom/snippets/src/main/java/CreateCourseWithAlias.java +++ b/classroom/snippets/src/main/java/CreateCourseWithAlias.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_course_with_alias_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -32,9 +31,9 @@ public class CreateCourseWithAlias { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Create a new course with an alias. Set the new course id to the desired alias. @@ -49,9 +48,9 @@ public static Course createCourseWithAlias() throws GeneralSecurityException, IO final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -61,20 +60,21 @@ public static Course createCourseWithAlias() throws GeneralSecurityException, IO /* 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"); + 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 { 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 + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 409) { System.out.printf("The course alias already exists: %s.\n", content.getId()); diff --git a/classroom/snippets/src/main/java/CreateCourseWork.java b/classroom/snippets/src/main/java/CreateCourseWork.java index 32afb896..5d6c948d 100644 --- a/classroom/snippets/src/main/java/CreateCourseWork.java +++ b/classroom/snippets/src/main/java/CreateCourseWork.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_coursework_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; @@ -34,9 +33,9 @@ public class CreateCourseWork { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Creates course work. @@ -53,9 +52,9 @@ public static CourseWork createCourseWork(String courseId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -64,9 +63,10 @@ public static CourseWork createCourseWork(String courseId) CourseWork courseWork = null; try { // 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"); + 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)); @@ -76,21 +76,22 @@ public static CourseWork createCourseWork(String courseId) 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("Supersonic aviation") - .setDescription("Read about how the SR-71 Blackbird, the world’s fastest and " - + "highest-flying manned aircraft, was built.") - .setMaterials(materials) - .setWorkType("ASSIGNMENT") - .setState("PUBLISHED"); + CourseWork content = + new CourseWork() + .setTitle("Supersonic aviation") + .setDescription( + "Read about how the SR-71 Blackbird, the world’s fastest and " + + "highest-flying manned aircraft, was built.") + .setMaterials(materials) + .setWorkType("ASSIGNMENT") + .setState("PUBLISHED"); - courseWork = service.courses().courseWork().create(courseId, content) - .execute(); + 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 + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId does not exist: %s.\n", courseId); @@ -106,4 +107,4 @@ public static CourseWork createCourseWork(String courseId) // [END classroom_create_coursework_code_snippet] } } -// [END classroom_create_coursework_class] \ No newline at end of file +// [END classroom_create_coursework_class] diff --git a/classroom/snippets/src/main/java/CreateTopic.java b/classroom/snippets/src/main/java/CreateTopic.java index f09aa875..1be964a3 100644 --- a/classroom/snippets/src/main/java/CreateTopic.java +++ b/classroom/snippets/src/main/java/CreateTopic.java @@ -31,9 +31,9 @@ public class CreateTopic { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); /** * Create a new topic in a course. @@ -49,9 +49,9 @@ public static Topic createTopic(String courseId) throws GeneralSecurityException final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -64,7 +64,7 @@ public static Topic createTopic(String courseId) throws GeneralSecurityException 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 + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId does not exist: %s.\n", courseId); diff --git a/classroom/snippets/src/main/java/DeleteTopic.java b/classroom/snippets/src/main/java/DeleteTopic.java index b94ce294..9508db3f 100644 --- a/classroom/snippets/src/main/java/DeleteTopic.java +++ b/classroom/snippets/src/main/java/DeleteTopic.java @@ -15,7 +15,6 @@ // [START classroom_delete_topic_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; diff --git a/classroom/snippets/src/main/java/GetCourse.java b/classroom/snippets/src/main/java/GetCourse.java index 1efea231..94d07f75 100644 --- a/classroom/snippets/src/main/java/GetCourse.java +++ b/classroom/snippets/src/main/java/GetCourse.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_get_course] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -33,7 +32,8 @@ public class GetCourse { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Retrieve a single course's metadata. @@ -49,9 +49,9 @@ public static Course getCourse(String courseId) throws GeneralSecurityException, final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -71,4 +71,4 @@ public static Course getCourse(String courseId) throws GeneralSecurityException, return course; } } -// [END classroom_get_course] \ No newline at end of file +// [END classroom_get_course] diff --git a/classroom/snippets/src/main/java/GetTopic.java b/classroom/snippets/src/main/java/GetTopic.java index facf7ea5..5fb42012 100644 --- a/classroom/snippets/src/main/java/GetTopic.java +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -32,8 +32,8 @@ public class GetTopic { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); /** * Get a topic in a course. @@ -51,9 +51,9 @@ public static Topic getTopic(String courseId, String topicId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -65,7 +65,7 @@ public static Topic getTopic(String courseId, String topicId) 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 + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId); diff --git a/classroom/snippets/src/main/java/ListCourseAliases.java b/classroom/snippets/src/main/java/ListCourseAliases.java index 3a6a93cc..dbec2299 100644 --- a/classroom/snippets/src/main/java/ListCourseAliases.java +++ b/classroom/snippets/src/main/java/ListCourseAliases.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_list_aliases_class] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -35,7 +34,8 @@ public class ListCourseAliases { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Retrieve the aliases for a course. @@ -52,9 +52,9 @@ public static List listCourseAliases(String courseId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -66,10 +66,14 @@ public static List listCourseAliases(String courseId) try { // List of aliases of specified course do { - ListCourseAliasesResponse response = service.courses().aliases().list(courseId) - .setPageSize(100) - .setPageToken(pageToken) - .execute(); + ListCourseAliasesResponse response = + service + .courses() + .aliases() + .list(courseId) + .setPageSize(100) + .setPageToken(pageToken) + .execute(); courseAliases.addAll(response.getAliases()); pageToken = response.getNextPageToken(); } while (pageToken != null); @@ -96,4 +100,4 @@ public static List listCourseAliases(String courseId) // [END classroom_list_aliases_code_snippet] } } -// [END classroom_list_aliases_class] \ No newline at end of file +// [END classroom_list_aliases_class] diff --git a/classroom/snippets/src/main/java/ListCourses.java b/classroom/snippets/src/main/java/ListCourses.java index 3ba78a13..c5a00b25 100644 --- a/classroom/snippets/src/main/java/ListCourses.java +++ b/classroom/snippets/src/main/java/ListCourses.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_list_courses] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -33,7 +32,8 @@ public class ListCourses { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Retrieves all courses with metadata @@ -48,9 +48,9 @@ public static List listCourses() throws GeneralSecurityException, IOExce final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -59,10 +59,8 @@ public static List listCourses() throws GeneralSecurityException, IOExce try { do { - ListCoursesResponse response = service.courses().list() - .setPageSize(100) - .setPageToken(pageToken) - .execute(); + ListCoursesResponse response = + service.courses().list().setPageSize(100).setPageToken(pageToken).execute(); courses.addAll(response.getCourses()); pageToken = response.getNextPageToken(); } while (pageToken != null); @@ -82,4 +80,4 @@ public static List listCourses() throws GeneralSecurityException, IOExce return courses; } } -// [END classroom_list_courses] \ No newline at end of file +// [END classroom_list_courses] diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index a0d6dc9e..acd6598a 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -34,7 +34,8 @@ public class ListStudentSubmissions { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Retrieves a specific student's submissions for the specified course work. @@ -46,16 +47,17 @@ public class ListStudentSubmissions { * @throws IOException - if credentials file not found. * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static List listStudentSubmissions(String courseId, String courseWorkId, String userId) + public static List listStudentSubmissions( + String courseId, String courseWorkId, String userId) throws GeneralSecurityException, IOException { // Create the classroom API client. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ListSubmissions.java b/classroom/snippets/src/main/java/ListSubmissions.java index 84bf5fa6..105c9b41 100644 --- a/classroom/snippets/src/main/java/ListSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -34,7 +34,8 @@ public class ListSubmissions { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Retrieves submissions for all students for the specified course work in a course. * @@ -51,9 +52,9 @@ public static List listSubmissions(String courseId, String co final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ListTopics.java b/classroom/snippets/src/main/java/ListTopics.java index f2e2dd0c..ea35cf90 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -34,8 +34,8 @@ public class ListTopics { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); /** * List topics in a course. @@ -52,9 +52,9 @@ public static List listTopics(String courseId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index 13d439dd..d047ac2b 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -12,7 +12,6 @@ // 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.javanet.GoogleNetHttpTransport; @@ -35,10 +34,9 @@ public class ModifyAttachmentsStudentSubmission { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Modify attachments on a student submission. @@ -57,9 +55,9 @@ public static StudentSubmission modifyAttachments(String courseId, String course final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -70,28 +68,34 @@ public static StudentSubmission modifyAttachments(String courseId, String course // 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)); + 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(); + 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()); + System.out.printf( + "Modified student submission attachments: '%s'.\n", + studentSubmission.getAssignmentSubmission().getAttachments()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // 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 " - + "not exist.\n", courseId, courseWorkId, id); + System.out.printf( + "The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", + courseId, courseWorkId, id); } else { throw e; } - } catch(Exception e) { + } catch (Exception e) { throw e; } return studentSubmission; @@ -100,4 +104,4 @@ public static StudentSubmission modifyAttachments(String courseId, String course } } -// [END classroom_modify_attachments_student_submissions_class] \ No newline at end of file +// [END classroom_modify_attachments_student_submissions_class] diff --git a/classroom/snippets/src/main/java/PatchCourse.java b/classroom/snippets/src/main/java/PatchCourse.java index afd85ba1..05bc71ac 100644 --- a/classroom/snippets/src/main/java/PatchCourse.java +++ b/classroom/snippets/src/main/java/PatchCourse.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_patch_course] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -33,7 +32,8 @@ public class PatchCourse { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Updates one or more fields in a course. @@ -49,20 +49,16 @@ public static Course patchCourse(String courseId) throws GeneralSecurityExceptio final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); Course course = null; try { - course = new Course() - .setSection("Period 3") - .setRoom("302"); - course = service.courses().patch(courseId, course) - .setUpdateMask("section,room") - .execute(); + course = new Course().setSection("Period 3").setRoom("302"); + course = service.courses().patch(courseId, course).setUpdateMask("section,room").execute(); System.out.printf("Course '%s' updated.\n", course.getName()); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately @@ -76,4 +72,4 @@ public static Course patchCourse(String courseId) throws GeneralSecurityExceptio return course; } } -// [END classroom_patch_course] \ No newline at end of file +// [END classroom_patch_course] diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 7931179b..33ed5165 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -12,7 +12,6 @@ // 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.javanet.GoogleNetHttpTransport; @@ -32,9 +31,9 @@ public class PatchStudentSubmission { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Updates the draft grade and/or assigned grade of a student submission. * @@ -45,16 +44,17 @@ public class PatchStudentSubmission { * @throws IOException - if credentials file not found. * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static StudentSubmission patchStudentSubmission(String courseId, String courseWorkId, - String id) throws GeneralSecurityException, IOException { + public static StudentSubmission patchStudentSubmission( + String courseId, String courseWorkId, String id) + throws GeneralSecurityException, IOException { // Create the classroom API client. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -63,28 +63,38 @@ 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(); + StudentSubmission content = + service + .courses() + .courseWork() + .studentSubmissions() + .get(courseId, courseWorkId, id) + .execute(); 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(); + 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()); + 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 + // 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 " - + "not exist.\n", courseId, courseWorkId, id); + System.out.printf( + "The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", + courseId, courseWorkId, id); } else { throw e; } @@ -97,4 +107,4 @@ public static StudentSubmission patchStudentSubmission(String courseId, String c } } -// [END classroom_patch_student_submissions_class] \ No newline at end of file +// [END classroom_patch_student_submissions_class] diff --git a/classroom/snippets/src/main/java/ReturnStudentSubmission.java b/classroom/snippets/src/main/java/ReturnStudentSubmission.java index dcf8e7bd..61c891a0 100644 --- a/classroom/snippets/src/main/java/ReturnStudentSubmission.java +++ b/classroom/snippets/src/main/java/ReturnStudentSubmission.java @@ -12,7 +12,6 @@ // 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.javanet.GoogleNetHttpTransport; @@ -32,10 +31,11 @@ public class ReturnStudentSubmission { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** - * Return a student submission back to the student which updates the submission state to `RETURNED`. + * 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. @@ -50,24 +50,29 @@ public static void returnSubmission(String courseId, String courseWorkId, String final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); // [START classroom_return_student_submissions_code_snippet] try { - service.courses().courseWork().studentSubmissions() + service + .courses() + .courseWork() + .studentSubmissions() .classroomReturn(courseId, courseWorkId, id, null) .execute(); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // 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 " - + "not exist.\n", courseId, courseWorkId, id); + System.out.printf( + "The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", + courseId, courseWorkId, id); } else { throw e; } diff --git a/classroom/snippets/src/main/java/UpdateCourse.java b/classroom/snippets/src/main/java/UpdateCourse.java index 431640c7..fdfd6062 100644 --- a/classroom/snippets/src/main/java/UpdateCourse.java +++ b/classroom/snippets/src/main/java/UpdateCourse.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_update_course] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; @@ -32,9 +31,9 @@ public class UpdateCourse { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Updates a course's metadata. * @@ -49,9 +48,9 @@ public static Course updateCourse(String courseId) throws GeneralSecurityExcepti final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -76,4 +75,4 @@ public static Course updateCourse(String courseId) throws GeneralSecurityExcepti return course; } } -// [END classroom_update_course] \ No newline at end of file +// [END classroom_update_course] diff --git a/classroom/snippets/src/main/java/UpdateTopic.java b/classroom/snippets/src/main/java/UpdateTopic.java index 62546e22..e45c3cc8 100644 --- a/classroom/snippets/src/main/java/UpdateTopic.java +++ b/classroom/snippets/src/main/java/UpdateTopic.java @@ -32,8 +32,8 @@ public class UpdateTopic { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); /** * Update one or more fields in a topic in a course. @@ -50,9 +50,9 @@ public static Topic updateTopic(String courseId, String topicId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -67,10 +67,14 @@ public static Topic updateTopic(String courseId, String topicId) 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(); + be updated. */ + topic = + service + .courses() + .topics() + .patch(courseId, topicId, topicToUpdate) + .set("updateMask", "name") + .execute(); /* Prints the updated topic. */ System.out.printf("Topic '%s' updated.\n", topic.getName()); diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index d004570e..1b2eb04d 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -19,12 +19,11 @@ import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.CourseAlias; +import java.io.IOException; import java.security.GeneralSecurityException; import java.util.ArrayList; -import org.junit.After; - -import java.io.IOException; import java.util.UUID; +import org.junit.After; // Base class for integration tests. public class BaseTest { diff --git a/classroom/snippets/src/test/java/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index 980ef941..2b185c9a 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -13,10 +13,10 @@ // limitations under the License. import com.google.api.services.classroom.model.Student; +import java.io.IOException; import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Add Student classroom snippet public class TestAddStudent extends BaseTest { diff --git a/classroom/snippets/src/test/java/TestAddTeacher.java b/classroom/snippets/src/test/java/TestAddTeacher.java index 09c86a29..58eaae57 100644 --- a/classroom/snippets/src/test/java/TestAddTeacher.java +++ b/classroom/snippets/src/test/java/TestAddTeacher.java @@ -13,10 +13,10 @@ // limitations under the License. import com.google.api.services.classroom.model.Teacher; +import java.io.IOException; import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Add Teacher classroom snippet public class TestAddTeacher extends BaseTest { diff --git a/classroom/snippets/src/test/java/TestBatchAddStudents.java b/classroom/snippets/src/test/java/TestBatchAddStudents.java index b185f28b..1f66874a 100644 --- a/classroom/snippets/src/test/java/TestBatchAddStudents.java +++ b/classroom/snippets/src/test/java/TestBatchAddStudents.java @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -import java.security.GeneralSecurityException; -import org.junit.Test; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.Arrays; import java.util.List; +import org.junit.Test; // Unit test class for Batch Add Students classroom snippet public class TestBatchAddStudents extends BaseTest { diff --git a/classroom/snippets/src/test/java/TestCreateCourse.java b/classroom/snippets/src/test/java/TestCreateCourse.java index 5606b101..a251f15c 100644 --- a/classroom/snippets/src/test/java/TestCreateCourse.java +++ b/classroom/snippets/src/test/java/TestCreateCourse.java @@ -13,10 +13,10 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Create Course classroom snippet public class TestCreateCourse extends BaseTest { From 051a3cc447ffb76548b82cb301da374e9fa7121e Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Thu, 9 Feb 2023 14:58:30 -0500 Subject: [PATCH 46/50] google java format DeleteTopic --- classroom/snippets/src/main/java/DeleteTopic.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/classroom/snippets/src/main/java/DeleteTopic.java b/classroom/snippets/src/main/java/DeleteTopic.java index 9508db3f..15188de6 100644 --- a/classroom/snippets/src/main/java/DeleteTopic.java +++ b/classroom/snippets/src/main/java/DeleteTopic.java @@ -30,8 +30,8 @@ public class DeleteTopic { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); /** * Delete a topic in a course. @@ -49,9 +49,9 @@ public static void deleteTopic(String courseId, String topicId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); From c018e9dce0e08e509f89c8ab39ea3ffda4fcfaee Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 14 Feb 2023 15:43:04 -0500 Subject: [PATCH 47/50] added test cases to CreateInvitation and DeleteInvitation classes --- .../snippets/src/test/java/TestCreateInvitation.java | 9 +++++++++ .../snippets/src/test/java/TestDeleteInvitation.java | 12 ++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/classroom/snippets/src/test/java/TestCreateInvitation.java b/classroom/snippets/src/test/java/TestCreateInvitation.java index c6daeebc..13095342 100644 --- a/classroom/snippets/src/test/java/TestCreateInvitation.java +++ b/classroom/snippets/src/test/java/TestCreateInvitation.java @@ -12,6 +12,7 @@ // 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.Invitation; import java.io.IOException; import java.security.GeneralSecurityException; @@ -31,4 +32,12 @@ public void testCreateInvitation() throws GeneralSecurityException, IOException Assert.assertEquals(invitation.getUserId(), userId); } + @Test + public void testCreateInvitationWithInvalidCourseId() + throws GeneralSecurityException, IOException { + setup(CreateInvitation.SCOPES); + Assert.assertThrows( + GoogleJsonResponseException.class, + () -> CreateInvitation.createInvitation("invalid-course-id", userId)); + } } diff --git a/classroom/snippets/src/test/java/TestDeleteInvitation.java b/classroom/snippets/src/test/java/TestDeleteInvitation.java index 9d53a751..84448189 100644 --- a/classroom/snippets/src/test/java/TestDeleteInvitation.java +++ b/classroom/snippets/src/test/java/TestDeleteInvitation.java @@ -28,7 +28,15 @@ public void testDeleteInvitation() throws GeneralSecurityException, IOException setup(DeleteInvitation.SCOPES); Invitation invitation = CreateInvitation.createInvitation(testCourse.getId(), userId); DeleteInvitation.deleteInvitation(invitation.getId()); - Assert.assertThrows(GoogleJsonResponseException.class, - () -> GetInvitation.getInvitation(invitation.getId())); + Assert.assertThrows( + GoogleJsonResponseException.class, () -> GetInvitation.getInvitation(invitation.getId())); + } + + @Test + public void testDeleteInvitationWithInvalidId() throws GeneralSecurityException, IOException { + setup(DeleteInvitation.SCOPES); + Assert.assertThrows( + GoogleJsonResponseException.class, + () -> DeleteInvitation.deleteInvitation("invalid-invitation-id")); } } From b2b3da4606b4c932a7ab21cfdd21d3287512d041 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 21 Feb 2023 13:10:17 -0500 Subject: [PATCH 48/50] additional comment in accept invitation test case --- .../snippets/src/test/java/TestAcceptInvitation.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/classroom/snippets/src/test/java/TestAcceptInvitation.java b/classroom/snippets/src/test/java/TestAcceptInvitation.java index d34c2d43..98a6fec3 100644 --- a/classroom/snippets/src/test/java/TestAcceptInvitation.java +++ b/classroom/snippets/src/test/java/TestAcceptInvitation.java @@ -25,13 +25,15 @@ public class TestAcceptInvitation { @Test public void testAcceptInvitation() throws GeneralSecurityException, IOException { AcceptInvitation.acceptInvitation(invitationId); - Assert.assertThrows(GoogleJsonResponseException.class, - () -> GetInvitation.getInvitation(invitationId)); + /* Once an invitation is accepted, it is removed and the user is added to the course as a + teacher or a student. */ + Assert.assertThrows( + GoogleJsonResponseException.class, () -> GetInvitation.getInvitation(invitationId)); } @Test public void testAcceptInvitationWithInvalidId() { - Assert.assertThrows(GoogleJsonResponseException.class, - () -> AcceptInvitation.acceptInvitation("invalid-id")); + Assert.assertThrows( + GoogleJsonResponseException.class, () -> AcceptInvitation.acceptInvitation("invalid-id")); } } From 32d289d4a7977720da70260a0306a0c9486eaf24 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Fri, 24 Feb 2023 12:47:49 -0500 Subject: [PATCH 49/50] fixing java formatting --- .../src/main/java/AcceptInvitation.java | 12 ++++---- .../src/main/java/CreateInvitation.java | 28 +++++++++---------- .../src/main/java/DeleteInvitation.java | 12 ++++---- .../snippets/src/main/java/GetInvitation.java | 15 +++++----- .../src/main/java/ListStudentSubmissions.java | 12 ++++---- 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/classroom/snippets/src/main/java/AcceptInvitation.java b/classroom/snippets/src/main/java/AcceptInvitation.java index 241a80c9..137a23a0 100644 --- a/classroom/snippets/src/main/java/AcceptInvitation.java +++ b/classroom/snippets/src/main/java/AcceptInvitation.java @@ -31,8 +31,8 @@ public class AcceptInvitation { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Accepts an invitation to a course. @@ -47,9 +47,9 @@ public static void acceptInvitation(String id) throws GeneralSecurityException, final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -69,4 +69,4 @@ public static void acceptInvitation(String id) throws GeneralSecurityException, // [END classroom_accept_invitation_code_snippet] } } -// [END classroom_accept_invitation] \ No newline at end of file +// [END classroom_accept_invitation] diff --git a/classroom/snippets/src/main/java/CreateInvitation.java b/classroom/snippets/src/main/java/CreateInvitation.java index b6a01588..3e3d22ac 100644 --- a/classroom/snippets/src/main/java/CreateInvitation.java +++ b/classroom/snippets/src/main/java/CreateInvitation.java @@ -31,9 +31,9 @@ public class CreateInvitation { /* Scopes required by this API call. If modifying these scopes, delete your previously saved - tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Create an invitation to allow a user to join a course. @@ -51,9 +51,9 @@ public static Invitation createInvitation(String courseId, String userId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -63,17 +63,16 @@ public static Invitation createInvitation(String courseId, String userId) try { /* Set the role the user is invited to have in the course. Possible values of CourseRole can be found here: https://developers.google.com/classroom/reference/rest/v1/invitations#courserole.*/ - Invitation content = new Invitation() - .setCourseId(courseId) - .setUserId(userId) - .setRole("TEACHER"); + Invitation content = + new Invitation().setCourseId(courseId).setUserId(userId).setRole("TEACHER"); invitation = service.invitations().create(content).execute(); - System.out.printf("User (%s) has been invited to course (%s).\n", invitation.getUserId(), - invitation.getCourseId()); + System.out.printf( + "User (%s) has been invited to course (%s).\n", + invitation.getUserId(), invitation.getCourseId()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The course or user does not exist.\n"); @@ -86,6 +85,5 @@ public static Invitation createInvitation(String courseId, String userId) // [END classroom_create_invitation_code_snippet] } - } -// [END classroom_create_invitation] \ No newline at end of file +// [END classroom_create_invitation] diff --git a/classroom/snippets/src/main/java/DeleteInvitation.java b/classroom/snippets/src/main/java/DeleteInvitation.java index d11fee03..eca59e92 100644 --- a/classroom/snippets/src/main/java/DeleteInvitation.java +++ b/classroom/snippets/src/main/java/DeleteInvitation.java @@ -31,8 +31,8 @@ public class DeleteInvitation { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Deletes an invitation. @@ -47,9 +47,9 @@ public static void deleteInvitation(String id) throws GeneralSecurityException, final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -69,4 +69,4 @@ public static void deleteInvitation(String id) throws GeneralSecurityException, // [END classroom_delete_invitation_code_snippet] } } -// [END classroom_delete_invitation] \ No newline at end of file +// [END classroom_delete_invitation] diff --git a/classroom/snippets/src/main/java/GetInvitation.java b/classroom/snippets/src/main/java/GetInvitation.java index b689d851..77d0598f 100644 --- a/classroom/snippets/src/main/java/GetInvitation.java +++ b/classroom/snippets/src/main/java/GetInvitation.java @@ -31,8 +31,8 @@ public class GetInvitation { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>( - Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); /** * Retrieves an invitation. @@ -47,9 +47,9 @@ public static Invitation getInvitation(String id) throws GeneralSecurityExceptio final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -57,7 +57,8 @@ public static Invitation getInvitation(String id) throws GeneralSecurityExceptio Invitation invitation = null; try { invitation = service.invitations().get(id).execute(); - System.out.printf("Invitation (%s) for user (%s) in course (%s) retrieved.\n", + System.out.printf( + "Invitation (%s) for user (%s) in course (%s) retrieved.\n", invitation.getId(), invitation.getUserId(), invitation.getCourseId()); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); @@ -72,4 +73,4 @@ public static Invitation getInvitation(String id) throws GeneralSecurityExceptio // [END classroom_get_invitation_code_snippet] } } -// [END classroom_get_invitation] \ No newline at end of file +// [END classroom_get_invitation] diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index a0d6dc9e..acd6598a 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -34,7 +34,8 @@ public class ListStudentSubmissions { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - static ArrayList SCOPES = new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Retrieves a specific student's submissions for the specified course work. @@ -46,16 +47,17 @@ public class ListStudentSubmissions { * @throws IOException - if credentials file not found. * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static List listStudentSubmissions(String courseId, String courseWorkId, String userId) + public static List listStudentSubmissions( + String courseId, String courseWorkId, String userId) throws GeneralSecurityException, IOException { // Create the classroom API client. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); From d07fdd218388fffb32c83b4491e8178331276f48 Mon Sep 17 00:00:00 2001 From: Mahima Desetty Date: Tue, 28 Feb 2023 11:38:54 -0500 Subject: [PATCH 50/50] google java format fix --- .../src/main/java/ListStudentSubmissions.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index fb2ddcef..20e322a1 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -62,7 +62,6 @@ public static List listStudentSubmissions( .build(); // [START classroom_list_student_submissions_code_snippet] - List studentSubmissions = new ArrayList<>(); String pageToken = null; @@ -93,6 +92,7 @@ public static List listStudentSubmissions( System.out.printf("Student submission: %s.\n", submission.getId()); } } + // [END classroom_list_student_submissions_code_snippet] } catch (GoogleJsonResponseException e) { // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); @@ -107,7 +107,6 @@ public static List listStudentSubmissions( throw e; } return studentSubmissions; - // [END classroom_list_student_submissions_code_snippet] } /** @@ -125,9 +124,9 @@ public static void listAssignedGrades(String courseId, String courseWorkId) final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - HTTP_TRANSPORT, - GsonFactory.getDefaultInstance(), - ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); @@ -136,7 +135,7 @@ public static void listAssignedGrades(String courseId, String courseWorkId) try { do { - // [START classroom_list_assigned_grades_code_snippet] + // [START classroom_list_assigned_grades_code_snippet] ListStudentSubmissionsResponse response = service .courses() @@ -157,18 +156,18 @@ public static void listAssignedGrades(String courseId, String courseWorkId) System.out.println("No student submissions found."); } else { for (StudentSubmission submission : studentSubmissions) { - System.out.printf("User ID %s, Assigned grade: %s\n", submission.getUserId(), - submission.getAssignedGrade()); + System.out.printf( + "User ID %s, Assigned grade: %s\n", + submission.getUserId(), submission.getAssignedGrade()); } } - // [END classroom_list_assigned_grades_code_snippet] + // [END classroom_list_assigned_grades_code_snippet] } 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) does not exist.\n", - courseId, courseWorkId); + "The courseId (%s) or courseWorkId (%s) does not exist.\n", courseId, courseWorkId); } else { throw e; }