下面列出了io.grpc.auth.ClientAuthInterceptor#com.google.pubsub.v1.ListTopicsRequest 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void listTopics() {
ListTopicsRequest request =
ListTopicsRequest.newBuilder().setProject("projects/project-1").build();
ListTopicsResponse response = blockingStub.listTopics(request);
assertThat(
response.getTopicsList(),
Matchers.contains(
com.google.pubsub.v1.Topic.newBuilder()
.setName(TestHelpers.PROJECT1_TOPIC1)
.putLabels(KAFKA_TOPIC, "kafka-topic-1")
.build(),
com.google.pubsub.v1.Topic.newBuilder()
.setName(TestHelpers.PROJECT1_TOPIC2)
.putLabels(KAFKA_TOPIC, "kafka-topic-2")
.build()));
}
@Override
public List<TopicPath> listTopics(ProjectPath project) throws IOException {
ListTopicsRequest.Builder request =
ListTopicsRequest.newBuilder().setProject(project.getPath()).setPageSize(LIST_BATCH_SIZE);
ListTopicsResponse response = publisherStub().listTopics(request.build());
if (response.getTopicsCount() == 0) {
return ImmutableList.of();
}
List<TopicPath> topics = new ArrayList<>(response.getTopicsCount());
while (true) {
for (Topic topic : response.getTopicsList()) {
topics.add(topicPathFromPath(topic.getName()));
}
if (response.getNextPageToken().isEmpty()) {
break;
}
request.setPageToken(response.getNextPageToken());
response = publisherStub().listTopics(request.build());
}
return topics;
}
/**
* Get topics (max 10) for our project ID: the topic list is logged to the logger.
*
* @param projectID the GCP project ID to get the pubsub topics for. This is a string like
* "projects/balmy-cirrus-225307" where "balmy-cirrus-225307" is
* the project ID for the project you created.
*/
public void getTopics(String projectID) {
logger.log(Level.INFO, "Will try to get topics for project {0} ...", projectID);
ListTopicsRequest request = ListTopicsRequest.newBuilder()
.setPageSize(10) // get max 10 topics
.setProject(projectID) // for our projectID
.build();
ListTopicsResponse response;
try {
response = blockingStub.listTopics(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.log(Level.INFO, "Topics list:\n {0}", response.getTopicsList());
}
@Override
public void listTopics(
ListTopicsRequest request, StreamObserver<ListTopicsResponse> responseObserver) {
logger.atFine().log("Listing Topics for %s", request);
PaginationManager<Topic> paginationManager =
new PaginationManager<>(
configurationManager.getTopics(request.getProject()), Topic::getName);
ListTopicsResponse response =
ListTopicsResponse.newBuilder()
.addAllTopics(paginationManager.paginate(request.getPageSize(), request.getPageToken()))
.setNextPageToken(paginationManager.getNextToken(Topic::getName))
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
@Test
public void listTopics_withPagination() {
ListTopicsRequest request =
ListTopicsRequest.newBuilder().setProject("projects/project-1").setPageSize(1).build();
ListTopicsResponse response = blockingStub.listTopics(request);
assertThat(response.getTopicsList(), Matchers.hasSize(1));
assertThat(
response.getTopicsList(),
Matchers.contains(
Topic.newBuilder()
.setName(TestHelpers.PROJECT1_TOPIC1)
.putLabels(KAFKA_TOPIC, "kafka-topic-1")
.build()));
assertThat(response.getNextPageToken(), Matchers.not(Matchers.isEmptyOrNullString()));
request = request.toBuilder().setPageToken(response.getNextPageToken()).setPageSize(0).build();
response = blockingStub.listTopics(request);
assertThat(response.getTopicsList(), Matchers.hasSize(1));
assertThat(
response.getTopicsList(),
Matchers.contains(
Topic.newBuilder()
.setName("projects/project-1/topics/topic-2")
.putLabels(KAFKA_TOPIC, "kafka-topic-2")
.build()));
assertThat(response.getNextPageToken(), Matchers.isEmptyOrNullString());
}
public static void main(final String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Please specify your project name.");
System.exit(1);
}
final String project = args[0];
ManagedChannelImpl channelImpl = NettyChannelBuilder
.forAddress("pubsub.googleapis.com", 443)
.negotiationType(NegotiationType.TLS)
.build();
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
// Down-scope the credential to just the scopes required by the service
creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
// Intercept the channel to bind the credential
ExecutorService executor = Executors.newSingleThreadExecutor();
ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
// Create a stub using the channel that has the bound credential
PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
ListTopicsRequest request = ListTopicsRequest.newBuilder()
.setPageSize(10)
.setProject("projects/" + project)
.build();
ListTopicsResponse resp = publisherStub.listTopics(request);
System.out.println("Found " + resp.getTopicsCount() + " topics.");
for (Topic topic : resp.getTopicsList()) {
System.out.println(topic.getName());
}
}
public TestApp() {
try
{
/*
// For GoogleAPIs
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
//ComputeCredential credential = new ComputeCredential.Builder(httpTransport, jsonFactory).build();
GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);
if (credential.createScopedRequired())
credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));
Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("oauth client")
.build();
Userinfoplus ui = service.userinfo().get().execute();
System.out.println(ui.getEmail());
*/
// Using Google Cloud APIs
Storage storage_service = StorageOptions.newBuilder()
.build()
.getService();
for (Bucket b : storage_service.list().iterateAll()){
System.out.println(b);
}
// String cred_file = "/path/to/cred.json";
//GoogleCredentials creds = GoogleCredentials.fromStream(new FileInputStream(cred_file));
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(creds);
///ManagedChannel channel = ManagedChannelBuilder.forTarget("pubsub.googleapis.com:443").build();
//TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
TransportChannelProvider channelProvider = TopicAdminSettings.defaultTransportChannelProvider();
TopicAdminClient topicClient =
TopicAdminClient.create(
TopicAdminSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.build());
ListTopicsRequest listTopicsRequest =
ListTopicsRequest.newBuilder()
.setProject(ProjectName.format("your_project"))
.build();
ListTopicsPagedResponse response = topicClient.listTopics(listTopicsRequest);
Iterable<Topic> topics = response.iterateAll();
for (Topic topic : topics)
System.out.println(topic);
}
catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
public TestApp() {
try
{
// use env or set the path directly
String cred_env = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
cred_env = "/path/to/your/cert.json";
/*
<!--use:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-oauth2</artifactId>
<version>v2-rev114-1.22.0</version>
</dependency>
-->
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// unset GOOGLE_APPLICATION_CREDENTIALS
//String SERVICE_ACCOUNT_JSON_FILE = "YOUR_SERVICE_ACCOUNT_JSON_FILE.json";
//FileInputStream inputStream = new FileInputStream(new File(SERVICE_ACCOUNT_JSON_FILE));
//GoogleCredential credential = GoogleCredential.fromStream(inputStream, httpTransport, jsonFactory);
// to use application default credentials and a JSON file, set the environment variable first:
// export GOOGLE_APPLICATION_CREDENTIALS=YOUR_SERVICE_ACCOUNT_JSON_FILE.json
GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);
if (credential.createScopedRequired())
credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));
Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("oauth client")
.build();
Userinfoplus ui = service.userinfo().get().execute();
System.out.println(ui.getEmail());
*/
/*
Using Google Cloud APIs with service account file
// You can also just export an export GOOGLE_APPLICATION_CREDENTIALS and use StorageOptions.defaultInstance().service()
// see: https://github.com/google/google-auth-library-java#google-auth-library-oauth2-http
uncomment the dependencies for google-api-client
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.35.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.35.0</version>
</dependency>
*/
Storage storage_service = StorageOptions.newBuilder()
.build()
.getService();
for (Bucket b : storage_service.list().iterateAll()){
System.out.println(b);
}
//GoogleCredentials creds = GoogleCredentials.fromStream(new FileInputStream(cred_env));
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(creds);
///ManagedChannel channel = ManagedChannelBuilder.forTarget("pubsub.googleapis.com:443").build();
//TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
TransportChannelProvider channelProvider = TopicAdminSettings.defaultTransportChannelProvider();
TopicAdminClient topicClient =
TopicAdminClient.create(
TopicAdminSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.build());
ListTopicsRequest listTopicsRequest =
ListTopicsRequest.newBuilder()
.setProject(ProjectName.format("your_project"))
.build();
ListTopicsPagedResponse response = topicClient.listTopics(listTopicsRequest);
Iterable<Topic> topics = response.iterateAll();
for (Topic topic : topics)
System.out.println(topic);
}
catch (Exception ex) {
System.out.println("Error: " + ex);
}
}