Skip to content

Commit 3c050b1

Browse files
authored
Merge pull request #5 from parthea/java-analytics-admin-migration-7
migrate code from https://github.com/googleapis/java-analytics-admin
2 parents 2374674 + 6d7ff0a commit 3c050b1

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.analytics;
18+
19+
/* This application demonstrates the usage of the Analytics Admin API using
20+
service account credentials. For more information on service accounts, see
21+
https://cloud.google.com/iam/docs/understanding-service-accounts
22+
23+
The following document provides instructions on setting service account
24+
credentials for your application:
25+
https://cloud.google.com/docs/authentication/production
26+
27+
In a nutshell, you need to:
28+
29+
1. Create a service account and download the key JSON file.
30+
https://cloud.google.com/docs/authentication/production#creating_a_service_account
31+
32+
2. Provide service account credentials using one of the following options:
33+
- set the GOOGLE_APPLICATION_CREDENTIALS environment variable, the API
34+
client will use the value of this variable to find the service account key
35+
JSON file.
36+
37+
https://cloud.google.com/docs/authentication/production#setting_the_environment_variable
38+
39+
OR
40+
41+
- manually pass the path to the service account key JSON file to the API client
42+
by specifying the keyFilename parameter in the constructor.
43+
https://cloud.google.com/docs/authentication/production#passing_the_path_to_the_service_account_key_in_code
44+
45+
To run this sample using Maven:
46+
cd java-analytics-admin/samples/snippets
47+
mvn install
48+
mvn exec:java -Dexec.mainClass="com.example.analytics.QuickstartSample"
49+
*/
50+
51+
// [START analytics_admin_quickstart]
52+
53+
import com.google.analytics.admin.v1beta.Account;
54+
import com.google.analytics.admin.v1beta.AnalyticsAdminServiceClient;
55+
import com.google.analytics.admin.v1beta.AnalyticsAdminServiceClient.ListAccountsPage;
56+
import com.google.analytics.admin.v1beta.AnalyticsAdminServiceClient.ListAccountsPagedResponse;
57+
import com.google.analytics.admin.v1beta.ListAccountsRequest;
58+
59+
public class QuickstartSample {
60+
61+
public static void main(String... args) throws Exception {
62+
listAccounts();
63+
}
64+
65+
// This is an example snippet that calls the Google Analytics Admin API and lists all Google
66+
// Analytics accounts available to the authenticated user.
67+
static void listAccounts() throws Exception {
68+
// Instantiates a client using default credentials.
69+
// See https://cloud.google.com/docs/authentication/production for more information
70+
// about managing credentials.
71+
try (AnalyticsAdminServiceClient analyticsAdmin = AnalyticsAdminServiceClient.create()) {
72+
// Calls listAccounts() method of the Google Analytics Admin API and prints
73+
// the response for each account.
74+
ListAccountsPagedResponse response =
75+
analyticsAdmin.listAccounts(ListAccountsRequest.newBuilder().build());
76+
for (ListAccountsPage page : response.iteratePages()) {
77+
for (Account account : page.iterateAll()) {
78+
System.out.printf("Account name: %s%n", account.getName());
79+
System.out.printf("Display name: %s%n", account.getDisplayName());
80+
System.out.printf("Country code: %s%n", account.getRegionCode());
81+
System.out.printf("Create time: %s%n", account.getCreateTime().getSeconds());
82+
System.out.printf("Update time: %s%n", account.getUpdateTime().getSeconds());
83+
System.out.println();
84+
}
85+
}
86+
}
87+
}
88+
}
89+
90+
// [END analytics_admin_quickstart]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.analytics;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
/** Tests for quickstart sample. */
30+
@RunWith(JUnit4.class)
31+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
32+
public class QuickstartSampleTest {
33+
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
@Before
38+
public void setUp() {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
System.setOut(out);
42+
}
43+
44+
@After
45+
public void tearDown() {
46+
System.setOut(null);
47+
}
48+
49+
@Test
50+
public void testQuickstart() throws Exception {
51+
// Act
52+
QuickstartSample.main();
53+
54+
// Assert
55+
String got = bout.toString();
56+
assertThat(got).contains("Account name:");
57+
}
58+
}

0 commit comments

Comments
 (0)