@Override public Map<String, String> get() { ListLabelsResponse response = null; try { response = getOrCreateGmail(authData).users().labels().list(USER).execute(); } catch (IOException e) { throw new RuntimeException("Unable to list labels for user", e); } ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); for (Label label : response.getLabels()) { // TODO: remove system labels builder.put(label.getName(), label.getId()); } return builder.build(); } };
/** Creates the given {@code labelName} in the import service provider and returns the id. */ private String createImportedLabelId(TokensAndUrlAuthData authData, String labelName) throws IOException { Label newLabel = new Label() .setName(labelName) .setLabelListVisibility("labelShow") .setMessageListVisibility("show"); return getOrCreateGmail(authData).users().labels().create(USER, newLabel).execute().getId(); }
@Before public void setUp() throws IOException { Label label = new Label(); label.setId(LABEL1); label.setName(LABEL1); labelsListResponse = new ListLabelsResponse().setLabels(Collections.singletonList(label)); jobStore = new LocalJobStore(); Monitor monitor = new Monitor() {}; googleMailImporter = new GoogleMailImporter(googleCredentialFactory, jobStore, gmail, monitor); when(gmail.users()).thenReturn(users); when(users.messages()).thenReturn(messages); when(messages.insert(Matchers.anyString(), Matchers.any(Message.class))).thenReturn(insert); when(users.labels()).thenReturn(labels); when(labels.list(Matchers.anyString())).thenReturn(labelsList); when(labelsList.execute()).thenReturn(labelsListResponse); when(labels.create(Matchers.anyString(), Matchers.any(Label.class))).thenReturn(labelsCreate); when(labelsCreate.execute()).thenReturn(label); verifyZeroInteractions(googleCredentialFactory); }
/** * Updates the specified label. * * Create a request for the method "labels.update". * * This request holds the parameters needed by the the gmail server. After setting any optional * parameters, call the {@link Update#execute()} method to invoke the remote operation. <p> {@link * Update#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must * be called to initialize this instance immediately after invoking the constructor. </p> * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param id The ID of the label to update. * @param content the {@link com.google.api.services.gmail.model.Label} * @since 1.13 */ protected Update(java.lang.String userId, java.lang.String id, com.google.api.services.gmail.model.Label content) { super(Gmail.this, "PUT", REST_PATH, content, com.google.api.services.gmail.model.Label.class); this.userId = com.google.api.client.util.Preconditions.checkNotNull(userId, "Required parameter userId must be specified."); this.id = com.google.api.client.util.Preconditions.checkNotNull(id, "Required parameter id must be specified."); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getId(), "Label.getId()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getLabelListVisibility(), "Label.getLabelListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getMessageListVisibility(), "Label.getMessageListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getName(), "Label.getName()"); }
@Override public void onSuccess( Message responseMessage, HttpHeaders responseHeaders) throws IOException { Preconditions.checkState( message.getId().equals(responseMessage.getId()), "Message ids must be equal"); List<String> gmailMessageIds = responseMessage.getLabelIds() == null ? ImmutableList.of() : responseMessage.getLabelIds(); System.out.format("For message %s, got labels: %s\n", responseMessage.getId(), gmailMessageIds.stream() .map(id -> labelsById.getOrDefault( id, new Label().setName(id))) .map(Label::getName) .collect(Collectors.joining(", "))); message.setLabelIds(gmailMessageIds); } });
/** * Creates a new label. * * Create a request for the method "labels.create". * * This request holds the parameters needed by the the gmail server. After setting any optional * parameters, call the {@link Create#execute()} method to invoke the remote operation. <p> {@link * Create#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must * be called to initialize this instance immediately after invoking the constructor. </p> * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param content the {@link com.google.api.services.gmail.model.Label} * @since 1.13 */ protected Create(java.lang.String userId, com.google.api.services.gmail.model.Label content) { super(Gmail.this, "POST", REST_PATH, content, com.google.api.services.gmail.model.Label.class); this.userId = com.google.api.client.util.Preconditions.checkNotNull(userId, "Required parameter userId must be specified."); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getLabelListVisibility(), "Label.getLabelListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getMessageListVisibility(), "Label.getMessageListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getName(), "Label.getName()"); }
public static void main(String... args) throws IOException, GeneralSecurityException { // Build a new authorized API client service. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) .setApplicationName(APPLICATION_NAME) .build(); // Print the labels in the user's account. String user = "me"; ListLabelsResponse listResponse = service.users().labels().list(user).execute(); List<Label> labels = listResponse.getLabels(); if (labels.isEmpty()) { System.out.println("No labels found."); } else { System.out.println("Labels:"); for (Label label : labels) { System.out.printf("- %s\n", label.getName()); } } } }
gmailMessageIds.stream() .map(id -> labelsById.getOrDefault( id, new Label().setName(id))) .map(Label::getName) .collect(Collectors.joining(", ")));
gmailApi.users() .labels() .create(user.getEmailAddress(), new Label() .setName(label) .setLabelListVisibility("labelHide") .setMessageListVisibility("show")) .queue(batchRequest, new JsonBatchCallback<Label>() { @Override