/** * Lists matching topics. * * <p>Sample code: * * <pre><code> * try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { * ProjectName project = ProjectName.of("[PROJECT]"); * for (Topic element : topicAdminClient.listTopics(project.toString()).iterateAll()) { * // doThingsWith(element); * } * } * </code></pre> * * @param project The name of the project in which to list topics. Format is * `projects/{project-id}`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ListTopicsPagedResponse listTopics(String project) { ListTopicsRequest request = ListTopicsRequest.newBuilder().setProject(project).build(); return listTopics(request); }
public static void executeNoCatch(String projectId) throws Exception { try (TopicAdminClient client = TopicAdminClient.create()) { ProjectName project = ProjectName.of(projectId); ListTopicsPagedResponse pagedResponse = client.listTopics(project); } }
/** * Lists matching topics. * * <p>Sample code: * * <pre><code> * try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { * ProjectName project = ProjectName.of("[PROJECT]"); * for (Topic element : topicAdminClient.listTopics(project).iterateAll()) { * // doThingsWith(element); * } * } * </code></pre> * * @param project The name of the project in which to list topics. Format is * `projects/{project-id}`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ListTopicsPagedResponse listTopics(ProjectName project) { ListTopicsRequest request = ListTopicsRequest.newBuilder() .setProject(project == null ? null : project.toString()) .build(); return listTopics(request); }
/** Example of listing topics. */ public ListTopicsPagedResponse listTopics() throws Exception { // [START pubsub_list_topics] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ListTopicsRequest listTopicsRequest = ListTopicsRequest.newBuilder().setProject(ProjectName.format(projectId)).build(); ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest); Iterable<Topic> topics = response.iterateAll(); for (Topic topic : topics) { // do something with the topic } return response; } // [END pubsub_list_topics] }
@Test @SuppressWarnings("all") public void listTopicsExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockPublisher.addException(exception); try { ProjectName project = ProjectName.of("[PROJECT]"); client.listTopics(project); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void listTopicsTest() { String nextPageToken = ""; Topic topicsElement = Topic.newBuilder().build(); List<Topic> topics = Arrays.asList(topicsElement); ListTopicsResponse expectedResponse = ListTopicsResponse.newBuilder() .setNextPageToken(nextPageToken) .addAllTopics(topics) .build(); mockPublisher.addResponse(expectedResponse); ProjectName project = ProjectName.of("[PROJECT]"); ListTopicsPagedResponse pagedListResponse = client.listTopics(project); List<Topic> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getTopicsList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListTopicsRequest actualRequest = (ListTopicsRequest) actualRequests.get(0); Assert.assertEquals(project, ProjectName.parse(actualRequest.getProject())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Return every topic in a project. * <p>If there are multiple pages, they will all be merged into the same result. * @return a list of topics */ public List<Topic> listTopics() { TopicAdminClient.ListTopicsPagedResponse topicListPage = this.topicAdminClient.listTopics(ProjectName.of(this.projectId)); List<Topic> topics = new ArrayList<>(); topicListPage.iterateAll().forEach(topics::add); return Collections.unmodifiableList(topics); }
/** * Return every topic in a project. * <p>If there are multiple pages, they will all be merged into the same result. * @return a list of topics */ public List<Topic> listTopics() { TopicAdminClient.ListTopicsPagedResponse topicListPage = this.topicAdminClient.listTopics(ProjectName.of(this.projectId)); return Lists.newArrayList(topicListPage.iterateAll()); }