Skip to content

Commit 5cd51b4

Browse files
docs(samples): added samples and test for recaptcha key operations (#643)
* docs(samples): added samples and test for recaptcha key operations * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): modified return type of ListSiteKey to be re-used in Migrate Key sample * docs(samples): added comment Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 4147a26 commit 5cd51b4

File tree

4 files changed

+155
-4
lines changed

4 files changed

+155
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2021 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 recaptcha;
18+
19+
// [START recaptcha_enterprise_get_metrics_site_key]
20+
21+
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
22+
import com.google.recaptchaenterprise.v1.GetMetricsRequest;
23+
import com.google.recaptchaenterprise.v1.Metrics;
24+
import com.google.recaptchaenterprise.v1.MetricsName;
25+
import com.google.recaptchaenterprise.v1.ScoreMetrics;
26+
import java.io.IOException;
27+
28+
public class GetMetrics {
29+
30+
public static void main(String[] args) throws IOException {
31+
String projectId = "project-id";
32+
String recaptchaSiteKey = "recaptcha-site-key";
33+
34+
getMetrics(projectId, recaptchaSiteKey);
35+
}
36+
37+
/**
38+
* Get metrics specific to a recaptcha site key. E.g: score bucket count for a key or number of
39+
* times the checkbox key failed/ passed etc.,
40+
*
41+
* @param projectId: Google Cloud Project Id.
42+
* @param recaptchaSiteKey: Specify the site key to get metrics.
43+
*/
44+
public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests. After completing all of your requests, call
47+
// the `client.close()` method on the client to safely
48+
// clean up any remaining background resources.
49+
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
50+
51+
GetMetricsRequest getMetricsRequest =
52+
GetMetricsRequest.newBuilder()
53+
.setName(MetricsName.of(projectId, recaptchaSiteKey).toString())
54+
.build();
55+
56+
Metrics response = client.getMetrics(getMetricsRequest);
57+
58+
// Retrieve the metrics you want from the key.
59+
// If the site key is checkbox type: then use response.getChallengeMetricsList() instead of
60+
// response.getScoreMetricsList()
61+
for (ScoreMetrics scoreMetrics : response.getScoreMetricsList()) {
62+
// Each ScoreMetrics is in the granularity of one day.
63+
int scoreBucketCount = scoreMetrics.getOverallMetrics().getScoreBucketsCount();
64+
System.out.println(scoreBucketCount);
65+
}
66+
System.out.printf("Retrieved the bucket count for score based key: %s", recaptchaSiteKey);
67+
}
68+
}
69+
}
70+
// [END recaptcha_enterprise_get_metrics_site_key]

recaptcha_enterprise/cloud-client/src/main/java/recaptcha/ListSiteKeys.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// [START recaptcha_enterprise_list_site_keys]
2020

2121
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
22+
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.ListKeysPagedResponse;
2223
import com.google.recaptchaenterprise.v1.Key;
2324
import com.google.recaptchaenterprise.v1.ListKeysRequest;
2425
import com.google.recaptchaenterprise.v1.ProjectName;
@@ -36,9 +37,9 @@ public static void main(String[] args) throws IOException {
3637
/**
3738
* List all keys present under the given project ID.
3839
*
39-
* @param projectID: GCloud Project ID.
40+
* @param projectID : GCloud Project ID.
4041
*/
41-
public static void listSiteKeys(String projectID) throws IOException {
42+
public static ListKeysPagedResponse listSiteKeys(String projectID) throws IOException {
4243
// Initialize client that will be used to send requests. This client only needs to be created
4344
// once, and can be reused for multiple requests. After completing all of your requests, call
4445
// the `client.close()` method on the client to safely
@@ -48,10 +49,12 @@ public static void listSiteKeys(String projectID) throws IOException {
4849
ListKeysRequest listKeysRequest =
4950
ListKeysRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).build();
5051

52+
ListKeysPagedResponse response = client.listKeys(listKeysRequest);
5153
System.out.println("Listing reCAPTCHA site keys: ");
52-
for (Key key : client.listKeys(listKeysRequest).iterateAll()) {
54+
for (Key key : response.iterateAll()) {
5355
System.out.println(key.getName());
5456
}
57+
return response;
5558
}
5659
}
5760
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2021 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 recaptcha;
18+
19+
// [START recaptcha_enterprise_migrate_site_key]
20+
21+
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
22+
import com.google.recaptchaenterprise.v1.Key;
23+
import com.google.recaptchaenterprise.v1.KeyName;
24+
import com.google.recaptchaenterprise.v1.MigrateKeyRequest;
25+
import java.io.IOException;
26+
27+
public class MigrateKey {
28+
29+
public static void main(String[] args) throws IOException {
30+
String projectId = "project-id";
31+
String recaptchaSiteKey = "recaptcha-site-key";
32+
33+
migrateKey(projectId, recaptchaSiteKey);
34+
}
35+
36+
/**
37+
* Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise. If you created the key
38+
* using Admin console: https://www.google.com/recaptcha/admin/site, then use this API to migrate
39+
* to reCAPTCHA Enterprise. For more info, see:
40+
* https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
41+
*
42+
* @param projectId: Google Cloud Project Id.
43+
* @param recaptchaSiteKey: Specify the site key to migrate.
44+
*/
45+
public static void migrateKey(String projectId, String recaptchaSiteKey) throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the `client.close()` method on the client to safely
49+
// clean up any remaining background resources.
50+
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
51+
52+
// Specify the key name to migrate.
53+
MigrateKeyRequest migrateKeyRequest =
54+
MigrateKeyRequest.newBuilder()
55+
.setName(KeyName.of(projectId, recaptchaSiteKey).toString())
56+
.build();
57+
58+
Key response = client.migrateKey(migrateKeyRequest);
59+
60+
// To verify if the site key has been migrated, use 'ListSiteKeys' and check if the
61+
// key is present.
62+
for (Key key : recaptcha.ListSiteKeys.listSiteKeys(projectId).iterateAll()) {
63+
if (key.equals(response)) {
64+
System.out.printf("Key migrated successfully: %s", recaptchaSiteKey);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
// [END recaptcha_enterprise_migrate_site_key]

recaptcha_enterprise/cloud-client/src/test/java/app/SnippetsIT.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
5151
import org.springframework.web.util.UriComponentsBuilder;
5252
import recaptcha.AnnotateAssessment;
53+
import recaptcha.GetMetrics;
5354

5455
@RunWith(SpringJUnit4ClassRunner.class)
5556
@EnableAutoConfiguration
@@ -73,7 +74,7 @@ public static void requireEnvVar(String envVarName) {
7374
}
7475

7576
@BeforeClass
76-
public static void setUp() throws IOException, InterruptedException, JSONException {
77+
public static void setUp() throws IOException, InterruptedException {
7778
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
7879
requireEnvVar("GOOGLE_CLOUD_PROJECT");
7980

@@ -162,6 +163,13 @@ public void testCreateAnnotateAssessment()
162163
assertThat(stdOut.toString()).contains("Annotated response sent successfully ! ");
163164
}
164165

166+
@Test
167+
public void testGetMetrics() throws IOException {
168+
GetMetrics.getMetrics(PROJECT_ID, RECAPTCHA_SITE_KEY_1);
169+
assertThat(stdOut.toString())
170+
.contains("Retrieved the bucket count for score based key: " + RECAPTCHA_SITE_KEY_1);
171+
}
172+
165173
public JSONObject createAssessment(String testURL)
166174
throws IOException, JSONException, InterruptedException {
167175

0 commit comments

Comments
 (0)