Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
d94f7d1
add alias snippet and unit test
mahima-desetty Dec 1, 2022
abd08ec
java snippets for topics, create coursework, create alias
mahima-desetty Dec 1, 2022
ac9b458
updates based on team review
mahima-desetty Dec 6, 2022
a5cbd30
renamed alias methods, added devsite comments
mahima-desetty Dec 9, 2022
e0eba07
Merge pull request #1 from mahima-desetty/java-classroom-code-snippets
mahima-desetty Dec 9, 2022
f567ffe
added missing license header to TestDeleteTopic.java
mahima-desetty Dec 9, 2022
e9d825d
Adding List, ModifyAttachments, Patch, Return StudentSubmission methods
mahima-desetty Dec 9, 2022
0cfab78
minor fixes
mahima-desetty Dec 9, 2022
bd46dd4
adding comments for developer
mahima-desetty Dec 9, 2022
5bf22f2
Merge branch 'googleworkspace:main' into main
mahima-desetty Dec 12, 2022
59b07b3
modifications based on team review
mahima-desetty Dec 13, 2022
9968e72
modifications based on team review
mahima-desetty Dec 13, 2022
ae66989
Merge pull request #2 from mahima-desetty/java-classroom-code-snippets
mahima-desetty Dec 14, 2022
dbf1401
Adding print statements and devsite tag to ListCourseAliases
mahima-desetty Dec 16, 2022
2391573
added class templates for guardians and guardianInvitations
mahima-desetty Dec 16, 2022
fa1c6b0
guardian and guardian invitation implementation
mahima-desetty Dec 16, 2022
d8a0421
tests for list guardians and list guardian invitations
mahima-desetty Dec 20, 2022
d7fc3ec
adding tests for guardians and guardian invites. removing use of ADC …
mahima-desetty Dec 21, 2022
448fe9a
base test updates
mahima-desetty Dec 21, 2022
25efd52
updates based on team feedback
mahima-desetty Jan 9, 2023
3c112f0
Merge branch 'main' into java-classroom-code-snippets
mahima-desetty Jan 9, 2023
c0b8c1b
fixing BaseTest.java
mahima-desetty Jan 11, 2023
191f57f
Merge branch 'java-classroom-code-snippets' of https://github.com/mah…
mahima-desetty Jan 11, 2023
0bd8702
Merge pull request #3 from mahima-desetty/java-classroom-code-snippets
mahima-desetty Jan 11, 2023
58ef8c5
Merge branch 'main' into java-code-snippets
mahima-desetty Jan 11, 2023
0b209ee
updating copyright year to 2023 for new files
mahima-desetty Jan 17, 2023
1cfb939
testing google-java-format plugin to fix lint errors
mahima-desetty Jan 17, 2023
0e91d79
updated files after running google-java-format plugin
mahima-desetty Jan 17, 2023
09c634c
updated BaseTest
mahima-desetty Jan 17, 2023
fde753d
fixing copyright header for BaseTest
mahima-desetty Jan 17, 2023
b01e297
Merge pull request #4 from mahima-desetty/java-code-snippets
mahima-desetty Jan 23, 2023
f69ff9f
updated auth for AddAlias and CreateCourse classes
mahima-desetty Jan 23, 2023
ab81119
removing unused imports
mahima-desetty Jan 23, 2023
1cb5583
Fixing comment.
mahima-desetty Jan 24, 2023
ef20f9f
specifying errors in method signatures
mahima-desetty Jan 25, 2023
676cffa
updated BaseTest and AddStudent
mahima-desetty Jan 25, 2023
130240f
Adding annotation for GeneralSecurityException
mahima-desetty Jan 25, 2023
b2cbc21
updating AddStudent method signature and test
mahima-desetty Jan 26, 2023
c32006a
updated AddTeacher and AddStudent test
mahima-desetty Jan 26, 2023
a55e9ec
updated auth for BatchAddStudents
mahima-desetty Jan 26, 2023
86b7655
updated auth for CreateCourseWork
mahima-desetty Jan 26, 2023
4c3b0e1
updated auth for Topic related classes and tests
mahima-desetty Jan 26, 2023
4c92d15
added link to CourseState info and updated GetTopic error handling
mahima-desetty Feb 6, 2023
4342b9f
updated auth for remaining classes
mahima-desetty Feb 7, 2023
a351fd5
updated auth for CreateCourseWithAlias
mahima-desetty Feb 7, 2023
18e44d0
Merge pull request #5 from mahima-desetty/update-auth-snippets
mahima-desetty Feb 7, 2023
f31c151
CreateInvitation code snippet
mahima-desetty Feb 7, 2023
fd73ed3
accept, create, delete invitation classes
mahima-desetty Feb 8, 2023
c3965e2
list invitations class
mahima-desetty Feb 8, 2023
6bf7c4a
listAssignedGrades method in ListStudentSubmissions class
mahima-desetty Feb 8, 2023
4399486
deleting ListInvitations classes until bug is fixed
mahima-desetty Feb 8, 2023
1732524
team review updates
mahima-desetty Feb 9, 2023
e06cb80
Merge branch 'main' into main
mahima-desetty Feb 9, 2023
a1503fe
google java format for tests
mahima-desetty Feb 9, 2023
95311b0
Merge branch 'main' of https://github.com/mahima-desetty/java-samples
mahima-desetty Feb 9, 2023
8ae450a
google-java-format on classes
mahima-desetty Feb 9, 2023
051a3cc
google java format DeleteTopic
mahima-desetty Feb 9, 2023
c018e9d
added test cases to CreateInvitation and DeleteInvitation classes
mahima-desetty Feb 14, 2023
b2b3da4
additional comment in accept invitation test case
mahima-desetty Feb 21, 2023
d1d94f0
Merge pull request #7 from mahima-desetty/manage-coursework
mahima-desetty Feb 21, 2023
b6abf42
Merge pull request #6 from mahima-desetty/manage-invitations
mahima-desetty Feb 24, 2023
32d289d
fixing java formatting
mahima-desetty Feb 24, 2023
e66b3ee
Merge pull request #8 from mahima-desetty/manage-invitations
mahima-desetty Feb 24, 2023
d07fdd2
google java format fix
mahima-desetty Feb 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions classroom/snippets/src/main/java/AcceptInvitation.java
Original file line number Diff line number Diff line change
@@ -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<String> SCOPES =
new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS));

/**
* Accepts an invitation to a course.
*
* @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]
46 changes: 23 additions & 23 deletions classroom/snippets/src/main/java/AddAliasToCourse.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,61 @@
// 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;
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.security.GeneralSecurityException;
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<String> SCOPES =
new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES));

/**
* Add an alias on 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.
* @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created.
*/
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 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_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()
.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);
Expand Down
61 changes: 34 additions & 27 deletions classroom/snippets/src/main/java/AddStudent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,64 @@
// 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;
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<String> SCOPES =
new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS));

/**
* 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 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);
public static Student addStudent(String courseId, String enrollmentCode, String studentId)
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");
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
Expand All @@ -78,4 +85,4 @@ public static Student addStudent(String courseId, String enrollmentCode)
return student;
}
}
// [END classroom_add_student]
// [END classroom_add_student]
47 changes: 25 additions & 22 deletions classroom/snippets/src/main/java/AddTeacher.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,58 @@
// 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<String> 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 {
// 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
Expand All @@ -76,4 +79,4 @@ public static Teacher addTeacher(String courseId, String teacherEmail)
return teacher;
}
}
// [END classroom_add_teacher]
// [END classroom_add_teacher]
Loading