Skip to content

Commit 0bcf262

Browse files
committed
fix(adminSDK): address PR comments, improve safety checks
1 parent 579b40d commit 0bcf262

File tree

3 files changed

+41
-52
lines changed

3 files changed

+41
-52
lines changed

adminSDK/directory/quickstart.gs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,25 @@ function listUsers() {
2424
maxResults: 10,
2525
orderBy: 'email'
2626
};
27-
try {
28-
if (!AdminDirectory || !AdminDirectory.Users) {
29-
throw new Error('Enable the AdminDirectory Advanced Service.');
30-
}
31-
const response = AdminDirectory.Users.list(optionalArgs);
32-
const users = response.users;
33-
if (!users || users.length === 0) {
34-
console.log('No users found.');
35-
return;
36-
}
37-
// Print the list of user's full name and email
38-
console.log('Users:');
39-
for (const user of users) {
27+
if (!AdminDirectory || !AdminDirectory.Users) {
28+
throw new Error('Enable the AdminDirectory Advanced Service.');
29+
}
30+
const response = AdminDirectory.Users.list(optionalArgs);
31+
const users = response.users;
32+
if (!users || users.length === 0) {
33+
console.log('No users found.');
34+
return;
35+
}
36+
// Print the list of user's full name and email
37+
console.log('Users:');
38+
for (const user of users) {
39+
if (user.primaryEmail) {
4040
if (user.name?.fullName) {
4141
console.log('%s (%s)', user.primaryEmail, user.name.fullName);
4242
} else {
4343
console.log('%s', user.primaryEmail);
4444
}
4545
}
46-
} catch (err) {
47-
// TODO (developer)- Handle exception from the Directory API
48-
console.log('Failed with error %s', /** @type {Error} */ (err).message);
4946
}
5047
}
5148
// [END admin_sdk_directory_quickstart]

adminSDK/reports/quickstart.gs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,23 @@ function listLogins() {
2424
const optionalArgs = {
2525
maxResults: 10
2626
};
27-
try {
28-
if (!AdminReports || !AdminReports.Activities) {
29-
throw new Error('Enable the AdminReports Advanced Service.');
30-
}
31-
const response = AdminReports.Activities.list(
32-
userKey, applicationName, optionalArgs);
33-
const activities = response.items;
34-
if (!activities || activities.length === 0) {
35-
console.log('No logins found.');
36-
return;
37-
}
38-
// Print login events
39-
console.log('Logins:');
40-
for (const activity of activities) {
41-
if (activity.id?.time && activity.actor?.email && activity.events?.[0]?.name) {
42-
console.log('%s: %s (%s)', activity.id.time, activity.actor.email,
43-
activity.events[0].name);
44-
}
27+
if (!AdminReports || !AdminReports.Activities) {
28+
throw new Error('Enable the AdminReports Advanced Service.');
29+
}
30+
const response = AdminReports.Activities.list(
31+
userKey, applicationName, optionalArgs);
32+
const activities = response.items;
33+
if (!activities || activities.length === 0) {
34+
console.log('No logins found.');
35+
return;
36+
}
37+
// Print login events
38+
console.log('Logins:');
39+
for (const activity of activities) {
40+
if (activity.id?.time && activity.actor?.email && activity.events?.[0]?.name) {
41+
console.log('%s: %s (%s)', activity.id.time, activity.actor.email,
42+
activity.events[0].name);
4543
}
46-
} catch (err) {
47-
// TODO (developer)- Handle exception from the Report API
48-
console.log('Failed with error %s', /** @type {Error} */ (err).message);
4944
}
5045
}
5146
// [END admin_sdk_reports_quickstart]

adminSDK/reseller/quickstart.gs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,25 @@ function listSubscriptions() {
2222
const optionalArgs = {
2323
maxResults: 10
2424
};
25-
try {
26-
if (!AdminReseller || !AdminReseller.Subscriptions) {
27-
throw new Error('Enable the AdminReseller Advanced Service.');
28-
}
29-
const response = AdminReseller.Subscriptions.list(optionalArgs);
30-
const subscriptions = response.subscriptions;
31-
if (!subscriptions || subscriptions.length === 0) {
32-
console.log('No subscriptions found.');
33-
return;
34-
}
35-
console.log('Subscriptions:');
36-
for (const subscription of subscriptions) {
25+
if (!AdminReseller || !AdminReseller.Subscriptions) {
26+
throw new Error('Enable the AdminReseller Advanced Service.');
27+
}
28+
const response = AdminReseller.Subscriptions.list(optionalArgs);
29+
const subscriptions = response.subscriptions;
30+
if (!subscriptions || subscriptions.length === 0) {
31+
console.log('No subscriptions found.');
32+
return;
33+
}
34+
console.log('Subscriptions:');
35+
for (const subscription of subscriptions) {
36+
if (subscription.customerId && subscription.skuId) {
3737
if (subscription.plan?.planName) {
3838
console.log('%s (%s, %s)', subscription.customerId, subscription.skuId,
3939
subscription.plan.planName);
4040
} else {
4141
console.log('%s (%s)', subscription.customerId, subscription.skuId);
4242
}
4343
}
44-
} catch (err) {
45-
// TODO (developer)- Handle exception from the Reseller API
46-
console.log('Failed with error %s', /** @type {Error} */ (err).message);
4744
}
4845
}
4946
// [END admin_sdk_reseller_quickstart]

0 commit comments

Comments
 (0)