Search in sources :

Example 1 with Subscriptions

use of com.google.api.services.pubsub.Pubsub.Projects.Subscriptions in project beam by apache.

the class PubsubJsonClient method listSubscriptions.

@Override
public List<SubscriptionPath> listSubscriptions(ProjectPath project, TopicPath topic) throws IOException {
    Subscriptions.List request = pubsub.projects().subscriptions().list(project.getPath());
    ListSubscriptionsResponse response = request.execute();
    if (response.getSubscriptions() == null || response.getSubscriptions().isEmpty()) {
        return ImmutableList.of();
    }
    List<SubscriptionPath> subscriptions = new ArrayList<>(response.getSubscriptions().size());
    while (true) {
        for (Subscription subscription : response.getSubscriptions()) {
            if (subscription.getTopic().equals(topic.getPath())) {
                subscriptions.add(subscriptionPathFromPath(subscription.getName()));
            }
        }
        if (Strings.isNullOrEmpty(response.getNextPageToken())) {
            break;
        }
        request.setPageToken(response.getNextPageToken());
        response = request.execute();
    }
    return subscriptions;
}
Also used : ListSubscriptionsResponse(com.google.api.services.pubsub.model.ListSubscriptionsResponse) Subscriptions(com.google.api.services.pubsub.Pubsub.Projects.Subscriptions) ArrayList(java.util.ArrayList) Subscription(com.google.api.services.pubsub.model.Subscription)

Example 2 with Subscriptions

use of com.google.api.services.pubsub.Pubsub.Projects.Subscriptions in project beam by apache.

the class PubsubJsonClientTest method listSubscriptions.

@Test
public void listSubscriptions() throws Exception {
    ListSubscriptionsResponse expectedResponse1 = new ListSubscriptionsResponse();
    expectedResponse1.setSubscriptions(Collections.singletonList(buildSubscription(1)));
    expectedResponse1.setNextPageToken("AVgJH3Z7aHxiDBs");
    ListSubscriptionsResponse expectedResponse2 = new ListSubscriptionsResponse();
    expectedResponse2.setSubscriptions(Collections.singletonList(buildSubscription(2)));
    Subscriptions.List request = mockPubsub.projects().subscriptions().list(PROJECT.getPath());
    when((Object) request.execute()).thenReturn(expectedResponse1, expectedResponse2);
    final TopicPath topic101 = PubsubClient.topicPathFromName("testProject", "Topic2");
    List<SubscriptionPath> subscriptionPaths = client.listSubscriptions(PROJECT, topic101);
    assertEquals(1, subscriptionPaths.size());
}
Also used : ListSubscriptionsResponse(com.google.api.services.pubsub.model.ListSubscriptionsResponse) TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) Subscriptions(com.google.api.services.pubsub.Pubsub.Projects.Subscriptions) SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) Test(org.junit.Test)

Aggregations

Subscriptions (com.google.api.services.pubsub.Pubsub.Projects.Subscriptions)2 ListSubscriptionsResponse (com.google.api.services.pubsub.model.ListSubscriptionsResponse)2 Subscription (com.google.api.services.pubsub.model.Subscription)1 ArrayList (java.util.ArrayList)1 SubscriptionPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)1 TopicPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath)1 Test (org.junit.Test)1