|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START classroom_cancel_guardian_invitation_class] |
| 16 | + |
| 17 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 18 | +import com.google.api.client.googleapis.json.GoogleJsonError; |
| 19 | +import com.google.api.client.googleapis.json.GoogleJsonResponseException; |
| 20 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 21 | +import com.google.api.client.json.gson.GsonFactory; |
| 22 | +import com.google.api.services.classroom.Classroom; |
| 23 | +import com.google.api.services.classroom.ClassroomScopes; |
| 24 | +import com.google.api.services.classroom.model.GuardianInvitation; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +/* Class to demonstrate the use of Classroom Patch Guardian Invitation API. */ |
| 30 | +public class CancelGuardianInvitation { |
| 31 | + /** |
| 32 | + * Cancel a guardian invitation by modifying the state of the invite. |
| 33 | + * |
| 34 | + * @param studentId - the id of the student. |
| 35 | + * @param invitationId - the id of the guardian invitation to modify. |
| 36 | + * @return - the modified guardian invitation. |
| 37 | + * @throws IOException - if credentials file not found. |
| 38 | + */ |
| 39 | + public static GuardianInvitation cancelGuardianInvitation(String studentId, String invitationId) |
| 40 | + throws Exception { |
| 41 | + |
| 42 | + /* Scopes required by this API call. If modifying these scopes, delete your previously saved |
| 43 | + tokens/ folder. */ |
| 44 | + final List<String> SCOPES = |
| 45 | + Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS); |
| 46 | + |
| 47 | + // Create the classroom API client |
| 48 | + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
| 49 | + Classroom service = |
| 50 | + new Classroom.Builder( |
| 51 | + HTTP_TRANSPORT, |
| 52 | + GsonFactory.getDefaultInstance(), |
| 53 | + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) |
| 54 | + .setApplicationName("Classroom samples") |
| 55 | + .build(); |
| 56 | + |
| 57 | + // [START classroom_cancel_guardian_invitation_code_snippet] |
| 58 | + |
| 59 | + GuardianInvitation guardianInvitation = null; |
| 60 | + |
| 61 | + try { |
| 62 | + /* Change the state of the GuardianInvitation from PENDING to COMPLETE. See |
| 63 | + https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate |
| 64 | + for other possible states of guardian invitations. */ |
| 65 | + GuardianInvitation content = |
| 66 | + service.userProfiles().guardianInvitations().get(studentId, invitationId).execute(); |
| 67 | + content.setState("COMPLETE"); |
| 68 | + |
| 69 | + guardianInvitation = |
| 70 | + service |
| 71 | + .userProfiles() |
| 72 | + .guardianInvitations() |
| 73 | + .patch(studentId, invitationId, content) |
| 74 | + .set("updateMask", "state") |
| 75 | + .execute(); |
| 76 | + |
| 77 | + System.out.printf( |
| 78 | + "Invitation (%s) state set to %s\n.", |
| 79 | + guardianInvitation.getInvitationId(), guardianInvitation.getState()); |
| 80 | + } catch (GoogleJsonResponseException e) { |
| 81 | + // TODO (developer) - handle error appropriately |
| 82 | + GoogleJsonError error = e.getDetails(); |
| 83 | + if (error.getCode() == 404) { |
| 84 | + System.out.printf( |
| 85 | + "There is no record of studentId (%s) or invitationId (%s).", studentId, invitationId); |
| 86 | + } else { |
| 87 | + throw e; |
| 88 | + } |
| 89 | + } catch (Exception e) { |
| 90 | + throw e; |
| 91 | + } |
| 92 | + return guardianInvitation; |
| 93 | + |
| 94 | + // [END classroom_cancel_guardian_invitation_code_snippet] |
| 95 | + } |
| 96 | +} |
| 97 | +// [END classroom_cancel_guardian_invitation_class] |
0 commit comments